Skip to content

Add missing arguments to typeToTypeNode. #38336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 5, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 8 additions & 14 deletions src/services/codefixes/fixReturnTypeInAsyncFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*/ func, /*flags*/ undefined);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like CFA can't tell func is not undefined here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right - we can tell that returnTypeNode is defined but we don't tie that info back.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically if we removed the check though, we'd be in a bad situation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just observing that there seemed to be a potential improvement that could be made to CFA, not suggesting any changes to this code.

if (promisedTypeNode) {
return { returnTypeNode, returnType, promisedTypeNode, promisedType };
}
Expand All @@ -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;
}
}
}