Skip to content

Commit

Permalink
Allow of in await using declarations in for-of loops (#55558)
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored Oct 31, 2023
1 parent 42d0e51 commit 3e12250
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 26 deletions.
7 changes: 2 additions & 5 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6892,7 +6892,8 @@ namespace Parser {
if (
token() === SyntaxKind.VarKeyword || token() === SyntaxKind.LetKeyword || token() === SyntaxKind.ConstKeyword ||
token() === SyntaxKind.UsingKeyword && lookAhead(nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLineDisallowOf) ||
token() === SyntaxKind.AwaitKeyword && lookAhead(nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLineDisallowOf)
// this one is meant to allow of
token() === SyntaxKind.AwaitKeyword && lookAhead(nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine)
) {
initializer = parseVariableDeclarationList(/*inForStatementInitializer*/ true);
}
Expand Down Expand Up @@ -7308,10 +7309,6 @@ namespace Parser {
return lookAhead(nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLine);
}

function nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLineDisallowOf() {
return nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine(/*disallowOf*/ true);
}

function nextTokenIsUsingKeywordThenBindingIdentifierOrStartOfObjectDestructuringOnSameLine(disallowOf?: boolean) {
if (nextToken() === SyntaxKind.UsingKeyword) {
return nextTokenIsBindingIdentifierOrStartOfDestructuringOnSameLine(disallowOf);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
awaitUsingDeclarationsInForAwaitOf.2.ts(4,32): error TS2448: Block-scoped variable 'of' used before its declaration.


==== awaitUsingDeclarationsInForAwaitOf.2.ts (1 errors) ====
// https://github.com/microsoft/TypeScript/issues/55555

async function test() {
for await (await using of of of) {};
~~
!!! error TS2448: Block-scoped variable 'of' used before its declaration.
!!! related TS2728 awaitUsingDeclarationsInForAwaitOf.2.ts:4:26: 'of' is declared here.
}

16 changes: 16 additions & 0 deletions tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.2.ts] ////

//// [awaitUsingDeclarationsInForAwaitOf.2.ts]
// https://github.com/microsoft/TypeScript/issues/55555

async function test() {
for await (await using of of of) {};
}


//// [awaitUsingDeclarationsInForAwaitOf.2.js]
// https://github.com/microsoft/TypeScript/issues/55555
async function test() {
for await (await using of of of) { }
;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ async function main() {

//// [awaitUsingDeclarationsInForOf.2.js]
async function main() {
for (await using of of[]) {
for (await using of of []) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
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.
awaitUsingDeclarationsInForOf.4.ts(4,26): error TS2448: Block-scoped variable 'of' used before its declaration.


==== awaitUsingDeclarationsInForOf.4.ts (2 errors) ====
// https://github.com/microsoft/TypeScript/issues/55555

{
for (await using of of of) {};
~~~~~
!!! 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.
~~
!!! error TS2448: Block-scoped variable 'of' used before its declaration.
!!! related TS2728 awaitUsingDeclarationsInForOf.4.ts:4:20: 'of' is declared here.
}

16 changes: 16 additions & 0 deletions tests/baselines/reference/awaitUsingDeclarationsInForOf.4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForOf.4.ts] ////

//// [awaitUsingDeclarationsInForOf.4.ts]
// https://github.com/microsoft/TypeScript/issues/55555

{
for (await using of of of) {};
}


//// [awaitUsingDeclarationsInForOf.4.js]
// https://github.com/microsoft/TypeScript/issues/55555
{
for (await using of of of) { }
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @target: esnext
// @module: esnext
// @lib: esnext
// @noTypesAndSymbols: true

// https://github.com/microsoft/TypeScript/issues/55555

async function test() {
for await (await using of of of) {};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @target: esnext
// @module: esnext
// @lib: esnext
// @noTypesAndSymbols: true

// https://github.com/microsoft/TypeScript/issues/55555

{
for (await using of of of) {};
}

0 comments on commit 3e12250

Please sign in to comment.