From 4779c5db11d4da3faae9e3ece90f28d77a29fd3d Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 30 Nov 2021 21:43:28 +0900 Subject: [PATCH 1/8] Don't print trailing comma when tuple is empty --- src/language-js/print/type-annotation.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/language-js/print/type-annotation.js b/src/language-js/print/type-annotation.js index 7772e909925d..f90ed5c9cf02 100644 --- a/src/language-js/print/type-annotation.js +++ b/src/language-js/print/type-annotation.js @@ -4,7 +4,7 @@ const { printComments, printDanglingComments, } = require("../../main/comments.js"); -const { getLast } = require("../../common/util.js"); +const { getLast, isNonEmptyArray } = require("../../common/util.js"); const { builders: { group, join, line, softline, indent, align, ifBreak }, } = require("../../document/index.js"); @@ -287,13 +287,15 @@ function printFunctionType(path, options, print) { function printTupleType(path, options, print) { const node = path.getValue(); const typesField = node.type === "TSTupleType" ? "elementTypes" : "types"; - const hasRest = - node[typesField].length > 0 && - getLast(node[typesField]).type === "TSRestType"; + const types = node[typesField]; + const isNotEmptyTuple = isNonEmptyArray(types); + const hasRest = isNotEmptyTuple && getLast(types).type === "TSRestType"; return group([ "[", indent([softline, printArrayItems(path, options, typesField, print)]), - ifBreak(shouldPrintComma(options, "all") && !hasRest ? "," : ""), + ifBreak( + isNotEmptyTuple && shouldPrintComma(options, "all") && !hasRest ? "," : "" + ), printDanglingComments(path, options, /* sameIndent */ true), softline, "]", From 4c9b455305eeda3f8d56eeb407767b86eba3a8a6 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 30 Nov 2021 21:45:28 +0900 Subject: [PATCH 2/8] Add tests --- .../tuple/__snapshots__/jsfmt.spec.js.snap | 50 +++++++++++++++++++ .../tuple/trailing-comma-for-empty-tuples.ts | 1 + 2 files changed, 51 insertions(+) create mode 100644 tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts diff --git a/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap index 9681ac3ce717..63b508577c55 100644 --- a/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap @@ -208,6 +208,56 @@ export interface ShopQueryResult { ================================================================================ `; +exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"all"} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +trailingComma: "all" + | printWidth +=====================================input====================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] + +=====================================output===================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [ + +]; + +================================================================================ +`; + +exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"none"} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +trailingComma: "none" + | printWidth +=====================================input====================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] + +=====================================output===================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [ + +]; + +================================================================================ +`; + +exports[`trailing-comma-for-empty-tuples.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] + +=====================================output===================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [ + +]; + +================================================================================ +`; + exports[`tuple.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts b/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts new file mode 100644 index 000000000000..86552a51fcf5 --- /dev/null +++ b/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts @@ -0,0 +1 @@ +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] From d6d3face03badd3abafba86146331435d77610b0 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 30 Nov 2021 21:54:29 +0900 Subject: [PATCH 3/8] Don't print softline --- src/language-js/print/type-annotation.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/language-js/print/type-annotation.js b/src/language-js/print/type-annotation.js index f90ed5c9cf02..58805de142ad 100644 --- a/src/language-js/print/type-annotation.js +++ b/src/language-js/print/type-annotation.js @@ -290,14 +290,18 @@ function printTupleType(path, options, print) { const types = node[typesField]; const isNotEmptyTuple = isNonEmptyArray(types); const hasRest = isNotEmptyTuple && getLast(types).type === "TSRestType"; + const bracketsDelimiterLine = isNotEmptyTuple ? softline : ""; return group([ "[", - indent([softline, printArrayItems(path, options, typesField, print)]), + indent([ + bracketsDelimiterLine, + printArrayItems(path, options, typesField, print), + ]), ifBreak( isNotEmptyTuple && shouldPrintComma(options, "all") && !hasRest ? "," : "" ), printDanglingComments(path, options, /* sameIndent */ true), - softline, + bracketsDelimiterLine, "]", ]); } From 508e8daeb89800871a3dafa54308fee740b6d5d6 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 30 Nov 2021 21:57:11 +0900 Subject: [PATCH 4/8] Add tests --- .../tuple/__snapshots__/jsfmt.spec.js.snap | 574 +++++++++++++++++- tests/format/typescript/tuple/jsfmt.spec.js | 3 + .../tuple/trailing-comma-for-empty-tuples.ts | 2 + 3 files changed, 574 insertions(+), 5 deletions(-) diff --git a/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap index 63b508577c55..67d1e9ede7f8 100644 --- a/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap @@ -1,5 +1,62 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`no-trailing-comma-after-rest.ts - {"printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 + | printWidth +=====================================input====================================== +type ValidateArgs = [ + { + [key: string]: any; + }, + string, + string, + ...string[], +]; + +=====================================output===================================== +type ValidateArgs = [ + { + [key: string]: any; + }, + string, + string, + ...string[] +]; + +================================================================================ +`; + +exports[`no-trailing-comma-after-rest.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "all" + | printWidth +=====================================input====================================== +type ValidateArgs = [ + { + [key: string]: any; + }, + string, + string, + ...string[], +]; + +=====================================output===================================== +type ValidateArgs = [ + { + [key: string]: any; + }, + string, + string, + ...string[] +]; + +================================================================================ +`; + exports[`no-trailing-comma-after-rest.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -29,6 +86,35 @@ type ValidateArgs = [ ================================================================================ `; +exports[`no-trailing-comma-after-rest.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "none" + | printWidth +=====================================input====================================== +type ValidateArgs = [ + { + [key: string]: any; + }, + string, + string, + ...string[], +]; + +=====================================output===================================== +type ValidateArgs = [ + { + [key: string]: any; + }, + string, + string, + ...string[] +]; + +================================================================================ +`; + exports[`no-trailing-comma-after-rest.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -86,6 +172,87 @@ type ValidateArgs = [ ================================================================================ `; +exports[`trailing-comma.ts - {"printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 + | printWidth +=====================================input====================================== +export interface ShopQueryResult { + chic: boolean; + location: number[]; + menus: Menu[]; + openingDays: number[]; + closingDays: [ + { + from: string, + to: string, + }, // <== this one + ]; + shop: string; + distance: number; +} + +=====================================output===================================== +export interface ShopQueryResult { + chic: boolean; + location: number[]; + menus: Menu[]; + openingDays: number[]; + closingDays: [ + { + from: string; + to: string; + } // <== this one + ]; + shop: string; + distance: number; +} + +================================================================================ +`; + +exports[`trailing-comma.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "all" + | printWidth +=====================================input====================================== +export interface ShopQueryResult { + chic: boolean; + location: number[]; + menus: Menu[]; + openingDays: number[]; + closingDays: [ + { + from: string, + to: string, + }, // <== this one + ]; + shop: string; + distance: number; +} + +=====================================output===================================== +export interface ShopQueryResult { + chic: boolean; + location: number[]; + menus: Menu[]; + openingDays: number[]; + closingDays: [ + { + from: string; + to: string; + }, // <== this one + ]; + shop: string; + distance: number; +} + +================================================================================ +`; + exports[`trailing-comma.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -127,6 +294,47 @@ export interface ShopQueryResult { ================================================================================ `; +exports[`trailing-comma.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "none" + | printWidth +=====================================input====================================== +export interface ShopQueryResult { + chic: boolean; + location: number[]; + menus: Menu[]; + openingDays: number[]; + closingDays: [ + { + from: string, + to: string, + }, // <== this one + ]; + shop: string; + distance: number; +} + +=====================================output===================================== +export interface ShopQueryResult { + chic: boolean; + location: number[]; + menus: Menu[]; + openingDays: number[]; + closingDays: [ + { + from: string; + to: string; + } // <== this one + ]; + shop: string; + distance: number; +} + +================================================================================ +`; + exports[`trailing-comma.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -208,6 +416,49 @@ export interface ShopQueryResult { ================================================================================ `; +exports[`trailing-comma-for-empty-tuples.ts - {"printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 + | printWidth +=====================================input====================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] + +type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never + +=====================================output===================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; + +type kjhudifkalmcnf< + obj extends Record, + aaddffgg extends string[] = aabbccdd +> = aaddffgg extends [] ? never : never; + +================================================================================ +`; + +exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "all" + | printWidth +=====================================input====================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] + +type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never + +=====================================output===================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; + +type kjhudifkalmcnf< + obj extends Record, + aaddffgg extends string[] = aabbccdd, +> = aaddffgg extends [] ? never : never; + +================================================================================ +`; + exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -217,10 +468,38 @@ trailingComma: "all" =====================================input====================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] +type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never + =====================================output===================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [ +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = + []; -]; +type kjhudifkalmcnf< + obj extends Record, + aaddffgg extends string[] = aabbccdd, +> = aaddffgg extends [] ? never : never; + +================================================================================ +`; + +exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "none" + | printWidth +=====================================input====================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] + +type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never + +=====================================output===================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; + +type kjhudifkalmcnf< + obj extends Record, + aaddffgg extends string[] = aabbccdd +> = aaddffgg extends [] ? never : never; ================================================================================ `; @@ -234,10 +513,16 @@ trailingComma: "none" =====================================input====================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] +type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never + =====================================output===================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [ +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = + []; -]; +type kjhudifkalmcnf< + obj extends Record, + aaddffgg extends string[] = aabbccdd +> = aaddffgg extends [] ? never : never; ================================================================================ `; @@ -250,9 +535,74 @@ printWidth: 80 =====================================input====================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] +type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never + +=====================================output===================================== +type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = + []; + +type kjhudifkalmcnf< + obj extends Record, + aaddffgg extends string[] = aabbccdd +> = aaddffgg extends [] ? never : never; + +================================================================================ +`; + +exports[`tuple.ts - {"printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 + | printWidth +=====================================input====================================== + +export type SCMRawResource = [ + number /*handle*/, + string /*resourceUri*/, + modes.Command /*command*/, + string[] /*icons: light, dark*/, + boolean /*strike through*/, + boolean /*faded*/ +]; + =====================================output===================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [ +export type SCMRawResource = [ + number /*handle*/, + string /*resourceUri*/, + modes.Command /*command*/, + string[] /*icons: light, dark*/, + boolean /*strike through*/, + boolean /*faded*/ +]; + +================================================================================ +`; + +exports[`tuple.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "all" + | printWidth +=====================================input====================================== + +export type SCMRawResource = [ + number /*handle*/, + string /*resourceUri*/, + modes.Command /*command*/, + string[] /*icons: light, dark*/, + boolean /*strike through*/, + boolean /*faded*/ +]; +=====================================output===================================== +export type SCMRawResource = [ + number /*handle*/, + string /*resourceUri*/, + modes.Command /*command*/, + string[] /*icons: light, dark*/, + boolean /*strike through*/, + boolean /*faded*/, ]; ================================================================================ @@ -288,6 +638,36 @@ export type SCMRawResource = [ ================================================================================ `; +exports[`tuple.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "none" + | printWidth +=====================================input====================================== + +export type SCMRawResource = [ + number /*handle*/, + string /*resourceUri*/, + modes.Command /*command*/, + string[] /*icons: light, dark*/, + boolean /*strike through*/, + boolean /*faded*/ +]; + +=====================================output===================================== +export type SCMRawResource = [ + number /*handle*/, + string /*resourceUri*/, + modes.Command /*command*/, + string[] /*icons: light, dark*/, + boolean /*strike through*/, + boolean /*faded*/ +]; + +================================================================================ +`; + exports[`tuple.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -347,6 +727,91 @@ export type SCMRawResource = [ ================================================================================ `; +exports[`tuple-labeled.ts - {"printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 + | printWidth +=====================================input====================================== +// https://github.com/babel/babel/pull/11754 + +type T = [x: A, y?: B, ...z: C]; + +type T = [A, y: B]; + +let x: [A: string, ...B: number[]] + +type T = [foo: string, bar?: number]; + +type T = [x?: A, y: B]; + +type T = [x: A, ...B]; + +type T = [...B, x: A]; + +=====================================output===================================== +// https://github.com/babel/babel/pull/11754 + +type T = [x: A, y?: B, ...z: C]; + +type T = [A, y: B]; + +let x: [A: string, ...B: number[]]; + +type T = [foo: string, bar?: number]; + +type T = [x?: A, y: B]; + +type T = [x: A, ...B]; + +type T = [...B, x: A]; + +================================================================================ +`; + +exports[`tuple-labeled.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "all" + | printWidth +=====================================input====================================== +// https://github.com/babel/babel/pull/11754 + +type T = [x: A, y?: B, ...z: C]; + +type T = [A, y: B]; + +let x: [A: string, ...B: number[]] + +type T = [foo: string, bar?: number]; + +type T = [x?: A, y: B]; + +type T = [x: A, ...B]; + +type T = [...B, x: A]; + +=====================================output===================================== +// https://github.com/babel/babel/pull/11754 + +type T = [x: A, y?: B, ...z: C]; + +type T = [A, y: B]; + +let x: [A: string, ...B: number[]]; + +type T = [foo: string, bar?: number]; + +type T = [x?: A, y: B]; + +type T = [x: A, ...B]; + +type T = [...B, x: A]; + +================================================================================ +`; + exports[`tuple-labeled.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -390,6 +855,49 @@ type T = [...B, x: A]; ================================================================================ `; +exports[`tuple-labeled.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "none" + | printWidth +=====================================input====================================== +// https://github.com/babel/babel/pull/11754 + +type T = [x: A, y?: B, ...z: C]; + +type T = [A, y: B]; + +let x: [A: string, ...B: number[]] + +type T = [foo: string, bar?: number]; + +type T = [x?: A, y: B]; + +type T = [x: A, ...B]; + +type T = [...B, x: A]; + +=====================================output===================================== +// https://github.com/babel/babel/pull/11754 + +type T = [x: A, y?: B, ...z: C]; + +type T = [A, y: B]; + +let x: [A: string, ...B: number[]]; + +type T = [foo: string, bar?: number]; + +type T = [x?: A, y: B]; + +type T = [x: A, ...B]; + +type T = [...B, x: A]; + +================================================================================ +`; + exports[`tuple-labeled.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -475,6 +983,43 @@ type T = [...B, x: A]; ================================================================================ `; +exports[`tuple-rest-not-last.ts - {"printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 + | printWidth +=====================================input====================================== +// https://github.com/babel/babel/pull/11753 + +let x: [...[number, string], string] + +=====================================output===================================== +// https://github.com/babel/babel/pull/11753 + +let x: [...[number, string], string]; + +================================================================================ +`; + +exports[`tuple-rest-not-last.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "all" + | printWidth +=====================================input====================================== +// https://github.com/babel/babel/pull/11753 + +let x: [...[number, string], string] + +=====================================output===================================== +// https://github.com/babel/babel/pull/11753 + +let x: [...[number, string], string]; + +================================================================================ +`; + exports[`tuple-rest-not-last.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -494,6 +1039,25 @@ let x: [...[number, string], string]; ================================================================================ `; +exports[`tuple-rest-not-last.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 120 +trailingComma: "none" + | printWidth +=====================================input====================================== +// https://github.com/babel/babel/pull/11753 + +let x: [...[number, string], string] + +=====================================output===================================== +// https://github.com/babel/babel/pull/11753 + +let x: [...[number, string], string]; + +================================================================================ +`; + exports[`tuple-rest-not-last.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/tuple/jsfmt.spec.js b/tests/format/typescript/tuple/jsfmt.spec.js index 31c3b0a4ff6b..6574b4b1f786 100644 --- a/tests/format/typescript/tuple/jsfmt.spec.js +++ b/tests/format/typescript/tuple/jsfmt.spec.js @@ -1,3 +1,6 @@ run_spec(__dirname, ["typescript"], { trailingComma: "none" }); +run_spec(__dirname, ["typescript"], { trailingComma: "none", printWidth: 120 }); run_spec(__dirname, ["typescript"]); +run_spec(__dirname, ["typescript"], { printWidth: 120 }); run_spec(__dirname, ["typescript"], { trailingComma: "all" }); +run_spec(__dirname, ["typescript"], { trailingComma: "all", printWidth: 120 }); diff --git a/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts b/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts index 86552a51fcf5..69b83bb6c6f4 100644 --- a/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts +++ b/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts @@ -1 +1,3 @@ type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] + +type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never From 96996002f4dd8fc16ef9cbc255099dad1bd377eb Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 30 Nov 2021 22:03:16 +0900 Subject: [PATCH 5/8] Update tests --- .../tuple/__snapshots__/jsfmt.spec.js.snap | 579 +----------------- tests/format/typescript/tuple/jsfmt.spec.js | 3 - .../tuple/trailing-comma-for-empty-tuples.ts | 2 +- 3 files changed, 16 insertions(+), 568 deletions(-) diff --git a/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap index 67d1e9ede7f8..e2c610475a59 100644 --- a/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap @@ -1,62 +1,5 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[`no-trailing-comma-after-rest.ts - {"printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 - | printWidth -=====================================input====================================== -type ValidateArgs = [ - { - [key: string]: any; - }, - string, - string, - ...string[], -]; - -=====================================output===================================== -type ValidateArgs = [ - { - [key: string]: any; - }, - string, - string, - ...string[] -]; - -================================================================================ -`; - -exports[`no-trailing-comma-after-rest.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "all" - | printWidth -=====================================input====================================== -type ValidateArgs = [ - { - [key: string]: any; - }, - string, - string, - ...string[], -]; - -=====================================output===================================== -type ValidateArgs = [ - { - [key: string]: any; - }, - string, - string, - ...string[] -]; - -================================================================================ -`; - exports[`no-trailing-comma-after-rest.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -86,35 +29,6 @@ type ValidateArgs = [ ================================================================================ `; -exports[`no-trailing-comma-after-rest.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "none" - | printWidth -=====================================input====================================== -type ValidateArgs = [ - { - [key: string]: any; - }, - string, - string, - ...string[], -]; - -=====================================output===================================== -type ValidateArgs = [ - { - [key: string]: any; - }, - string, - string, - ...string[] -]; - -================================================================================ -`; - exports[`no-trailing-comma-after-rest.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -172,87 +86,6 @@ type ValidateArgs = [ ================================================================================ `; -exports[`trailing-comma.ts - {"printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 - | printWidth -=====================================input====================================== -export interface ShopQueryResult { - chic: boolean; - location: number[]; - menus: Menu[]; - openingDays: number[]; - closingDays: [ - { - from: string, - to: string, - }, // <== this one - ]; - shop: string; - distance: number; -} - -=====================================output===================================== -export interface ShopQueryResult { - chic: boolean; - location: number[]; - menus: Menu[]; - openingDays: number[]; - closingDays: [ - { - from: string; - to: string; - } // <== this one - ]; - shop: string; - distance: number; -} - -================================================================================ -`; - -exports[`trailing-comma.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "all" - | printWidth -=====================================input====================================== -export interface ShopQueryResult { - chic: boolean; - location: number[]; - menus: Menu[]; - openingDays: number[]; - closingDays: [ - { - from: string, - to: string, - }, // <== this one - ]; - shop: string; - distance: number; -} - -=====================================output===================================== -export interface ShopQueryResult { - chic: boolean; - location: number[]; - menus: Menu[]; - openingDays: number[]; - closingDays: [ - { - from: string; - to: string; - }, // <== this one - ]; - shop: string; - distance: number; -} - -================================================================================ -`; - exports[`trailing-comma.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -294,47 +127,6 @@ export interface ShopQueryResult { ================================================================================ `; -exports[`trailing-comma.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "none" - | printWidth -=====================================input====================================== -export interface ShopQueryResult { - chic: boolean; - location: number[]; - menus: Menu[]; - openingDays: number[]; - closingDays: [ - { - from: string, - to: string, - }, // <== this one - ]; - shop: string; - distance: number; -} - -=====================================output===================================== -export interface ShopQueryResult { - chic: boolean; - location: number[]; - menus: Menu[]; - openingDays: number[]; - closingDays: [ - { - from: string; - to: string; - } // <== this one - ]; - shop: string; - distance: number; -} - -================================================================================ -`; - exports[`trailing-comma.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -416,49 +208,6 @@ export interface ShopQueryResult { ================================================================================ `; -exports[`trailing-comma-for-empty-tuples.ts - {"printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 - | printWidth -=====================================input====================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] - -type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never - -=====================================output===================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; - -type kjhudifkalmcnf< - obj extends Record, - aaddffgg extends string[] = aabbccdd -> = aaddffgg extends [] ? never : never; - -================================================================================ -`; - -exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "all" - | printWidth -=====================================input====================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] - -type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never - -=====================================output===================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; - -type kjhudifkalmcnf< - obj extends Record, - aaddffgg extends string[] = aabbccdd, -> = aaddffgg extends [] ? never : never; - -================================================================================ -`; - exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -468,38 +217,15 @@ trailingComma: "all" =====================================input====================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] -type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never - +type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4; =====================================output===================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; -type kjhudifkalmcnf< - obj extends Record, - aaddffgg extends string[] = aabbccdd, -> = aaddffgg extends [] ? never : never; - -================================================================================ -`; - -exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "none" - | printWidth -=====================================input====================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] - -type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never - -=====================================output===================================== -type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; - -type kjhudifkalmcnf< - obj extends Record, - aaddffgg extends string[] = aabbccdd -> = aaddffgg extends [] ? never : never; +type Foo = + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] + ? Foo3 + : Foo4; ================================================================================ `; @@ -513,16 +239,15 @@ trailingComma: "none" =====================================input====================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] -type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never - +type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4; =====================================output===================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; -type kjhudifkalmcnf< - obj extends Record, - aaddffgg extends string[] = aabbccdd -> = aaddffgg extends [] ? never : never; +type Foo = + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] + ? Foo3 + : Foo4; ================================================================================ `; @@ -535,75 +260,15 @@ printWidth: 80 =====================================input====================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] -type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never - +type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4; =====================================output===================================== type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []; -type kjhudifkalmcnf< - obj extends Record, - aaddffgg extends string[] = aabbccdd -> = aaddffgg extends [] ? never : never; - -================================================================================ -`; - -exports[`tuple.ts - {"printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 - | printWidth -=====================================input====================================== - -export type SCMRawResource = [ - number /*handle*/, - string /*resourceUri*/, - modes.Command /*command*/, - string[] /*icons: light, dark*/, - boolean /*strike through*/, - boolean /*faded*/ -]; - -=====================================output===================================== -export type SCMRawResource = [ - number /*handle*/, - string /*resourceUri*/, - modes.Command /*command*/, - string[] /*icons: light, dark*/, - boolean /*strike through*/, - boolean /*faded*/ -]; - -================================================================================ -`; - -exports[`tuple.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "all" - | printWidth -=====================================input====================================== - -export type SCMRawResource = [ - number /*handle*/, - string /*resourceUri*/, - modes.Command /*command*/, - string[] /*icons: light, dark*/, - boolean /*strike through*/, - boolean /*faded*/ -]; - -=====================================output===================================== -export type SCMRawResource = [ - number /*handle*/, - string /*resourceUri*/, - modes.Command /*command*/, - string[] /*icons: light, dark*/, - boolean /*strike through*/, - boolean /*faded*/, -]; +type Foo = + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] + ? Foo3 + : Foo4; ================================================================================ `; @@ -638,36 +303,6 @@ export type SCMRawResource = [ ================================================================================ `; -exports[`tuple.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "none" - | printWidth -=====================================input====================================== - -export type SCMRawResource = [ - number /*handle*/, - string /*resourceUri*/, - modes.Command /*command*/, - string[] /*icons: light, dark*/, - boolean /*strike through*/, - boolean /*faded*/ -]; - -=====================================output===================================== -export type SCMRawResource = [ - number /*handle*/, - string /*resourceUri*/, - modes.Command /*command*/, - string[] /*icons: light, dark*/, - boolean /*strike through*/, - boolean /*faded*/ -]; - -================================================================================ -`; - exports[`tuple.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -727,91 +362,6 @@ export type SCMRawResource = [ ================================================================================ `; -exports[`tuple-labeled.ts - {"printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 - | printWidth -=====================================input====================================== -// https://github.com/babel/babel/pull/11754 - -type T = [x: A, y?: B, ...z: C]; - -type T = [A, y: B]; - -let x: [A: string, ...B: number[]] - -type T = [foo: string, bar?: number]; - -type T = [x?: A, y: B]; - -type T = [x: A, ...B]; - -type T = [...B, x: A]; - -=====================================output===================================== -// https://github.com/babel/babel/pull/11754 - -type T = [x: A, y?: B, ...z: C]; - -type T = [A, y: B]; - -let x: [A: string, ...B: number[]]; - -type T = [foo: string, bar?: number]; - -type T = [x?: A, y: B]; - -type T = [x: A, ...B]; - -type T = [...B, x: A]; - -================================================================================ -`; - -exports[`tuple-labeled.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "all" - | printWidth -=====================================input====================================== -// https://github.com/babel/babel/pull/11754 - -type T = [x: A, y?: B, ...z: C]; - -type T = [A, y: B]; - -let x: [A: string, ...B: number[]] - -type T = [foo: string, bar?: number]; - -type T = [x?: A, y: B]; - -type T = [x: A, ...B]; - -type T = [...B, x: A]; - -=====================================output===================================== -// https://github.com/babel/babel/pull/11754 - -type T = [x: A, y?: B, ...z: C]; - -type T = [A, y: B]; - -let x: [A: string, ...B: number[]]; - -type T = [foo: string, bar?: number]; - -type T = [x?: A, y: B]; - -type T = [x: A, ...B]; - -type T = [...B, x: A]; - -================================================================================ -`; - exports[`tuple-labeled.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -855,49 +405,6 @@ type T = [...B, x: A]; ================================================================================ `; -exports[`tuple-labeled.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "none" - | printWidth -=====================================input====================================== -// https://github.com/babel/babel/pull/11754 - -type T = [x: A, y?: B, ...z: C]; - -type T = [A, y: B]; - -let x: [A: string, ...B: number[]] - -type T = [foo: string, bar?: number]; - -type T = [x?: A, y: B]; - -type T = [x: A, ...B]; - -type T = [...B, x: A]; - -=====================================output===================================== -// https://github.com/babel/babel/pull/11754 - -type T = [x: A, y?: B, ...z: C]; - -type T = [A, y: B]; - -let x: [A: string, ...B: number[]]; - -type T = [foo: string, bar?: number]; - -type T = [x?: A, y: B]; - -type T = [x: A, ...B]; - -type T = [...B, x: A]; - -================================================================================ -`; - exports[`tuple-labeled.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -983,43 +490,6 @@ type T = [...B, x: A]; ================================================================================ `; -exports[`tuple-rest-not-last.ts - {"printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 - | printWidth -=====================================input====================================== -// https://github.com/babel/babel/pull/11753 - -let x: [...[number, string], string] - -=====================================output===================================== -// https://github.com/babel/babel/pull/11753 - -let x: [...[number, string], string]; - -================================================================================ -`; - -exports[`tuple-rest-not-last.ts - {"trailingComma":"all","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "all" - | printWidth -=====================================input====================================== -// https://github.com/babel/babel/pull/11753 - -let x: [...[number, string], string] - -=====================================output===================================== -// https://github.com/babel/babel/pull/11753 - -let x: [...[number, string], string]; - -================================================================================ -`; - exports[`tuple-rest-not-last.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -1039,25 +509,6 @@ let x: [...[number, string], string]; ================================================================================ `; -exports[`tuple-rest-not-last.ts - {"trailingComma":"none","printWidth":120} format 1`] = ` -====================================options===================================== -parsers: ["typescript"] -printWidth: 120 -trailingComma: "none" - | printWidth -=====================================input====================================== -// https://github.com/babel/babel/pull/11753 - -let x: [...[number, string], string] - -=====================================output===================================== -// https://github.com/babel/babel/pull/11753 - -let x: [...[number, string], string]; - -================================================================================ -`; - exports[`tuple-rest-not-last.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/tuple/jsfmt.spec.js b/tests/format/typescript/tuple/jsfmt.spec.js index 6574b4b1f786..31c3b0a4ff6b 100644 --- a/tests/format/typescript/tuple/jsfmt.spec.js +++ b/tests/format/typescript/tuple/jsfmt.spec.js @@ -1,6 +1,3 @@ run_spec(__dirname, ["typescript"], { trailingComma: "none" }); -run_spec(__dirname, ["typescript"], { trailingComma: "none", printWidth: 120 }); run_spec(__dirname, ["typescript"]); -run_spec(__dirname, ["typescript"], { printWidth: 120 }); run_spec(__dirname, ["typescript"], { trailingComma: "all" }); -run_spec(__dirname, ["typescript"], { trailingComma: "all", printWidth: 120 }); diff --git a/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts b/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts index 69b83bb6c6f4..5c1b157251d7 100644 --- a/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts +++ b/tests/format/typescript/tuple/trailing-comma-for-empty-tuples.ts @@ -1,3 +1,3 @@ type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = [] -type kjhudifkalmcnf, aaddffgg extends string[] = aabbccdd> = aaddffgg extends []? never: never +type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4; \ No newline at end of file From f8f0ac6f22c477a97cc703c29472f0eddf80481f Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 30 Nov 2021 22:06:37 +0900 Subject: [PATCH 6/8] Add changelog --- changelog_unreleased/typescript/11884.md | 34 ++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 changelog_unreleased/typescript/11884.md diff --git a/changelog_unreleased/typescript/11884.md b/changelog_unreleased/typescript/11884.md new file mode 100644 index 000000000000..7df538800ae7 --- /dev/null +++ b/changelog_unreleased/typescript/11884.md @@ -0,0 +1,34 @@ +#### Improve formatting for empty tuple types (#11884 by @sosukesuzuki) + + + + +```tsx +// Input +type Foo = + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] + ? Foo3 + : Foo4; + +// Prettier stable +type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [ + +] + ? Foo3 + : Foo4; + +// Prettier stable (tailingCommma = all) +// Invalid TypeScript code +type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [ + , +] + ? Foo3 + : Foo4; + +// Prettier main +type Foo = + Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] + ? Foo3 + : Foo4; + +``` From fefd080f8a04711988e4254a35e942947f570713 Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Tue, 30 Nov 2021 22:32:35 +0900 Subject: [PATCH 7/8] Update changelog_unreleased/typescript/11884.md Co-authored-by: fisker Cheung --- changelog_unreleased/typescript/11884.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/changelog_unreleased/typescript/11884.md b/changelog_unreleased/typescript/11884.md index 7df538800ae7..a2cdcfbb9146 100644 --- a/changelog_unreleased/typescript/11884.md +++ b/changelog_unreleased/typescript/11884.md @@ -1,7 +1,5 @@ #### Improve formatting for empty tuple types (#11884 by @sosukesuzuki) - - ```tsx // Input From 5d582a8b5259685abef4e1324054f5080a090013 Mon Sep 17 00:00:00 2001 From: sosukesuzuki Date: Tue, 30 Nov 2021 22:36:42 +0900 Subject: [PATCH 8/8] Fix names --- src/language-js/print/type-annotation.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/language-js/print/type-annotation.js b/src/language-js/print/type-annotation.js index 58805de142ad..c1e2d6497ef0 100644 --- a/src/language-js/print/type-annotation.js +++ b/src/language-js/print/type-annotation.js @@ -288,9 +288,9 @@ function printTupleType(path, options, print) { const node = path.getValue(); const typesField = node.type === "TSTupleType" ? "elementTypes" : "types"; const types = node[typesField]; - const isNotEmptyTuple = isNonEmptyArray(types); - const hasRest = isNotEmptyTuple && getLast(types).type === "TSRestType"; - const bracketsDelimiterLine = isNotEmptyTuple ? softline : ""; + const isNonEmptyTuple = isNonEmptyArray(types); + const hasRest = isNonEmptyTuple && getLast(types).type === "TSRestType"; + const bracketsDelimiterLine = isNonEmptyTuple ? softline : ""; return group([ "[", indent([ @@ -298,7 +298,7 @@ function printTupleType(path, options, print) { printArrayItems(path, options, typesField, print), ]), ifBreak( - isNotEmptyTuple && shouldPrintComma(options, "all") && !hasRest ? "," : "" + isNonEmptyTuple && shouldPrintComma(options, "all") && !hasRest ? "," : "" ), printDanglingComments(path, options, /* sameIndent */ true), bracketsDelimiterLine,