From ef06d9d36161cd9e7b3cc01b3d98ca5cbb2d5e01 Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Mon, 31 Oct 2022 18:52:23 +0800 Subject: [PATCH 1/3] fix --- .../src/util.ts | 21 ++++++++++++------- .../destructuring-empty-in-for/input.js | 2 ++ .../destructuring-empty-in-for/output.js | 8 +++++++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index c52cc6ffe54d..daf5f97e94ab 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,8 +639,6 @@ export function convertVariableDeclaration( } } - const inForInit = t.isForStatement(path.parent, { init: node }); - let tail: t.VariableDeclaration | null = null; const nodesOut = []; for (const node of nodes) { @@ -663,9 +659,20 @@ export function convertVariableDeclaration( if (!node.loc) { node.loc = nodeLoc; } - nodesOut.push( - inForInit && node.type === "ExpressionStatement" ? node.expression : node, - ); + nodesOut.push(node); + } + + // 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..889d11fb3da3 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,11 @@ for (var _x2 in y) { babelHelpers.objectDestructuringEmpty(_x2); } } +for (function () { + var _ret; + var _ = 0; + _ret = babelHelpers.objectDestructuringEmpty(_); + return _ret; +}(); 0;) { + ; +} From 17afced14a965fccf697788b0361681f8d63c95e Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Mon, 31 Oct 2022 21:22:05 +0800 Subject: [PATCH 2/3] review --- .../src/util.ts | 28 +++++++++++++------ .../destructuring-empty-in-for/output.js | 7 +---- .../empty-object-pattern/output.js | 3 +- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index daf5f97e94ab..6c7cc7576c6f 100644 --- a/packages/babel-plugin-transform-destructuring/src/util.ts +++ b/packages/babel-plugin-transform-destructuring/src/util.ts @@ -640,7 +640,7 @@ export function convertVariableDeclaration( } let tail: t.VariableDeclaration | null = null; - const nodesOut = []; + let nodesOut = []; for (const node of nodes) { if (t.isVariableDeclaration(node)) { if (tail !== null) { @@ -662,15 +662,27 @@ export function convertVariableDeclaration( nodesOut.push(node); } - // 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)) + nodesOut.length === 2 && + t.isVariableDeclaration(nodesOut[0]) && + t.isExpressionStatement(nodesOut[1]) && + t.isCallExpression(nodesOut[1].expression) && + nodesOut[0].declarations.length === 1 ) { - for (let i = 0; i < nodesOut.length; i++) { - const node: t.Node = nodesOut[i]; - if (t.isExpressionStatement(node)) { - nodesOut[i] = node.expression; + 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; + } } } } 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 889d11fb3da3..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,11 +14,6 @@ for (var _x2 in y) { babelHelpers.objectDestructuringEmpty(_x2); } } -for (function () { - var _ret; - var _ = 0; - _ret = babelHelpers.objectDestructuringEmpty(_); - return _ret; -}(); 0;) { +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); From 303410ee59f00da5e5ffb411da488562f381616a Mon Sep 17 00:00:00 2001 From: liuxingbaoyu <30521560+liuxingbaoyu@users.noreply.github.com> Date: Tue, 1 Nov 2022 02:00:38 +0800 Subject: [PATCH 3/3] Update packages/babel-plugin-transform-destructuring/src/util.ts MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Nicolò Ribaudo --- packages/babel-plugin-transform-destructuring/src/util.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/babel-plugin-transform-destructuring/src/util.ts b/packages/babel-plugin-transform-destructuring/src/util.ts index 6c7cc7576c6f..e34bc7cd07ce 100644 --- a/packages/babel-plugin-transform-destructuring/src/util.ts +++ b/packages/babel-plugin-transform-destructuring/src/util.ts @@ -669,6 +669,12 @@ export function convertVariableDeclaration( 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];