Skip to content

Commit

Permalink
fix(43879): forbid async in the left hand in a for-of statement (#43886)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk committed May 12, 2021
1 parent 463c794 commit 7aacd6b
Show file tree
Hide file tree
Showing 19 changed files with 156 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/compiler/checker.ts
Expand Up @@ -41471,6 +41471,12 @@ namespace ts {
}
}

if (isForOfStatement(forInOrOfStatement) && !(forInOrOfStatement.flags & NodeFlags.AwaitContext) &&
isIdentifier(forInOrOfStatement.initializer) && forInOrOfStatement.initializer.escapedText === "async") {
grammarErrorOnNode(forInOrOfStatement.initializer, Diagnostics.The_left_hand_side_of_a_for_of_statement_may_not_be_async);
return false;
}

if (forInOrOfStatement.initializer.kind === SyntaxKind.VariableDeclarationList) {
const variableList = <VariableDeclarationList>forInOrOfStatement.initializer;
if (!checkGrammarVariableDeclarationList(variableList)) {
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Expand Up @@ -319,6 +319,10 @@
"category": "Error",
"code": 1105
},
"The left-hand side of a 'for...of' statement may not be 'async'.": {
"category": "Error",
"code": 1106
},
"Jump target cannot cross function boundary.": {
"category": "Error",
"code": 1107
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/for-inStatementsAsyncIdentifier.js
@@ -0,0 +1,8 @@
//// [for-inStatementsAsyncIdentifier.ts]
var async;
for (async in { a: 1, b: 2 }) {}


//// [for-inStatementsAsyncIdentifier.js]
var async;
for (async in { a: 1, b: 2 }) { }
@@ -0,0 +1,9 @@
=== tests/cases/conformance/statements/for-inStatements/for-inStatementsAsyncIdentifier.ts ===
var async;
>async : Symbol(async, Decl(for-inStatementsAsyncIdentifier.ts, 0, 3))

for (async in { a: 1, b: 2 }) {}
>async : Symbol(async, Decl(for-inStatementsAsyncIdentifier.ts, 0, 3))
>a : Symbol(a, Decl(for-inStatementsAsyncIdentifier.ts, 1, 15))
>b : Symbol(b, Decl(for-inStatementsAsyncIdentifier.ts, 1, 21))

12 changes: 12 additions & 0 deletions tests/baselines/reference/for-inStatementsAsyncIdentifier.types
@@ -0,0 +1,12 @@
=== tests/cases/conformance/statements/for-inStatements/for-inStatementsAsyncIdentifier.ts ===
var async;
>async : any

for (async in { a: 1, b: 2 }) {}
>async : any
>{ a: 1, b: 2 } : { a: number; b: number; }
>a : number
>1 : 1
>b : number
>2 : 2

9 changes: 9 additions & 0 deletions tests/baselines/reference/parserForOfStatement22.errors.txt
@@ -0,0 +1,9 @@
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'.


==== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement22.ts (1 errors) ====
var async;
for (async of [1, 2]) {}
~~~~~
!!! error TS1106: The left-hand side of a 'for...of' statement may not be 'async'.

8 changes: 8 additions & 0 deletions tests/baselines/reference/parserForOfStatement22.js
@@ -0,0 +1,8 @@
//// [parserForOfStatement22.ts]
var async;
for (async of [1, 2]) {}


//// [parserForOfStatement22.js]
var async;
for (async of [1, 2]) { }
7 changes: 7 additions & 0 deletions tests/baselines/reference/parserForOfStatement22.symbols
@@ -0,0 +1,7 @@
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement22.ts ===
var async;
>async : Symbol(async, Decl(parserForOfStatement22.ts, 0, 3))

for (async of [1, 2]) {}
>async : Symbol(async, Decl(parserForOfStatement22.ts, 0, 3))

10 changes: 10 additions & 0 deletions tests/baselines/reference/parserForOfStatement22.types
@@ -0,0 +1,10 @@
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement22.ts ===
var async;
>async : any

for (async of [1, 2]) {}
>async : any
>[1, 2] : number[]
>1 : 1
>2 : 2

12 changes: 12 additions & 0 deletions tests/baselines/reference/parserForOfStatement23.js
@@ -0,0 +1,12 @@
//// [parserForOfStatement23.ts]
async function foo(x: any) {
var async;
for await (async of x) {}
}


//// [parserForOfStatement23.js]
async function foo(x) {
var async;
for await (async of x) { }
}
13 changes: 13 additions & 0 deletions tests/baselines/reference/parserForOfStatement23.symbols
@@ -0,0 +1,13 @@
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement23.ts ===
async function foo(x: any) {
>foo : Symbol(foo, Decl(parserForOfStatement23.ts, 0, 0))
>x : Symbol(x, Decl(parserForOfStatement23.ts, 0, 19))

var async;
>async : Symbol(async, Decl(parserForOfStatement23.ts, 1, 7))

for await (async of x) {}
>async : Symbol(async, Decl(parserForOfStatement23.ts, 1, 7))
>x : Symbol(x, Decl(parserForOfStatement23.ts, 0, 19))
}

13 changes: 13 additions & 0 deletions tests/baselines/reference/parserForOfStatement23.types
@@ -0,0 +1,13 @@
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement23.ts ===
async function foo(x: any) {
>foo : (x: any) => Promise<void>
>x : any

var async;
>async : any

for await (async of x) {}
>async : any
>x : any
}

9 changes: 9 additions & 0 deletions tests/baselines/reference/parserForOfStatement24.js
@@ -0,0 +1,9 @@
//// [parserForOfStatement24.ts]
var async;
for ((async) of [1, 2]);


//// [parserForOfStatement24.js]
var async;
for ((async) of [1, 2])
;
7 changes: 7 additions & 0 deletions tests/baselines/reference/parserForOfStatement24.symbols
@@ -0,0 +1,7 @@
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement24.ts ===
var async;
>async : Symbol(async, Decl(parserForOfStatement24.ts, 0, 3))

for ((async) of [1, 2]);
>async : Symbol(async, Decl(parserForOfStatement24.ts, 0, 3))

11 changes: 11 additions & 0 deletions tests/baselines/reference/parserForOfStatement24.types
@@ -0,0 +1,11 @@
=== tests/cases/conformance/parser/ecmascript6/Iterators/parserForOfStatement24.ts ===
var async;
>async : any

for ((async) of [1, 2]);
>(async) : any
>async : any
>[1, 2] : number[]
>1 : 1
>2 : 2

@@ -0,0 +1,4 @@
// @target: esnext

var async;
for (async of [1, 2]) {}
@@ -0,0 +1,6 @@
// @target: esnext

async function foo(x: any) {
var async;
for await (async of x) {}
}
@@ -0,0 +1,4 @@
// @target: esnext

var async;
for ((async) of [1, 2]);
@@ -0,0 +1,4 @@
// @target: esnext

var async;
for (async in { a: 1, b: 2 }) {}

0 comments on commit 7aacd6b

Please sign in to comment.