@@ -38441,18 +38441,34 @@ namespace ts {
38441
38441
38442
38442
if (forInOrOfStatement.kind === SyntaxKind.ForOfStatement && forInOrOfStatement.awaitModifier) {
38443
38443
if ((forInOrOfStatement.flags & NodeFlags.AwaitContext) === NodeFlags.None) {
38444
- // use of 'for-await-of' in non-async function
38445
38444
const sourceFile = getSourceFileOfNode(forInOrOfStatement);
38446
- if (!hasParseDiagnostics(sourceFile)) {
38447
- const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier, Diagnostics.A_for_await_of_statement_is_only_allowed_within_an_async_function_or_async_generator);
38448
- const func = getContainingFunction(forInOrOfStatement);
38449
- if (func && func.kind !== SyntaxKind.Constructor) {
38450
- Debug.assert((getFunctionFlags(func) & FunctionFlags.Async) === 0, "Enclosing function should never be an async function.");
38451
- const relatedInfo = createDiagnosticForNode(func, Diagnostics.Did_you_mean_to_mark_this_function_as_async);
38452
- addRelatedInfo(diagnostic, relatedInfo);
38453
- }
38454
- diagnostics.add(diagnostic);
38455
- return true;
38445
+ if (isInTopLevelContext(forInOrOfStatement)) {
38446
+ if (!hasParseDiagnostics(sourceFile)) {
38447
+ if (!isEffectiveExternalModule(sourceFile, compilerOptions)) {
38448
+ const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier,
38449
+ 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);
38450
+ diagnostics.add(diagnostic);
38451
+ }
38452
+ if ((moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.System) || languageVersion < ScriptTarget.ES2017) {
38453
+ const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier,
38454
+ Diagnostics.Top_level_await_expressions_are_only_allowed_when_the_module_option_is_set_to_esnext_or_system_and_the_target_option_is_set_to_es2017_or_higher);
38455
+ diagnostics.add(diagnostic);
38456
+ }
38457
+ }
38458
+ }
38459
+ else {
38460
+ // use of 'for-await-of' in non-async function
38461
+ if (!hasParseDiagnostics(sourceFile)) {
38462
+ const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier, Diagnostics.A_for_await_of_statement_is_only_allowed_within_an_async_function_or_async_generator);
38463
+ const func = getContainingFunction(forInOrOfStatement);
38464
+ if (func && func.kind !== SyntaxKind.Constructor) {
38465
+ Debug.assert((getFunctionFlags(func) & FunctionFlags.Async) === 0, "Enclosing function should never be an async function.");
38466
+ const relatedInfo = createDiagnosticForNode(func, Diagnostics.Did_you_mean_to_mark_this_function_as_async);
38467
+ addRelatedInfo(diagnostic, relatedInfo);
38468
+ }
38469
+ diagnostics.add(diagnostic);
38470
+ return true;
38471
+ }
38456
38472
}
38457
38473
return false;
38458
38474
}
0 commit comments