Skip to content

Commit 7a75c09

Browse files
committed
Allow of in await using declarations in for-of loops
1 parent 7c417bf commit 7a75c09

9 files changed

+85
-26
lines changed

src/compiler/parser.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6877,7 +6877,8 @@ namespace Parser {
68776877
if (
68786878
token() === SyntaxKind.VarKeyword || token() === SyntaxKind.LetKeyword || token() === SyntaxKind.ConstKeyword ||
68796879
token() === SyntaxKind.UsingKeyword && lookAhead(nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLineDisallowOf) ||
6880-
token() === SyntaxKind.AwaitKeyword && lookAhead(nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLineDisallowOf)
6880+
// this one is meant to allow of
6881+
token() === SyntaxKind.AwaitKeyword && lookAhead(nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine)
68816882
) {
68826883
initializer = parseVariableDeclarationList(/*inForStatementInitializer*/ true);
68836884
}
@@ -7293,10 +7294,6 @@ namespace Parser {
72937294
return lookAhead(nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLine);
72947295
}
72957296

7296-
function nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLineDisallowOf() {
7297-
return nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine(/*disallowOf*/ true);
7298-
}
7299-
73007297
function nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine(disallowOf?: boolean) {
73017298
if (nextToken() === SyntaxKind.UsingKeyword) {
73027299
return nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLine(disallowOf);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
awaitUsingDeclarationsInForAwaitOf.2.ts(4,7): error TS1431: 'for await' loops are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
2+
awaitUsingDeclarationsInForAwaitOf.2.ts(4,32): error TS2448: Block-scoped variable 'of' used before its declaration.
3+
4+
5+
==== awaitUsingDeclarationsInForAwaitOf.2.ts (2 errors) ====
6+
// https://github.com/microsoft/TypeScript/issues/55555
7+
8+
{
9+
for await (await using of of of) {};
10+
~~~~~
11+
!!! error TS1431: 'for await' loops are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
12+
~~
13+
!!! error TS2448: Block-scoped variable 'of' used before its declaration.
14+
!!! related TS2728 awaitUsingDeclarationsInForAwaitOf.2.ts:4:26: 'of' is declared here.
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.2.ts] ////
2+
3+
//// [awaitUsingDeclarationsInForAwaitOf.2.ts]
4+
// https://github.com/microsoft/TypeScript/issues/55555
5+
6+
{
7+
for await (await using of of of) {};
8+
}
9+
10+
//// [awaitUsingDeclarationsInForAwaitOf.2.js]
11+
// https://github.com/microsoft/TypeScript/issues/55555
12+
{
13+
for await (await using of of of) { }
14+
;
15+
}

tests/baselines/reference/awaitUsingDeclarationsInForOf.2.errors.txt

Lines changed: 0 additions & 20 deletions
This file was deleted.

tests/baselines/reference/awaitUsingDeclarationsInForOf.2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ async function main() {
99

1010
//// [awaitUsingDeclarationsInForOf.2.js]
1111
async function main() {
12-
for (await using of of[]) {
12+
for (await using of of []) {
1313
}
1414
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
awaitUsingDeclarationsInForOf.4.ts(4,8): error TS2853: 'await using' statements are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
2+
awaitUsingDeclarationsInForOf.4.ts(4,26): error TS2448: Block-scoped variable 'of' used before its declaration.
3+
4+
5+
==== awaitUsingDeclarationsInForOf.4.ts (2 errors) ====
6+
// https://github.com/microsoft/TypeScript/issues/55555
7+
8+
{
9+
for (await using of of of) {};
10+
~~~~~
11+
!!! error TS2853: 'await using' statements are only allowed at the top level of a file when that file is a module, but this file has no imports or exports. Consider adding an empty 'export {}' to make this file a module.
12+
~~
13+
!!! error TS2448: Block-scoped variable 'of' used before its declaration.
14+
!!! related TS2728 awaitUsingDeclarationsInForOf.4.ts:4:20: 'of' is declared here.
15+
}
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForOf.4.ts] ////
2+
3+
//// [awaitUsingDeclarationsInForOf.4.ts]
4+
// https://github.com/microsoft/TypeScript/issues/55555
5+
6+
{
7+
for (await using of of of) {};
8+
}
9+
10+
11+
//// [awaitUsingDeclarationsInForOf.4.js]
12+
// https://github.com/microsoft/TypeScript/issues/55555
13+
{
14+
for (await using of of of) { }
15+
;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: esnext
2+
// @module: esnext
3+
// @lib: esnext
4+
// @noTypesAndSymbols: true
5+
6+
// https://github.com/microsoft/TypeScript/issues/55555
7+
8+
{
9+
for await (await using of of of) {};
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @target: esnext
2+
// @module: esnext
3+
// @lib: esnext
4+
// @noTypesAndSymbols: true
5+
6+
// https://github.com/microsoft/TypeScript/issues/55555
7+
8+
{
9+
for (await using of of of) {};
10+
}

0 commit comments

Comments
 (0)