forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cherry-pick PR microsoft#36654 into release-3.9
Component commits: 766a201 feat(36266): add a quick fix for incorrect return types in async functions 889e82b Make `getAwaitedType` private Also, fix an additional baseline change and break up huge line.
- Loading branch information
1 parent
90570df
commit 650a176
Showing
27 changed files
with
336 additions
and
20 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,70 @@ | ||
/* @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; | ||
} | ||
} | ||
} |
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
10 changes: 10 additions & 0 deletions
10
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,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> {}` | ||
}); |
13 changes: 13 additions & 0 deletions
13
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction10.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 | ||
////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> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction11.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 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> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction12.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 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> {}` | ||
}); |
13 changes: 13 additions & 0 deletions
13
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction13.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 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> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction14.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 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> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction15.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/codeFixReturnTypeInAsyncFunction16.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> {} | ||
}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction17.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/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 | ||
////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> {}` | ||
}); |
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 | ||
////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> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
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,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> {}` | ||
}); |
10 changes: 10 additions & 0 deletions
10
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,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> {}` | ||
}); |
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 fn(): undefined {} | ||
|
||
verify.codeFix({ | ||
index: 0, | ||
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "undefined", "undefined"], | ||
newFileContent: `async function fn(): Promise<undefined> {}` | ||
}); |
Oops, something went wrong.