Skip to content

Commit 1e3f239

Browse files
hikarino-myelibarzilay
authored andcommitted
Support Top Level "for await of".
1 parent 6430211 commit 1e3f239

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/compiler/checker.ts

+27-11
Original file line numberDiff line numberDiff line change
@@ -38441,18 +38441,34 @@ namespace ts {
3844138441

3844238442
if (forInOrOfStatement.kind === SyntaxKind.ForOfStatement && forInOrOfStatement.awaitModifier) {
3844338443
if ((forInOrOfStatement.flags & NodeFlags.AwaitContext) === NodeFlags.None) {
38444-
// use of 'for-await-of' in non-async function
3844538444
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+
}
3845638472
}
3845738473
return false;
3845838474
}

0 commit comments

Comments
 (0)