File tree 5 files changed +35
-21
lines changed
changelog_unreleased/flow
tests/format/typescript/typeof
5 files changed +35
-21
lines changed Original file line number Diff line number Diff line change
1
+ #### Add parentheses for ` TypeofTypeAnnotation ` to improve readability (#14458 by @fisker )
2
+
3
+ <!-- prettier-ignore -->
4
+ ``` tsx
5
+ // Input
6
+ type A = (typeof node .children )[];
7
+
8
+ // Prettier stable
9
+ type A = typeof node .children [];
10
+
11
+ // Prettier main
12
+ type A = (typeof node .children )[];
13
+ ```
Original file line number Diff line number Diff line change @@ -529,6 +529,14 @@ function needsParens(path, options) {
529
529
( name === "objectType" && parent . type === "TSIndexedAccessType" ) ||
530
530
( name === "elementType" && parent . type === "TSArrayType" )
531
531
) ;
532
+ // Same as `TSTypeQuery`, but for Flow syntax
533
+ case "TypeofTypeAnnotation" :
534
+ return (
535
+ ( name === "objectType" &&
536
+ ( parent . type === "IndexedAccessType" ||
537
+ parent . type === "OptionalIndexedAccessType" ) ) ||
538
+ ( name === "elementType" && parent . type === "ArrayTypeAnnotation" )
539
+ ) ;
532
540
case "ArrayTypeAnnotation" :
533
541
return parent . type === "NullableTypeAnnotation" ;
534
542
@@ -583,13 +591,6 @@ function needsParens(path, options) {
583
591
case "OptionalIndexedAccessType" :
584
592
return name === "objectType" && parent . type === "IndexedAccessType" ;
585
593
586
- case "TypeofTypeAnnotation" :
587
- return (
588
- name === "objectType" &&
589
- ( parent . type === "IndexedAccessType" ||
590
- parent . type === "OptionalIndexedAccessType" )
591
- ) ;
592
-
593
594
case "StringLiteral" :
594
595
case "NumericLiteral" :
595
596
case "Literal" :
Original file line number Diff line number Diff line change 2
2
3
3
exports [` typeof.ts format 1` ] = `
4
4
====================================options=====================================
5
- parsers: ["typescript"]
5
+ parsers: ["typescript", "flow" ]
6
6
printWidth: 80
7
7
| printWidth
8
8
=====================================input======================================
9
- a as (typeof node.children)[number]
10
- a as (typeof node.children)[]
11
- a as ((typeof node.children)[number])[]
12
- a as number[(typeof node.children)]
9
+ type A = (typeof node.children)[number];
10
+ type B = (typeof node.children)[];
11
+ type C = ((typeof node.children)[number])[];
12
+ type D = number[(typeof node.children)];
13
13
14
14
=====================================output=====================================
15
- a as (typeof node.children)[number];
16
- a as (typeof node.children)[];
17
- a as (typeof node.children)[number][];
18
- a as number[typeof node.children];
15
+ type A = (typeof node.children)[number];
16
+ type B = (typeof node.children)[];
17
+ type C = (typeof node.children)[number][];
18
+ type D = number[typeof node.children];
19
19
20
20
================================================================================
21
21
` ;
Original file line number Diff line number Diff line change 1
- run_spec ( __dirname , [ "typescript" ] ) ;
1
+ run_spec ( __dirname , [ "typescript" , "flow" ] ) ;
Original file line number Diff line number Diff line change 1
- a as ( typeof node . children ) [ number ]
2
- a as ( typeof node . children ) [ ]
3
- a as ( ( typeof node . children ) [ number ] ) [ ]
4
- a as number [ ( typeof node . children ) ]
1
+ type A = ( typeof node . children ) [ number ] ;
2
+ type B = ( typeof node . children ) [ ] ;
3
+ type C = ( ( typeof node . children ) [ number ] ) [ ] ;
4
+ type D = number [ ( typeof node . children ) ] ;
You can’t perform that action at this time.
0 commit comments