Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upQuick 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); |
This comment has been minimized.
This comment has been minimized.
DanielRosenwasser
May 14, 2019
Member
@rbuckton is this going to cause any trouble? Here, the type-checker is going to jump into synthetic nodes.
| "category": "Message", | ||
| "code": 95080 | ||
| }, | ||
| "Surmise all return value": { |
This comment has been minimized.
This comment has been minimized.
DanielRosenwasser
May 14, 2019
Member
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?
This comment has been minimized.
This comment has been minimized.
RyanCavanaugh
Jan 8, 2020
Member
"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) { |
This comment has been minimized.
This comment has been minimized.
|
@Kingwl Overall looking pretty good; just need a couple fixes here before merge. Thanks! |
| "category": "Message", | ||
| "code": 95080 | ||
| }, | ||
| "Surmise all return value": { |
This comment has been minimized.
This comment has been minimized.
RyanCavanaugh
Jan 8, 2020
Member
"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. |
|
I made some nit-picky comments, but it looks good. Tomorrow, I'll build and play with it. Thanks! |
| ]; | ||
|
|
||
| enum FixKind { | ||
| MissingReturnStatement, |
This comment has been minimized.
This comment has been minimized.
amcasey
Mar 25, 2020
Member
Arguably, these are problem kinds. The corresponding fix kinds would be "AddReturnStatement" and "AddParentheses".
This comment has been minimized.
This comment has been minimized.
amcasey
Mar 25, 2020
Member
Why are there only two kinds of fixes? Shouldn't there also be one for removing the braces?
This comment has been minimized.
This comment has been minimized.
| } | ||
|
|
||
| function removeBlockBodyBrace(changes: textChanges.ChangeTracker, sourceFile: SourceFile, declaration: ArrowFunction, expression: Expression, withParen: boolean) { | ||
| changes.replaceNode(sourceFile, declaration.body, (withParen || needsParentheses(expression)) ? createParen(expression) : expression); |
This comment has been minimized.
This comment has been minimized.
amcasey
Mar 25, 2020
Member
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).
This comment has been minimized.
This comment has been minimized.
amcasey
Mar 26, 2020
Member
Looks like it might delete {/*this comment*/1}. Not the end of the world.
|
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); |
This comment has been minimized.
This comment has been minimized.
amcasey
Mar 26, 2020
Member
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' }, |
This comment has been minimized.
This comment has been minimized.
amcasey
Mar 26, 2020
Member
Actually, I think @andrewbranch did a bunch of work to make semicolons match the surrounding style automatically.
|
@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? |
|
No, please feel free to merge |
* 'master' of https://github.com/Microsoft/TypeScript: (258 commits) Extra check in assignment of intersections with generic constituents (microsoft#37537) Generic functions are never context sensitive (microsoft#37811) Fix serialisation of static class members in JS (microsoft#37780) Allow Source Mapping inside destructuring assignment (microsoft#37298) Consider arrays and tuples within one another as possibly requiring deferral (microsoft#37776) Fix crash for private identifier in expando assignments (microsoft#37764) LEGO: check in for master to temporary branch. Cache the regularized form of union types (microsoft#37749) Reduce intersections with conflicting privates, elaborate on reasons (microsoft#37762) Fix variable name collisions (microsoft#37761) Remove error when spreading optional any (microsoft#37757) fix(37456): add tests for JsxOpeningElement nodes (microsoft#37752) Quick fix for functions lacking return expressions (microsoft#26434) Fix goto implementation does not suggest all subtypes (microsoft#33652) Fix rename for type symbols imported as a different name (microsoft#37745) fix(37431): allow only one space between async keyword and method name (microsoft#37504) Reuse input type nodes when serializing signature parameter and return types (microsoft#37444) Add replacement span for string literal (microsoft#37490) Explicitly merge module augmentation members into the exports added by export * declarations (microsoft#37691) fix(37456): omit type arguments from JsxSelfClosingElement, JsxOpeningElement nodes (microsoft#37739) ...


Kingwl commentedAug 14, 2018
•
edited
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: