fix(TypeScript): specific union type breaks after opening parenthesis, but not before closing (#6307)
parent
ce366f0834
commit
9e5a5435a1
|
@ -368,6 +368,30 @@ foo(
|
|||
);
|
||||
```
|
||||
|
||||
#### TypeScript: Fix specific union type breaks after opening parenthesis, but not before closing ([#6307] by [@sosukesuzuki])
|
||||
|
||||
Previously, union type that put with `as` , `keyof`, `[]`, other union(`|`) and intersection(`&`) breaks after opening parenthesis, but not before closing. Please see [#6303](https://github.com/prettier/prettier/issues/6303) for detail.
|
||||
|
||||
<!-- prettier-ignore-->
|
||||
```ts
|
||||
// Input
|
||||
const foo = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
|
||||
| string
|
||||
| undefined
|
||||
)[];
|
||||
|
||||
// Prettier (stable)
|
||||
const foo = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
|
||||
| string
|
||||
| undefined)[];
|
||||
|
||||
// Prettier (master)
|
||||
const foo = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
|
||||
| string
|
||||
| undefined
|
||||
)[];
|
||||
```
|
||||
|
||||
[#5910]: https://github.com/prettier/prettier/pull/5910
|
||||
[#6186]: https://github.com/prettier/prettier/pull/6186
|
||||
[#6206]: https://github.com/prettier/prettier/pull/6206
|
||||
|
@ -380,6 +404,7 @@ foo(
|
|||
[#6332]: https://github.com/prettier/prettier/pull/6332
|
||||
[#6284]: https://github.com/prettier/prettier/pull/6284
|
||||
[#6301]: https://github.com/prettier/prettier/pull/6301
|
||||
[#6307]: https://github.com/prettier/prettier/pull/6307
|
||||
[@duailibe]: https://github.com/duailibe
|
||||
[@gavinjoyce]: https://github.com/gavinjoyce
|
||||
[@sosukesuzuki]: https://github.com/sosukesuzuki
|
||||
|
|
|
@ -2813,15 +2813,22 @@ function printPathNoParens(path, options, print, args) {
|
|||
let hasParens;
|
||||
|
||||
if (n.type === "TSUnionType") {
|
||||
const grandParent = path.getNode(2);
|
||||
const greatGrandParent = path.getParentNode(2);
|
||||
const greatGreatGrandParent = path.getParentNode(3);
|
||||
|
||||
hasParens =
|
||||
greatGrandParent &&
|
||||
greatGrandParent.type === "TSParenthesizedType" &&
|
||||
greatGreatGrandParent &&
|
||||
(greatGreatGrandParent.type === "TSUnionType" ||
|
||||
greatGreatGrandParent.type === "TSIntersectionType");
|
||||
(parent.type === "TSParenthesizedType" &&
|
||||
(grandParent.type === "TSAsExpression" ||
|
||||
grandParent.type === "TSUnionType" ||
|
||||
grandParent.type === "TSIntersectionType" ||
|
||||
grandParent.type === "TSTypeOperator" ||
|
||||
grandParent.type === "TSArrayType")) ||
|
||||
(greatGrandParent &&
|
||||
greatGrandParent.type === "TSParenthesizedType" &&
|
||||
greatGreatGrandParent &&
|
||||
(greatGreatGrandParent.type === "TSUnionType" ||
|
||||
greatGreatGrandParent.type === "TSIntersectionType"));
|
||||
} else {
|
||||
hasParens = pathNeedsParens(path, options);
|
||||
}
|
||||
|
|
|
@ -123,6 +123,34 @@ type State = {
|
|||
| { discriminant: "BAZ"; baz: any }
|
||||
);
|
||||
|
||||
const foo = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
|
||||
| string
|
||||
| undefined
|
||||
)[];
|
||||
|
||||
const foo: (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
)[] = [];
|
||||
|
||||
const foo: keyof (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
) = bar;
|
||||
|
||||
const foo:
|
||||
| foo
|
||||
| (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
) = bar;
|
||||
|
||||
=====================================output=====================================
|
||||
export type A =
|
||||
| aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
|
||||
|
@ -159,7 +187,36 @@ type State = {
|
|||
} & (
|
||||
| { discriminant: "FOO"; foo: any }
|
||||
| { discriminant: "BAR"; bar: any }
|
||||
| { discriminant: "BAZ"; baz: any });
|
||||
| { discriminant: "BAZ"; baz: any }
|
||||
);
|
||||
|
||||
const foo = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
|
||||
| string
|
||||
| undefined
|
||||
)[];
|
||||
|
||||
const foo: (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
)[] = [];
|
||||
|
||||
const foo: keyof (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
) = bar;
|
||||
|
||||
const foo:
|
||||
| foo
|
||||
| (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
) = bar;
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
|
|
@ -38,3 +38,31 @@ type State = {
|
|||
| { discriminant: "BAR"; bar: any }
|
||||
| { discriminant: "BAZ"; baz: any }
|
||||
);
|
||||
|
||||
const foo = [abc, def, ghi, jkl, mno, pqr, stu, vwx, yz] as (
|
||||
| string
|
||||
| undefined
|
||||
)[];
|
||||
|
||||
const foo: (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
)[] = [];
|
||||
|
||||
const foo: keyof (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
) = bar;
|
||||
|
||||
const foo:
|
||||
| foo
|
||||
| (
|
||||
| AAAAAAAAAAAAAAAAAAAAAA
|
||||
| BBBBBBBBBBBBBBBBBBBBBB
|
||||
| CCCCCCCCCCCCCCCCCCCCCC
|
||||
| DDDDDDDDDDDDDDDDDDDDDD
|
||||
) = bar;
|
||||
|
|
Loading…
Reference in New Issue