Skip to content

Commit 45d0ef9

Browse files
ajafffRyanCavanaugh
authored andcommitted
factory: parenthesize for-of expression when necessary (#34229)
Fixes: #33856
1 parent 5d20c57 commit 45d0ef9

File tree

5 files changed

+44
-1
lines changed

5 files changed

+44
-1
lines changed

src/compiler/factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1811,7 +1811,7 @@ namespace ts {
18111811
const node = <ForOfStatement>createSynthesizedNode(SyntaxKind.ForOfStatement);
18121812
node.awaitModifier = awaitModifier;
18131813
node.initializer = initializer;
1814-
node.expression = expression;
1814+
node.expression = isCommaSequence(expression) ? createParen(expression) : expression;
18151815
node.statement = asEmbeddedStatement(statement);
18161816
return node;
18171817
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//// [nullishCoalescingOperator12.ts]
2+
const obj: { arr: any[] } = { arr: [] };
3+
for (const i of obj?.arr ?? []) { }
4+
5+
6+
//// [nullishCoalescingOperator12.js]
7+
"use strict";
8+
var _a, _b;
9+
const obj = { arr: [] };
10+
for (const i of (_b = (_a = obj) === null || _a === void 0 ? void 0 : _a.arr, (_b !== null && _b !== void 0 ? _b : []))) { }
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator12.ts ===
2+
const obj: { arr: any[] } = { arr: [] };
3+
>obj : Symbol(obj, Decl(nullishCoalescingOperator12.ts, 0, 5))
4+
>arr : Symbol(arr, Decl(nullishCoalescingOperator12.ts, 0, 12))
5+
>arr : Symbol(arr, Decl(nullishCoalescingOperator12.ts, 0, 29))
6+
7+
for (const i of obj?.arr ?? []) { }
8+
>i : Symbol(i, Decl(nullishCoalescingOperator12.ts, 1, 10))
9+
>obj?.arr : Symbol(arr, Decl(nullishCoalescingOperator12.ts, 0, 12))
10+
>obj : Symbol(obj, Decl(nullishCoalescingOperator12.ts, 0, 5))
11+
>arr : Symbol(arr, Decl(nullishCoalescingOperator12.ts, 0, 12))
12+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/conformance/expressions/nullishCoalescingOperator/nullishCoalescingOperator12.ts ===
2+
const obj: { arr: any[] } = { arr: [] };
3+
>obj : { arr: any[]; }
4+
>arr : any[]
5+
>{ arr: [] } : { arr: never[]; }
6+
>arr : never[]
7+
>[] : never[]
8+
9+
for (const i of obj?.arr ?? []) { }
10+
>i : any
11+
>obj?.arr ?? [] : any[]
12+
>obj?.arr : any[]
13+
>obj : { arr: any[]; }
14+
>arr : any[]
15+
>[] : never[]
16+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @strict: true
2+
// @target: ES2015
3+
4+
const obj: { arr: any[] } = { arr: [] };
5+
for (const i of obj?.arr ?? []) { }

0 commit comments

Comments
 (0)