From 58ae337fea1003ef0166cf0e97a9a89fb49fa864 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 25 Jan 2023 12:44:12 +0800 Subject: [PATCH] Treat `--trailing-comma=es5` as `all` when printing tuple types (#14086) --- src/language-js/print/array.js | 7 +- .../__snapshots__/jsfmt.spec.js.snap | 124 ++++++++++++++++++ tests/format/flow/tuple-types/jsfmt.spec.js | 3 + .../format/flow/tuple-types/trailing-comma.js | 14 ++ .../tuple/__snapshots__/jsfmt.spec.js.snap | 6 +- 5 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 tests/format/flow/tuple-types/__snapshots__/jsfmt.spec.js.snap create mode 100644 tests/format/flow/tuple-types/jsfmt.spec.js create mode 100644 tests/format/flow/tuple-types/trailing-comma.js diff --git a/src/language-js/print/array.js b/src/language-js/print/array.js index 3f6f9fae6..8be25d281 100644 --- a/src/language-js/print/array.js +++ b/src/language-js/print/array.js @@ -109,12 +109,7 @@ function printArray(path, options, print) { ? "" : needsForcedTrailingComma ? "," - : !shouldPrintComma( - options, - node.type === "TSTupleType" || node.type === "TupleTypeAnnotation" - ? "all" - : "es5" - ) + : !shouldPrintComma(options) ? "" : shouldUseConciseFormatting ? ifBreak(",", "", { groupId }) diff --git a/tests/format/flow/tuple-types/__snapshots__/jsfmt.spec.js.snap b/tests/format/flow/tuple-types/__snapshots__/jsfmt.spec.js.snap new file mode 100644 index 000000000..f71a1305e --- /dev/null +++ b/tests/format/flow/tuple-types/__snapshots__/jsfmt.spec.js.snap @@ -0,0 +1,124 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`trailing-comma.js - {"trailingComma":"all"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +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.js - {"trailingComma":"es5"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +trailingComma: "es5" + | 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.js - {"trailingComma":"none"} format 1`] = ` +====================================options===================================== +parsers: ["flow", "babel-flow"] +printWidth: 80 +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; +} + +================================================================================ +`; diff --git a/tests/format/flow/tuple-types/jsfmt.spec.js b/tests/format/flow/tuple-types/jsfmt.spec.js new file mode 100644 index 000000000..c7da7a9c1 --- /dev/null +++ b/tests/format/flow/tuple-types/jsfmt.spec.js @@ -0,0 +1,3 @@ +run_spec(import.meta, ["flow", "babel-flow"], { trailingComma: "none" }); +run_spec(import.meta, ["flow", "babel-flow"], { trailingComma: "es5" }); +run_spec(import.meta, ["flow", "babel-flow"], { trailingComma: "all" }); diff --git a/tests/format/flow/tuple-types/trailing-comma.js b/tests/format/flow/tuple-types/trailing-comma.js new file mode 100644 index 000000000..b73a76077 --- /dev/null +++ b/tests/format/flow/tuple-types/trailing-comma.js @@ -0,0 +1,14 @@ +export interface ShopQueryResult { + chic: boolean; + location: number[]; + menus: Menu[]; + openingDays: number[]; + closingDays: [ + { + from: string, + to: string, + }, // <== this one + ]; + shop: string; + distance: number; +} diff --git a/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap b/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap index a6c5949d3..1d0384dde 100644 --- a/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap +++ b/tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap @@ -271,7 +271,7 @@ export interface ShopQueryResult { { from: string; to: string; - } // <== this one + }, // <== this one ]; shop: string; distance: number; @@ -439,7 +439,7 @@ type ValidateArgs = [ }, string, string, - ...string[] + ...string[], ]; ================================================================================ @@ -528,7 +528,7 @@ export type SCMRawResource = [ modes.Command /*command*/, string[] /*icons: light, dark*/, boolean /*strike through*/, - boolean /*faded*/ + boolean /*faded*/, ]; ================================================================================