Fixed an issue with top-level for-await loops not being allowed with --module preserve (#59042)

This commit is contained in:
Mateusz Burzyński authored and GitHub committed 2024-06-26 12:40:07 -07:00
1 parent 15f67e0b48
commit c8f2405456
7 files changed
+94

No files matched your search

+1
View File
@@ -51097,6 +51097,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// fallthrough
case ModuleKind.ES2022:
case ModuleKind.ESNext:
case ModuleKind.Preserve:
case ModuleKind.System:
if (languageVersion >= ScriptTarget.ES2017) {
break;
@@ -0,0 +1,14 @@
modulePreserveTopLevelAwait1.ts(1,5): error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
modulePreserveTopLevelAwait1.ts(2,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
==== modulePreserveTopLevelAwait1.ts (2 errors) ====
for await (const x of []) {}
~~~~~
!!! error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
await Promise.resolve();
~~~~~
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
export {};
@@ -0,0 +1,13 @@
//// [tests/cases/compiler/modulePreserveTopLevelAwait1.ts] ////
=== modulePreserveTopLevelAwait1.ts ===
for await (const x of []) {}
>x : Symbol(x, Decl(modulePreserveTopLevelAwait1.ts, 0, 16))
await Promise.resolve();
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
export {};
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/modulePreserveTopLevelAwait1.ts] ////
=== modulePreserveTopLevelAwait1.ts ===
for await (const x of []) {}
>x : any
> : ^^^
>[] : undefined[]
> : ^^^^^^^^^^^
await Promise.resolve();
>await Promise.resolve() : void
> : ^^^^
>Promise.resolve() : Promise<void>
> : ^^^^^^^^^^^^^
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^
>Promise : PromiseConstructor
> : ^^^^^^^^^^^^^^^^^^
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^
export {};
@@ -0,0 +1,13 @@
//// [tests/cases/compiler/modulePreserveTopLevelAwait1.ts] ////
=== modulePreserveTopLevelAwait1.ts ===
for await (const x of []) {}
>x : Symbol(x, Decl(modulePreserveTopLevelAwait1.ts, 0, 16))
await Promise.resolve();
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --))
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
export {};
@@ -0,0 +1,22 @@
//// [tests/cases/compiler/modulePreserveTopLevelAwait1.ts] ////
=== modulePreserveTopLevelAwait1.ts ===
for await (const x of []) {}
>x : any
>[] : undefined[]
> : ^^^^^^^^^^^
await Promise.resolve();
>await Promise.resolve() : void
> : ^^^^
>Promise.resolve() : Promise<void>
> : ^^^^^^^^^^^^^
>Promise.resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^
>Promise : PromiseConstructor
> : ^^^^^^^^^^^^^^^^^^
>resolve : { (): Promise<void>; <T>(value: T): Promise<Awaited<T>>; <T>(value: T | PromiseLike<T>): Promise<Awaited<T>>; }
> : ^^^^^^ ^^^ ^^ ^^ ^^^ ^^^ ^^ ^^ ^^^ ^^^
export {};
@@ -0,0 +1,8 @@
// @module: preserve
// @target: es2016, esnext
// @noEmit: true
for await (const x of []) {}
await Promise.resolve();
export {};