Skip to content

fix(42519): 'Infer function return type' quick fix generates invalid code for arrow functions without parens #42532

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 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
16 changes: 14 additions & 2 deletions src/services/refactors/inferFunctionReturnType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ namespace ts.refactor.inferFunctionReturnType {
function getEditsForAction(context: RefactorContext): RefactorEditInfo | undefined {
const info = getInfo(context);
if (info && !isRefactorErrorInfo(info)) {
const edits = textChanges.ChangeTracker.with(context, t =>
t.tryInsertTypeAnnotation(context.file, info.declaration, info.returnTypeNode));
const edits = textChanges.ChangeTracker.with(context, t => doChange(context.file, t, info.declaration, info.returnTypeNode));
return { renameFilename: undefined, renameLocation: undefined, edits };
}
return undefined;
Expand Down Expand Up @@ -55,6 +54,19 @@ namespace ts.refactor.inferFunctionReturnType {
returnTypeNode: TypeNode;
}

function doChange(sourceFile: SourceFile, changes: textChanges.ChangeTracker, declaration: ConvertibleDeclaration, typeNode: TypeNode) {
const closeParen = findChildOfKind(declaration, SyntaxKind.CloseParenToken, sourceFile);
const needParens = isArrowFunction(declaration) && closeParen === undefined;
const endNode = needParens ? first(declaration.parameters) : closeParen;
if (endNode) {
if (needParens) {
changes.insertNodeBefore(sourceFile, endNode, factory.createToken(SyntaxKind.OpenParenToken));
changes.insertNodeAfter(sourceFile, endNode, factory.createToken(SyntaxKind.CloseParenToken));
}
changes.insertNodeAt(sourceFile, endNode.end, typeNode, { prefix: ": " });
}
}

function getInfo(context: RefactorContext): FunctionInfo | RefactorErrorInfo | undefined {
if (isInJSFile(context.file) || !refactorKindBeginsWith(inferReturnTypeAction.kind, context.kind)) return;

Expand Down
16 changes: 16 additions & 0 deletions tests/cases/fourslash/refactorInferFunctionReturnType22.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />

////const foo = async /*a*//*b*/a => {
//// return 1;
////}

goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Infer function return type",
actionName: "Infer function return type",
actionDescription: "Infer function return type",
newContent:
`const foo = async (a): Promise<number> => {
return 1;
}`
});
16 changes: 16 additions & 0 deletions tests/cases/fourslash/refactorInferFunctionReturnType23.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />

////const foo = async /*a*//*b*/(a) => {
//// return 1;
////}

goTo.select("a", "b");
edit.applyRefactor({
refactorName: "Infer function return type",
actionName: "Infer function return type",
actionDescription: "Infer function return type",
newContent:
`const foo = async (a): Promise<number> => {
return 1;
}`
});