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

Add parentheses to TypeofTypeAnnotation #14458

Merged
merged 5 commits into from Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions changelog_unreleased/flow/14458.md
@@ -0,0 +1,13 @@
#### Add parentheses for `TypeofTypeAnnotation` to improve readability (#14458 by @fisker)

<!-- prettier-ignore -->
```tsx
// Input
type A = (typeof node.children)[];

// Prettier stable
type A = typeof node.children[];

// Prettier main
type A = (typeof node.children)[];
```
15 changes: 8 additions & 7 deletions src/language-js/needs-parens.js
Expand Up @@ -529,6 +529,14 @@ function needsParens(path, options) {
(name === "objectType" && parent.type === "TSIndexedAccessType") ||
(name === "elementType" && parent.type === "TSArrayType")
);
// Same as `TSTypeQuery`, but for Flow syntax
case "TypeofTypeAnnotation":
return (
(name === "objectType" &&
(parent.type === "IndexedAccessType" ||
parent.type === "OptionalIndexedAccessType")) ||
(name === "elementType" && parent.type === "ArrayTypeAnnotation")
);
case "ArrayTypeAnnotation":
return parent.type === "NullableTypeAnnotation";

Expand Down Expand Up @@ -583,13 +591,6 @@ function needsParens(path, options) {
case "OptionalIndexedAccessType":
return name === "objectType" && parent.type === "IndexedAccessType";

case "TypeofTypeAnnotation":
return (
name === "objectType" &&
(parent.type === "IndexedAccessType" ||
parent.type === "OptionalIndexedAccessType")
);

case "StringLiteral":
case "NumericLiteral":
case "Literal":
Expand Down
18 changes: 9 additions & 9 deletions tests/format/typescript/typeof/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -2,20 +2,20 @@

exports[`typeof.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
parsers: ["typescript", "flow"]
printWidth: 80
| printWidth
=====================================input======================================
a as (typeof node.children)[number]
a as (typeof node.children)[]
a as ((typeof node.children)[number])[]
a as number[(typeof node.children)]
type A = (typeof node.children)[number];
type B = (typeof node.children)[];
type C = ((typeof node.children)[number])[];
type D = number[(typeof node.children)];

=====================================output=====================================
a as (typeof node.children)[number];
a as (typeof node.children)[];
a as (typeof node.children)[number][];
a as number[typeof node.children];
type A = (typeof node.children)[number];
type B = (typeof node.children)[];
type C = (typeof node.children)[number][];
type D = number[typeof node.children];

================================================================================
`;
2 changes: 1 addition & 1 deletion tests/format/typescript/typeof/jsfmt.spec.js
@@ -1 +1 @@
run_spec(__dirname, ["typescript"]);
run_spec(__dirname, ["typescript", "flow"]);
8 changes: 4 additions & 4 deletions tests/format/typescript/typeof/typeof.ts
@@ -1,4 +1,4 @@
a as (typeof node.children)[number]
a as (typeof node.children)[]
a as ((typeof node.children)[number])[]
a as number[(typeof node.children)]
type A = (typeof node.children)[number];
type B = (typeof node.children)[];
type C = ((typeof node.children)[number])[];
type D = number[(typeof node.children)];