-
Notifications
You must be signed in to change notification settings - Fork 12.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(36266): add a quick fix for incorrect return types in async func…
…tions
- Loading branch information
1 parent
6d7539a
commit a813d63
Showing
19 changed files
with
194 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* @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, | ||
]; | ||
|
||
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) { | ||
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @target: es2015 | ||
////async function foo(): number {} | ||
|
||
verify.codeFix({ | ||
index: 0, | ||
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "number", "number"], | ||
newFileContent: `async function foo(): Promise<number> {}` | ||
}); |
15 changes: 15 additions & 0 deletions
15
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction1.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @target: es2015 | ||
////async function a(): number {} | ||
////async function b(): string {} | ||
////async function c(): string {} | ||
|
||
verify.codeFixAll({ | ||
fixId: "fixReturnTypeInAsyncFunction", | ||
fixAllDescription: ts.Diagnostics.Fix_all_incorrect_return_type_of_an_async_functions.message, | ||
newFileContent: | ||
`async function a(): Promise<number> {} | ||
async function b(): Promise<string> {} | ||
async function c(): Promise<string> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction2.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> => {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction3.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> {}` | ||
}); |
15 changes: 15 additions & 0 deletions
15
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction4.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> {} | ||
}` | ||
}); |
13 changes: 13 additions & 0 deletions
13
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction5.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @target: es2015 | ||
////interface Foo<T> {} | ||
////async function foo(): 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 foo(): Promise<Foo<number>> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction6.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @target: es2015 | ||
////async function foo(): PromiseLike<void> {} | ||
|
||
verify.codeFix({ | ||
index: 0, | ||
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "PromiseLike<void>", "void"], | ||
newFileContent: `async function foo(): Promise<void> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction7.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @target: es2015 | ||
////async function foo(): {} {} | ||
|
||
verify.codeFix({ | ||
index: 0, | ||
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "{}", "{}"], | ||
newFileContent: `async function foo(): Promise<{}> {}` | ||
}); |
13 changes: 13 additions & 0 deletions
13
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction8.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
// @target: es2015 | ||
////declare class Thenable { then(): void; } | ||
////async function foo(): Thenable {} | ||
|
||
verify.codeFix({ | ||
index: 0, | ||
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "Thenable", "void"], | ||
newFileContent: | ||
`declare class Thenable { then(): void; } | ||
async function foo(): Promise<void> {}` | ||
}); |