Skip to content

Commit c89c9c3

Browse files
committed
fix(46149): Add specific top level await error messages
1 parent 65ae16c commit c89c9c3

File tree

42 files changed

+3628
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+3628
-66
lines changed

src/compiler/checker.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32183,10 +32183,23 @@ namespace ts {
3218332183
Diagnostics.await_expressions_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);
3218432184
diagnostics.add(diagnostic);
3218532185
}
32186-
if ((moduleKind !== ModuleKind.ES2022 && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017) {
32187-
span = getSpanOfTokenAtPosition(sourceFile, node.pos);
32186+
span = getSpanOfTokenAtPosition(sourceFile, node.pos);
32187+
if (isNotTopLevelAwaitSupportedModuleKind(moduleKind, getSourceFileOfNode(node).impliedNodeFormat)) {
32188+
if(moduleKind !== ModuleKind.NodeNext) {
32189+
const suggestedKind = moduleKind === ModuleKind.Node12 ? ModuleKind.NodeNext : ModuleKind.ES2022;
32190+
const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length,
32191+
Diagnostics.The_0_setting_1_does_not_support_top_level_await_expressions_Consider_switching_to_2, "module", ModuleKind[moduleKind], ModuleKind[suggestedKind]);
32192+
diagnostics.add(diagnostic);
32193+
}
32194+
else {
32195+
const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length,
32196+
Diagnostics._0_is_not_allowed_in_CommonJS_modules_Please_convert_to_ES_module, "Top-level 'await' expression");
32197+
diagnostics.add(diagnostic);
32198+
}
32199+
}
32200+
if(languageVersion < ScriptTarget.ES2017) {
3218832201
const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length,
32189-
Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher);
32202+
Diagnostics.The_0_setting_1_does_not_support_top_level_await_expressions_Consider_switching_to_2, "target", ScriptTarget[languageVersion], ScriptTarget[ScriptTarget.ES2017]);
3219032203
diagnostics.add(diagnostic);
3219132204
}
3219232205
}
@@ -42867,9 +42880,23 @@ namespace ts {
4286742880
diagnostics.add(createDiagnosticForNode(forInOrOfStatement.awaitModifier,
4286842881
Diagnostics.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));
4286942882
}
42870-
if ((moduleKind !== ModuleKind.ES2022 && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(forInOrOfStatement).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017) {
42871-
diagnostics.add(createDiagnosticForNode(forInOrOfStatement.awaitModifier,
42872-
Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher));
42883+
if (isNotTopLevelAwaitSupportedModuleKind(moduleKind, getSourceFileOfNode(forInOrOfStatement).impliedNodeFormat)) {
42884+
if(moduleKind !== ModuleKind.NodeNext) {
42885+
const suggestedKind = moduleKind === ModuleKind.Node12 ? ModuleKind.NodeNext : ModuleKind.ES2022;
42886+
const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier,
42887+
Diagnostics.The_0_setting_1_does_not_support_top_level_for_await_loops_Consider_switching_to_2, "module", ModuleKind[moduleKind], ModuleKind[suggestedKind]);
42888+
diagnostics.add(diagnostic);
42889+
}
42890+
else {
42891+
const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier,
42892+
Diagnostics._0_is_not_allowed_in_CommonJS_modules_Please_convert_to_ES_module, "Top-level 'for await' loop");
42893+
diagnostics.add(diagnostic);
42894+
}
42895+
}
42896+
if(languageVersion < ScriptTarget.ES2017) {
42897+
const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier,
42898+
Diagnostics.The_0_setting_1_does_not_support_top_level_for_await_loops_Consider_switching_to_2, "target", ScriptTarget[languageVersion], ScriptTarget[ScriptTarget.ES2017]);
42899+
diagnostics.add(diagnostic);
4287342900
}
4287442901
}
4287542902
}

src/compiler/diagnosticMessages.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@
11161116
"category": "Message",
11171117
"code": 1377
11181118
},
1119-
"Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.": {
1119+
"The '{0}' setting '{1}' does not support top-level 'await' expressions. Consider switching to '{2}'.": {
11201120
"category": "Error",
11211121
"code": 1378
11221122
},
@@ -1324,7 +1324,7 @@
13241324
"category": "Error",
13251325
"code": 1431
13261326
},
1327-
"Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.": {
1327+
"The '{0}' setting '{1}' does not support top-level 'for await' loops. Consider switching to '{2}'.": {
13281328
"category": "Error",
13291329
"code": 1432
13301330
},
@@ -1405,6 +1405,10 @@
14051405
"category": "Error",
14061406
"code": 1471
14071407
},
1408+
"{0} is not allowed in CommonJS modules. Please convert to ES module.": {
1409+
"category": "Error",
1410+
"code": 1472
1411+
},
14081412

14091413
"The types of '{0}' are incompatible between these types.": {
14101414
"category": "Error",

src/compiler/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6247,7 +6247,7 @@ namespace ts {
62476247
Deferred = 7
62486248
}
62496249

6250-
export const enum ScriptTarget {
6250+
export enum ScriptTarget {
62516251
ES3 = 0,
62526252
ES5 = 1,
62536253
ES2015 = 2,

src/compiler/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,10 @@ namespace ts {
800800
return isExternalModule(node) || compilerOptions.isolatedModules || (isCommonJSContainingModuleKind(getEmitModuleKind(compilerOptions)) && !!node.commonJsModuleIndicator);
801801
}
802802

803+
export function isNotTopLevelAwaitSupportedModuleKind(kind: ModuleKind, impliedNodeFormat?: ModuleKind.ESNext | ModuleKind.CommonJS){
804+
return kind !== ModuleKind.ES2022 && kind !== ModuleKind.ESNext && kind !== ModuleKind.System && !(kind === ModuleKind.NodeNext && impliedNodeFormat === ModuleKind.ESNext);
805+
}
806+
803807
/**
804808
* Returns whether the source file will be treated as if it were in strict mode at runtime.
805809
*/

src/services/codefixes/fixModuleAndTargetOptions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
namespace ts.codefix {
33
registerCodeFix({
44
errorCodes: [
5-
Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher.code,
6-
Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher.code,
5+
Diagnostics.The_0_setting_1_does_not_support_top_level_await_expressions_Consider_switching_to_2.code,
6+
Diagnostics.The_0_setting_1_does_not_support_top_level_for_await_loops_Consider_switching_to_2.code,
77
],
88
getCodeActions: context => {
99
const compilerOptions = context.program.getCompilerOptions();

tests/baselines/reference/nodeModulesAllowJsTopLevelAwait(module=node12).errors.txt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
tests/cases/conformance/node/allowJs/index.js(2,11): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
2-
tests/cases/conformance/node/allowJs/index.js(4,5): error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
3-
tests/cases/conformance/node/allowJs/subfolder/index.js(2,11): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
4-
tests/cases/conformance/node/allowJs/subfolder/index.js(4,5): error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
1+
tests/cases/conformance/node/allowJs/index.js(2,11): error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
2+
tests/cases/conformance/node/allowJs/index.js(4,5): error TS1432: The 'module' setting 'Node12' does not support top-level 'for await' loops. Consider switching to 'NodeNext'.
3+
tests/cases/conformance/node/allowJs/subfolder/index.js(2,11): error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
4+
tests/cases/conformance/node/allowJs/subfolder/index.js(4,5): error TS1432: The 'module' setting 'Node12' does not support top-level 'for await' loops. Consider switching to 'NodeNext'.
55

66

77
==== tests/cases/conformance/node/allowJs/subfolder/index.js (2 errors) ====
88
// cjs format file
99
const x = await 1;
1010
~~~~~
11-
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
11+
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
1212
export {x};
1313
for await (const y of []) {}
1414
~~~~~
15-
!!! error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
15+
!!! error TS1432: The 'module' setting 'Node12' does not support top-level 'for await' loops. Consider switching to 'NodeNext'.
1616
==== tests/cases/conformance/node/allowJs/index.js (2 errors) ====
1717
// esm format file
1818
const x = await 1;
1919
~~~~~
20-
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
20+
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
2121
export {x};
2222
for await (const y of []) {}
2323
~~~~~
24-
!!! error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
24+
!!! error TS1432: The 'module' setting 'Node12' does not support top-level 'for await' loops. Consider switching to 'NodeNext'.
2525
==== tests/cases/conformance/node/allowJs/package.json (0 errors) ====
2626
{
2727
"name": "package",

tests/baselines/reference/nodeModulesAllowJsTopLevelAwait(module=nodenext).errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
tests/cases/conformance/node/allowJs/subfolder/index.js(2,11): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
2-
tests/cases/conformance/node/allowJs/subfolder/index.js(4,5): error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
1+
tests/cases/conformance/node/allowJs/subfolder/index.js(2,11): error TS1472: Top-level 'await' expression is not allowed in CommonJS modules. Please convert to ES module.
2+
tests/cases/conformance/node/allowJs/subfolder/index.js(4,5): error TS1472: Top-level 'for await' loop is not allowed in CommonJS modules. Please convert to ES module.
33

44

55
==== tests/cases/conformance/node/allowJs/subfolder/index.js (2 errors) ====
66
// cjs format file
77
const x = await 1;
88
~~~~~
9-
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
9+
!!! error TS1472: Top-level 'await' expression is not allowed in CommonJS modules. Please convert to ES module.
1010
export {x};
1111
for await (const y of []) {}
1212
~~~~~
13-
!!! error TS1432: Top-level 'for await' loops are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
13+
!!! error TS1472: Top-level 'for await' loop is not allowed in CommonJS modules. Please convert to ES module.
1414
==== tests/cases/conformance/node/allowJs/index.js (0 errors) ====
1515
// esm format file
1616
const x = await 1;

tests/baselines/reference/nodeModulesExportsBlocksSpecifierResolution(module=node12).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other' or its corresponding type declarations.
22
tests/cases/conformance/node/index.ts(3,14): error TS2742: The inferred type of 'a' cannot be named without a reference to './node_modules/inner/other.js'. This is likely not portable. A type annotation is necessary.
3-
tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
3+
tests/cases/conformance/node/index.ts(3,19): error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
44

55

66
==== tests/cases/conformance/node/index.ts (3 errors) ====
@@ -12,7 +12,7 @@ tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' exp
1212
~
1313
!!! error TS2742: The inferred type of 'a' cannot be named without a reference to './node_modules/inner/other.js'. This is likely not portable. A type annotation is necessary.
1414
~~~~~
15-
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
15+
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
1616
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
1717
// esm format file
1818
export { x } from "./other.js";

tests/baselines/reference/nodeModulesExportsSpecifierGenerationConditions(module=node12).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other.js' or its corresponding type declarations.
2-
tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
2+
tests/cases/conformance/node/index.ts(3,19): error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
33

44

55
==== tests/cases/conformance/node/index.ts (2 errors) ====
@@ -9,7 +9,7 @@ tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' exp
99
!!! error TS2307: Cannot find module 'inner/other.js' or its corresponding type declarations.
1010
export const a = (await import("inner")).x();
1111
~~~~~
12-
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
12+
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
1313
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
1414
// esm format file
1515
export { x } from "./other.js";

tests/baselines/reference/nodeModulesExportsSpecifierGenerationDirectory(module=node12).errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other' or its corresponding type declarations.
2-
tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
2+
tests/cases/conformance/node/index.ts(3,19): error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
33

44

55
==== tests/cases/conformance/node/index.ts (2 errors) ====
@@ -9,7 +9,7 @@ tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' exp
99
!!! error TS2307: Cannot find module 'inner/other' or its corresponding type declarations.
1010
export const a = (await import("inner/index.js")).x();
1111
~~~~~
12-
!!! error TS1378: Top-level 'await' expressions are only allowed when the 'module' option is set to 'es2022', 'esnext', 'system', or 'nodenext', and the 'target' option is set to 'es2017' or higher.
12+
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
1313
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
1414
// esm format file
1515
export { x } from "./other.js";

0 commit comments

Comments
 (0)