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 function only contains one jsx element and have not the return statement #25751

Closed
4 tasks done
Kingwl opened this issue Jul 18, 2018 · 6 comments · Fixed by #26434
Closed
4 tasks done
Labels
Domain: Error Messages The issue relates to error messaging Domain: Quick Fixes Editor-provided fixes, often called code actions. Good First Issue Well scoped, documented and has the green light Help Wanted You can do this Suggestion An idea for TypeScript Update Docs on Next Release Indicates that this PR affects docs

Comments

@Kingwl
Copy link
Contributor

Kingwl commented Jul 18, 2018

Search Terms

quickfix, jsx, function

Suggestion

quick fix for function only contains one jsx element and have not the return statement

Use Cases

the pattern of a function with block body that only have one jsx element without return statement
that seems a error
could we provide a quickfix for that?

Examples

items.map(x => {
  <div>123</div>
})

to

items.map(x => (
  <div>123</div>
))

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jul 18, 2018

Great idea. It doesn't even have to be limited to JSX. I'm not sure how we'd plumb the logic through, but there are two fixes I'd have in mind:

  1. For any block body containing a single ExpressionStatement whose expression's type matches the expected type, suggest the following:
  • Add a return statement.
  • Remove block body braces.
  • Replace braces with parentheses.
  1. For any block body containing only a labeled ExpressionStatement, check to see if the expected type contains a property with the same name as the label, and if so, suggest
  • Wrap this block with parentheses.

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Domain: Error Messages The issue relates to error messaging Domain: Quick Fixes Editor-provided fixes, often called code actions. labels Jul 18, 2018
@DanielRosenwasser DanielRosenwasser added the Help Wanted You can do this label Jul 18, 2018
@DanielRosenwasser DanielRosenwasser added this to the TypeScript 3.1 milestone Jul 18, 2018
@Kingwl
Copy link
Contributor Author

Kingwl commented Jul 18, 2018

should that a quickfix? maybe refactor is more suitable

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Jul 18, 2018

Well, I guess your example is a refactoring since there's no error (you just end up producing a void[].

The cases I have in mind should have an associated error.

@Kingwl
Copy link
Contributor Author

Kingwl commented Jul 18, 2018

sometime we cannot infer the return type(no context, no type annotation)
will we ignore those case?

@Kingwl
Copy link
Contributor Author

Kingwl commented Aug 10, 2018

need some help:
how could i know type are related in quick fix without isTypeRelatedTo😢

@DanielRosenwasser DanielRosenwasser added the Good First Issue Well scoped, documented and has the green light label Oct 30, 2018
@RyanCavanaugh RyanCavanaugh modified the milestones: Community, Backlog Mar 7, 2019
@DanielRosenwasser DanielRosenwasser added the Update Docs on Next Release Indicates that this PR affects docs label Apr 2, 2020
@DanielRosenwasser
Copy link
Member

Thanks @Kingwl!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Domain: Error Messages The issue relates to error messaging Domain: Quick Fixes Editor-provided fixes, often called code actions. Good First Issue Well scoped, documented and has the green light Help Wanted You can do this Suggestion An idea for TypeScript Update Docs on Next Release Indicates that this PR affects docs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@DanielRosenwasser @weswigham @RyanCavanaugh @Kingwl and others