mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-04-17 01:49:41 -05:00
feat(46986): offer QF for using await outside of async context (#46994)
This commit is contained in:
@@ -22544,6 +22544,11 @@ namespace ts {
|
||||
case "BigInt64Array":
|
||||
case "BigUint64Array":
|
||||
return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_1_or_later;
|
||||
case "await":
|
||||
if (isCallExpression(node.parent)) {
|
||||
return Diagnostics.Cannot_find_name_0_Did_you_mean_to_write_this_in_an_async_function;
|
||||
}
|
||||
// falls through
|
||||
default:
|
||||
if (node.parent.kind === SyntaxKind.ShorthandPropertyAssignment) {
|
||||
return Diagnostics.No_value_exists_in_scope_for_the_shorthand_property_0_Either_declare_one_or_provide_an_initializer;
|
||||
|
||||
@@ -1491,6 +1491,10 @@
|
||||
"category": "Error",
|
||||
"code": 2310
|
||||
},
|
||||
"Cannot find name '{0}'. Did you mean to write this in an async function?": {
|
||||
"category": "Error",
|
||||
"code": 2311
|
||||
},
|
||||
"An interface can only extend an object type or intersection of object types with statically known members.": {
|
||||
"category": "Error",
|
||||
"code": 2312
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace ts.codefix {
|
||||
const errorCodes = [
|
||||
Diagnostics.await_expressions_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
|
||||
Diagnostics.for_await_loops_are_only_allowed_within_async_functions_and_at_the_top_levels_of_modules.code,
|
||||
Diagnostics.Cannot_find_name_0_Did_you_mean_to_write_this_in_an_async_function.code
|
||||
];
|
||||
registerCodeFix({
|
||||
errorCodes,
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
tests/cases/compiler/awaitCallExpressionInSyncFunction.ts(2,16): error TS2311: Cannot find name 'await'. Did you mean to write this in an async function?
|
||||
|
||||
|
||||
==== tests/cases/compiler/awaitCallExpressionInSyncFunction.ts (1 errors) ====
|
||||
function foo() {
|
||||
const foo = await(Promise.resolve(1));
|
||||
~~~~~
|
||||
!!! error TS2311: Cannot find name 'await'. Did you mean to write this in an async function?
|
||||
return foo;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
//// [awaitCallExpressionInSyncFunction.ts]
|
||||
function foo() {
|
||||
const foo = await(Promise.resolve(1));
|
||||
return foo;
|
||||
}
|
||||
|
||||
|
||||
//// [awaitCallExpressionInSyncFunction.js]
|
||||
function foo() {
|
||||
const foo = await(Promise.resolve(1));
|
||||
return foo;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
=== tests/cases/compiler/awaitCallExpressionInSyncFunction.ts ===
|
||||
function foo() {
|
||||
>foo : Symbol(foo, Decl(awaitCallExpressionInSyncFunction.ts, 0, 0))
|
||||
|
||||
const foo = await(Promise.resolve(1));
|
||||
>foo : Symbol(foo, Decl(awaitCallExpressionInSyncFunction.ts, 1, 8))
|
||||
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
|
||||
>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, --, --))
|
||||
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
|
||||
|
||||
return foo;
|
||||
>foo : Symbol(foo, Decl(awaitCallExpressionInSyncFunction.ts, 1, 8))
|
||||
}
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
=== tests/cases/compiler/awaitCallExpressionInSyncFunction.ts ===
|
||||
function foo() {
|
||||
>foo : () => any
|
||||
|
||||
const foo = await(Promise.resolve(1));
|
||||
>foo : any
|
||||
>await(Promise.resolve(1)) : any
|
||||
>await : any
|
||||
>Promise.resolve(1) : Promise<number>
|
||||
>Promise.resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
|
||||
>Promise : PromiseConstructor
|
||||
>resolve : { (): Promise<void>; <T>(value: T | PromiseLike<T>): Promise<T>; }
|
||||
>1 : 1
|
||||
|
||||
return foo;
|
||||
>foo : any
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
// @target: esnext
|
||||
|
||||
function foo() {
|
||||
const foo = await(Promise.resolve(1));
|
||||
return foo;
|
||||
}
|
||||
16
tests/cases/fourslash/codeFixAwaitInSyncFunction16.ts
Normal file
16
tests/cases/fourslash/codeFixAwaitInSyncFunction16.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
/// <reference path='fourslash.ts' />
|
||||
|
||||
////function foo() {
|
||||
//// const foo = await(Promise.resolve(1));
|
||||
//// return foo;
|
||||
////}
|
||||
|
||||
verify.codeFix({
|
||||
index: 0,
|
||||
description: "Add async modifier to containing function",
|
||||
newFileContent:
|
||||
`async function foo() {
|
||||
const foo = await(Promise.resolve(1));
|
||||
return foo;
|
||||
}`,
|
||||
});
|
||||
Reference in New Issue
Block a user