Skip to content

Commit 84f5077

Browse files
fix(javascript): keep a sloppy function writing a for-in or for-of target out of a class
A loop head writes its target without an assignment, so a function that creates a global through `for (x of ...)` was still moved into a class, where the write throws. Claude-Session: https://claude.ai/code/session_011CuRTB8fAv8PE7di49qDL4
1 parent fcdb0ec commit 84f5077

2 files changed

Lines changed: 11 additions & 7 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17339,12 +17339,13 @@ const installCorrect = (modules) => {
1733917339
if (A.isUnaryNode(node) && node.operator === "delete") {
1734017340
return A.walk_abort;
1734117341
}
17342-
const target = A.isAssignNode(node)
17343-
? node.left
17344-
: A.isUnaryNode(node) &&
17345-
(node.operator === "++" || node.operator === "--")
17346-
? node.argument
17347-
: undefined;
17342+
const target =
17343+
A.isAssignNode(node) || A.isForInNode(node) || A.isForOfNode(node)
17344+
? node.left
17345+
: A.isUnaryNode(node) &&
17346+
(node.operator === "++" || node.operator === "--")
17347+
? node.argument
17348+
: undefined;
1734817349
return target !== undefined &&
1734917350
(A.isPropAccessNode(target) ||
1735017351
A.isDestructuringNode(target) ||

‎test/unitCases/syntax-printer.unittest.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6484,7 +6484,10 @@ describe("syntax-printer", () => {
64846484
"return delete o.b;",
64856485
"return o.a++;",
64866486
"[o.a] = [5]; return o.a;",
6487-
"o.c = 3; return o.c;"
6487+
"o.c = 3; return o.c;",
6488+
"for (gx in o); return gx;",
6489+
"for ([gy] of [[1]]); return gy;",
6490+
"for (o.d of [1]); return o.d;"
64886491
];
64896492
for (const body of bodies) {
64906493
const input = `var o = { a: 1 }; function f() { ${body} } class C { m() { return f(); } } console.log(new C().m());`;

0 commit comments

Comments
 (0)