Skip to content

Commit c5d9200

Browse files
authored
feat(46986): offer QF for using await outside of async context (microsoft#46994)
1 parent 6a1af7c commit c5d9200

9 files changed

+87
-0
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22544,6 +22544,11 @@ namespace ts {
2254422544
case "BigInt64Array":
2254522545
case "BigUint64Array":
2254622546
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_1_or_later;
22547+
case "await":
22548+
if (isCallExpression(node.parent)) {
22549+
return Diagnostics.Cannot_find_name_0_Did_you_mean_to_write_this_in_an_async_function;
22550+
}
22551+
// falls through
2254722552
default:
2254822553
if (node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
2254922554
return Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer;

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,10 @@
14911491
"category": "Error",
14921492
"code": 2310
14931493
},
1494+
"Cannot find name '{0}'. Did you mean to write this in an async function?": {
1495+
"category": "Error",
1496+
"code": 2311
1497+
},
14941498
"An interface can only extend an object type or intersection of object types with statically known members.": {
14951499
"category": "Error",
14961500
"code": 2312

src/services/codefixes/fixAwaitInSyncFunction.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace ts.codefix {
44
const errorCodes = [
55
Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
66
Diagnostics.for_await_loops_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
7+
Diagnostics.Cannot_find_name_0_Did_you_mean_to_write_this_in_an_async_function.code
78
];
89
registerCodeFix({
910
errorCodes,
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/awaitCallExpressionInSyncFunction.ts(2,16): error TS2311: Cannot find name 'await'. Did you mean to write this in an async function?
2+
3+
4+
==== tests/cases/compiler/awaitCallExpressionInSyncFunction.ts (1 errors) ====
5+
function foo() {
6+
const foo = await(Promise.resolve(1));
7+
~~~~~
8+
!!! error TS2311: Cannot find name 'await'. Did you mean to write this in an async function?
9+
return foo;
10+
}
11+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//// [awaitCallExpressionInSyncFunction.ts]
2+
function foo() {
3+
const foo = await(Promise.resolve(1));
4+
return foo;
5+
}
6+
7+
8+
//// [awaitCallExpressionInSyncFunction.js]
9+
function foo() {
10+
const foo = await(Promise.resolve(1));
11+
return foo;
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/awaitCallExpressionInSyncFunction.ts ===
2+
function foo() {
3+
>foo : Symbol(foo, Decl(awaitCallExpressionInSyncFunction.ts, 0, 0))
4+
5+
const foo = await(Promise.resolve(1));
6+
>foo : Symbol(foo, Decl(awaitCallExpressionInSyncFunction.ts, 1, 8))
7+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
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+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
10+
11+
return foo;
12+
>foo : Symbol(foo, Decl(awaitCallExpressionInSyncFunction.ts, 1, 8))
13+
}
14+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/awaitCallExpressionInSyncFunction.ts ===
2+
function foo() {
3+
>foo : () => any
4+
5+
const foo = await(Promise.resolve(1));
6+
>foo : any
7+
>await(Promise.resolve(1)) : any
8+
>await : any
9+
>Promise.resolve(1) : Promise<number>
10+
>Promise.resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
11+
>Promise : PromiseConstructor
12+
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
13+
>1 : 1
14+
15+
return foo;
16+
>foo : any
17+
}
18+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// @target: esnext
2+
3+
function foo() {
4+
const foo = await(Promise.resolve(1));
5+
return foo;
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////function foo() {
4+
//// const foo = await(Promise.resolve(1));
5+
//// return foo;
6+
////}
7+
8+
verify.codeFix({
9+
index: 0,
10+
description: "Add async modifier to containing function",
11+
newFileContent:
12+
`async function foo() {
13+
const foo = await(Promise.resolve(1));
14+
return foo;
15+
}`,
16+
});

0 commit comments

Comments
 (0)