Skip to content

Commit 0ada54c

Browse files
authored
fix(49483): throw error on await inside non-async function (#49496)
1 parent 3fc5f96 commit 0ada54c

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed

src/compiler/program.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,7 @@ namespace ts {
954954
Diagnostics.let_is_not_allowed_to_be_used_as_a_name_in_let_or_const_declarations.code,
955955
Diagnostics.Class_constructor_may_not_be_a_generator.code,
956956
Diagnostics.Class_constructor_may_not_be_an_accessor.code,
957+
Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
957958
]);
958959

959960
/**
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/a.js(2,5): error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
2+
3+
4+
==== tests/cases/conformance/salsa/plainJSGrammarErrors3.js (0 errors) ====
5+
6+
==== /a.js (1 errors) ====
7+
function foo() {
8+
await new Promise(undefined);
9+
~~~~~
10+
!!! error TS1308: 'await' expressions are only allowed within async functions and at the top levels of modules.
11+
!!! related TS1356 /a.js:1:10: Did you mean to mark this function as 'async'?
12+
}
13+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/conformance/salsa/plainJSGrammarErrors3.ts] ////
2+
3+
//// [plainJSGrammarErrors3.js]
4+
5+
//// [a.js]
6+
function foo() {
7+
await new Promise(undefined);
8+
}
9+
10+
11+
//// [plainJSGrammarErrors3.js]
12+
//// [a.js]
13+
function foo() {
14+
await new Promise(undefined);
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
=== tests/cases/conformance/salsa/plainJSGrammarErrors3.js ===
2+
3+
No type information for this code.=== /a.js ===
4+
function foo() {
5+
>foo : Symbol(foo, Decl(a.js, 0, 0))
6+
7+
await new Promise(undefined);
8+
>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, --, --))
9+
>undefined : Symbol(undefined)
10+
}
11+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/conformance/salsa/plainJSGrammarErrors3.js ===
2+
3+
No type information for this code.=== /a.js ===
4+
function foo() {
5+
>foo : () => void
6+
7+
await new Promise(undefined);
8+
>await new Promise(undefined) : any
9+
>new Promise(undefined) : Promise<any>
10+
>Promise : PromiseConstructor
11+
>undefined : undefined
12+
}
13+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @outdir: out/
2+
// @target: esnext
3+
// @module: esnext
4+
// @allowJs: true
5+
// @filename: plainJSGrammarErrors3.js
6+
7+
// @filename: /a.js
8+
function foo() {
9+
await new Promise(undefined);
10+
}

0 commit comments

Comments
 (0)