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 30c0c5c8c3..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); } }; @@ -125,6 +130,11 @@ export default function () { visitor: { ThisExpression(path, state) { + // If other plugins run after this plugin's Program#exit handler, we allow them to + // insert top-level `this` values. This allows the AMD and UMD plugins to + // function properly. + if (this.ranCommonJS) return; + if ( state.opts.allowTopLevelThis !== true && !path.findParent((path) => !path.is("shadow") && @@ -136,6 +146,8 @@ export default function () { Program: { exit(path) { + this.ranCommonJS = true; + let strict = !!this.opts.strict; let { scope } = path; @@ -419,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" + ] +} diff --git a/packages/babel-traverse/src/path/context.js b/packages/babel-traverse/src/path/context.js index fcd06d5ca0..2c2c0ed366 100644 --- a/packages/babel-traverse/src/path/context.js +++ b/packages/babel-traverse/src/path/context.js @@ -208,10 +208,22 @@ export function setKey(key) { this.type = this.node && this.node.type; } -export function requeue(path = this) { - if (path.removed) return; +export function requeue(pathToQueue = this) { + if (pathToQueue.removed) return; - for (let context of this.contexts) { - context.maybeQueue(path); + let contexts = this._getQueueContexts(); + + for (let context of contexts) { + context.maybeQueue(pathToQueue); } } + +export function _getQueueContexts(){ + let path = this; + let contexts = this.contexts; + while (!contexts.length) { + path = path.parentPath; + contexts = path.contexts; + } + return contexts; +} diff --git a/packages/babel-traverse/src/path/modification.js b/packages/babel-traverse/src/path/modification.js index ce141aa6c5..5811a19514 100644 --- a/packages/babel-traverse/src/path/modification.js +++ b/packages/babel-traverse/src/path/modification.js @@ -60,12 +60,7 @@ export function _containerInsert(from, nodes) { } } - let contexts = this.contexts; - let path = this; - while (!contexts.length) { - path = path.parentPath; - contexts = path.contexts; - } + let contexts = this._getQueueContexts(); for (let path of paths) { path.setScope();