X Tutup
The Wayback Machine - https://web.archive.org/web/20200528061916/https://github.com/microsoft/TypeScript/pull/26434
Skip to content
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

Merged
merged 28 commits into from Apr 2, 2020

Conversation

@Kingwl
Copy link
Member

Kingwl commented Aug 14, 2018

Fixes #25751

this pr surmise return type and check the Assignable

the code fix handle follow 4 cases:

    1. function like with return type annotation
    1. call expression with a function like parameter, and argument is a function like expression
    1. assignment with a type annotation, and initializer is a function like expression
    1. jsx property assignment whitch can surmise the return type

and surmise follow 3 rules:

    1. only one expression statement in Block body and the type is assignable
    1. only one Block in Block body and the block only contains one labeled statement, make an object literal generated from label and statement, and that is assignable
      eg:
function A () {
    { a: 'b' }
}

a => {
    { a: 'b' }
}
    1. declaration is arrow function and only one labeled statement in Block body, make an object literal generated from label and statement, and that is assignable
      eg:
a => {
    a: 'b'
}

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:

    1. add test case of fix all
    1. add test case of actions
    1. add more edge case for fix (eg. object literal or comma with the paren )
    1. add more test for labeled statement fix
    1. add jsx support
    1. add property assign / class property declartion support
Kingwl added 3 commits Aug 10, 2018
Kingwl added 2 commits Aug 16, 2018
@Kingwl Kingwl changed the title [WIP] [Review need] Return value surmise Return value surmise Aug 20, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Sep 17, 2018
@RyanCavanaugh
Copy link
Member

RyanCavanaugh commented Jan 25, 2019

@DanielRosenwasser I'd like to talk about this one offline

@Kingwl
Copy link
Member Author

Kingwl commented Jan 30, 2019

any offline update here? 🤣

@Kingwl
Copy link
Member Author

Kingwl commented Apr 1, 2019

🙋🏻‍♂️

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Apr 2, 2019

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.

@Kingwl
Copy link
Member Author

Kingwl commented Apr 29, 2019

Maybe I could support more case:

function foo () {
  return // line feed
  {
    a: 1
  }
}
@DanielRosenwasser DanielRosenwasser changed the title Return value surmise Quick fix for functions lacking return expressions Apr 29, 2019
Kingwl added 4 commits Apr 30, 2019
@Kingwl
Copy link
Member Author

Kingwl commented May 10, 2019

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.

Copy link
@DanielRosenwasser

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.

Copy link
@DanielRosenwasser

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.

Copy link
@RyanCavanaugh

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.

Copy link
@DanielRosenwasser

DanielRosenwasser May 14, 2019

Member

Upper-case on fix, here and below

Copy link
Member

RyanCavanaugh left a comment

@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.

Copy link
@RyanCavanaugh

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.

@Kingwl
Copy link
Member Author

Kingwl commented Jan 9, 2020

@typescript-bot test this.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Jan 9, 2020

Heya @Kingwl, I've started to run the extended test suite on this PR at 537e806. You can monitor the build here. It should now contribute to this PR's status checks.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jan 9, 2020

did you mean

@typescript-bot pack this

?

The tests shouldn't uncover anything since this is an editor feature, right?

Kingwl added 2 commits Mar 18, 2020
@Kingwl
Copy link
Member Author

Kingwl commented Mar 18, 2020

@typescript-bot pack this.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 18, 2020

Heya @Kingwl, I've started to run the tarball bundle task on this PR at 7fe9a82. You can monitor the build here.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 18, 2020

Hey @Kingwl, I've packed this into an installable tgz. You can install it for testing by referencing it in your package.json like so:

{
    "devDependencies": {
        "typescript": "https://typescript.visualstudio.com/cf7ac146-d525-443c-b23c-0d58337efebc/_apis/build/builds/68505/artifacts?artifactName=tgz&fileId=8C63B8D64E32F09B6D654098D56F19BF5BD7814BCE0D6A377468DAB51806CFA502&fileName=/typescript-3.9.0-insiders.20200318.tgz"
    }
}

and then running npm install.


There is also a playground for this build.

@Kingwl
Copy link
Member Author

Kingwl commented Mar 24, 2020

Friendly ping @amcasey

@amcasey
Copy link
Member

amcasey commented Mar 24, 2020

@Kingwl The commits kept coming, so I figured I'd just wait for your ping. 😄

Copy link
Member

amcasey left a comment

I made some nit-picky comments, but it looks good. Tomorrow, I'll build and play with it. Thanks!

src/compiler/diagnosticMessages.json Outdated Show resolved Hide resolved
];

enum FixKind {
MissingReturnStatement,

This comment has been minimized.

Copy link
@amcasey

amcasey Mar 25, 2020

Member

Arguably, these are problem kinds. The corresponding fix kinds would be "AddReturnStatement" and "AddParentheses".

This comment has been minimized.

Copy link
@amcasey

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.

Copy link
@amcasey

amcasey Mar 25, 2020

Member

And why not just use the fix IDs from above?

src/services/codefixes/returnValueCorrect.ts Outdated Show resolved Hide resolved
tests/cases/fourslash/codeFixSurmiseReturnValue1.ts Outdated Show resolved Hide resolved
}

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.

Copy link
@amcasey

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.

Copy link
@amcasey

amcasey Mar 26, 2020

Member

Looks like it might delete {/*this comment*/1}. Not the end of the world.

Copy link
Member

amcasey left a comment

Feels good when I use it locally. Still some minor comments open about code cleanliness, but I'd basically merge this as-is.

src/compiler/diagnosticMessages.json Outdated Show resolved Hide resolved
}

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.

Copy link
@amcasey

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.

Copy link
@amcasey

amcasey Mar 26, 2020

Member

Actually, I think @andrewbranch did a bunch of work to make semicolons match the surrounding style automatically. 😄

Kingwl added 3 commits Mar 26, 2020
@Kingwl Kingwl force-pushed the Kingwl:returnValueSurmise branch from 16d90f0 to 175cf4e Mar 26, 2020
@Kingwl
Copy link
Member Author

Kingwl commented Mar 26, 2020

@typescript-bot pack this.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Mar 26, 2020

Heya @Kingwl, I've started to run the tarball bundle task on this PR at 8b332fe. You can monitor the build here.

@Kingwl
Copy link
Member Author

Kingwl commented Mar 27, 2020

@DanielRosenwasser Is there any possibility to get this done in 3.9?

PR Backlog automation moved this from Waiting on author to Needs merge Apr 1, 2020
@amcasey
Copy link
Member

amcasey commented Apr 2, 2020

@Kingwl Anything you're still working on (besides the merge conflict?) or shall I merge?

@Kingwl Kingwl force-pushed the Kingwl:returnValueSurmise branch from 31a0931 to 522cac8 Apr 2, 2020
@Kingwl
Copy link
Member Author

Kingwl commented Apr 2, 2020

No, please feel free to merge

@amcasey amcasey merged commit afc41f0 into microsoft:master Apr 2, 2020
8 checks passed
8 checks passed
build (8.x)
Details
build (10.x)
Details
build (12.x)
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
license/cla All CLA requirements met.
Details
node10 Build #70163 succeeded
Details
node12 Build #70161 succeeded
Details
node8 Build #70162 succeeded
Details
PR Backlog automation moved this from Needs merge to Done Apr 2, 2020
@Kingwl Kingwl deleted the Kingwl:returnValueSurmise branch Apr 2, 2020
PopGoesTheWza pushed a commit to PopGoesTheWza/TypeScript that referenced this pull request Apr 7, 2020
* '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)
  ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
X Tutup