fix(49483): throw error on await inside non-async function (#49496)

This commit is contained in:
Oleksandr T
2022-06-15 02:08:25 +03:00
committed by GitHub
parent 3fc5f968ca
commit 0ada54c006
6 changed files with 63 additions and 0 deletions

View File

@@ -954,6 +954,7 @@ namespace ts {
Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations.code,
Diagnostics.Class_constructor_may_not_be_a_generator.code,
Diagnostics.Class_constructor_may_not_be_an_accessor.code,
Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
]);
/**

View File

@@ -0,0 +1,13 @@
/a.js(2,5): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
==== tests/cases/conformance/salsa/plainJSGrammarErrors3.js (0 errors) ====
==== /a.js (1 errors) ====
function foo() {
await new Promise(undefined);
~~~~~
!!! error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
!!! related TS1356 /a.js:1:10: Did you mean to mark this function as 'async'?
}

View File

@@ -0,0 +1,15 @@
//// [tests/cases/conformance/salsa/plainJSGrammarErrors3.ts] ////
//// [plainJSGrammarErrors3.js]
//// [a.js]
function foo() {
await new Promise(undefined);
}
//// [plainJSGrammarErrors3.js]
//// [a.js]
function foo() {
await new Promise(undefined);
}

View File

@@ -0,0 +1,11 @@
=== tests/cases/conformance/salsa/plainJSGrammarErrors3.js ===
No type information for this code.=== /a.js ===
function foo() {
>foo : Symbol(foo, Decl(a.js, 0, 0))
await new Promise(undefined);
>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, --, --))
>undefined : Symbol(undefined)
}

View File

@@ -0,0 +1,13 @@
=== tests/cases/conformance/salsa/plainJSGrammarErrors3.js ===
No type information for this code.=== /a.js ===
function foo() {
>foo : () => void
await new Promise(undefined);
>await new Promise(undefined) : any
>new Promise(undefined) : Promise<any>
>Promise : PromiseConstructor
>undefined : undefined
}

View File

@@ -0,0 +1,10 @@
// @outdir: out/
// @target: esnext
// @module: esnext
// @allowJs: true
// @filename: plainJSGrammarErrors3.js
// @filename: /a.js
function foo() {
await new Promise(undefined);
}