Skip to content

Commit

Permalink
Add parentheses to TypeofTypeAnnotation (#14458)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Mar 8, 2023
1 parent 3d70637 commit f1b18f2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
13 changes: 13 additions & 0 deletions changelog_unreleased/flow/14458.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -1 +1 @@
run_spec(__dirname, ["typescript"]);
run_spec(__dirname, ["typescript", "flow"]);
8 changes: 4 additions & 4 deletions tests/format/typescript/typeof/typeof.ts
Original file line number Diff line number Diff line change
@@ -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)];

0 comments on commit f1b18f2

Please sign in to comment.