From ed2055b81f3a56c290882420d5c0464695489bdf Mon Sep 17 00:00:00 2001 From: Sosuke Suzuki Date: Mon, 3 Jun 2019 21:59:34 +0900 Subject: [PATCH] [TypeScript]Keep a trailing comma on tuple types (#6172) --- CHANGELOG.unreleased.md | 26 +++++++ src/language-js/printer-estree.js | 5 +- .../__snapshots__/jsfmt.spec.js.snap | 71 +++++++++++++++++++ tests/typescript_tuple/jsfmt.spec.js | 1 + 4 files changed, 99 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.unreleased.md b/CHANGELOG.unreleased.md index a5faa7d4..974484f7 100644 --- a/CHANGELOG.unreleased.md +++ b/CHANGELOG.unreleased.md @@ -433,6 +433,31 @@ f[(a::b)]; f[a::b]; ``` +### TypeScript: Add trailing comma on tuple types when `trailing-commma` options is `all` ([#6172] by [@sosukesuzuki]) + +TypeScript supports a trailing comma on tuple types since version 3.3. + + +```ts +// Input +export type Foo = [ + number, + number, // comment +]; + +// Output (Prettier stable) +export type Foo = [ + number, + number // comment +]; + +// Output (Prettier master); +export type Foo = [ + number, + number, // comment +]; +``` + [#5979]: https://github.com/prettier/prettier/pull/5979 [#6086]: https://github.com/prettier/prettier/pull/6086 [#6088]: https://github.com/prettier/prettier/pull/6088 @@ -454,6 +479,7 @@ f[a::b]; [#6146]: https://github.com/prettier/prettier/pull/6146 [#6152]: https://github.com/prettier/prettier/pull/6152 [#6159]: https://github.com/prettier/prettier/pull/6159 +[#6172]: https://github.com/prettier/prettier/pull/6172 [@belochub]: https://github.com/belochub [@brainkim]: https://github.com/brainkim [@duailibe]: https://github.com/duailibe diff --git a/src/language-js/printer-estree.js b/src/language-js/printer-estree.js index b47eebe9..3bfeb293 100644 --- a/src/language-js/printer-estree.js +++ b/src/language-js/printer-estree.js @@ -2487,10 +2487,7 @@ function printPathNoParens(path, options, print, args) { printArrayItems(path, options, typesField, print) ]) ), - // TypeScript doesn't support trailing commas in tuple types - n.type === "TSTupleType" - ? "" - : ifBreak(shouldPrintComma(options) ? "," : ""), + ifBreak(shouldPrintComma(options) ? "," : ""), comments.printDanglingComments(path, options, /* sameIndent */ true), softline, "]" diff --git a/tests/typescript_tuple/__snapshots__/jsfmt.spec.js.snap b/tests/typescript_tuple/__snapshots__/jsfmt.spec.js.snap index c30e729f..ea436406 100644 --- a/tests/typescript_tuple/__snapshots__/jsfmt.spec.js.snap +++ b/tests/typescript_tuple/__snapshots__/jsfmt.spec.js.snap @@ -40,6 +40,47 @@ export interface ShopQueryResult { ================================================================================ `; +exports[`trailing-comma.ts 2`] = ` +====================================options===================================== +parsers: ["typescript"] +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[`tuple.ts 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -68,3 +109,33 @@ export type SCMRawResource = [ ================================================================================ `; + +exports[`tuple.ts 2`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +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*/, +]; + +================================================================================ +`; diff --git a/tests/typescript_tuple/jsfmt.spec.js b/tests/typescript_tuple/jsfmt.spec.js index 2ea3bb6e..a17d1d35 100644 --- a/tests/typescript_tuple/jsfmt.spec.js +++ b/tests/typescript_tuple/jsfmt.spec.js @@ -1 +1,2 @@ run_spec(__dirname, ["typescript"]); +run_spec(__dirname, ["typescript"], { trailingComma: "all" });