Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/services/codefixes/fixAwaitInSyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

12 changes: 12 additions & 0 deletions tests/baselines/reference/awaitCallExpressionInSyncFunction.js
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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))
}

18 changes: 18 additions & 0 deletions tests/baselines/reference/awaitCallExpressionInSyncFunction.types
Original file line number Diff line number Diff line change
@@ -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
}

6 changes: 6 additions & 0 deletions tests/cases/compiler/awaitCallExpressionInSyncFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// @target: esnext

function foo() {
const foo = await(Promise.resolve(1));
return foo;
}
16 changes: 16 additions & 0 deletions tests/cases/fourslash/codeFixAwaitInSyncFunction16.ts
Original file line number Diff line number Diff line change
@@ -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;
}`,
});