diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index c52cc6ffe54d..e34bc7cd07ce 100644 --- a/packages/babel-plugin-transform-destructuring/src/util.ts +++ b/packages/babel-plugin-transform-destructuring/src/util.ts @@ -463,8 +463,6 @@ export class DestructuringTransformer { this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray)); } - // - for (let i = 0; i < pattern.elements.length; i++) { const elem = pattern.elements[i]; @@ -641,10 +639,8 @@ export function convertVariableDeclaration( } } - const inForInit = t.isForStatement(path.parent, { init: node }); - let tail: t.VariableDeclaration | null = null; - const nodesOut = []; + let nodesOut = []; for (const node of nodes) { if (t.isVariableDeclaration(node)) { if (tail !== null) { @@ -663,9 +659,38 @@ export function convertVariableDeclaration( if (!node.loc) { node.loc = nodeLoc; } - nodesOut.push( - inForInit && node.type === "ExpressionStatement" ? node.expression : node, - ); + nodesOut.push(node); + } + + if ( + nodesOut.length === 2 && + t.isVariableDeclaration(nodesOut[0]) && + t.isExpressionStatement(nodesOut[1]) && + t.isCallExpression(nodesOut[1].expression) && + nodesOut[0].declarations.length === 1 + ) { + // This can only happen when we generate this code: + // var _ref = DESTRUCTURED_VALUE; + // babelHelpers.objectDestructuringEmpty(_ref); + // Since pushing those two statements to the for loop .init will require an IIFE, + // we can optimize them to + // babelHelpers.objectDestructuringEmpty(DESTRUCTURED_VALUE); + const expr = nodesOut[1].expression; + expr.arguments = [nodesOut[0].declarations[0].init]; + nodesOut = [expr]; + } else { + // We must keep nodes all are expressions or statements, so `replaceWithMultiple` can work. + if ( + t.isForStatement(path.parent, { init: node }) && + !nodesOut.some(v => t.isVariableDeclaration(v)) + ) { + for (let i = 0; i < nodesOut.length; i++) { + const node: t.Node = nodesOut[i]; + if (t.isExpressionStatement(node)) { + nodesOut[i] = node.expression; + } + } + } } if (nodesOut.length === 1) { diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/destructuring-empty-in-for/input.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/destructuring-empty-in-for/input.js index 947529b3cc6f..49d7ac4153a6 100644 --- a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/destructuring-empty-in-for/input.js +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/destructuring-empty-in-for/input.js @@ -3,3 +3,5 @@ for ( let x in y ) for ( ; ; ) var { } = x ; for ( let x in y ) for ( var { } = x ; ; ) ; for ( let x in y ) for ( ; { } = x ; {} = x ) var { } = x ; + +for ( let { } = 0 ; 0 ; ) ; diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/destructuring-empty-in-for/output.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/destructuring-empty-in-for/output.js index 9340fed6b029..f75ff1967a0d 100644 --- a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/destructuring-empty-in-for/output.js +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/destructuring-empty-in-for/output.js @@ -14,3 +14,6 @@ for (var _x2 in y) { babelHelpers.objectDestructuringEmpty(_x2); } } +for (babelHelpers.objectDestructuringEmpty(0); 0;) { + ; +} diff --git a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/empty-object-pattern/output.js b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/empty-object-pattern/output.js index 61ae1a06dc61..1d34b2dca3a8 100644 --- a/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/empty-object-pattern/output.js +++ b/packages/babel-plugin-transform-destructuring/test/fixtures/destructuring/empty-object-pattern/output.js @@ -1,2 +1 @@ -var _ref = null; -babelHelpers.objectDestructuringEmpty(_ref); +babelHelpers.objectDestructuringEmpty(null);