-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Quick fix for functions lacking return expressions #26434
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
Changes from 27 commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
3f59aa9
stash
Kingwl 7671221
add surmise for return type
Kingwl 0b1e6cc
add support for more case
Kingwl 1f9b9c0
add more test case
Kingwl aca1722
add more testcase and fix all test
Kingwl 7cfd0fb
Merge branch 'master' into returnValueSurmise
Kingwl cbe59bb
fix changed diagnosis
Kingwl 48f1cca
Merge branch 'master' into returnValueSurmise
Kingwl 98d7eba
fix broken test case
Kingwl 537e806
add more case
Kingwl 505e299
Merge branch 'master' into returnValueSurmise
Kingwl 6fecf32
rename quickfix
Kingwl 4c0af72
fix conflict
Kingwl 98377f3
fix fix desc
Kingwl 49d21bf
fix semi
Kingwl a2b15ed
Merge branch 'master' into returnValueSurmise
Kingwl eabc5a4
Merge branch 'master' into returnValueSurmise
Kingwl 944ddf4
Avoid replace brace with paren
Kingwl 6c88a01
Split fix all action
Kingwl 94f845a
Add return work in same line
Kingwl a7f37a3
Merge branch 'master' into returnValueSurmise
Kingwl 7fe9a82
fix test cases
Kingwl af9552e
rename baseline
Kingwl 601fc5e
refactor and handle comment
Kingwl 175cf4e
Support semi
Kingwl 8b332fe
Merge branch 'master' into returnValueSurmise
Kingwl 5d8355c
make helper internal
Kingwl 522cac8
Merge branch 'master' into returnValueSurmise
Kingwl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,208 @@ | ||
/* @internal */ | ||
namespace ts.codefix { | ||
const fixId = "returnValueCorrect"; | ||
const fixIdAddReturnStatement = "fixAddReturnStatement"; | ||
const fixIdRemoveBlockBodyBrace = "fixRemoveBlockBodyBrace"; | ||
const fixIdWrapTheBlockWithParen = "fixWrapTheBlockWithParen"; | ||
const errorCodes = [ | ||
Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code, | ||
Diagnostics.Type_0_is_not_assignable_to_type_1.code, | ||
Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code | ||
]; | ||
|
||
enum ProblemKind { | ||
MissingReturnStatement, | ||
MissingParentheses | ||
} | ||
|
||
interface MissingReturnInfo { | ||
kind: ProblemKind.MissingReturnStatement; | ||
declaration: FunctionLikeDeclaration; | ||
expression: Expression; | ||
statement: Statement; | ||
commentSource: Node; | ||
} | ||
|
||
interface MissingParenInfo { | ||
kind: ProblemKind.MissingParentheses; | ||
declaration: ArrowFunction; | ||
expression: Expression; | ||
statement: Statement; | ||
commentSource: Node; | ||
} | ||
|
||
type Info = MissingReturnInfo | MissingParenInfo; | ||
|
||
registerCodeFix({ | ||
errorCodes, | ||
fixIds: [fixIdAddReturnStatement, fixIdRemoveBlockBodyBrace, fixIdWrapTheBlockWithParen], | ||
getCodeActions: context => { | ||
const { program, sourceFile, span: { start }, errorCode } = context; | ||
const info = getInfo(program.getTypeChecker(), sourceFile, start, errorCode); | ||
if (!info) return undefined; | ||
|
||
if (info.kind === ProblemKind.MissingReturnStatement) { | ||
return append( | ||
[getActionForfixAddReturnStatement(context, info.expression, info.statement)], | ||
isArrowFunction(info.declaration) ? getActionForfixRemoveBlockBodyBrace(context, info.declaration, info.expression, info.commentSource): undefined); | ||
} | ||
else { | ||
return [getActionForfixWrapTheBlockWithParen(context, info.declaration, info.expression)]; | ||
} | ||
}, | ||
getAllCodeActions: context => codeFixAll(context, errorCodes, (changes, diag) => { | ||
const info = getInfo(context.program.getTypeChecker(), diag.file, diag.start, diag.code); | ||
if (!info) return undefined; | ||
|
||
switch (context.fixId) { | ||
case fixIdAddReturnStatement: | ||
addReturnStatement(changes, diag.file, info.expression, info.statement); | ||
break; | ||
case fixIdRemoveBlockBodyBrace: | ||
if (!isArrowFunction(info.declaration)) return undefined; | ||
removeBlockBodyBrace(changes, diag.file, info.declaration, info.expression, info.commentSource, /* withParen */ false); | ||
break; | ||
case fixIdWrapTheBlockWithParen: | ||
if (!isArrowFunction(info.declaration)) return undefined; | ||
wrapBlockWithParen(changes, diag.file, info.declaration, info.expression); | ||
break; | ||
default: | ||
Debug.fail(JSON.stringify(context.fixId)); | ||
} | ||
}), | ||
}); | ||
|
||
function getFixInfo(checker: TypeChecker, declaration: FunctionLikeDeclaration, expectType: Type, isFunctionType: boolean): Info | undefined { | ||
if (!declaration.body || !isBlock(declaration.body) || length(declaration.body.statements) !== 1) return undefined; | ||
|
||
const firstStatement = first(declaration.body.statements); | ||
if (isExpressionStatement(firstStatement) && checkFixedAssignableTo(checker, declaration, firstStatement.expression, expectType, isFunctionType)) { | ||
return { | ||
declaration, | ||
kind: ProblemKind.MissingReturnStatement, | ||
expression: firstStatement.expression, | ||
statement: firstStatement, | ||
commentSource: firstStatement.expression | ||
}; | ||
} | ||
else if (isLabeledStatement(firstStatement) && isExpressionStatement(firstStatement.statement)) { | ||
const node = createObjectLiteral([createPropertyAssignment(firstStatement.label, firstStatement.statement.expression)]); | ||
if (checkFixedAssignableTo(checker, declaration, node, expectType, isFunctionType)) { | ||
return isArrowFunction(declaration) ? { | ||
declaration, | ||
kind: ProblemKind.MissingParentheses, | ||
expression: node, | ||
statement: firstStatement, | ||
commentSource: firstStatement.statement.expression | ||
} : { | ||
declaration, | ||
kind: ProblemKind.MissingReturnStatement, | ||
expression: node, | ||
statement: firstStatement, | ||
commentSource: firstStatement.statement.expression | ||
}; | ||
} | ||
} | ||
else if (isBlock(firstStatement) && length(firstStatement.statements) === 1) { | ||
const firstBlockStatement = first(firstStatement.statements); | ||
if (isLabeledStatement(firstBlockStatement) && isExpressionStatement(firstBlockStatement.statement)) { | ||
const node = createObjectLiteral([createPropertyAssignment(firstBlockStatement.label, firstBlockStatement.statement.expression)]); | ||
if (checkFixedAssignableTo(checker, declaration, node, expectType, isFunctionType)) { | ||
return { | ||
declaration, | ||
kind: ProblemKind.MissingReturnStatement, | ||
expression: node, | ||
statement: firstStatement, | ||
commentSource: firstBlockStatement | ||
}; | ||
} | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
function checkFixedAssignableTo(checker: TypeChecker, declaration: FunctionLikeDeclaration, expr: Expression, type: Type, isFunctionType: boolean) { | ||
return checker.isTypeAssignableTo(checker.getTypeAtLocation(isFunctionType ? updateFunctionLikeBody(declaration, createBlock([createReturn(expr)])) : expr), type); | ||
} | ||
|
||
function getInfo(checker: TypeChecker, sourceFile: SourceFile, position: number, errorCode: number): Info | undefined { | ||
const node = getTokenAtPosition(sourceFile, position); | ||
if (!node.parent) return undefined; | ||
|
||
const declaration = findAncestor(node.parent, isFunctionLikeDeclaration); | ||
switch (errorCode) { | ||
case Diagnostics.A_function_whose_declared_type_is_neither_void_nor_any_must_return_a_value.code: | ||
if (!declaration || !declaration.body || !declaration.type || !rangeContainsRange(declaration.type, node)) return undefined; | ||
return getFixInfo(checker, declaration, checker.getTypeFromTypeNode(declaration.type), /* isFunctionType */ false); | ||
case Diagnostics.Argument_of_type_0_is_not_assignable_to_parameter_of_type_1.code: | ||
if (!declaration || !isCallExpression(declaration.parent) || !declaration.body) return undefined; | ||
const pos = declaration.parent.arguments.indexOf(<Expression>declaration); | ||
const type = checker.getContextualTypeForArgumentAtIndex(declaration.parent, pos); | ||
if (!type) return undefined; | ||
return getFixInfo(checker, declaration, type, /* isFunctionType */ true); | ||
case Diagnostics.Type_0_is_not_assignable_to_type_1.code: | ||
if (!isDeclarationName(node) || !isVariableLike(node.parent) && !isJsxAttribute(node.parent)) return undefined; | ||
const initializer = getVariableLikeInitializer(node.parent); | ||
if (!initializer || !isFunctionLikeDeclaration(initializer) || !initializer.body) return undefined; | ||
return getFixInfo(checker, initializer, checker.getTypeAtLocation(node.parent), /* isFunctionType */ true); | ||
} | ||
return undefined; | ||
} | ||
|
||
function getVariableLikeInitializer(declaration: VariableLikeDeclaration): Expression | undefined { | ||
switch (declaration.kind) { | ||
case SyntaxKind.VariableDeclaration: | ||
case SyntaxKind.Parameter: | ||
case SyntaxKind.BindingElement: | ||
case SyntaxKind.PropertyDeclaration: | ||
case SyntaxKind.PropertyAssignment: | ||
return declaration.initializer; | ||
case SyntaxKind.JsxAttribute: | ||
return declaration.initializer && (isJsxExpression(declaration.initializer) ? declaration.initializer.expression : undefined); | ||
case SyntaxKind.ShorthandPropertyAssignment: | ||
case SyntaxKind.PropertySignature: | ||
case SyntaxKind.EnumMember: | ||
case SyntaxKind.JSDocPropertyTag: | ||
case SyntaxKind.JSDocParameterTag: | ||
return undefined; | ||
} | ||
} | ||
|
||
function addReturnStatement(changes: textChanges.ChangeTracker, sourceFile: SourceFile, expression: Expression, statement: Statement) { | ||
suppressLeadingAndTrailingTrivia(expression); | ||
const probablyNeedSemi = probablyUsesSemicolons(sourceFile); | ||
changes.replaceNode(sourceFile, statement, createReturn(expression), { | ||
leadingTriviaOption: textChanges.LeadingTriviaOption.Exclude, | ||
trailingTriviaOption: textChanges.TrailingTriviaOption.Exclude, | ||
suffix: probablyNeedSemi ? ";" : undefined | ||
}); | ||
} | ||
|
||
function removeBlockBodyBrace(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ArrowFunction, expression: Expression, commentSource: Node, withParen: boolean) { | ||
const newBody = (withParen || needsParentheses(expression)) ? createParen(expression) : expression; | ||
suppressLeadingAndTrailingTrivia(commentSource); | ||
copyComments(commentSource, newBody); | ||
|
||
changes.replaceNode(sourceFile, declaration.body, newBody); | ||
} | ||
|
||
function wrapBlockWithParen(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ArrowFunction, expression: Expression) { | ||
changes.replaceNode(sourceFile, declaration.body, createParen(expression)); | ||
} | ||
|
||
function getActionForfixAddReturnStatement(context: CodeFixContext, expression: Expression, statement: Statement) { | ||
const changes = textChanges.ChangeTracker.with(context, t => addReturnStatement(t, context.sourceFile, expression, statement)); | ||
return createCodeFixAction(fixId, changes, Diagnostics.Add_a_return_statement, fixIdAddReturnStatement, Diagnostics.Add_all_missing_return_statement); | ||
} | ||
|
||
function getActionForfixRemoveBlockBodyBrace(context: CodeFixContext, declaration: ArrowFunction, expression: Expression, commentSource: Node) { | ||
const changes = textChanges.ChangeTracker.with(context, t => removeBlockBodyBrace(t, context.sourceFile, declaration, expression, commentSource, /* withParen */ false)); | ||
return createCodeFixAction(fixId, changes, Diagnostics.Remove_block_body_braces, fixIdRemoveBlockBodyBrace, Diagnostics.Remove_all_incorrect_body_block_braces); | ||
} | ||
|
||
function getActionForfixWrapTheBlockWithParen(context: CodeFixContext, declaration: ArrowFunction, expression: Expression) { | ||
const changes = textChanges.ChangeTracker.with(context, t => wrapBlockWithParen(t, context.sourceFile, declaration, expression)); | ||
return createCodeFixAction(fixId, changes, Diagnostics.Wrap_the_following_body_with_parentheses_which_should_be_an_object_literal, fixIdWrapTheBlockWithParen, Diagnostics.Wrap_all_object_literal_with_parentheses); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Arguably, these are problem kinds. The corresponding fix kinds would be "AddReturnStatement" and "AddParentheses".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are there only two kinds of fixes? Shouldn't there also be one for removing the braces?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And why not just use the fix IDs from above?