Skip to content

Commit b0143bb

Browse files
committed
add relatedInfo to error message for 'await' used in non-async function
1 parent e8161ef commit b0143bb

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23087,7 +23087,20 @@ namespace ts {
2308723087
// Grammar checking
2308823088
if (produceDiagnostics) {
2308923089
if (!(node.flags & NodeFlags.AwaitContext)) {
23090-
grammarErrorOnFirstToken(node, Diagnostics.await_expression_is_only_allowed_within_an_async_function);
23090+
// use of 'await' in non-async function
23091+
const sourceFile = getSourceFileOfNode(node);
23092+
if (!hasParseDiagnostics(sourceFile)) {
23093+
const span = getSpanOfTokenAtPosition(sourceFile, node.pos);
23094+
const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.await_expression_is_only_allowed_within_an_async_function);
23095+
const func = getContainingFunction(node);
23096+
if (func) {
23097+
Debug.assert((getFunctionFlags(func) & FunctionFlags.Async) === 0, "Enclosing function should never be an async function.");
23098+
const relatedInfo = createDiagnosticForNode(func, Diagnostics.Did_you_mean_to_mark_this_function_as_async);
23099+
addRelatedInfo(diagnostic, relatedInfo);
23100+
23101+
}
23102+
diagnostics.add(diagnostic);
23103+
}
2309123104
}
2309223105

2309323106
if (isInParameterInitializerBeforeContainingFunction(node)) {

src/compiler/diagnosticMessages.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,10 @@
10351035
"category": "Error",
10361036
"code": 1355
10371037
},
1038+
"Did you mean to mark this function as 'async'?": {
1039+
"category": "Error",
1040+
"code": 1356
1041+
},
10381042

10391043
"Duplicate identifier '{0}'.": {
10401044
"category": "Error",
@@ -2959,7 +2963,7 @@
29592963
"category": "Error",
29602964
"code": 4104
29612965
},
2962-
2966+
29632967
"The current host does not support the '{0}' option.": {
29642968
"category": "Error",
29652969
"code": 5001

0 commit comments

Comments
 (0)