MCPcopy Index your code
hub / github.com/nodejs/node / checkGrammarForInOrForOfStatement

Function checkGrammarForInOrForOfStatement

test/fixtures/snapshot/typescript.js:88428–88519  ·  view source on GitHub ↗
(forInOrOfStatement)

Source from the content-addressed store, hash-verified

88426 }
88427 }
88428 function checkGrammarForInOrForOfStatement(forInOrOfStatement) {
88429 if (checkGrammarStatementInAmbientContext(forInOrOfStatement)) {
88430 return true;
88431 }
88432 if (forInOrOfStatement.kind === 244 /* SyntaxKind.ForOfStatement */ && forInOrOfStatement.awaitModifier) {
88433 if (!(forInOrOfStatement.flags & 32768 /* NodeFlags.AwaitContext */)) {
88434 var sourceFile = ts.getSourceFileOfNode(forInOrOfStatement);
88435 if (ts.isInTopLevelContext(forInOrOfStatement)) {
88436 if (!hasParseDiagnostics(sourceFile)) {
88437 if (!ts.isEffectiveExternalModule(sourceFile, compilerOptions)) {
88438 diagnostics.add(ts.createDiagnosticForNode(forInOrOfStatement.awaitModifier, ts.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));
88439 }
88440 switch (moduleKind) {
88441 case ts.ModuleKind.Node16:
88442 case ts.ModuleKind.NodeNext:
88443 if (sourceFile.impliedNodeFormat === ts.ModuleKind.CommonJS) {
88444 diagnostics.add(ts.createDiagnosticForNode(forInOrOfStatement.awaitModifier, ts.Diagnostics.The_current_file_is_a_CommonJS_module_and_cannot_use_await_at_the_top_level));
88445 break;
88446 }
88447 // fallthrough
88448 case ts.ModuleKind.ES2022:
88449 case ts.ModuleKind.ESNext:
88450 case ts.ModuleKind.System:
88451 if (languageVersion >= 4 /* ScriptTarget.ES2017 */) {
88452 break;
88453 }
88454 // fallthrough
88455 default:
88456 diagnostics.add(ts.createDiagnosticForNode(forInOrOfStatement.awaitModifier, ts.Diagnostics.Top_level_for_await_loops_are_only_allowed_when_the_module_option_is_set_to_es2022_esnext_system_node16_or_nodenext_and_the_target_option_is_set_to_es2017_or_higher));
88457 break;
88458 }
88459 }
88460 }
88461 else {
88462 // use of 'for-await-of' in non-async function
88463 if (!hasParseDiagnostics(sourceFile)) {
88464 var diagnostic = ts.createDiagnosticForNode(forInOrOfStatement.awaitModifier, ts.Diagnostics.for_await_loops_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules);
88465 var func = ts.getContainingFunction(forInOrOfStatement);
88466 if (func && func.kind !== 171 /* SyntaxKind.Constructor */) {
88467 ts.Debug.assert((ts.getFunctionFlags(func) & 2 /* FunctionFlags.Async */) === 0, "Enclosing function should never be an async function.");
88468 var relatedInfo = ts.createDiagnosticForNode(func, ts.Diagnostics.Did_you_mean_to_mark_this_function_as_async);
88469 ts.addRelatedInfo(diagnostic, relatedInfo);
88470 }
88471 diagnostics.add(diagnostic);
88472 return true;
88473 }
88474 }
88475 return false;
88476 }
88477 }
88478 if (ts.isForOfStatement(forInOrOfStatement) && !(forInOrOfStatement.flags & 32768 /* NodeFlags.AwaitContext */) &&
88479 ts.isIdentifier(forInOrOfStatement.initializer) && forInOrOfStatement.initializer.escapedText === "async") {
88480 grammarErrorOnNode(forInOrOfStatement.initializer, ts.Diagnostics.The_left_hand_side_of_a_for_of_statement_may_not_be_async);
88481 return false;
88482 }
88483 if (forInOrOfStatement.initializer.kind === 255 /* SyntaxKind.VariableDeclarationList */) {
88484 var variableList = forInOrOfStatement.initializer;
88485 if (!checkGrammarVariableDeclarationList(variableList)) {

Callers 2

checkForOfStatementFunction · 0.85
checkForInStatementFunction · 0.85

Calls 7

hasParseDiagnosticsFunction · 0.85
grammarErrorOnNodeFunction · 0.85
grammarErrorOnFirstTokenFunction · 0.85
assertMethod · 0.80
addMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…