Skip to content

Commit 97d51a7

Browse files
authored
Update implied default for module based on target (#63076)
1 parent 1fd0c53 commit 97d51a7

2 files changed

Lines changed: 19 additions & 11 deletions

File tree

src/compiler/utilities.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9000,9 +9000,23 @@ const _computedOptions = createComputedCompilerOptions({
90009000
module: {
90019001
dependencies: ["target"],
90029002
computeValue: (compilerOptions): ModuleKind => {
9003-
return typeof compilerOptions.module === "number" ?
9004-
compilerOptions.module :
9005-
_computedOptions.target.computeValue(compilerOptions) >= ScriptTarget.ES2015 ? ModuleKind.ES2015 : ModuleKind.CommonJS;
9003+
if (typeof compilerOptions.module === "number") {
9004+
return compilerOptions.module;
9005+
}
9006+
const target = _computedOptions.target.computeValue(compilerOptions);
9007+
if (target === ScriptTarget.ESNext) {
9008+
return ModuleKind.ESNext;
9009+
}
9010+
if (target >= ScriptTarget.ES2022) {
9011+
return ModuleKind.ES2022;
9012+
}
9013+
if (target >= ScriptTarget.ES2020) {
9014+
return ModuleKind.ES2020;
9015+
}
9016+
if (target >= ScriptTarget.ES2015) {
9017+
return ModuleKind.ES2015;
9018+
}
9019+
return ModuleKind.CommonJS;
90069020
},
90079021
},
90089022
moduleResolution: {

tests/baselines/reference/awaitInNonAsyncFunction.errors.txt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ awaitInNonAsyncFunction.ts(30,9): error TS1103: 'for await' loops are only allow
1212
awaitInNonAsyncFunction.ts(31,5): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
1313
awaitInNonAsyncFunction.ts(34,7): error TS1103: 'for await' loops are only allowed within async functions and at the top levels of modules.
1414
awaitInNonAsyncFunction.ts(35,5): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
15-
awaitInNonAsyncFunction.ts(39,5): error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
16-
awaitInNonAsyncFunction.ts(40,1): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
1715

1816

19-
==== awaitInNonAsyncFunction.ts (16 errors) ====
17+
==== awaitInNonAsyncFunction.ts (14 errors) ====
2018
// https://github.com/Microsoft/TypeScript/issues/26586
2119

2220
function normalFunc(p: Promise<number>) {
@@ -96,8 +94,4 @@ awaitInNonAsyncFunction.ts(40,1): error TS1378: Top-level 'await' expressions ar
9694
}
9795

9896
for await (const _ of []);
99-
~~~~~
100-
!!! error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
101-
await null;
102-
~~~~~
103-
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', 'node16', 'node18', 'node20', 'nodenext', or 'preserve', and the 'target' option is set to 'es2017' or higher.
97+
await null;

0 commit comments

Comments
 (0)