MCPcopy Create free account
hub / github.com/nodejs/node / checkAwaitExpressionGrammar

Function checkAwaitExpressionGrammar

test/fixtures/snapshot/typescript.js:78398–78454  ·  view source on GitHub ↗
(node)

Source from the content-addressed store, hash-verified

78396 return undefinedWideningType;
78397 }
78398 function checkAwaitExpressionGrammar(node) {
78399 // Grammar checking
78400 var container = ts.getContainingFunctionOrClassStaticBlock(node);
78401 if (container && ts.isClassStaticBlockDeclaration(container)) {
78402 error(node, ts.Diagnostics.Await_expression_cannot_be_used_inside_a_class_static_block);
78403 }
78404 else if (!(node.flags & 32768 /* NodeFlags.AwaitContext */)) {
78405 if (ts.isInTopLevelContext(node)) {
78406 var sourceFile = ts.getSourceFileOfNode(node);
78407 if (!hasParseDiagnostics(sourceFile)) {
78408 var span = void 0;
78409 if (!ts.isEffectiveExternalModule(sourceFile, compilerOptions)) {
78410 span !== null && span !== void 0 ? span : (span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos));
78411 var diagnostic = ts.createFileDiagnostic(sourceFile, span.start, span.length, 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);
78412 diagnostics.add(diagnostic);
78413 }
78414 switch (moduleKind) {
78415 case ts.ModuleKind.Node16:
78416 case ts.ModuleKind.NodeNext:
78417 if (sourceFile.impliedNodeFormat === ts.ModuleKind.CommonJS) {
78418 span !== null && span !== void 0 ? span : (span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos));
78419 diagnostics.add(ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.The_current_file_is_a_CommonJS_module_and_cannot_use_await_at_the_top_level));
78420 break;
78421 }
78422 // fallthrough
78423 case ts.ModuleKind.ES2022:
78424 case ts.ModuleKind.ESNext:
78425 case ts.ModuleKind.System:
78426 if (languageVersion >= 4 /* ScriptTarget.ES2017 */) {
78427 break;
78428 }
78429 // fallthrough
78430 default:
78431 span !== null && span !== void 0 ? span : (span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos));
78432 diagnostics.add(ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.Top_level_await_expressions_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));
78433 break;
78434 }
78435 }
78436 }
78437 else {
78438 // use of 'await' in non-async function
78439 var sourceFile = ts.getSourceFileOfNode(node);
78440 if (!hasParseDiagnostics(sourceFile)) {
78441 var span = ts.getSpanOfTokenAtPosition(sourceFile, node.pos);
78442 var diagnostic = ts.createFileDiagnostic(sourceFile, span.start, span.length, ts.Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules);
78443 if (container && container.kind !== 171 /* SyntaxKind.Constructor */ && (ts.getFunctionFlags(container) & 2 /* FunctionFlags.Async */) === 0) {
78444 var relatedInfo = ts.createDiagnosticForNode(container, ts.Diagnostics.Did_you_mean_to_mark_this_function_as_async);
78445 ts.addRelatedInfo(diagnostic, relatedInfo);
78446 }
78447 diagnostics.add(diagnostic);
78448 }
78449 }
78450 }
78451 if (isInParameterInitializerBeforeContainingFunction(node)) {
78452 error(node, ts.Diagnostics.await_expressions_cannot_be_used_in_a_parameter_initializer);
78453 }
78454 }
78455 function checkAwaitExpression(node) {

Callers 1

checkAwaitExpressionFunction · 0.85

Calls 4

hasParseDiagnosticsFunction · 0.85
errorFunction · 0.70
addMethod · 0.65

Tested by

no test coverage detected