Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow of in await using declarations in for-of loops #55558

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {};
}
Loading