From 122689eac737b9d20a39314990647780e726765f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20=C5=BDarkovi=C4=87?= Date: Wed, 24 Jan 2018 17:34:05 +0100 Subject: [PATCH] Add support for object spread/rest operator. Before this commit, using rewire on a module that had object spread/rest operator in it would throw an exception. Now it doesn't. --- lib/moduleEnv.js | 5 ++++- package-lock.json | 14 ++++++++++++++ package.json | 3 ++- testLib/objectRestOperator.js | 2 ++ testLib/objectSpreadOperator.js | 1 + testLib/sharedTestCases.js | 12 ++++++++++++ 6 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 testLib/objectRestOperator.js create mode 100644 testLib/objectSpreadOperator.js diff --git a/lib/moduleEnv.js b/lib/moduleEnv.js index a9f410a..cbd5e25 100644 --- a/lib/moduleEnv.js +++ b/lib/moduleEnv.js @@ -76,7 +76,10 @@ function jsExtension(module, filename) { module._compile = function (content, filename) { content = babelCore.transform(content, { - plugins: [require.resolve("babel-plugin-transform-es2015-block-scoping")], + plugins: [ + require.resolve("babel-plugin-transform-es2015-block-scoping"), + require.resolve("babel-plugin-transform-object-rest-spread") + ], retainLines: true, filename: filename, babelrc: false diff --git a/package-lock.json b/package-lock.json index 7cbcd08..19647ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -105,6 +105,11 @@ "babel-runtime": "6.26.0" } }, + "babel-plugin-syntax-object-rest-spread": { + "version": "6.13.0", + "resolved": "https://registry.npmjs.org/babel-plugin-syntax-object-rest-spread/-/babel-plugin-syntax-object-rest-spread-6.13.0.tgz", + "integrity": "sha1-/WU28rzhODb/o6VFjEkDpZe7O/U=" + }, "babel-plugin-transform-es2015-block-scoping": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-plugin-transform-es2015-block-scoping/-/babel-plugin-transform-es2015-block-scoping-6.26.0.tgz", @@ -117,6 +122,15 @@ "lodash": "4.17.4" } }, + "babel-plugin-transform-object-rest-spread": { + "version": "6.26.0", + "resolved": "https://registry.npmjs.org/babel-plugin-transform-object-rest-spread/-/babel-plugin-transform-object-rest-spread-6.26.0.tgz", + "integrity": "sha1-DzZpLVD+9rfi1LOsFHgTepY7ewY=", + "requires": { + "babel-plugin-syntax-object-rest-spread": "6.13.0", + "babel-runtime": "6.26.0" + } + }, "babel-register": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-register/-/babel-register-6.26.0.tgz", diff --git a/package.json b/package.json index 1079122..579b88f 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ }, "dependencies": { "babel-core": "^6.26.0", - "babel-plugin-transform-es2015-block-scoping": "^6.26.0" + "babel-plugin-transform-es2015-block-scoping": "^6.26.0", + "babel-plugin-transform-object-rest-spread": "^6.26.0" } } diff --git a/testLib/objectRestOperator.js b/testLib/objectRestOperator.js new file mode 100644 index 0000000..4a2753d --- /dev/null +++ b/testLib/objectRestOperator.js @@ -0,0 +1,2 @@ +let { ...a } = {}; +module.exports = a; diff --git a/testLib/objectSpreadOperator.js b/testLib/objectSpreadOperator.js new file mode 100644 index 0000000..83b7414 --- /dev/null +++ b/testLib/objectSpreadOperator.js @@ -0,0 +1 @@ +module.exports = { ...{} }; diff --git a/testLib/sharedTestCases.js b/testLib/sharedTestCases.js index b6caf6a..991453b 100644 --- a/testLib/sharedTestCases.js +++ b/testLib/sharedTestCases.js @@ -238,6 +238,18 @@ module.exports = function () { }).to.not.throwException(); }); + it("should not be a problem to have a module that uses object spread operator", function() { + expect(function() { + var rewired = rewire("./objectSpreadOperator.js"); + }).to.not.throwException(); + }); + + it("should not be a problem to have a module that uses object rest operator", function() { + expect(function() { + var rewired = rewire("./objectRestOperator.js"); + }).to.not.throwException(); + }); + it("should not influence the original require if nothing has been required within the rewired module", function () { rewire("./emptyModule.js"); // nothing happens here because emptyModule doesn't require anything expect(require("./moduleA.js").__set__).to.be(undefined); // if restoring the original node require didn't worked, the module would have a setter