diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/actual.js b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/actual.js new file mode 100644 index 0000000000..f6ee2a1d9d --- /dev/null +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/actual.js @@ -0,0 +1,6 @@ +(function () { + let q; + let w; + let e; + if (true) [q, w, e] = [1, 2, 3].map(()=>123); +})(); diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/expected.js b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/expected.js new file mode 100644 index 0000000000..d28969c2ed --- /dev/null +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/expected.js @@ -0,0 +1,17 @@ +(function () { + var q = void 0; + var w = void 0; + var e = void 0; + + if (true) { + var _map = [1, 2, 3].map(function () { + return 123; + }); + + var _map2 = babelHelpers.slicedToArray(_map, 3); + + q = _map2[0]; + w = _map2[1]; + e = _map2[2]; + } +})(); diff --git a/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/options.json b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/options.json new file mode 100644 index 0000000000..2dacc028c5 --- /dev/null +++ b/packages/babel-plugin-transform-es2015-destructuring/test/fixtures/destructuring/issue-5628/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["external-helpers", "transform-es2015-arrow-functions", "transform-es2015-destructuring", "transform-es2015-spread", "transform-es2015-parameters", "transform-es2015-block-scoping", "transform-object-rest-spread", "transform-regenerator"] +} diff --git a/packages/babel-traverse/src/path/modification.js b/packages/babel-traverse/src/path/modification.js index 104ed99aa7..afd395383f 100644 --- a/packages/babel-traverse/src/path/modification.js +++ b/packages/babel-traverse/src/path/modification.js @@ -104,6 +104,10 @@ export function _containerInsertAfter(nodes) { */ export function insertAfter(nodes) { + return this._insertAfter(nodes); +} + +export function _insertAfter(nodes, shouldRequeue = false) { this._assertUnremoved(); nodes = this._verifyNodeList(nodes); @@ -112,7 +116,10 @@ export function insertAfter(nodes) { this.parentPath.isExpressionStatement() || this.parentPath.isLabeledStatement() ) { - return this.parentPath.insertAfter(nodes); + // `replaceWithMultiple` requeues if there's a replacement for this.node, + // but not for an ancestor's. Set `shouldRequeue` to true so that any replacement + // for an ancestor node can be enqueued. Fix #5628 and #5023. + return this.parentPath._insertAfter(nodes, true); } else if ( this.isNodeType("Expression") || (this.parentPath.isForStatement() && this.key === "init") @@ -136,6 +143,9 @@ export function insertAfter(nodes) { nodes.unshift(this.node); } this._replaceWith(t.blockStatement(nodes)); + if (shouldRequeue) { + this.requeue(); + } } else { throw new Error( "We don't know what to do with this node type. " +