TypeScript: Fix incorrectly removes double parentheses around types (#6604)

master
Sosuke Suzuki 2019-10-08 22:56:08 +09:00 committed by Evilebot Tnawi
parent affa24ce76
commit 02cbd15ec2
11 changed files with 143 additions and 3 deletions

View File

@ -742,6 +742,29 @@ Previously, the flag was not applied on html attributes.
<div class='a-class-name'></div>
```
#### TypeScript: Fix incorrectly removes double parentheses around types ([#6604] by [@sosukesuzuki])
<!-- prettier-ignore -->
```ts
// Input
type A = 0 extends ((1 extends 2 ? 3 : 4)) ? 5 : 6;
type B = ((0 extends 1 ? 2 : 3)) extends 4 ? 5 : 6:
type C = ((number | string))["toString"];
type D = ((keyof T1))["foo"];
// Prettier (stable)
type A = 0 extends 1 extends 2 ? 3 : 4 ? 5 : 6;
type B = 0 extends 1 ? 2 : 3 extends 4 ? 5 : 6;
type C = number | string["toString"];
type D = keyof T1["foo"];
// Prettier (master)
type A = 0 extends (1 extends 2 ? 3 : 4) ? 5 : 6;
type B = (0 extends 1 ? 2 : 3) extends 4 ? 5 : 6;
type C = (number | string)["toString"];
type D = (keyof T1)["foo"];
```
[#5910]: https://github.com/prettier/prettier/pull/5910
[#6033]: https://github.com/prettier/prettier/pull/6033
[#6186]: https://github.com/prettier/prettier/pull/6186
@ -768,6 +791,7 @@ Previously, the flag was not applied on html attributes.
[#6514]: https://github.com/prettier/prettier/pull/6514
[#6467]: https://github.com/prettier/prettier/pull/6467
[#6377]: https://github.com/prettier/prettier/pull/6377
[#6604]: https://github.com/prettier/prettier/pull/6604
[@brainkim]: https://github.com/brainkim
[@duailibe]: https://github.com/duailibe
[@gavinjoyce]: https://github.com/gavinjoyce

View File

@ -411,7 +411,11 @@ function needsParens(path, options) {
// Delegate to inner TSParenthesizedType
if (
node.typeAnnotation.type === "TSParenthesizedType" &&
parent.type !== "TSArrayType"
parent.type !== "TSArrayType" &&
parent.type !== "TSIndexedAccessType" &&
parent.type !== "TSConditionalType" &&
parent.type !== "TSIntersectionType" &&
parent.type !== "TSUnionType"
) {
return false;
}

View File

@ -24,6 +24,15 @@ type TypeName<T> =
T extends Function ? "function" :
"object";
type Type01 = 0 extends (1 extends 2 ? 3 : 4) ? 5 : 6;
type Type02 = 0 extends ((1 extends 2 ? 3 : 4)) ? 5 : 6;
type Type03 = 0 extends (((1 extends 2 ? 3 : 4))) ? 5 : 6;
type Type04 = 0 extends ((((1 extends 2 ? 3 : 4)))) ? 5 : 6;
type Type05 = (0 extends 1 ? 2 : 3) extends 4 ? 5 : 6;
type Type06 = ((0 extends 1 ? 2 : 3)) extends 4 ? 5 : 6;
type Type07 = (((0 extends 1 ? 2 : 3))) extends 4 ? 5 : 6;
type Type08 = ((((0 extends 1 ? 2 : 3)))) extends 4 ? 5 : 6;
=====================================output=====================================
export type DeepReadonly<T> = T extends any[]
? DeepReadonlyArray<T[number]>
@ -53,6 +62,15 @@ type TypeName<T> = T extends string
? "function"
: "object";
type Type01 = 0 extends (1 extends 2 ? 3 : 4) ? 5 : 6;
type Type02 = 0 extends (1 extends 2 ? 3 : 4) ? 5 : 6;
type Type03 = 0 extends (1 extends 2 ? 3 : 4) ? 5 : 6;
type Type04 = 0 extends (1 extends 2 ? 3 : 4) ? 5 : 6;
type Type05 = (0 extends 1 ? 2 : 3) extends 4 ? 5 : 6;
type Type06 = (0 extends 1 ? 2 : 3) extends 4 ? 5 : 6;
type Type07 = (0 extends 1 ? 2 : 3) extends 4 ? 5 : 6;
type Type08 = (0 extends 1 ? 2 : 3) extends 4 ? 5 : 6;
================================================================================
`;

View File

@ -15,3 +15,12 @@ type TypeName<T> =
T extends undefined ? "undefined" :
T extends Function ? "function" :
"object";
type Type01 = 0 extends (1 extends 2 ? 3 : 4) ? 5 : 6;
type Type02 = 0 extends ((1 extends 2 ? 3 : 4)) ? 5 : 6;
type Type03 = 0 extends (((1 extends 2 ? 3 : 4))) ? 5 : 6;
type Type04 = 0 extends ((((1 extends 2 ? 3 : 4)))) ? 5 : 6;
type Type05 = (0 extends 1 ? 2 : 3) extends 4 ? 5 : 6;
type Type06 = ((0 extends 1 ? 2 : 3)) extends 4 ? 5 : 6;
type Type07 = (((0 extends 1 ? 2 : 3))) extends 4 ? 5 : 6;
type Type08 = ((((0 extends 1 ? 2 : 3)))) extends 4 ? 5 : 6;

View File

@ -0,0 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`intersection-parens.ts 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
type A = (number | string) & boolean;
type B = ((number | string)) & boolean;
type C = (((number | string))) & boolean;
type D = ((((number | string)))) & boolean;
=====================================output=====================================
type A = (number | string) & boolean;
type B = (number | string) & boolean;
type C = (number | string) & boolean;
type D = (number | string) & boolean;
================================================================================
`;
exports[`intersection-parens.ts 2`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
semi: false
| printWidth
=====================================input======================================
type A = (number | string) & boolean;
type B = ((number | string)) & boolean;
type C = (((number | string))) & boolean;
type D = ((((number | string)))) & boolean;
=====================================output=====================================
type A = (number | string) & boolean
type B = (number | string) & boolean
type C = (number | string) & boolean
type D = (number | string) & boolean
================================================================================
`;

View File

@ -0,0 +1,4 @@
type A = (number | string) & boolean;
type B = ((number | string)) & boolean;
type C = (((number | string))) & boolean;
type D = ((((number | string)))) & boolean;

View File

@ -0,0 +1,2 @@
run_spec(__dirname, ["typescript"]);
run_spec(__dirname, ["typescript"], { semi: false });

View File

@ -12,7 +12,10 @@ type C = keyof T | U;
type D = keyof X & Y;
type E = (keyof T)[];
type F = ((keyof T))[];
type G = (keyof T1)["foo"];
type H = ((keyof T1))["foo"];
type I = (((keyof T1)))["foo"];
type J = ((((keyof T1))))["foo"];
=====================================output=====================================
type A = keyof (T | U);
@ -21,6 +24,10 @@ type C = keyof T | U;
type D = keyof X & Y;
type E = (keyof T)[];
type F = (keyof T)[];
type G = (keyof T1)["foo"];
type H = (keyof T1)["foo"];
type I = (keyof T1)["foo"];
type J = (keyof T1)["foo"];
================================================================================
`;

View File

@ -4,4 +4,7 @@ type C = keyof T | U;
type D = keyof X & Y;
type E = (keyof T)[];
type F = ((keyof T))[];
type G = (keyof T1)["foo"];
type H = ((keyof T1))["foo"];
type I = (((keyof T1)))["foo"];
type J = ((((keyof T1))))["foo"];

View File

@ -39,6 +39,15 @@ type window = Window & {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: Function;
};
type T1 = (number | string)["toString"];
type T2 = ((number | string))["toString"];
type T3 = (((number | string)))["toString"];
type T4 = ((((number | string))))["toString"];
type T5 = number | ((arg: any) => void);
type T6 = number | (((arg: any) => void));
type T7 = number | ((((arg: any) => void)));
type T8 = number | (((((arg: any) => void))));
=====================================output=====================================
interface RelayProps {
articles: a | null;
@ -73,6 +82,15 @@ type window = Window & {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: Function;
};
type T1 = (number | string)["toString"];
type T2 = (number | string)["toString"];
type T3 = (number | string)["toString"];
type T4 = (number | string)["toString"];
type T5 = number | ((arg: any) => void);
type T6 = number | ((arg: any) => void);
type T7 = number | ((arg: any) => void);
type T8 = number | ((arg: any) => void);
================================================================================
`;

View File

@ -30,3 +30,12 @@ type UploadState<E, EM, D>
type window = Window & {
__REDUX_DEVTOOLS_EXTENSION_COMPOSE__: Function;
};
type T1 = (number | string)["toString"];
type T2 = ((number | string))["toString"];
type T3 = (((number | string)))["toString"];
type T4 = ((((number | string))))["toString"];
type T5 = number | ((arg: any) => void);
type T6 = number | (((arg: any) => void));
type T7 = number | ((((arg: any) => void)));
type T8 = number | (((((arg: any) => void))));