Add missing arguments to typeToTypeNode. (#38336)

* Add missing arguments to typeToTypeNode.

* Use returnTypeNode as the enclosingDeclaration.

* Add a test.
This commit is contained in:
Daniel Rosenwasser 2020-05-04 19:32:27 -07:00 committed by GitHub
parent e6390efb01
commit 94c5c3ff47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 14 deletions

View file

@ -39,14 +39,20 @@ namespace ts.codefix {
});
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) {
return undefined;
}
const returnType = checker.getTypeFromTypeNode(returnTypeNode);
const promisedType = checker.getAwaitedType(returnType) || checker.getVoidType();
const promisedTypeNode = checker.typeToTypeNode(promisedType);
const promisedTypeNode = checker.typeToTypeNode(promisedType, /*enclosingDeclaration*/ returnTypeNode, /*flags*/ undefined);
if (promisedTypeNode) {
return { returnTypeNode, returnType, promisedTypeNode, promisedType };
}
@ -55,16 +61,4 @@ namespace ts.codefix {
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

@ -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 {}
}`
});