From b0143bb4464b88cd4fb4aded467bc8848edc9d84 Mon Sep 17 00:00:00 2001 From: uhyo Date: Thu, 2 May 2019 01:09:29 +0900 Subject: [PATCH] add relatedInfo to error message for 'await' used in non-async function --- src/compiler/checker.ts | 15 ++++++++++++++- src/compiler/diagnosticMessages.json | 6 +++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 978970927313f..2ae614b2dd8f1 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23087,7 +23087,20 @@ namespace ts { // Grammar checking if (produceDiagnostics) { if (!(node.flags & NodeFlags.AwaitContext)) { - grammarErrorOnFirstToken(node, Diagnostics.await_expression_is_only_allowed_within_an_async_function); + // use of 'await' in non-async function + const sourceFile = getSourceFileOfNode(node); + if (!hasParseDiagnostics(sourceFile)) { + const span = getSpanOfTokenAtPosition(sourceFile, node.pos); + const diagnostic = createFileDiagnostic(sourceFile, span.start, span.length, Diagnostics.await_expression_is_only_allowed_within_an_async_function); + const func = getContainingFunction(node); + if (func) { + Debug.assert((getFunctionFlags(func) & FunctionFlags.Async) === 0, "Enclosing function should never be an async function."); + const relatedInfo = createDiagnosticForNode(func, Diagnostics.Did_you_mean_to_mark_this_function_as_async); + addRelatedInfo(diagnostic, relatedInfo); + + } + diagnostics.add(diagnostic); + } } if (isInParameterInitializerBeforeContainingFunction(node)) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 75a21022adbe9..8342a4dd6c4d5 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1035,6 +1035,10 @@ "category": "Error", "code": 1355 }, + "Did you mean to mark this function as 'async'?": { + "category": "Error", + "code": 1356 + }, "Duplicate identifier '{0}'.": { "category": "Error", @@ -2959,7 +2963,7 @@ "category": "Error", "code": 4104 }, - + "The current host does not support the '{0}' option.": { "category": "Error", "code": 5001