Skip to content

Commit

Permalink
fix: Destructuring exceptions for ( let { } = 0 ; 0 ; ) (#15104)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
liuxingbaoyu and nicolo-ribaudo committed Nov 1, 2022
1 parent b9f7644 commit 53185ea
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 10 deletions.
41 changes: 33 additions & 8 deletions packages/babel-plugin-transform-destructuring/src/util.ts
Expand Up @@ -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];

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Expand Up @@ -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 ; ) ;
Expand Up @@ -14,3 +14,6 @@ for (var _x2 in y) {
babelHelpers.objectDestructuringEmpty(_x2);
}
}
for (babelHelpers.objectDestructuringEmpty(0); 0;) {
;
}
@@ -1,2 +1 @@
var _ref = null;
babelHelpers.objectDestructuringEmpty(_ref);
babelHelpers.objectDestructuringEmpty(null);

0 comments on commit 53185ea

Please sign in to comment.