-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Fix to add parentheses for TSTypeQuery in some case #14042
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
4357463
add TSTypeQuery to needsParens
onishi-kohei 8bd1d57
fix to lint prettier
onishi-kohei 6075690
add test
onishi-kohei 13d63c2
remove comment
onishi-kohei 20e5a29
add changelog
onishi-kohei 2fa910b
fix lint
onishi-kohei 338124e
remove template comments at top in changelog
onishi-kohei 15096f1
Fix changelog
fisker 57d3a18
Add new line
fisker 59a7a56
check objectType
onishi-kohei f3f7503
remove fallthrough comment
onishi-kohei 7035317
check property name
onishi-kohei 7ea60a6
Simplify changelog
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 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,19 @@ | ||
#### Add parentheses for TSTypeQuery to improve readability (#14042 by @onishi-kohei) | ||
|
||
<!-- prettier-ignore --> | ||
```tsx | ||
// Input | ||
a as (typeof node.children)[number] | ||
a as (typeof node.children)[] | ||
a as ((typeof node.children)[number])[] | ||
|
||
// Prettier stable | ||
a as typeof node.children[number]; | ||
a as typeof node.children[]; | ||
a as typeof node.children[number][]; | ||
|
||
// Prettier main | ||
a as (typeof node.children)[number]; | ||
a as (typeof node.children)[]; | ||
a as (typeof node.children)[number][]; | ||
``` |
This file contains 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 |
---|---|---|
|
@@ -456,7 +456,14 @@ function needsParens(path, options) { | |
(parent.type === "TSTypeAnnotation" && | ||
path.getParentNode(1).type.startsWith("TSJSDoc")) | ||
); | ||
|
||
case "TSTypeQuery": | ||
if ( | ||
parent.type === "TSIndexedAccessType" || | ||
parent.type === "TSArrayType" | ||
) { | ||
return true; | ||
} | ||
// fallthrough | ||
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. I don't think |
||
case "ArrayTypeAnnotation": | ||
return parent.type === "NullableTypeAnnotation"; | ||
|
||
|
19 changes: 19 additions & 0 deletions
19
tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap
This file contains 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,19 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`typeof.ts format 1`] = ` | ||
====================================options===================================== | ||
parsers: ["typescript"] | ||
printWidth: 80 | ||
| printWidth | ||
=====================================input====================================== | ||
a as (typeof node.children)[number] | ||
a as (typeof node.children)[] | ||
a as ((typeof node.children)[number])[] | ||
|
||
=====================================output===================================== | ||
a as (typeof node.children)[number]; | ||
a as (typeof node.children)[]; | ||
a as (typeof node.children)[number][]; | ||
|
||
================================================================================ | ||
`; |
This file contains 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 @@ | ||
run_spec(__dirname, ["typescript"]); |
This file contains 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,3 @@ | ||
a as (typeof node.children)[number] | ||
a as (typeof node.children)[] | ||
a as ((typeof node.children)[number])[] |
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.
We need check the property name here, in this case
name === "objectType"
.Prettier pr-14042
Playground link
Input:
Output:
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.
this property name was
elementType
May I add a case like the code below?
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.
No, should be
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.
Fixed in this commit. 7035317