-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Support conditional type and infer type in Flow #14573
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5cb1db5
Support conditional type in Flow
SamChou19815 16e9455
Fix changelog
fisker f8c6d88
Fix comments and cursor tracking
fisker b6e25c9
Revert example change
fisker 29cea8c
Fix condition
fisker 8cae9e0
Fix needParen logic for infer type nested inside function type
SamChou19815 3983381
Update comment
fisker 3e90968
Concat string
fisker 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #### Support conditional type and infer type (#14573 by @SamChou19815) | ||
|
|
||
| <!-- prettier-ignore --> | ||
| ```jsx | ||
| // Input | ||
| type TestReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : any; | ||
|
|
||
| // Prettier stable | ||
| // Does not parse | ||
|
|
||
| // Prettier main | ||
| type TestReturnType<T extends (...args: any[]) => any> = T extends ( | ||
| ...args: any[] | ||
| ) => infer R | ||
| ? R | ||
| : any; | ||
| ``` |
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
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
161 changes: 161 additions & 0 deletions
161
tests/format/flow/conditional-types-comments/__snapshots__/jsfmt.spec.js.snap
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,161 @@ | ||
| // Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
|
||
| exports[`comments.js [babel-flow] format 1`] = ` | ||
| "Unexpected token (3:12) | ||
| 1 | // @flow | ||
| 2 | | ||
| > 3 | type A = B extends T | ||
| | ^ | ||
| 4 | ? // comment | ||
| 5 | foo | ||
| 6 | : bar;" | ||
| `; | ||
|
|
||
| exports[`comments.js format 1`] = ` | ||
| ====================================options===================================== | ||
| parsers: ["flow"] | ||
| printWidth: 80 | ||
| | printWidth | ||
| =====================================input====================================== | ||
| // @flow | ||
|
|
||
| type A = B extends T | ||
| ? // comment | ||
| foo | ||
| : bar; | ||
|
|
||
| type A = B extends test /* comment | ||
| comment | ||
| comment | ||
| */ | ||
| ? foo | ||
| : bar; | ||
|
|
||
| type T = test extends B | ||
| ? /* comment | ||
| comment | ||
| comment | ||
| comment | ||
| */ | ||
| foo | ||
| : bar; | ||
|
|
||
| type T = test extends B | ||
| ? /* comment | ||
| comment | ||
| comment | ||
| comment | ||
| */ | ||
| foo | ||
| : test extends B | ||
| ? /* comment | ||
| comment | ||
| comment */ | ||
| foo | ||
| : bar; | ||
|
|
||
| type T = test extends B | ||
| ? /* comment */ | ||
| foo | ||
| : bar; | ||
|
|
||
| type T = test extends B | ||
| ? foo | ||
| : /* comment | ||
| comment | ||
| comment | ||
| comment | ||
| */ | ||
| bar; | ||
|
|
||
| type T = test extends B | ||
| ? foo | ||
| : /* comment | ||
| comment | ||
| comment | ||
| comment | ||
| */ | ||
| test extends B | ||
| ? foo | ||
| : /* comment | ||
| comment | ||
| comment | ||
| */ | ||
| bar; | ||
|
|
||
| type T = test extends B | ||
| ? foo | ||
| : /* comment */ | ||
| bar; | ||
|
|
||
| type T = test extends B ? test extends B /* c | ||
| c */? foo : bar : bar; | ||
|
|
||
| =====================================output===================================== | ||
| // @flow | ||
|
|
||
| type A = B extends T // comment | ||
| ? foo | ||
| : bar; | ||
|
|
||
| type A = B extends test /* comment | ||
| comment | ||
| comment | ||
| */ | ||
| ? foo | ||
| : bar; | ||
|
|
||
| type T = test extends B /* comment | ||
| comment | ||
| comment | ||
| comment | ||
| */ | ||
| ? foo | ||
| : bar; | ||
|
|
||
| type T = test extends B /* comment | ||
| comment | ||
| comment | ||
| comment | ||
| */ | ||
| ? foo | ||
| : test extends B /* comment | ||
| comment | ||
| comment */ | ||
| ? foo | ||
| : bar; | ||
|
|
||
| type T = test extends B /* comment */ ? foo : bar; | ||
|
|
||
| type T = test extends B | ||
| ? foo /* comment | ||
| comment | ||
| comment | ||
| comment | ||
| */ | ||
| : bar; | ||
|
|
||
| type T = test extends B | ||
| ? foo /* comment | ||
| comment | ||
| comment | ||
| comment | ||
| */ | ||
| : test extends B | ||
| ? foo /* comment | ||
| comment | ||
| comment | ||
| */ | ||
| : bar; | ||
|
|
||
| type T = test extends B ? foo /* comment */ : bar; | ||
|
|
||
| type T = test extends B | ||
| ? test extends B /* c | ||
| c */ | ||
| ? foo | ||
| : bar | ||
| : bar; | ||
|
|
||
| ================================================================================ | ||
| `; |
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.
Loos like we are missing cases, it's bad we are not sharing logic between TS and Flow.
Maybe better extract some code first to reuse?
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 fixed the logic and pass the test. I'm not sure how to better extract the code, since there are very little code sharing currently in the file.
My personal opinion is that the existing fallthrough logic for TS nodes already made it harder to understand, and I actually like the more explicit logic among those Flow nodes that don't use fallthrough...