From b0143bb4464b88cd4fb4aded467bc8848edc9d84 Mon Sep 17 00:00:00 2001 From: uhyo Date: Thu, 2 May 2019 01:09:29 +0900 Subject: [PATCH 1/6] 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 From 246b66c714aa6ac93e6e168a933cc1f18807c10c Mon Sep 17 00:00:00 2001 From: uhyo Date: Thu, 2 May 2019 01:16:25 +0900 Subject: [PATCH 2/6] add related info to error for use of for-await-of in non-async function --- src/compiler/checker.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2ae614b2dd8f1..728c10dcf5637 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -31598,7 +31598,20 @@ namespace ts { if (forInOrOfStatement.kind === SyntaxKind.ForOfStatement && forInOrOfStatement.awaitModifier) { if ((forInOrOfStatement.flags & NodeFlags.AwaitContext) === NodeFlags.None) { - return grammarErrorOnNode(forInOrOfStatement.awaitModifier, Diagnostics.A_for_await_of_statement_is_only_allowed_within_an_async_function_or_async_generator); + // use of 'for-await-of' in non-async function + const sourceFile = getSourceFileOfNode(forInOrOfStatement); + if (!hasParseDiagnostics(sourceFile)) { + const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier, Diagnostics.A_for_await_of_statement_is_only_allowed_within_an_async_function_or_async_generator); + const func = getContainingFunction(forInOrOfStatement); + 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); + return true; + } + return false; } } From 8e9556a86065fe83abf04f4a06fa67a362f77fe0 Mon Sep 17 00:00:00 2001 From: uhyo Date: Thu, 2 May 2019 01:37:26 +0900 Subject: [PATCH 3/6] fix existing tests to accept new behavior --- tests/baselines/reference/awaitLiteralValues.errors.txt | 6 ++++++ tests/baselines/reference/parser.forAwait.es2018.errors.txt | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/tests/baselines/reference/awaitLiteralValues.errors.txt b/tests/baselines/reference/awaitLiteralValues.errors.txt index cf3e155c2809f..eb3237186e37f 100644 --- a/tests/baselines/reference/awaitLiteralValues.errors.txt +++ b/tests/baselines/reference/awaitLiteralValues.errors.txt @@ -11,35 +11,41 @@ tests/cases/compiler/awaitLiteralValues.ts(22,5): error TS1308: 'await' expressi await 'literal'; ~~~~~ !!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitLiteralValues.ts:1:10: Did you mean to mark this function as 'async'? } function awaitNumber() { await 1; ~~~~~ !!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitLiteralValues.ts:5:10: Did you mean to mark this function as 'async'? } function awaitTrue() { await true; ~~~~~ !!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitLiteralValues.ts:9:10: Did you mean to mark this function as 'async'? } function awaitFalse() { await false; ~~~~~ !!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitLiteralValues.ts:13:10: Did you mean to mark this function as 'async'? } function awaitNull() { await null; ~~~~~ !!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitLiteralValues.ts:17:10: Did you mean to mark this function as 'async'? } function awaitUndefined() { await undefined; ~~~~~ !!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitLiteralValues.ts:21:10: Did you mean to mark this function as 'async'? } \ No newline at end of file diff --git a/tests/baselines/reference/parser.forAwait.es2018.errors.txt b/tests/baselines/reference/parser.forAwait.es2018.errors.txt index e0e848db769be..a6eeaf670b796 100644 --- a/tests/baselines/reference/parser.forAwait.es2018.errors.txt +++ b/tests/baselines/reference/parser.forAwait.es2018.errors.txt @@ -52,6 +52,7 @@ tests/cases/conformance/parser/ecmascript2018/forAwait/topLevelWithExprIsError.t for await (const x of y) { ~~~~~ !!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/conformance/parser/ecmascript2018/forAwait/inFunctionDeclWithDeclIsError.ts:1:10: Did you mean to mark this function as 'async'? } } ==== tests/cases/conformance/parser/ecmascript2018/forAwait/inFunctionDeclWithExprIsError.ts (1 errors) ==== @@ -60,6 +61,7 @@ tests/cases/conformance/parser/ecmascript2018/forAwait/topLevelWithExprIsError.t for await (x of y) { ~~~~~ !!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/conformance/parser/ecmascript2018/forAwait/inFunctionDeclWithExprIsError.ts:1:10: Did you mean to mark this function as 'async'? } } ==== tests/cases/conformance/parser/ecmascript2018/forAwait/inAsyncFunctionWithDeclIsOk.ts (0 errors) ==== @@ -92,6 +94,7 @@ tests/cases/conformance/parser/ecmascript2018/forAwait/topLevelWithExprIsError.t for await (const x of y) { ~~~~~ !!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/conformance/parser/ecmascript2018/forAwait/inGeneratorWithDeclIsError.ts:1:11: Did you mean to mark this function as 'async'? } } ==== tests/cases/conformance/parser/ecmascript2018/forAwait/inGeneratorWithExprIsError.ts (1 errors) ==== @@ -100,6 +103,7 @@ tests/cases/conformance/parser/ecmascript2018/forAwait/topLevelWithExprIsError.t for await (x of y) { ~~~~~ !!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/conformance/parser/ecmascript2018/forAwait/inGeneratorWithExprIsError.ts:1:11: Did you mean to mark this function as 'async'? } } \ No newline at end of file From 1fd9de321473909199929faf38cbfcd181a13815 Mon Sep 17 00:00:00 2001 From: uhyo Date: Thu, 2 May 2019 01:57:41 +0900 Subject: [PATCH 4/6] do not suggest to mark constructor 'async' --- src/compiler/checker.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 728c10dcf5637..19f4c3b055d3f 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23093,7 +23093,7 @@ namespace ts { 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) { + if (func && func.kind !== SyntaxKind.Constructor) { 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); @@ -31603,7 +31603,7 @@ namespace ts { if (!hasParseDiagnostics(sourceFile)) { const diagnostic = createDiagnosticForNode(forInOrOfStatement.awaitModifier, Diagnostics.A_for_await_of_statement_is_only_allowed_within_an_async_function_or_async_generator); const func = getContainingFunction(forInOrOfStatement); - if (func) { + if (func && func.kind !== SyntaxKind.Constructor) { 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); From 55bffe361682cf4e5dc0e935da0a79e51950e860 Mon Sep 17 00:00:00 2001 From: uhyo Date: Thu, 2 May 2019 02:07:44 +0900 Subject: [PATCH 5/6] add tests to check errors for use of 'await' in non-async function --- .../awaitInNonAsyncFunction.errors.txt | 103 +++++++++++++++++ .../reference/awaitInNonAsyncFunction.js | 84 ++++++++++++++ .../reference/awaitInNonAsyncFunction.symbols | 94 +++++++++++++++ .../reference/awaitInNonAsyncFunction.types | 108 ++++++++++++++++++ .../cases/compiler/awaitInNonAsyncFunction.ts | 41 +++++++ 5 files changed, 430 insertions(+) create mode 100644 tests/baselines/reference/awaitInNonAsyncFunction.errors.txt create mode 100644 tests/baselines/reference/awaitInNonAsyncFunction.js create mode 100644 tests/baselines/reference/awaitInNonAsyncFunction.symbols create mode 100644 tests/baselines/reference/awaitInNonAsyncFunction.types create mode 100644 tests/cases/compiler/awaitInNonAsyncFunction.ts diff --git a/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt b/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt new file mode 100644 index 0000000000000..5cd8468ac3eb6 --- /dev/null +++ b/tests/baselines/reference/awaitInNonAsyncFunction.errors.txt @@ -0,0 +1,103 @@ +tests/cases/compiler/awaitInNonAsyncFunction.ts(4,7): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +tests/cases/compiler/awaitInNonAsyncFunction.ts(5,10): error TS1308: 'await' expression is only allowed within an async function. +tests/cases/compiler/awaitInNonAsyncFunction.ts(9,7): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +tests/cases/compiler/awaitInNonAsyncFunction.ts(10,10): error TS1308: 'await' expression is only allowed within an async function. +tests/cases/compiler/awaitInNonAsyncFunction.ts(14,7): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +tests/cases/compiler/awaitInNonAsyncFunction.ts(15,3): error TS1308: 'await' expression is only allowed within an async function. +tests/cases/compiler/awaitInNonAsyncFunction.ts(19,7): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +tests/cases/compiler/awaitInNonAsyncFunction.ts(20,10): error TS1308: 'await' expression is only allowed within an async function. +tests/cases/compiler/awaitInNonAsyncFunction.ts(24,7): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +tests/cases/compiler/awaitInNonAsyncFunction.ts(25,9): error TS1308: 'await' expression is only allowed within an async function. +tests/cases/compiler/awaitInNonAsyncFunction.ts(30,9): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +tests/cases/compiler/awaitInNonAsyncFunction.ts(31,5): error TS1308: 'await' expression is only allowed within an async function. +tests/cases/compiler/awaitInNonAsyncFunction.ts(34,7): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +tests/cases/compiler/awaitInNonAsyncFunction.ts(35,5): error TS1308: 'await' expression is only allowed within an async function. +tests/cases/compiler/awaitInNonAsyncFunction.ts(39,5): error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +tests/cases/compiler/awaitInNonAsyncFunction.ts(40,1): error TS1308: 'await' expression is only allowed within an async function. + + +==== tests/cases/compiler/awaitInNonAsyncFunction.ts (16 errors) ==== + // https://github.com/Microsoft/TypeScript/issues/26586 + + function normalFunc(p: Promise) { + for await (const _ of []); + ~~~~~ +!!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:3:10: Did you mean to mark this function as 'async'? + return await p; + ~~~~~ +!!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:3:10: Did you mean to mark this function as 'async'? + } + + export function exportedFunc(p: Promise) { + for await (const _ of []); + ~~~~~ +!!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:8:17: Did you mean to mark this function as 'async'? + return await p; + ~~~~~ +!!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:8:17: Did you mean to mark this function as 'async'? + } + + const functionExpression = function(p: Promise) { + for await (const _ of []); + ~~~~~ +!!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:13:28: Did you mean to mark this function as 'async'? + await p; + ~~~~~ +!!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:13:28: Did you mean to mark this function as 'async'? + } + + const arrowFunc = (p: Promise) => { + for await (const _ of []); + ~~~~~ +!!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:18:19: Did you mean to mark this function as 'async'? + return await p; + ~~~~~ +!!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:18:19: Did you mean to mark this function as 'async'? + }; + + function* generatorFunc(p: Promise) { + for await (const _ of []); + ~~~~~ +!!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:23:11: Did you mean to mark this function as 'async'? + yield await p; + ~~~~~ +!!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:23:11: Did you mean to mark this function as 'async'? + } + + class clazz { + constructor(p: Promise) { + for await (const _ of []); + ~~~~~ +!!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. + await p; + ~~~~~ +!!! error TS1308: 'await' expression is only allowed within an async function. + } + method(p: Promise) { + for await (const _ of []); + ~~~~~ +!!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:33:3: Did you mean to mark this function as 'async'? + await p; + ~~~~~ +!!! error TS1308: 'await' expression is only allowed within an async function. +!!! related TS1356 tests/cases/compiler/awaitInNonAsyncFunction.ts:33:3: Did you mean to mark this function as 'async'? + } + } + + for await (const _ of []); + ~~~~~ +!!! error TS1103: A 'for-await-of' statement is only allowed within an async function or async generator. + await null; + ~~~~~ +!!! error TS1308: 'await' expression is only allowed within an async function. \ No newline at end of file diff --git a/tests/baselines/reference/awaitInNonAsyncFunction.js b/tests/baselines/reference/awaitInNonAsyncFunction.js new file mode 100644 index 0000000000000..83f75aa5633eb --- /dev/null +++ b/tests/baselines/reference/awaitInNonAsyncFunction.js @@ -0,0 +1,84 @@ +//// [awaitInNonAsyncFunction.ts] +// https://github.com/Microsoft/TypeScript/issues/26586 + +function normalFunc(p: Promise) { + for await (const _ of []); + return await p; +} + +export function exportedFunc(p: Promise) { + for await (const _ of []); + return await p; +} + +const functionExpression = function(p: Promise) { + for await (const _ of []); + await p; +} + +const arrowFunc = (p: Promise) => { + for await (const _ of []); + return await p; +}; + +function* generatorFunc(p: Promise) { + for await (const _ of []); + yield await p; +} + +class clazz { + constructor(p: Promise) { + for await (const _ of []); + await p; + } + method(p: Promise) { + for await (const _ of []); + await p; + } +} + +for await (const _ of []); +await null; + +//// [awaitInNonAsyncFunction.js] +// https://github.com/Microsoft/TypeScript/issues/26586 +function normalFunc(p) { + for await (const _ of []) + ; + return await p; +} +export function exportedFunc(p) { + for await (const _ of []) + ; + return await p; +} +const functionExpression = function (p) { + for await (const _ of []) + ; + await p; +}; +const arrowFunc = (p) => { + for await (const _ of []) + ; + return await p; +}; +function* generatorFunc(p) { + for await (const _ of []) + ; + yield await p; +} +class clazz { + constructor(p) { + for await (const _ of []) + ; + await p; + } + method(p) { + for await (const _ of []) + ; + await p; + } +} +for await (const _ of []) + ; +await null; diff --git a/tests/baselines/reference/awaitInNonAsyncFunction.symbols b/tests/baselines/reference/awaitInNonAsyncFunction.symbols new file mode 100644 index 0000000000000..cf184637e6f06 --- /dev/null +++ b/tests/baselines/reference/awaitInNonAsyncFunction.symbols @@ -0,0 +1,94 @@ +=== tests/cases/compiler/awaitInNonAsyncFunction.ts === +// https://github.com/Microsoft/TypeScript/issues/26586 + +function normalFunc(p: Promise) { +>normalFunc : Symbol(normalFunc, Decl(awaitInNonAsyncFunction.ts, 0, 0)) +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 2, 20)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) + + for await (const _ of []); +>_ : Symbol(_, Decl(awaitInNonAsyncFunction.ts, 3, 18)) + + return await p; +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 2, 20)) +} + +export function exportedFunc(p: Promise) { +>exportedFunc : Symbol(exportedFunc, Decl(awaitInNonAsyncFunction.ts, 5, 1)) +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 7, 29)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) + + for await (const _ of []); +>_ : Symbol(_, Decl(awaitInNonAsyncFunction.ts, 8, 18)) + + return await p; +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 7, 29)) +} + +const functionExpression = function(p: Promise) { +>functionExpression : Symbol(functionExpression, Decl(awaitInNonAsyncFunction.ts, 12, 5)) +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 12, 36)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) + + for await (const _ of []); +>_ : Symbol(_, Decl(awaitInNonAsyncFunction.ts, 13, 18)) + + await p; +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 12, 36)) +} + +const arrowFunc = (p: Promise) => { +>arrowFunc : Symbol(arrowFunc, Decl(awaitInNonAsyncFunction.ts, 17, 5)) +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 17, 19)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) + + for await (const _ of []); +>_ : Symbol(_, Decl(awaitInNonAsyncFunction.ts, 18, 18)) + + return await p; +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 17, 19)) + +}; + +function* generatorFunc(p: Promise) { +>generatorFunc : Symbol(generatorFunc, Decl(awaitInNonAsyncFunction.ts, 20, 2)) +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 22, 24)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) + + for await (const _ of []); +>_ : Symbol(_, Decl(awaitInNonAsyncFunction.ts, 23, 18)) + + yield await p; +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 22, 24)) +} + +class clazz { +>clazz : Symbol(clazz, Decl(awaitInNonAsyncFunction.ts, 25, 1)) + + constructor(p: Promise) { +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 28, 14)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) + + for await (const _ of []); +>_ : Symbol(_, Decl(awaitInNonAsyncFunction.ts, 29, 20)) + + await p; +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 28, 14)) + } + method(p: Promise) { +>method : Symbol(clazz.method, Decl(awaitInNonAsyncFunction.ts, 31, 3)) +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 32, 9)) +>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --), Decl(lib.es2018.promise.d.ts, --, --)) + + for await (const _ of []); +>_ : Symbol(_, Decl(awaitInNonAsyncFunction.ts, 33, 18)) + + await p; +>p : Symbol(p, Decl(awaitInNonAsyncFunction.ts, 32, 9)) + } +} + +for await (const _ of []); +>_ : Symbol(_, Decl(awaitInNonAsyncFunction.ts, 38, 16)) + +await null; diff --git a/tests/baselines/reference/awaitInNonAsyncFunction.types b/tests/baselines/reference/awaitInNonAsyncFunction.types new file mode 100644 index 0000000000000..78b9a6c5a9027 --- /dev/null +++ b/tests/baselines/reference/awaitInNonAsyncFunction.types @@ -0,0 +1,108 @@ +=== tests/cases/compiler/awaitInNonAsyncFunction.ts === +// https://github.com/Microsoft/TypeScript/issues/26586 + +function normalFunc(p: Promise) { +>normalFunc : (p: Promise) => number +>p : Promise + + for await (const _ of []); +>_ : any +>[] : undefined[] + + return await p; +>await p : number +>p : Promise +} + +export function exportedFunc(p: Promise) { +>exportedFunc : (p: Promise) => number +>p : Promise + + for await (const _ of []); +>_ : any +>[] : undefined[] + + return await p; +>await p : number +>p : Promise +} + +const functionExpression = function(p: Promise) { +>functionExpression : (p: Promise) => void +>function(p: Promise) { for await (const _ of []); await p;} : (p: Promise) => void +>p : Promise + + for await (const _ of []); +>_ : any +>[] : undefined[] + + await p; +>await p : number +>p : Promise +} + +const arrowFunc = (p: Promise) => { +>arrowFunc : (p: Promise) => number +>(p: Promise) => { for await (const _ of []); return await p;} : (p: Promise) => number +>p : Promise + + for await (const _ of []); +>_ : any +>[] : undefined[] + + return await p; +>await p : number +>p : Promise + +}; + +function* generatorFunc(p: Promise) { +>generatorFunc : (p: Promise) => IterableIterator +>p : Promise + + for await (const _ of []); +>_ : any +>[] : undefined[] + + yield await p; +>yield await p : any +>await p : number +>p : Promise +} + +class clazz { +>clazz : clazz + + constructor(p: Promise) { +>p : Promise + + for await (const _ of []); +>_ : any +>[] : undefined[] + + await p; +>await p : number +>p : Promise + } + method(p: Promise) { +>method : (p: Promise) => void +>p : Promise + + for await (const _ of []); +>_ : any +>[] : undefined[] + + await p; +>await p : number +>p : Promise + } +} + +for await (const _ of []); +>_ : any +>[] : undefined[] + +await null; +>await null : null +>null : null + diff --git a/tests/cases/compiler/awaitInNonAsyncFunction.ts b/tests/cases/compiler/awaitInNonAsyncFunction.ts new file mode 100644 index 0000000000000..5ebf37dc7ed04 --- /dev/null +++ b/tests/cases/compiler/awaitInNonAsyncFunction.ts @@ -0,0 +1,41 @@ +// @target: esnext +// https://github.com/Microsoft/TypeScript/issues/26586 + +function normalFunc(p: Promise) { + for await (const _ of []); + return await p; +} + +export function exportedFunc(p: Promise) { + for await (const _ of []); + return await p; +} + +const functionExpression = function(p: Promise) { + for await (const _ of []); + await p; +} + +const arrowFunc = (p: Promise) => { + for await (const _ of []); + return await p; +}; + +function* generatorFunc(p: Promise) { + for await (const _ of []); + yield await p; +} + +class clazz { + constructor(p: Promise) { + for await (const _ of []); + await p; + } + method(p: Promise) { + for await (const _ of []); + await p; + } +} + +for await (const _ of []); +await null; \ No newline at end of file From bafdf4baf869896920cec59df90e1823a3c715fa Mon Sep 17 00:00:00 2001 From: uhyo Date: Thu, 2 May 2019 03:45:17 +0900 Subject: [PATCH 6/6] remove extra newline --- src/compiler/checker.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 19f4c3b055d3f..9284465eefc28 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -23097,7 +23097,6 @@ namespace ts { 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); }