From 55bffe361682cf4e5dc0e935da0a79e51950e860 Mon Sep 17 00:00:00 2001 From: uhyo Date: Thu, 2 May 2019 02:07:44 +0900 Subject: [PATCH] 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