From 0a0407be4174d032453d6e8c8afa28042b41bb14 Mon Sep 17 00:00:00 2001 From: Logan Smyth Date: Sun, 21 Feb 2016 23:34:52 -0800 Subject: [PATCH] Re-queue remapped imports so they are re-traversed - fixes T6863 --- .../src/index.js | 16 +++++++++++++--- .../regression/es3-compatibility/actual.js | 5 +++++ .../regression/es3-compatibility/expected.js | 15 +++++++++++++++ .../regression/es3-compatibility/options.json | 7 +++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/actual.js create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/expected.js create mode 100644 packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/options.json diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js index 769259cd6b..a91e22f639 100644 --- a/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/src/index.js @@ -63,6 +63,7 @@ export default function () { } else { path.replaceWith(remap); } + this.requeueInParent(path); }, AssignmentExpression(path) { @@ -86,6 +87,7 @@ export default function () { } path.replaceWith(node); + this.requeueInParent(path); }, UpdateExpression(path) { @@ -102,7 +104,9 @@ export default function () { let node = t.assignmentExpression(path.node.operator[0] + "=", arg.node, t.numericLiteral(1)); if ((path.parentPath.isExpressionStatement() && !path.isCompletionRecord()) || path.node.prefix) { - return path.replaceWith(node); + path.replaceWith(node); + this.requeueInParent(path); + return; } let nodes = []; @@ -116,7 +120,8 @@ export default function () { } nodes.push(t.binaryExpression(operator, arg.node, t.numericLiteral(1))); - path.replaceWithMultiple(t.sequenceExpression(nodes)); + let newPaths = path.replaceWithMultiple(t.sequenceExpression(nodes)); + for (const newPath of newPaths) this.requeueInParent(newPath); } }; @@ -426,7 +431,12 @@ export default function () { } path.unshiftContainer("body", topNodes); - path.traverse(reassignmentVisitor, { remaps, scope, exports }); + path.traverse(reassignmentVisitor, { + remaps, + scope, + exports, + requeueInParent: (newPath) => path.requeue(newPath), + }); } } } diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/actual.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/actual.js new file mode 100644 index 0000000000..259f74503b --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/actual.js @@ -0,0 +1,5 @@ + +import foo from 'foo'; +console.log(foo); + +export default 5; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/expected.js b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/expected.js new file mode 100644 index 0000000000..26139932fd --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/expected.js @@ -0,0 +1,15 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); + +var _foo = require('foo'); + +var _foo2 = _interopRequireDefault(_foo); + +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } + +console.log(_foo2['default']); + +exports['default'] = 5; diff --git a/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/options.json b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/options.json new file mode 100644 index 0000000000..33996fe794 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-modules-commonjs/test/fixtures/regression/es3-compatibility/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + "transform-es2015-modules-commonjs", + "transform-es3-member-expression-literals", + "transform-es3-property-literals" + ] +}