diff --git a/changelog_unreleased/typescript/14654.md b/changelog_unreleased/typescript/14654.md new file mode 100644 index 000000000..e77ec4c3b --- /dev/null +++ b/changelog_unreleased/typescript/14654.md @@ -0,0 +1,34 @@ +#### Fix formatting of union type with single type (#14654 by @fisker and @auvred) + + +```ts +// Input +type T = + | ( + | { + value: number + } + | { + value: string + } + ) + +// Prettier stable +type T = + | + | { + value: number; + } + | { + value: string; + }; + +// Prettier main +type T = + | { + value: number; + } + | { + value: string; + }; +``` diff --git a/cspell.json b/cspell.json index 4a0d8afd1..f54d60e7f 100644 --- a/cspell.json +++ b/cspell.json @@ -26,6 +26,7 @@ "autoload", "autoloaded", "autoloading", + "auvred", "Azzola", "backticks", "behaviour", diff --git a/src/language-js/parse/postprocess/index.js b/src/language-js/parse/postprocess/index.js index 67efdc39a..cc57d2a2d 100644 --- a/src/language-js/parse/postprocess/index.js +++ b/src/language-js/parse/postprocess/index.js @@ -144,6 +144,13 @@ function postprocess(ast, options) { } break; } + // In Flow parser, it doesn't generate union/intersection types for single type + case "TSUnionType": + case "TSIntersectionType": + if (node.types.length === 1) { + return node.types[0]; + } + break; } }); diff --git a/tests/config/format-test.js b/tests/config/format-test.js index da27a1df1..d929cd05f 100644 --- a/tests/config/format-test.js +++ b/tests/config/format-test.js @@ -43,6 +43,7 @@ const unstableTests = new Map( ["js/identifier/parentheses/let.js", (options) => options.semi === false], "jsx/comments/in-attributes.js", ["js/ignore/semi/asi.js", (options) => options.semi === false], + "typescript/union/consistent-with-flow/single-type.ts", ].map((fixture) => { const [file, isUnstable = () => true] = Array.isArray(fixture) ? fixture diff --git a/tests/format/typescript/intersection/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/intersection/__snapshots__/jsfmt.spec.js.snap index 1a258900c..5aa71b049 100644 --- a/tests/format/typescript/intersection/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/intersection/__snapshots__/jsfmt.spec.js.snap @@ -1,202 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`intersection-parens.ts - {"semi":false} format 1`] = ` -====================================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; - -let b1 : C; -let b2 : & C; -let b3 : (& C); -let b4 : & (C); -let b5 : (& (C)); -let b6 : /*1*/ & C; -let b7 : /*1*/ & (C); -let b8 : /*1*/ (& C); -let b9 : (/*1*/ & C); -let b10: /*1*/ & /*2*/ C; -let b11: /*1*/ (& /*2*/ C); - -let bb1: /*1*/ & /*2*/ C & D; -let bb2: /*1*/ & /*2*/ C & /*3*/ D; -let bb3: /*1*/ & /*2*/ C & /*3*/ D /*5*/; - -type B2 = & C; -type B3 = (& C); -type B4 = & (C); -type B5 = (& (C)); -type B6 = /*1*/ & C; -type B7 = /*1*/ & (C); -type B8 = /*1*/ (& C); -type B9 = (/*1*/ & C); -type B10 = /*1*/ & /*2*/ C; -type B11 = /*1*/ (& /*2*/ C); -type B12 = /*1*/ & ( (C)); - -type Bb1 = /*1*/ & /*2*/ C & D; -type Bb2 = /*1*/ & /*2*/ C & /*3*/ D; -type Bb3 = /*1*/ & /*2*/ C & /*3*/ D /*4*/; - -type D1 = /*1*/ | a & b; -type D2 = /*1*/ | a & (b); -type D3 = /*1*/ | a & (| b); -type D4 = /*1*/ | (a & b); -type D5 = /*1*/ (| a & b); -type D6 /*0*/ = /*1*/ (| a & b); - -=====================================output===================================== -type A = (number | string) & boolean -type B = (number | string) & boolean -type C = (number | string) & boolean -type D = (number | string) & boolean - -let b1: C -let b2: C -let b3: C -let b4: C -let b5: C -let b6: /*1*/ C -let b7: /*1*/ C -let b8: /*1*/ C -let b9: /*1*/ C -let b10: /*1*/ /*2*/ C -let b11: /*1*/ /*2*/ C - -let bb1: /*1*/ /*2*/ C & D -let bb2: /*1*/ /*2*/ C & /*3*/ D -let bb3: /*1*/ /*2*/ C & /*3*/ D /*5*/ - -type B2 = C -type B3 = C -type B4 = C -type B5 = C -type B6 = /*1*/ C -type B7 = /*1*/ C -type B8 = /*1*/ C -type B9 = /*1*/ C -type B10 = /*1*/ /*2*/ C -type B11 = /*1*/ /*2*/ C -type B12 = /*1*/ C - -type Bb1 = /*1*/ /*2*/ C & D -type Bb2 = /*1*/ /*2*/ C & /*3*/ D -type Bb3 = /*1*/ /*2*/ C & /*3*/ D /*4*/ - -type D1 = /*1*/ a & b -type D2 = /*1*/ a & b -type D3 = /*1*/ a & b -type D4 = /*1*/ a & b -type D5 = /*1*/ a & b -type D6 /*0*/ = /*1*/ a & b - -================================================================================ -`; - -exports[`intersection-parens.ts format 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; - -let b1 : C; -let b2 : & C; -let b3 : (& C); -let b4 : & (C); -let b5 : (& (C)); -let b6 : /*1*/ & C; -let b7 : /*1*/ & (C); -let b8 : /*1*/ (& C); -let b9 : (/*1*/ & C); -let b10: /*1*/ & /*2*/ C; -let b11: /*1*/ (& /*2*/ C); - -let bb1: /*1*/ & /*2*/ C & D; -let bb2: /*1*/ & /*2*/ C & /*3*/ D; -let bb3: /*1*/ & /*2*/ C & /*3*/ D /*5*/; - -type B2 = & C; -type B3 = (& C); -type B4 = & (C); -type B5 = (& (C)); -type B6 = /*1*/ & C; -type B7 = /*1*/ & (C); -type B8 = /*1*/ (& C); -type B9 = (/*1*/ & C); -type B10 = /*1*/ & /*2*/ C; -type B11 = /*1*/ (& /*2*/ C); -type B12 = /*1*/ & ( (C)); - -type Bb1 = /*1*/ & /*2*/ C & D; -type Bb2 = /*1*/ & /*2*/ C & /*3*/ D; -type Bb3 = /*1*/ & /*2*/ C & /*3*/ D /*4*/; - -type D1 = /*1*/ | a & b; -type D2 = /*1*/ | a & (b); -type D3 = /*1*/ | a & (| b); -type D4 = /*1*/ | (a & b); -type D5 = /*1*/ (| a & b); -type D6 /*0*/ = /*1*/ (| a & b); - -=====================================output===================================== -type A = (number | string) & boolean; -type B = (number | string) & boolean; -type C = (number | string) & boolean; -type D = (number | string) & boolean; - -let b1: C; -let b2: C; -let b3: C; -let b4: C; -let b5: C; -let b6: /*1*/ C; -let b7: /*1*/ C; -let b8: /*1*/ C; -let b9: /*1*/ C; -let b10: /*1*/ /*2*/ C; -let b11: /*1*/ /*2*/ C; - -let bb1: /*1*/ /*2*/ C & D; -let bb2: /*1*/ /*2*/ C & /*3*/ D; -let bb3: /*1*/ /*2*/ C & /*3*/ D /*5*/; - -type B2 = C; -type B3 = C; -type B4 = C; -type B5 = C; -type B6 = /*1*/ C; -type B7 = /*1*/ C; -type B8 = /*1*/ C; -type B9 = /*1*/ C; -type B10 = /*1*/ /*2*/ C; -type B11 = /*1*/ /*2*/ C; -type B12 = /*1*/ C; - -type Bb1 = /*1*/ /*2*/ C & D; -type Bb2 = /*1*/ /*2*/ C & /*3*/ D; -type Bb3 = /*1*/ /*2*/ C & /*3*/ D /*4*/; - -type D1 = /*1*/ a & b; -type D2 = /*1*/ a & b; -type D3 = /*1*/ a & b; -type D4 = /*1*/ a & b; -type D5 = /*1*/ a & b; -type D6 /*0*/ = /*1*/ a & b; - -================================================================================ -`; - exports[`type-arguments.ts - {"semi":false} format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/intersection/consistent-with-flow/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/intersection/consistent-with-flow/__snapshots__/jsfmt.spec.js.snap index 41f0f7b80..6dd205cef 100644 --- a/tests/format/typescript/intersection/consistent-with-flow/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/intersection/consistent-with-flow/__snapshots__/jsfmt.spec.js.snap @@ -16,3 +16,101 @@ type A1 = /* 4 */ (A & B)[]; ================================================================================ `; + +exports[`intersection-parens.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +type A = (number | string) & boolean; +type B = ((number | string)) & boolean; +type C = (((number | string))) & boolean; +type D = ((((number | string)))) & boolean; + +let b1 : C; +let b2 : & C; +let b3 : (& C); +let b4 : & (C); +let b5 : (& (C)); +let b6 : /*1*/ & C; +let b7 : /*1*/ & (C); +let b8 : /*1*/ (& C); +let b9 : (/*1*/ & C); +let b10: /*1*/ & /*2*/ C; +let b11: /*1*/ (& /*2*/ C); + +let bb1: /*1*/ & /*2*/ C & D; +let bb2: /*1*/ & /*2*/ C & /*3*/ D; +let bb3: /*1*/ & /*2*/ C & /*3*/ D /*5*/; + +type B2 = & C; +type B3 = (& C); +type B4 = & (C); +type B5 = (& (C)); +type B6 = /*1*/ & C; +type B7 = /*1*/ & (C); +type B8 = /*1*/ (& C); +type B9 = (/*1*/ & C); +type B10 = /*1*/ & /*2*/ C; +type B11 = /*1*/ (& /*2*/ C); +type B12 = /*1*/ & ( (C)); + +type Bb1 = /*1*/ & /*2*/ C & D; +type Bb2 = /*1*/ & /*2*/ C & /*3*/ D; +type Bb3 = /*1*/ & /*2*/ C & /*3*/ D /*4*/; + +type D1 = /*1*/ | a & b; +type D2 = /*1*/ | a & (b); +type D3 = /*1*/ | a & (| b); +type D4 = /*1*/ | (a & b); +type D5 = /*1*/ (| a & b); +type D6 /*0*/ = /*1*/ (| a & b); + +=====================================output===================================== +type A = (number | string) & boolean; +type B = (number | string) & boolean; +type C = (number | string) & boolean; +type D = (number | string) & boolean; + +let b1: C; +let b2: C; +let b3: C; +let b4: C; +let b5: C; +let b6: /*1*/ C; +let b7: /*1*/ C; +let b8: /*1*/ C; +let b9: /*1*/ C; +let b10: /*1*/ /*2*/ C; +let b11: /*1*/ /*2*/ C; + +let bb1: /*1*/ /*2*/ C & D; +let bb2: /*1*/ /*2*/ C & /*3*/ D; +let bb3: /*1*/ /*2*/ C & /*3*/ D /*5*/; + +type B2 = C; +type B3 = C; +type B4 = C; +type B5 = C; +type B6 /*1*/ = C; +type B7 /*1*/ = C; +type B8 /*1*/ = C; +type B9 /*1*/ = C; +type B10 /*1*/ = /*2*/ C; +type B11 /*1*/ = /*2*/ C; +type B12 /*1*/ = C; + +type Bb1 = /*1*/ /*2*/ C & D; +type Bb2 = /*1*/ /*2*/ C & /*3*/ D; +type Bb3 = /*1*/ /*2*/ C & /*3*/ D /*4*/; + +type D1 /*1*/ = a & b; +type D2 /*1*/ = a & b; +type D3 /*1*/ = a & b; +type D4 /*1*/ = a & b; +type D5 /*1*/ = a & b; +type D6 /*0*/ /*1*/ = a & b; + +================================================================================ +`; diff --git a/tests/format/typescript/intersection/intersection-parens.ts b/tests/format/typescript/intersection/consistent-with-flow/intersection-parens.ts similarity index 100% rename from tests/format/typescript/intersection/intersection-parens.ts rename to tests/format/typescript/intersection/consistent-with-flow/intersection-parens.ts diff --git a/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap index c337bb606..9eb784026 100644 --- a/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/union/__snapshots__/jsfmt.spec.js.snap @@ -1,37 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`comments.ts format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -type A1 = a /* 1 */ | b; -type A2 = a | /* 1 */ b; -type A3 = (a /* 1 */) | b; -type A4 = a | (/* 1 */ b); -type A5 = (a) /* 1 */ | b; -type A6 = a | /* 1 */ (b); - -type B1 = a /* 1 */ /* 2 */ | b; -type B2 = a /* 1 */ | /* 2 */ b; -type B3 = a | /* 1 */ /* 2 */ b; - -=====================================output===================================== -type A1 = a /* 1 */ | b; -type A2 = a | /* 1 */ b; -type A3 = a /* 1 */ | b; -type A4 = a | /* 1 */ b; -type A5 = a /* 1 */ | b; -type A6 = a | /* 1 */ b; - -type B1 = a /* 1 */ /* 2 */ | b; -type B2 = a /* 1 */ | /* 2 */ b; -type B3 = a | /* 1 */ /* 2 */ b; - -================================================================================ -`; - exports[`inlining.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -181,68 +149,6 @@ type T8 = number | ((arg: any) => void); ================================================================================ `; -exports[`prettier-ignore.ts format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -export type a = - // foo - | foo1&foo2 - // bar - | bar1&bar2 - // prettier-ignore - | qux1&qux2; - -export type a = - // foo - | foo1&foo2 - // bar - | bar1&bar2 - // prettier-ignore - | qux1&qux2 - // baz - | baz1&baz2; - -export type a = - // prettier-ignore - | foo1&foo2 - // bar - | bar1&bar2 - // qux - | qux1&qux2; - -=====================================output===================================== -export type a = - // foo - | (foo1 & foo2) - // bar - | (bar1 & bar2) - // prettier-ignore - | (qux1&qux2); - -export type a = - // foo - | (foo1 & foo2) - // bar - | (bar1 & bar2) - // prettier-ignore - | (qux1&qux2) - // baz - | (baz1 & baz2); - -export type a = - // prettier-ignore - | (foo1&foo2) - // bar - | (bar1 & bar2) - // qux - | (qux1 & qux2); - -================================================================================ -`; - exports[`union-parens.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -449,13 +355,13 @@ type A2 = C; type A3 = C; type A4 = C; type A5 = C; -type A6 = /*1*/ C; -type A7 = /*1*/ C; -type A8 = /*1*/ C; -type A9 = /*1*/ C; -type A10 = /*1*/ /*2*/ C; -type A11 = /*1*/ /*2*/ C; -type A12 = /*1*/ C; +type A6 /*1*/ = C; +type A7 /*1*/ = C; +type A8 /*1*/ = C; +type A9 /*1*/ = C; +type A10 /*1*/ = /*2*/ C; +type A11 /*1*/ = /*2*/ C; +type A12 /*1*/ = C; type A13 = /*1*/ C; type Aa1 = /*1*/ /*2*/ C | D; @@ -465,7 +371,7 @@ type Aa3 = /*1*/ /*2*/ C | /*3*/ D /*4*/; type C1 = /*1*/ a | b; type C2 = /*1*/ a | b; type C3 = /*1*/ a | b; -type C4 = /*1*/ a | b; +type C4 /*1*/ = a | b; type C5 = /*1*/ a | b; type C6 /*0*/ = /*1*/ a | b; @@ -503,171 +409,3 @@ type GetChatsSagaEffects = ================================================================================ `; - -exports[`within-tuple.ts format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 80 - | printWidth -=====================================input====================================== -type A = [AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD] - -type B = [ - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD -] - -type B1 = [ - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ) -] - -type C = [ - | [AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD] - | [AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD] -] - -type D = [ - (AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD), - (AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD) -] - -type D1 = [ - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ), - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ) -] - -type D2 = [ - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD, - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD -] - -type E = [ AA | BB, AA | BB ] - -type F = [ - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD, - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB -] - -=====================================output===================================== -type A = [ - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD, -]; - -type B = [ - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD, -]; - -type B1 = [ - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD, -]; - -type C = [ - | [ - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD, - ] - | [ - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD, - ], -]; - -type D = [ - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ), - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ), -]; - -type D1 = [ - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ), - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ), -]; - -type D2 = [ - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ), - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ), -]; - -type E = [AA | BB, AA | BB]; - -type F = [ - ( - | AAAAAAAAAAAAAAAAAAAAAA - | BBBBBBBBBBBBBBBBBBBBBB - | CCCCCCCCCCCCCCCCCCCCCC - | DDDDDDDDDDDDDDDDDDDDDD - ), - AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB, -]; - -================================================================================ -`; diff --git a/tests/format/typescript/union/consistent-with-flow/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/union/consistent-with-flow/__snapshots__/jsfmt.spec.js.snap index e9688c8fc..5e7e3dec0 100644 --- a/tests/format/typescript/union/consistent-with-flow/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/union/consistent-with-flow/__snapshots__/jsfmt.spec.js.snap @@ -16,3 +16,397 @@ type A1 = /* 4 */ (A | B)[]; ================================================================================ `; + +exports[`comments.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +type A1 = a /* 1 */ | b; +type A2 = a | /* 1 */ b; +type A3 = (a /* 1 */) | b; +type A4 = a | (/* 1 */ b); +type A5 = (a) /* 1 */ | b; +type A6 = a | /* 1 */ (b); + +type B1 = a /* 1 */ /* 2 */ | b; +type B2 = a /* 1 */ | /* 2 */ b; +type B3 = a | /* 1 */ /* 2 */ b; + +=====================================output===================================== +type A1 = a /* 1 */ | b; +type A2 = a | /* 1 */ b; +type A3 = a /* 1 */ | b; +type A4 = a | /* 1 */ b; +type A5 = a /* 1 */ | b; +type A6 = a | /* 1 */ b; + +type B1 = a /* 1 */ /* 2 */ | b; +type B2 = a /* 1 */ | /* 2 */ b; +type B3 = a | /* 1 */ /* 2 */ b; + +================================================================================ +`; + +exports[`prettier-ignore.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +export type a = + // foo + | foo1&foo2 + // bar + | bar1&bar2 + // prettier-ignore + | qux1&qux2; + +export type b = + // foo + | foo1&foo2 + // bar + | bar1&bar2 + // prettier-ignore + | qux1&qux2 + // baz + | baz1&baz2; + +export type c = + // prettier-ignore + | foo1&foo2 + // bar + | bar1&bar2 + // qux + | qux1&qux2; + +=====================================output===================================== +export type a = + // foo + | (foo1 & foo2) + // bar + | (bar1 & bar2) + // prettier-ignore + | (qux1&qux2); + +export type b = + // foo + | (foo1 & foo2) + // bar + | (bar1 & bar2) + // prettier-ignore + | (qux1&qux2) + // baz + | (baz1 & baz2); + +export type c = + // prettier-ignore + | (foo1&foo2) + // bar + | (bar1 & bar2) + // qux + | (qux1 & qux2); + +================================================================================ +`; + +exports[`single-type.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +type A1 = + | ( + | ( + | ( + | A + // A comment to force break + | B + ) + ) + ); +type A2 = + | ( + | ( + | A + // A comment to force break + | B + ) + | ( + | A + // A comment to force break + | B + ) + ); +type A3 = + | ( | ( + | A + // A comment to force break + | B + ) ); +type A4 = + | ( | ( | ( + | A + // A comment to force break + | B + ) ) ); +type A5 = + | ( + | ( + | { key: string } + | { key: string } + | { key: string } + | { key: string } + ) + | { key: string } + | { key: string } + ); +type A6 = | ( + /*1*/ | ( + | ( + | A + // A comment to force break + | B + ) + ) + ); + +type B1 = + | ( + & ( + ( + | A + // A comment to force break + | B + ) + ) + ); +type B2 = + | ( + & ( + | ( + & ( + ( + | A + // A comment to force break + | B + ) + ) + ) + ) + ); + +=====================================output===================================== +type A1 = + | A + // A comment to force break + | B; +type A2 = + | ( + | A + // A comment to force break + | B + ) + | ( + | A + // A comment to force break + | B + ); +type A3 = + | A + // A comment to force break + | B; +type A4 = + | A + // A comment to force break + | B; +type A5 = + | ({ key: string } | { key: string } | { key: string } | { key: string }) + | { key: string } + | { key: string }; +type A6 = /*1*/ + | A + // A comment to force break + | B; + +type B1 = + | A + // A comment to force break + | B; +type B2 = + | A + // A comment to force break + | B; + +================================================================================ +`; + +exports[`within-tuple.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +type A = [AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD] + +type B = [ + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD +] + +type B1 = [ + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ) +] + +type C = [ + | [AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD] + | [AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD] +] + +type D = [ + (AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD), + (AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB | CCCCCCCCCCCCCCCCCCCCCC | DDDDDDDDDDDDDDDDDDDDDD) +] + +type D1 = [ + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ), + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ) +] + +type D2 = [ + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD, + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD +] + +type E = [ AA | BB, AA | BB ] + +type F = [ + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD, + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB +] + +=====================================output===================================== +type A = [ + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD, +]; + +type B = [ + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD, +]; + +type B1 = [ + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD, +]; + +type C = [ + | [ + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD, + ] + | [ + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD, + ], +]; + +type D = [ + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ), + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ), +]; + +type D1 = [ + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ), + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ), +]; + +type D2 = [ + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ), + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ), +]; + +type E = [AA | BB, AA | BB]; + +type F = [ + ( + | AAAAAAAAAAAAAAAAAAAAAA + | BBBBBBBBBBBBBBBBBBBBBB + | CCCCCCCCCCCCCCCCCCCCCC + | DDDDDDDDDDDDDDDDDDDDDD + ), + AAAAAAAAAAAAAAAAAAAAAA | BBBBBBBBBBBBBBBBBBBBBB, +]; + +================================================================================ +`; diff --git a/tests/format/typescript/union/comments.ts b/tests/format/typescript/union/consistent-with-flow/comments.ts similarity index 100% rename from tests/format/typescript/union/comments.ts rename to tests/format/typescript/union/consistent-with-flow/comments.ts diff --git a/tests/format/typescript/union/prettier-ignore.ts b/tests/format/typescript/union/consistent-with-flow/prettier-ignore.ts similarity index 89% rename from tests/format/typescript/union/prettier-ignore.ts rename to tests/format/typescript/union/consistent-with-flow/prettier-ignore.ts index 786b59686..3927f292e 100644 --- a/tests/format/typescript/union/prettier-ignore.ts +++ b/tests/format/typescript/union/consistent-with-flow/prettier-ignore.ts @@ -6,7 +6,7 @@ export type a = // prettier-ignore | qux1&qux2; -export type a = +export type b = // foo | foo1&foo2 // bar @@ -16,7 +16,7 @@ export type a = // baz | baz1&baz2; -export type a = +export type c = // prettier-ignore | foo1&foo2 // bar diff --git a/tests/format/typescript/union/consistent-with-flow/single-type.ts b/tests/format/typescript/union/consistent-with-flow/single-type.ts new file mode 100644 index 000000000..dcf1b7132 --- /dev/null +++ b/tests/format/typescript/union/consistent-with-flow/single-type.ts @@ -0,0 +1,80 @@ +type A1 = + | ( + | ( + | ( + | A + // A comment to force break + | B + ) + ) + ); +type A2 = + | ( + | ( + | A + // A comment to force break + | B + ) + | ( + | A + // A comment to force break + | B + ) + ); +type A3 = + | ( | ( + | A + // A comment to force break + | B + ) ); +type A4 = + | ( | ( | ( + | A + // A comment to force break + | B + ) ) ); +type A5 = + | ( + | ( + | { key: string } + | { key: string } + | { key: string } + | { key: string } + ) + | { key: string } + | { key: string } + ); +type A6 = | ( + /*1*/ | ( + | ( + | A + // A comment to force break + | B + ) + ) + ); + +type B1 = + | ( + & ( + ( + | A + // A comment to force break + | B + ) + ) + ); +type B2 = + | ( + & ( + | ( + & ( + ( + | A + // A comment to force break + | B + ) + ) + ) + ) + ); diff --git a/tests/format/typescript/union/within-tuple.ts b/tests/format/typescript/union/consistent-with-flow/within-tuple.ts similarity index 100% rename from tests/format/typescript/union/within-tuple.ts rename to tests/format/typescript/union/consistent-with-flow/within-tuple.ts diff --git a/tests/format/typescript/union/single-type/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/union/single-type/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..e0479624b --- /dev/null +++ b/tests/format/typescript/union/single-type/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,22 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`single-type.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type A1 = + /* 1 */ | /* 2 */ ( + /* 3 */ | /* 4 */ { + key: string; + } + ); + +=====================================output===================================== +type A1 /* 2 */ = /* 1 */ /* 3 */ /* 4 */ { + key: string; +}; + +================================================================================ +`; diff --git a/tests/format/typescript/union/single-type/jsfmt.spec.js b/tests/format/typescript/union/single-type/jsfmt.spec.js new file mode 100644 index 000000000..29084a9c8 --- /dev/null +++ b/tests/format/typescript/union/single-type/jsfmt.spec.js @@ -0,0 +1 @@ +run_spec(import.meta, ["typescript"]); diff --git a/tests/format/typescript/union/single-type/single-type.ts b/tests/format/typescript/union/single-type/single-type.ts new file mode 100644 index 000000000..a8ed5cc4f --- /dev/null +++ b/tests/format/typescript/union/single-type/single-type.ts @@ -0,0 +1,6 @@ +type A1 = + /* 1 */ | /* 2 */ ( + /* 3 */ | /* 4 */ { + key: string; + } + );