Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Destructuring exceptions for ( let { } = 0 ; 0 ; ) #15104

Merged
merged 3 commits into from Nov 1, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 27 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,32 @@ 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
) {
liuxingbaoyu marked this conversation as resolved.
Show resolved Hide resolved
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);