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 1 commit
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 @@ -6877,7 +6877,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 @@ -7293,10 +7294,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,15 @@
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.
awaitUsingDeclarationsInForAwaitOf.2.ts(4,32): error TS2448: Block-scoped variable 'of' used before its declaration.


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

{
for await (await using of of of) {};
~~~~~
!!! 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we put this in an async context, like an async function so that we don't have this spurious error in errors.txt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just pushed out the requested change. Could you take another look at this?

~~
!!! error TS2448: Block-scoped variable 'of' used before its declaration.
!!! related TS2728 awaitUsingDeclarationsInForAwaitOf.2.ts:4:26: 'of' is declared here.
}
15 changes: 15 additions & 0 deletions tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//// [tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.2.ts] ////

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

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

//// [awaitUsingDeclarationsInForAwaitOf.2.js]
// https://github.com/microsoft/TypeScript/issues/55555
{
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

{
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