feat(36266): add a quick fix for incorrect return types in async functions

This commit is contained in:
Alexander T 2020-04-10 14:19:39 +03:00 committed by Eli Barzilay
parent ca234d2b60
commit 0503da225d
28 changed files with 329 additions and 16 deletions

View file

@ -406,6 +406,7 @@ namespace ts {
},
getParameterType: getTypeAtPosition,
getPromisedTypeOfPromise,
getAwaitedType: type => getAwaitedType(type),
getReturnTypeOfSignature,
isNullableType,
getNullableType,
@ -30795,7 +30796,7 @@ namespace ts {
if (globalPromiseType !== emptyGenericType && !isReferenceToType(returnType, globalPromiseType)) {
// The promise type was not a valid type reference to the global promise type, so we
// report an error and return the unknown type.
error(returnTypeNode, Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type);
error(returnTypeNode, Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type_Did_you_mean_to_write_Promise_0, typeToString(getAwaitedType(returnType) || voidType));
return;
}
}

View file

@ -215,7 +215,7 @@
"category": "Error",
"code": 1063
},
"The return type of an async function or method must be the global Promise<T> type.": {
"The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{0}>'?": {
"category": "Error",
"code": 1064
},
@ -5197,6 +5197,14 @@
"category": "Message",
"code": 90035
},
"Replace '{0}' with 'Promise<{1}>'": {
"category": "Message",
"code": 90036
},
"Fix all incorrect return type of an async functions": {
"category": "Message",
"code": 90037
},
"Declare a private field named '{0}'.": {
"category": "Message",
"code": 90053

View file

@ -3421,6 +3421,7 @@ namespace ts {
getWidenedType(type: Type): Type;
/* @internal */
getPromisedTypeOfPromise(promise: Type, errorNode?: Node): Type | undefined;
getAwaitedType(type: Type): Type | undefined;
getReturnTypeOfSignature(signature: Signature): Type;
/**
* Gets the type of a parameter at a given position in a signature.

View file

@ -0,0 +1,66 @@
/* @internal */
namespace ts.codefix {
const fixId = "fixReturnTypeInAsyncFunction";
const errorCodes = [
Diagnostics.The_return_type_of_an_async_function_or_method_must_be_the_global_Promise_T_type_Did_you_mean_to_write_Promise_0.code,
];
interface Info {
readonly returnTypeNode: TypeNode;
readonly returnType: Type;
readonly promisedTypeNode: TypeNode;
readonly promisedType: Type;
}
registerCodeFix({
errorCodes,
fixIds: [fixId],
getCodeActions: context => {
const { sourceFile, program, span } = context;
const checker = program.getTypeChecker();
const info = getInfo(sourceFile, program.getTypeChecker(), span.start);
if (!info) {
return undefined;
}
const { returnTypeNode, returnType, promisedTypeNode, promisedType } = info;
const changes = textChanges.ChangeTracker.with(context, t => doChange(t, sourceFile, returnTypeNode, promisedTypeNode));
return [createCodeFixAction(fixId, changes, [Diagnostics.Replace_0_with_Promise_1, checker.typeToString(returnType), checker.typeToString(promisedType)], fixId, Diagnostics.Fix_all_incorrect_return_type_of_an_async_functions)];
},
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => {
const info = getInfo(diag.file, context.program.getTypeChecker(), diag.start);
if (info) {
doChange(changes, diag.file, info.returnTypeNode, info.promisedTypeNode);
}
})
});
function getInfo(sourceFile: SourceFile, checker: TypeChecker, pos: number): Info | undefined {
const returnTypeNode = getReturnTypeNode(sourceFile, pos);
if (!returnTypeNode) {
return undefined;
}
const returnType = checker.getTypeFromTypeNode(returnTypeNode);
const promisedType = checker.getAwaitedType(returnType) || checker.getVoidType();
const promisedTypeNode = checker.typeToTypeNode(promisedType);
if (promisedTypeNode) {
return { returnTypeNode, returnType, promisedTypeNode, promisedType };
}
}
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, returnTypeNode: TypeNode, promisedTypeNode: TypeNode): void {
changes.replaceNode(sourceFile, returnTypeNode, createTypeReferenceNode("Promise", [promisedTypeNode]));
}
function getReturnTypeNode(sourceFile: SourceFile, pos: number): TypeNode | undefined {
if (isInJSFile(sourceFile)) {
return undefined;
}
const token = getTokenAtPosition(sourceFile, pos);
const parent = findAncestor(token, isFunctionLikeDeclaration);
if (parent?.type) {
return parent.type;
}
}
}

View file

@ -85,6 +85,7 @@
"codefixes/fixMissingCallParentheses.ts",
"codefixes/fixAwaitInSyncFunction.ts",
"codefixes/inferFromUsage.ts",
"codefixes/fixReturnTypeInAsyncFunction.ts",
"codefixes/disableJsDiagnostics.ts",
"codefixes/helpers.ts",
"codefixes/fixInvalidImportSyntax.ts",

View file

@ -2035,6 +2035,7 @@ declare namespace ts {
getBaseTypes(type: InterfaceType): BaseType[];
getBaseTypeOfLiteralType(type: Type): Type;
getWidenedType(type: Type): Type;
getAwaitedType(type: Type): Type | undefined;
getReturnTypeOfSignature(signature: Signature): Type;
getNullableType(type: Type, flags: TypeFlags): Type;
getNonNullableType(type: Type): Type;

View file

@ -2035,6 +2035,7 @@ declare namespace ts {
getBaseTypes(type: InterfaceType): BaseType[];
getBaseTypeOfLiteralType(type: Type): Type;
getWidenedType(type: Type): Type;
getAwaitedType(type: Type): Type | undefined;
getReturnTypeOfSignature(signature: Signature): Type;
getNullableType(type: Type, flags: TypeFlags): Type;
getNonNullableType(type: Type): Type;

View file

@ -1,10 +1,10 @@
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{}>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(6,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(7,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(7,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<any>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<number>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(8,23): error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(9,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(10,23): error TS1064: The return type of an async function or method must be the global Promise<T> type.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(9,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(10,23): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(17,16): error TS1058: The return type of an async function must either be a valid promise or must not contain a callable 'then' member.
tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration15_es6.ts(23,25): error TS1320: Type of 'await' operand must either be a valid promise or must not contain a callable 'then' member.
@ -17,23 +17,23 @@ tests/cases/conformance/async/es6/functionDeclarations/asyncFunctionDeclaration1
async function fn1() { } // valid: Promise<void>
async function fn2(): { } { } // error
~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<{}>'?
~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
async function fn3(): any { } // error
~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<any>'?
async function fn4(): number { } // error
~~~~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<number>'?
~~~~~~
!!! error TS2355: A function whose declared type is neither 'void' nor 'any' must return a value.
async function fn5(): PromiseLike<void> { } // error
~~~~~~~~~~~~~~~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
async function fn6(): Thenable { } // error
~~~~~~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
async function fn7() { return; } // valid: Promise<void>
async function fn8() { return 1; } // valid: Promise<number>
async function fn9() { return null; } // valid: Promise<any>

View file

@ -1,4 +1,4 @@
tests/cases/conformance/async/es6/test.ts(3,25): error TS1064: The return type of an async function or method must be the global Promise<T> type.
tests/cases/conformance/async/es6/test.ts(3,25): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<T>'?
==== tests/cases/conformance/async/es6/task.ts (0 errors) ====
@ -9,5 +9,5 @@ tests/cases/conformance/async/es6/test.ts(3,25): error TS1064: The return type o
class Test {
async example<T>(): Task<T> { return; }
~~~~~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<T>'?
}

View file

@ -1,4 +1,4 @@
tests/cases/conformance/async/es6/asyncQualifiedReturnType_es6.ts(6,21): error TS1064: The return type of an async function or method must be the global Promise<T> type.
tests/cases/conformance/async/es6/asyncQualifiedReturnType_es6.ts(6,21): error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
==== tests/cases/conformance/async/es6/asyncQualifiedReturnType_es6.ts (1 errors) ====
@ -9,5 +9,5 @@ tests/cases/conformance/async/es6/asyncQualifiedReturnType_es6.ts(6,21): error T
async function f(): X.MyPromise<void> {
~~~~~~~~~~~~~~~~~
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type.
!!! error TS1064: The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
}

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): number {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
newFileContent: `async function fn(): Promise<number> {}`
});

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////type Foo = "a" | "b";
////async function fn(): Foo {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "Foo", "Foo"],
newFileContent:
`type Foo = "a" | "b";
async function fn(): Promise<Foo> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): PromiseLike<string> {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "PromiseLike<string>", "string"],
newFileContent: `async function fn(): Promise<string> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): PromiseLike<void> {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "PromiseLike<void>", "void"],
newFileContent: `async function fn(): Promise<void> {}`
});

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////declare class Thenable { then(): void; }
////async function fn(): Thenable {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "Thenable", "void"],
newFileContent:
`declare class Thenable { then(): void; }
async function fn(): Promise<void> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): string | symbol {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "string | symbol", "string | symbol"],
newFileContent: `async function fn(): Promise<string | symbol> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////const foo = async function (): number {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
newFileContent: `const foo = async function (): Promise<number> {}`
});

View file

@ -0,0 +1,15 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////class A {
//// async foo(): number {}
////}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
newFileContent:
`class A {
async foo(): Promise<number> {}
}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////const foo = async (): number => {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"],
newFileContent: `const foo = async (): Promise<number> => {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): boolean {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "boolean", "boolean"],
newFileContent: `async function fn(): Promise<boolean> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): string {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "string", "string"],
newFileContent: `async function fn(): Promise<string> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): void {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "void", "void"],
newFileContent: `async function fn(): Promise<void> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): null {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "null", "null"],
newFileContent: `async function fn(): Promise<null> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): undefined {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "undefined", "undefined"],
newFileContent: `async function fn(): Promise<undefined> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): any {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "any", "any"],
newFileContent: `async function fn(): Promise<any> {}`
});

View file

@ -0,0 +1,10 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////async function fn(): symbol {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "symbol", "symbol"],
newFileContent: `async function fn(): Promise<symbol> {}`
});

View file

@ -0,0 +1,13 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////interface Foo<T> {}
////async function fn(): Foo<number> {}
verify.codeFix({
index: 0,
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "Foo<number>", "Foo<number>"],
newFileContent:
`interface Foo<T> {}
async function fn(): Promise<Foo<number>> {}`
});

View file

@ -0,0 +1,50 @@
/// <reference path='fourslash.ts' />
// @target: es2015
////declare class Thenable { then(): void; }
////interface Foo<T> {}
////type Bar = "a" | "b";
////
////async function f1(): number {}
////async function f2(): boolean {}
////async function f3(): string {}
////async function f4(): void {}
////async function f5(): null {}
////async function f6(): undefined {}
////async function f7(): any {}
////async function f8(): symbol {}
////
////async function f9(): Foo<number> {}
////async function f10(): Bar {}
////async function f11(): PromiseLike<string> {}
////async function f12(): PromiseLike<void> {}
////async function f13(): Thenable {}
////
////async function f14(): string | symbol {}
verify.codeFixAll({
fixId: "fixReturnTypeInAsyncFunction",
fixAllDescription: ts.Diagnostics.Fix_all_incorrect_return_type_of_an_async_functions.message,
newFileContent:
`declare class Thenable { then(): void; }
interface Foo<T> {}
type Bar = "a" | "b";
async function f1(): Promise<number> {}
async function f2(): Promise<boolean> {}
async function f3(): Promise<string> {}
async function f4(): Promise<void> {}
async function f5(): Promise<null> {}
async function f6(): Promise<undefined> {}
async function f7(): Promise<any> {}
async function f8(): Promise<symbol> {}
async function f9(): Promise<Foo<number>> {}
async function f10(): Promise<Bar> {}
async function f11(): Promise<string> {}
async function f12(): Promise<void> {}
async function f13(): Promise<void> {}
async function f14(): Promise<string | symbol> {}`
});