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

Fix to add parentheses for TSTypeQuery in some case #14042

Merged
merged 13 commits into from
Dec 30, 2022
19 changes: 19 additions & 0 deletions changelog_unreleased/typescript/14042.md
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][];
```
9 changes: 8 additions & 1 deletion src/language-js/needs-parens.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Copy link
Member

@fisker fisker Dec 29, 2022

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

--parser typescript

Input:

a as (typeof node.children)[number]
a as (number)[typeof node.children]

Output:

a as (typeof node.children)[number];
a as number[(typeof node.children)];

Copy link
Contributor Author

@onishi-kohei onishi-kohei Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a as (typeof node.children)[]

this property name was elementType
May I add a case like the code below?

return (
        name === "objectType" || name === "elementType"   &&
        (parent.type === "TSIndexedAccessType" || parent.type === "TSArrayType")
      )

Copy link
Member

@fisker fisker Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, should be

return (
        (name === "objectType" && parent.type === "TSIndexedAccessType") ||
        (name === "elementType" && parent.type === "TSArrayType")
      )

Copy link
Contributor Author

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

// fallthrough
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think TSTypeQuery(TypeScript node type) can be a child of NullableTypeAnnotation(flow node type). This should not fallthrough.

case "ArrayTypeAnnotation":
return parent.type === "NullableTypeAnnotation";

Expand Down
19 changes: 19 additions & 0 deletions tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap
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][];

================================================================================
`;
1 change: 1 addition & 0 deletions tests/format/typescript/typeof/jsfmt.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run_spec(__dirname, ["typescript"]);
3 changes: 3 additions & 0 deletions tests/format/typescript/typeof/typeof.ts
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])[]