Skip to content

Commit

Permalink
fix(46149): Add specific top level await error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sidharthv96 committed Oct 22, 2021
1 parent 65ae16c commit c89c9c3
Show file tree
Hide file tree
Showing 42 changed files with 3,628 additions and 66 deletions.
39 changes: 33 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32183,10 +32183,23 @@ namespace ts {
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);
diagnostics.add(diagnostic);
}
if ((moduleKind !== ModuleKind.ES2022 && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017) {
span = getSpanOfTokenAtPosition(sourceFile, node.pos);
span = getSpanOfTokenAtPosition(sourceFile, node.pos);
if (isNotTopLevelAwaitSupportedModuleKind(moduleKind, getSourceFileOfNode(node).impliedNodeFormat)) {
if(moduleKind !== ModuleKind.NodeNext) {
const suggestedKind = moduleKind === ModuleKind.Node12 ? ModuleKind.NodeNext : ModuleKind.ES2022;
const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length,
Diagnostics.The_0_setting_1_does_not_support_top_level_await_expressions_Consider_switching_to_2, "module", ModuleKind[moduleKind], ModuleKind[suggestedKind]);
diagnostics.add(diagnostic);
}
else {
const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length,
Diagnostics._0_is_not_allowed_in_CommonJS_modules_Please_convert_to_ES_module, "Top-level 'await' expression");
diagnostics.add(diagnostic);
}
}
if(languageVersion < ScriptTarget.ES2017) {
const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length,
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);
Diagnostics.The_0_setting_1_does_not_support_top_level_await_expressions_Consider_switching_to_2, "target", ScriptTarget[languageVersion], ScriptTarget[ScriptTarget.ES2017]);
diagnostics.add(diagnostic);
}
}
Expand Down Expand Up @@ -42867,9 +42880,23 @@ namespace ts {
diagnostics.add(createDiagnosticForNode(forInOrOfStatement.awaitModifier,
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));
}
if ((moduleKind !== ModuleKind.ES2022 && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System && !(moduleKind === ModuleKind.NodeNext && getSourceFileOfNode(forInOrOfStatement).impliedNodeFormat === ModuleKind.ESNext)) || languageVersion < ScriptTarget.ES2017) {
diagnostics.add(createDiagnosticForNode(forInOrOfStatement.awaitModifier,
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));
if (isNotTopLevelAwaitSupportedModuleKind(moduleKind, getSourceFileOfNode(forInOrOfStatement).impliedNodeFormat)) {
if(moduleKind !== ModuleKind.NodeNext) {
const suggestedKind = moduleKind === ModuleKind.Node12 ? ModuleKind.NodeNext : ModuleKind.ES2022;
const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier,
Diagnostics.The_0_setting_1_does_not_support_top_level_for_await_loops_Consider_switching_to_2, "module", ModuleKind[moduleKind], ModuleKind[suggestedKind]);
diagnostics.add(diagnostic);
}
else {
const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier,
Diagnostics._0_is_not_allowed_in_CommonJS_modules_Please_convert_to_ES_module, "Top-level 'for await' loop");
diagnostics.add(diagnostic);
}
}
if(languageVersion < ScriptTarget.ES2017) {
const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier,
Diagnostics.The_0_setting_1_does_not_support_top_level_for_await_loops_Consider_switching_to_2, "target", ScriptTarget[languageVersion], ScriptTarget[ScriptTarget.ES2017]);
diagnostics.add(diagnostic);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1116,7 +1116,7 @@
"category": "Message",
"code": 1377
},
"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.": {
"The '{0}' setting '{1}' does not support top-level 'await' expressions. Consider switching to '{2}'.": {
"category": "Error",
"code": 1378
},
Expand Down Expand Up @@ -1324,7 +1324,7 @@
"category": "Error",
"code": 1431
},
"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.": {
"The '{0}' setting '{1}' does not support top-level 'for await' loops. Consider switching to '{2}'.": {
"category": "Error",
"code": 1432
},
Expand Down Expand Up @@ -1405,6 +1405,10 @@
"category": "Error",
"code": 1471
},
"{0} is not allowed in CommonJS modules. Please convert to ES module.": {
"category": "Error",
"code": 1472
},

"The types of '{0}' are incompatible between these types.": {
"category": "Error",
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6247,7 +6247,7 @@ namespace ts {
Deferred = 7
}

export const enum ScriptTarget {
export enum ScriptTarget {
ES3 = 0,
ES5 = 1,
ES2015 = 2,
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,10 @@ namespace ts {
return isExternalModule(node) || compilerOptions.isolatedModules || (isCommonJSContainingModuleKind(getEmitModuleKind(compilerOptions)) && !!node.commonJsModuleIndicator);
}

export function isNotTopLevelAwaitSupportedModuleKind(kind: ModuleKind, impliedNodeFormat?: ModuleKind.ESNext | ModuleKind.CommonJS){
return kind !== ModuleKind.ES2022 && kind !== ModuleKind.ESNext && kind !== ModuleKind.System && !(kind === ModuleKind.NodeNext && impliedNodeFormat === ModuleKind.ESNext);
}

/**
* Returns whether the source file will be treated as if it were in strict mode at runtime.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/services/codefixes/fixModuleAndTargetOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
namespace ts.codefix {
registerCodeFix({
errorCodes: [
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,
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,
Diagnostics.The_0_setting_1_does_not_support_top_level_await_expressions_Consider_switching_to_2.code,
Diagnostics.The_0_setting_1_does_not_support_top_level_for_await_loops_Consider_switching_to_2.code,
],
getCodeActions: context => {
const compilerOptions = context.program.getCompilerOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
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.
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.
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.
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.
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'.
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'.
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'.
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'.


==== tests/cases/conformance/node/allowJs/subfolder/index.js (2 errors) ====
// cjs format file
const x = await 1;
~~~~~
!!! 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.
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
export {x};
for await (const y of []) {}
~~~~~
!!! 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.
!!! error TS1432: The 'module' setting 'Node12' does not support top-level 'for await' loops. Consider switching to 'NodeNext'.
==== tests/cases/conformance/node/allowJs/index.js (2 errors) ====
// esm format file
const x = await 1;
~~~~~
!!! 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.
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
export {x};
for await (const y of []) {}
~~~~~
!!! 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.
!!! error TS1432: The 'module' setting 'Node12' does not support top-level 'for await' loops. Consider switching to 'NodeNext'.
==== tests/cases/conformance/node/allowJs/package.json (0 errors) ====
{
"name": "package",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
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.
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.
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.
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.


==== tests/cases/conformance/node/allowJs/subfolder/index.js (2 errors) ====
// cjs format file
const x = await 1;
~~~~~
!!! 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.
!!! error TS1472: Top-level 'await' expression is not allowed in CommonJS modules. Please convert to ES module.
export {x};
for await (const y of []) {}
~~~~~
!!! 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.
!!! error TS1472: Top-level 'for await' loop is not allowed in CommonJS modules. Please convert to ES module.
==== tests/cases/conformance/node/allowJs/index.js (0 errors) ====
// esm format file
const x = await 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other' or its corresponding type declarations.
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.
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.
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'.


==== tests/cases/conformance/node/index.ts (3 errors) ====
Expand All @@ -12,7 +12,7 @@ tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' exp
~
!!! 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.
~~~~~
!!! 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.
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
// esm format file
export { x } from "./other.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other.js' or its corresponding type declarations.
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.
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'.


==== tests/cases/conformance/node/index.ts (2 errors) ====
Expand All @@ -9,7 +9,7 @@ tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' exp
!!! error TS2307: Cannot find module 'inner/other.js' or its corresponding type declarations.
export const a = (await import("inner")).x();
~~~~~
!!! 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.
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
// esm format file
export { x } from "./other.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other' or its corresponding type declarations.
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.
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'.


==== tests/cases/conformance/node/index.ts (2 errors) ====
Expand All @@ -9,7 +9,7 @@ tests/cases/conformance/node/index.ts(3,19): error TS1378: Top-level 'await' exp
!!! error TS2307: Cannot find module 'inner/other' or its corresponding type declarations.
export const a = (await import("inner/index.js")).x();
~~~~~
!!! 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.
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
// esm format file
export { x } from "./other.js";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/node/index.ts(2,23): error TS2307: Cannot find module 'inner/other' or its corresponding type declarations.
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.
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'.
tests/cases/conformance/node/index.ts(3,32): error TS2307: Cannot find module 'inner/index.js' or its corresponding type declarations.


Expand All @@ -10,7 +10,7 @@ tests/cases/conformance/node/index.ts(3,32): error TS2307: Cannot find module 'i
!!! error TS2307: Cannot find module 'inner/other' or its corresponding type declarations.
export const a = (await import("inner/index.js")).x();
~~~~~
!!! 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.
!!! error TS1378: The 'module' setting 'Node12' does not support top-level 'await' expressions. Consider switching to 'NodeNext'.
~~~~~~~~~~~~~~~~
!!! error TS2307: Cannot find module 'inner/index.js' or its corresponding type declarations.
==== tests/cases/conformance/node/node_modules/inner/index.d.ts (0 errors) ====
Expand Down

0 comments on commit c89c9c3

Please sign in to comment.