-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Support conditional type and infer type in Flow #14573
Changes from 1 commit
5cb1db5
16e9455
f8c6d88
b6e25c9
29cea8c
8cae9e0
3983381
3e90968
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#### Support conditional type and infer type (#14573 by @SamChou19815) | ||
|
||
<!-- Optional description if it makes sense. --> | ||
|
||
<!-- 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; | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,7 +152,11 @@ function printTypeParameter(path, options, print) { | |
parts.push(name); | ||
|
||
if (node.bound) { | ||
parts.push(printTypeAnnotationProperty(path, print, "bound")); | ||
if (node.usesExtendsBound) { | ||
parts.push(" extends ", print(["bound", "typeAnnotation"])); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This won't work? parts.push(" extends ", print("bound")); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I understand the problem, we should still use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found a case Prettier pr-14573 --parser flow Input: type A = () => infer R extends/* comment */ string Output: Error: Comment "comment" was not printed. Please report this error!
at dn (https://deploy-preview-14573--prettier.netlify.app/lib/standalone.js:29:5817)
at Ze (https://deploy-preview-14573--prettier.netlify.app/lib/standalone.js:29:7308)
at async kn (https://deploy-preview-14573--prettier.netlify.app/lib/standalone.js:29:11045)
at async ju (https://deploy-preview-14573--prettier.netlify.app/lib/standalone.js:34:581)
at async formatCode (https://deploy-preview-14573--prettier.netlify.app/worker.js:159:12)
at async handleFormatMessage (https://deploy-preview-14573--prettier.netlify.app/worker.js:83:24)
at async self.onmessage (https://deploy-preview-14573--prettier.netlify.app/worker.js:38:14) |
||
} else { | ||
parts.push(printTypeAnnotationProperty(path, print, "bound")); | ||
} | ||
} | ||
|
||
if (node.constraint) { | ||
|
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; | ||
|
||
================================================================================ | ||
`; |
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...