Skip to content

Commit 614d98b

Browse files
fix: Destructuring empty in nested for expressions (#14841)
* fix * Update packages/babel-plugin-transform-destructuring/src/util.ts Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com> * review * review Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
1 parent 73d86fe commit 614d98b

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

  • packages/babel-plugin-transform-destructuring

packages/babel-plugin-transform-destructuring/src/util.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,6 @@ export class DestructuringTransformer {
327327
}
328328
}
329329
}
330-
//
331330

332331
for (let i = 0; i < pattern.properties.length; i++) {
333332
const prop = pattern.properties[i];
@@ -484,8 +483,6 @@ export class DestructuringTransformer {
484483
}
485484
}
486485

487-
//
488-
489486
this.push(pattern, ref);
490487

491488
return this.nodes;
@@ -620,6 +617,8 @@ export function convertVariableDeclaration(
620617
}
621618
}
622619

620+
const inForInit = t.isForStatement(path.parent, { init: node });
621+
623622
let tail: t.VariableDeclaration | null = null;
624623
const nodesOut = [];
625624
for (const node of nodes) {
@@ -640,7 +639,9 @@ export function convertVariableDeclaration(
640639
if (!node.loc) {
641640
node.loc = nodeLoc;
642641
}
643-
nodesOut.push(node);
642+
nodesOut.push(
643+
inForInit && node.type === "ExpressionStatement" ? node.expression : node,
644+
);
644645
}
645646

646647
if (nodesOut.length === 1) {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
for ( let x in y ) for ( ; ; ) var { } = x ;
2+
3+
for ( let x in y ) for ( var { } = x ; ; ) ;
4+
5+
for ( let x in y ) for ( ; { } = x ; {} = x ) var { } = x ;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
for (var x in y) {
2+
for (;;) {
3+
babelHelpers.objectDestructuringEmpty(x);
4+
}
5+
}
6+
7+
for (var _x in y) {
8+
for (babelHelpers.objectDestructuringEmpty(_x);;) {
9+
;
10+
}
11+
}
12+
13+
for (var _x2 in y) {
14+
for (; _x3 = _x2, babelHelpers.objectDestructuringEmpty(_x3), _x3; _x4 = _x2, babelHelpers.objectDestructuringEmpty(_x4), _x4) {
15+
var _x3, _x4;
16+
17+
babelHelpers.objectDestructuringEmpty(_x2);
18+
}
19+
}

0 commit comments

Comments
 (0)