Add missing arguments to typeToTypeNode. (#38336)
* Add missing arguments to typeToTypeNode. * Use returnTypeNode as the enclosingDeclaration. * Add a test.
This commit is contained in:
parent
e6390efb01
commit
94c5c3ff47
|
@ -39,14 +39,20 @@ namespace ts.codefix {
|
||||||
});
|
});
|
||||||
|
|
||||||
function getInfo(sourceFile: SourceFile, checker: TypeChecker, pos: number): Info | undefined {
|
function getInfo(sourceFile: SourceFile, checker: TypeChecker, pos: number): Info | undefined {
|
||||||
const returnTypeNode = getReturnTypeNode(sourceFile, pos);
|
if (isInJSFile(sourceFile)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
const token = getTokenAtPosition(sourceFile, pos);
|
||||||
|
const func = findAncestor(token, isFunctionLikeDeclaration);
|
||||||
|
const returnTypeNode = func?.type;
|
||||||
if (!returnTypeNode) {
|
if (!returnTypeNode) {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const returnType = checker.getTypeFromTypeNode(returnTypeNode);
|
const returnType = checker.getTypeFromTypeNode(returnTypeNode);
|
||||||
const promisedType = checker.getAwaitedType(returnType) || checker.getVoidType();
|
const promisedType = checker.getAwaitedType(returnType) || checker.getVoidType();
|
||||||
const promisedTypeNode = checker.typeToTypeNode(promisedType);
|
const promisedTypeNode = checker.typeToTypeNode(promisedType, /*enclosingDeclaration*/ returnTypeNode, /*flags*/ undefined);
|
||||||
if (promisedTypeNode) {
|
if (promisedTypeNode) {
|
||||||
return { returnTypeNode, returnType, promisedTypeNode, promisedType };
|
return { returnTypeNode, returnType, promisedTypeNode, promisedType };
|
||||||
}
|
}
|
||||||
|
@ -55,16 +61,4 @@ namespace ts.codefix {
|
||||||
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, returnTypeNode: TypeNode, promisedTypeNode: TypeNode): void {
|
function doChange(changes: textChanges.ChangeTracker, sourceFile: SourceFile, returnTypeNode: TypeNode, promisedTypeNode: TypeNode): void {
|
||||||
changes.replaceNode(sourceFile, returnTypeNode, createTypeReferenceNode("Promise", [promisedTypeNode]));
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
20
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction18.ts
Normal file
20
tests/cases/fourslash/codeFixReturnTypeInAsyncFunction18.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
/// <reference path='fourslash.ts' />
|
||||||
|
|
||||||
|
// @target: es2015
|
||||||
|
////
|
||||||
|
////interface A {}
|
||||||
|
////export { A as PublicA };
|
||||||
|
////async function foo(): A {
|
||||||
|
//// return {}
|
||||||
|
////}
|
||||||
|
|
||||||
|
verify.codeFix({
|
||||||
|
index: 0,
|
||||||
|
description: [ts.Diagnostics.Replace_0_with_Promise_1.message, "A", "A"],
|
||||||
|
newFileContent: `
|
||||||
|
interface A {}
|
||||||
|
export { A as PublicA };
|
||||||
|
async function foo(): Promise<A> {
|
||||||
|
return {}
|
||||||
|
}`
|
||||||
|
});
|
Loading…
Reference in a new issue