Skip to content

Commit 3e12250

Browse files
authored
Allow of in await using declarations in for-of loops (#55558)
1 parent 42d0e51 commit 3e12250

9 files changed

+84
-26
lines changed

src/compiler/parser.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -6892,7 +6892,8 @@ namespace Parser {
68926892
if (
68936893
token() === SyntaxKind.VarKeyword || token() === SyntaxKind.LetKeyword || token() === SyntaxKind.ConstKeyword ||
68946894
token() === SyntaxKind.UsingKeyword && lookAhead(nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLineDisallowOf) ||
6895-
token() === SyntaxKind.AwaitKeyword && lookAhead(nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLineDisallowOf)
6895+
// this one is meant to allow of
6896+
token() === SyntaxKind.AwaitKeyword && lookAhead(nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine)
68966897
) {
68976898
initializer = parseVariableDeclarationList(/*inForStatementInitializer*/ true);
68986899
}
@@ -7308,10 +7309,6 @@ namespace Parser {
73087309
return lookAhead(nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLine);
73097310
}
73107311

7311-
function nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLineDisallowOf() {
7312-
return nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine(/*disallowOf*/ true);
7313-
}
7314-
73157312
function nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine(disallowOf?: boolean) {
73167313
if (nextToken() === SyntaxKind.UsingKeyword) {
73177314
return nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLine(disallowOf);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
awaitUsingDeclarationsInForAwaitOf.2.ts(4,32): error TS2448: Block-scoped variable 'of' used before its declaration.
2+
3+
4+
==== awaitUsingDeclarationsInForAwaitOf.2.ts (1 errors) ====
5+
// https://github.com/microsoft/TypeScript/issues/55555
6+
7+
async function test() {
8+
for await (await using of of of) {};
9+
~~
10+
!!! error TS2448: Block-scoped variable 'of' used before its declaration.
11+
!!! related TS2728 awaitUsingDeclarationsInForAwaitOf.2.ts:4:26: 'of' is declared here.
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
async function test() {
7+
for await (await using of of of) {};
8+
}
9+
10+
11+
//// [awaitUsingDeclarationsInForAwaitOf.2.js]
12+
// https://github.com/microsoft/TypeScript/issues/55555
13+
async function test() {
14+
for await (await using of of of) { }
15+
;
16+
}

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

-20
This file was deleted.

tests/baselines/reference/awaitUsingDeclarationsInForOf.2.js

+1-1
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
}
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+
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+
async function test() {
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)