From 3e12250839fae5054b7cedac25124da5be928066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Burzy=C5=84ski?= Date: Tue, 31 Oct 2023 20:06:36 +0100 Subject: [PATCH] Allow of in `await using` declarations in for-of loops (#55558) --- src/compiler/parser.ts | 7 ++----- ...UsingDeclarationsInForAwaitOf.2.errors.txt | 13 ++++++++++++ .../awaitUsingDeclarationsInForAwaitOf.2.js | 16 +++++++++++++++ ...awaitUsingDeclarationsInForOf.2.errors.txt | 20 ------------------- .../awaitUsingDeclarationsInForOf.2.js | 2 +- ...awaitUsingDeclarationsInForOf.4.errors.txt | 16 +++++++++++++++ .../awaitUsingDeclarationsInForOf.4.js | 16 +++++++++++++++ .../awaitUsingDeclarationsInForAwaitOf.2.ts | 10 ++++++++++ .../awaitUsingDeclarationsInForOf.4.ts | 10 ++++++++++ 9 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.errors.txt create mode 100644 tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.js delete mode 100644 tests/baselines/reference/awaitUsingDeclarationsInForOf.2.errors.txt create mode 100644 tests/baselines/reference/awaitUsingDeclarationsInForOf.4.errors.txt create mode 100644 tests/baselines/reference/awaitUsingDeclarationsInForOf.4.js create mode 100644 tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.2.ts create mode 100644 tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForOf.4.ts diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 7511678393d2b..2789da867967f 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -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); } @@ -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); diff --git a/tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.errors.txt b/tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.errors.txt new file mode 100644 index 0000000000000..3130733919f08 --- /dev/null +++ b/tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.errors.txt @@ -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. + } + \ No newline at end of file diff --git a/tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.js b/tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.js new file mode 100644 index 0000000000000..2c1883eb8b22a --- /dev/null +++ b/tests/baselines/reference/awaitUsingDeclarationsInForAwaitOf.2.js @@ -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) { } + ; +} diff --git a/tests/baselines/reference/awaitUsingDeclarationsInForOf.2.errors.txt b/tests/baselines/reference/awaitUsingDeclarationsInForOf.2.errors.txt deleted file mode 100644 index 2fd47176982f0..0000000000000 --- a/tests/baselines/reference/awaitUsingDeclarationsInForOf.2.errors.txt +++ /dev/null @@ -1,20 +0,0 @@ -awaitUsingDeclarationsInForOf.2.ts(2,10): error TS2487: The left-hand side of a 'for...of' statement must be a variable or a property access. -awaitUsingDeclarationsInForOf.2.ts(2,16): error TS2304: Cannot find name 'using'. -awaitUsingDeclarationsInForOf.2.ts(2,25): error TS2304: Cannot find name 'of'. -awaitUsingDeclarationsInForOf.2.ts(2,29): error TS1011: An element access expression should take an argument. - - -==== awaitUsingDeclarationsInForOf.2.ts (4 errors) ==== - async function main() { - for (await using of of []) { - ~~~~~~~~~~~ -!!! error TS2487: The left-hand side of a 'for...of' statement must be a variable or a property access. - ~~~~~ -!!! error TS2304: Cannot find name 'using'. - ~~ -!!! error TS2304: Cannot find name 'of'. - -!!! error TS1011: An element access expression should take an argument. - } - } - \ No newline at end of file diff --git a/tests/baselines/reference/awaitUsingDeclarationsInForOf.2.js b/tests/baselines/reference/awaitUsingDeclarationsInForOf.2.js index 19d7b7d575da2..be904e59dc1f7 100644 --- a/tests/baselines/reference/awaitUsingDeclarationsInForOf.2.js +++ b/tests/baselines/reference/awaitUsingDeclarationsInForOf.2.js @@ -9,6 +9,6 @@ async function main() { //// [awaitUsingDeclarationsInForOf.2.js] async function main() { - for (await using of of[]) { + for (await using of of []) { } } diff --git a/tests/baselines/reference/awaitUsingDeclarationsInForOf.4.errors.txt b/tests/baselines/reference/awaitUsingDeclarationsInForOf.4.errors.txt new file mode 100644 index 0000000000000..5c7894d2b5153 --- /dev/null +++ b/tests/baselines/reference/awaitUsingDeclarationsInForOf.4.errors.txt @@ -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. + } + \ No newline at end of file diff --git a/tests/baselines/reference/awaitUsingDeclarationsInForOf.4.js b/tests/baselines/reference/awaitUsingDeclarationsInForOf.4.js new file mode 100644 index 0000000000000..5541669060c52 --- /dev/null +++ b/tests/baselines/reference/awaitUsingDeclarationsInForOf.4.js @@ -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) { } + ; +} diff --git a/tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.2.ts b/tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.2.ts new file mode 100644 index 0000000000000..77f00d6463624 --- /dev/null +++ b/tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForAwaitOf.2.ts @@ -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) {}; +} diff --git a/tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForOf.4.ts b/tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForOf.4.ts new file mode 100644 index 0000000000000..6edaa895a3bc7 --- /dev/null +++ b/tests/cases/conformance/statements/VariableStatements/usingDeclarations/awaitUsingDeclarationsInForOf.4.ts @@ -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) {}; +}