Skip to content

Commit 7aacd6b

Browse files
authored
fix(43879): forbid async in the left hand in a for-of statement (#43886)
1 parent 463c794 commit 7aacd6b

19 files changed

+156
-0
lines changed

src/compiler/checker.ts

+6
Original file line numberDiff line numberDiff line change
@@ -41471,6 +41471,12 @@ namespace ts {
4147141471
}
4147241472
}
4147341473

41474+
if (isForOfStatement(forInOrOfStatement) && !(forInOrOfStatement.flags & NodeFlags.AwaitContext) &&
41475+
isIdentifier(forInOrOfStatement.initializer) && forInOrOfStatement.initializer.escapedText === "async") {
41476+
grammarErrorOnNode(forInOrOfStatement.initializer, Diagnostics.The_left_hand_side_of_a_for_of_statement_may_not_be_async);
41477+
return false;
41478+
}
41479+
4147441480
if (forInOrOfStatement.initializer.kind === SyntaxKind.VariableDeclarationList) {
4147541481
const variableList = <VariableDeclarationList>forInOrOfStatement.initializer;
4147641482
if (!checkGrammarVariableDeclarationList(variableList)) {

src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,10 @@
319319
"category": "Error",
320320
"code": 1105
321321
},
322+
"The left-hand side of a 'for...of' statement may not be 'async'.": {
323+
"category": "Error",
324+
"code": 1106
325+
},
322326
"Jump target cannot cross function boundary.": {
323327
"category": "Error",
324328
"code": 1107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [for-inStatementsAsyncIdentifier.ts]
2+
var async;
3+
for (async in { a: 1, b: 2 }) {}
4+
5+
6+
//// [for-inStatementsAsyncIdentifier.js]
7+
var async;
8+
for (async in { a: 1, b: 2 }) { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/conformance/statements/for-inStatements/for-inStatementsAsyncIdentifier.ts ===
2+
var async;
3+
>async : Symbol(async, Decl(for-inStatementsAsyncIdentifier.ts, 0, 3))
4+
5+
for (async in { a: 1, b: 2 }) {}
6+
>async : Symbol(async, Decl(for-inStatementsAsyncIdentifier.ts, 0, 3))
7+
>a : Symbol(a, Decl(for-inStatementsAsyncIdentifier.ts, 1, 15))
8+
>b : Symbol(b, Decl(for-inStatementsAsyncIdentifier.ts, 1, 21))
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
=== tests/cases/conformance/statements/for-inStatements/for-inStatementsAsyncIdentifier.ts ===
2+
var async;
3+
>async : any
4+
5+
for (async in { a: 1, b: 2 }) {}
6+
>async : any
7+
>{ a: 1, b: 2 } : { a: number; b: number; }
8+
>a : number
9+
>1 : 1
10+
>b : number
11+
>2 : 2
12+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement22.ts(2,6): error TS1106: The left-hand side of a 'for...of' statement may not be 'async'.
2+
3+
4+
==== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement22.ts (1 errors) ====
5+
var async;
6+
for (async of [1, 2]) {}
7+
~~~~~
8+
!!! error TS1106: The left-hand side of a 'for...of' statement may not be 'async'.
9+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [parserForOfStatement22.ts]
2+
var async;
3+
for (async of [1, 2]) {}
4+
5+
6+
//// [parserForOfStatement22.js]
7+
var async;
8+
for (async of [1, 2]) { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement22.ts ===
2+
var async;
3+
>async : Symbol(async, Decl(parserForOfStatement22.ts, 0, 3))
4+
5+
for (async of [1, 2]) {}
6+
>async : Symbol(async, Decl(parserForOfStatement22.ts, 0, 3))
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement22.ts ===
2+
var async;
3+
>async : any
4+
5+
for (async of [1, 2]) {}
6+
>async : any
7+
>[1, 2] : number[]
8+
>1 : 1
9+
>2 : 2
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [parserForOfStatement23.ts]
2+
async function foo(x: any) {
3+
var async;
4+
for await (async of x) {}
5+
}
6+
7+
8+
//// [parserForOfStatement23.js]
9+
async function foo(x) {
10+
var async;
11+
for await (async of x) { }
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement23.ts ===
2+
async function foo(x: any) {
3+
>foo : Symbol(foo, Decl(parserForOfStatement23.ts, 0, 0))
4+
>x : Symbol(x, Decl(parserForOfStatement23.ts, 0, 19))
5+
6+
var async;
7+
>async : Symbol(async, Decl(parserForOfStatement23.ts, 1, 7))
8+
9+
for await (async of x) {}
10+
>async : Symbol(async, Decl(parserForOfStatement23.ts, 1, 7))
11+
>x : Symbol(x, Decl(parserForOfStatement23.ts, 0, 19))
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement23.ts ===
2+
async function foo(x: any) {
3+
>foo : (x: any) => Promise<void>
4+
>x : any
5+
6+
var async;
7+
>async : any
8+
9+
for await (async of x) {}
10+
>async : any
11+
>x : any
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [parserForOfStatement24.ts]
2+
var async;
3+
for ((async) of [1, 2]);
4+
5+
6+
//// [parserForOfStatement24.js]
7+
var async;
8+
for ((async) of [1, 2])
9+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement24.ts ===
2+
var async;
3+
>async : Symbol(async, Decl(parserForOfStatement24.ts, 0, 3))
4+
5+
for ((async) of [1, 2]);
6+
>async : Symbol(async, Decl(parserForOfStatement24.ts, 0, 3))
7+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement24.ts ===
2+
var async;
3+
>async : any
4+
5+
for ((async) of [1, 2]);
6+
>(async) : any
7+
>async : any
8+
>[1, 2] : number[]
9+
>1 : 1
10+
>2 : 2
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @target: esnext
2+
3+
var async;
4+
for (async of [1, 2]) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: esnext
2+
3+
async function foo(x: any) {
4+
var async;
5+
for await (async of x) {}
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @target: esnext
2+
3+
var async;
4+
for ((async) of [1, 2]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// @target: esnext
2+
3+
var async;
4+
for (async in { a: 1, b: 2 }) {}

0 commit comments

Comments
 (0)