-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Quick fix for functions lacking return expressions #26434
Conversation
@DanielRosenwasser I'd like to talk about this one offline |
any offline update here? 🤣 |
🙋🏻♂️ |
If I recall correctly, Ryan was just concerned that providing too many refactorings and quick fixes could become "noisy". I don't think that that should block this PR since these are legitimate fixes that can help users learn more about the language. |
Maybe I could support more case: function foo () {
return // line feed
{
a: 1
}
} |
Could this get down on 3.5? |
} | ||
|
||
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); |
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.
@rbuckton is this going to cause any trouble? Here, the type-checker is going to jump into synthetic nodes.
src/compiler/diagnosticMessages.json
Outdated
"category": "Message", | ||
"code": 95080 | ||
}, | ||
"Surmise all return value": { |
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.
I've got to admit: I don't know what the word surmise means, and I don't know if users will either. What's the intent here?
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.
"Correct all return expressions"
would be best here
"surmise" is not in basic English vocabulary and we need to avoid it entirely for clarity's sake.
changes.replaceNode(sourceFile, declaration.body, createParen(expression)); | ||
} | ||
|
||
function getActionForfixAddReturnStatement(context: CodeFixContext, declaration: FunctionLikeDeclaration, expression: Expression) { |
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.
Upper-case on fix
, here and below
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.
@Kingwl Overall looking pretty good; just need a couple fixes here before merge. Thanks!
src/compiler/diagnosticMessages.json
Outdated
"category": "Message", | ||
"code": 95080 | ||
}, | ||
"Surmise all return value": { |
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.
"Correct all return expressions"
would be best here
"surmise" is not in basic English vocabulary and we need to avoid it entirely for clarity's sake.
@typescript-bot test this. |
did you mean @typescript-bot pack this ? The tests shouldn't uncover anything since this is an editor feature, right? |
@typescript-bot pack this. |
Hey @Kingwl, I've packed this into an installable tgz. You can install it for testing by referencing it in your
and then running There is also a playground for this build. |
Friendly ping @amcasey |
@Kingwl The commits kept coming, so I figured I'd just wait for your ping. 😄 |
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.
I made some nit-picky comments, but it looks good. Tomorrow, I'll build and play with it. Thanks!
]; | ||
|
||
enum FixKind { | ||
MissingReturnStatement, |
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?
} | ||
|
||
function removeBlockBodyBrace(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ArrowFunction, expression: Expression, withParen: boolean) { | ||
changes.replaceNode(sourceFile, declaration.body, (withParen || needsParentheses(expression)) ? createParen(expression) : expression); |
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.
Is this going to delete comments? I'd be worried about anything between the end of the expression and the closing brace (and maybe things before the opening brace).
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.
Looks like it might delete {/*this comment*/1}
. Not the end of the world.
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.
Feels good when I use it locally. Still some minor comments open about code cleanliness, but I'd basically merge this as-is.
} | ||
|
||
function removeBlockBodyBrace(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ArrowFunction, expression: Expression, withParen: boolean) { | ||
changes.replaceNode(sourceFile, declaration.body, (withParen || needsParentheses(expression)) ? createParen(expression) : expression); |
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.
Looks like it might delete {/*this comment*/1}
. Not the end of the world.
//// const a: ((() => number) | (() => undefined)) = () => { 1 } | ||
|
||
verify.codeFixAvailable([ | ||
{ description: 'Add a return statement' }, |
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.
Actually, I think @andrewbranch did a bunch of work to make semicolons match the surrounding style automatically. 😄
16d90f0
to
175cf4e
Compare
@typescript-bot pack this. |
@DanielRosenwasser Is there any possibility to get this done in 3.9? |
@Kingwl Anything you're still working on (besides the merge conflict?) or shall I merge? |
31a0931
to
522cac8
Compare
No, please feel free to merge |
Fixes #25751
this pr surmise return type and check the Assignable
the code fix handle follow 4 cases:
and surmise follow 3 rules:
eg:
eg:
for first case, i try to get the return type from type annotation and compare with type of expression statement
second case, i try to get the parameter from the signature and check the Assignable with the fixed type( after insert the return type)
third case, i try to get the type of the initializer and check the Assignable type of from the variable declaration type annotation ( after insert the return type)
some thing need todo: