Skip to content

Commit c0250b3

Browse files
sosukesuzukifisker
andcommitted
Improve formatting for empty tuple types (#11884)
* Don't print trailing comma when tuple is empty * Add tests * Don't print softline * Add tests * Update tests * Add changelog * Update changelog_unreleased/typescript/11884.md Co-authored-by: fisker Cheung <lionkay@gmail.com> * Fix names Co-authored-by: fisker Cheung <lionkay@gmail.com>
1 parent 7fc196e commit c0250b3

4 files changed

Lines changed: 113 additions & 7 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#### Improve formatting for empty tuple types (#11884 by @sosukesuzuki)
2+
3+
<!-- prettier-ignore -->
4+
```tsx
5+
// Input
6+
type Foo =
7+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends []
8+
? Foo3
9+
: Foo4;
10+
11+
// Prettier stable
12+
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [
13+
14+
]
15+
? Foo3
16+
: Foo4;
17+
18+
// Prettier stable (tailingCommma = all)
19+
// Invalid TypeScript code
20+
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [
21+
,
22+
]
23+
? Foo3
24+
: Foo4;
25+
26+
// Prettier main
27+
type Foo =
28+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends []
29+
? Foo3
30+
: Foo4;
31+
32+
```

src/language-js/print/type-annotation.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
printComments,
55
printDanglingComments,
66
} = require("../../main/comments.js");
7-
const { getLast } = require("../../common/util.js");
7+
const { getLast, isNonEmptyArray } = require("../../common/util.js");
88
const {
99
builders: { group, join, line, softline, indent, align, ifBreak },
1010
} = require("../../document/index.js");
@@ -287,15 +287,21 @@ function printFunctionType(path, options, print) {
287287
function printTupleType(path, options, print) {
288288
const node = path.getValue();
289289
const typesField = node.type === "TSTupleType" ? "elementTypes" : "types";
290-
const hasRest =
291-
node[typesField].length > 0 &&
292-
getLast(node[typesField]).type === "TSRestType";
290+
const types = node[typesField];
291+
const isNonEmptyTuple = isNonEmptyArray(types);
292+
const hasRest = isNonEmptyTuple && getLast(types).type === "TSRestType";
293+
const bracketsDelimiterLine = isNonEmptyTuple ? softline : "";
293294
return group([
294295
"[",
295-
indent([softline, printArrayItems(path, options, typesField, print)]),
296-
ifBreak(shouldPrintComma(options, "all") && !hasRest ? "," : ""),
296+
indent([
297+
bracketsDelimiterLine,
298+
printArrayItems(path, options, typesField, print),
299+
]),
300+
ifBreak(
301+
isNonEmptyTuple && shouldPrintComma(options, "all") && !hasRest ? "," : ""
302+
),
297303
printDanglingComments(path, options, /* sameIndent */ true),
298-
softline,
304+
bracketsDelimiterLine,
299305
"]",
300306
]);
301307
}

tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,71 @@ export interface ShopQueryResult {
208208
================================================================================
209209
`;
210210

211+
exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"all"} format 1`] = `
212+
====================================options=====================================
213+
parsers: ["typescript"]
214+
printWidth: 80
215+
trailingComma: "all"
216+
| printWidth
217+
=====================================input======================================
218+
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []
219+
220+
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;
221+
=====================================output=====================================
222+
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong =
223+
[];
224+
225+
type Foo =
226+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends []
227+
? Foo3
228+
: Foo4;
229+
230+
================================================================================
231+
`;
232+
233+
exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"none"} format 1`] = `
234+
====================================options=====================================
235+
parsers: ["typescript"]
236+
printWidth: 80
237+
trailingComma: "none"
238+
| printWidth
239+
=====================================input======================================
240+
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []
241+
242+
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;
243+
=====================================output=====================================
244+
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong =
245+
[];
246+
247+
type Foo =
248+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends []
249+
? Foo3
250+
: Foo4;
251+
252+
================================================================================
253+
`;
254+
255+
exports[`trailing-comma-for-empty-tuples.ts format 1`] = `
256+
====================================options=====================================
257+
parsers: ["typescript"]
258+
printWidth: 80
259+
| printWidth
260+
=====================================input======================================
261+
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []
262+
263+
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;
264+
=====================================output=====================================
265+
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong =
266+
[];
267+
268+
type Foo =
269+
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends []
270+
? Foo3
271+
: Foo4;
272+
273+
================================================================================
274+
`;
275+
211276
exports[`tuple.ts - {"trailingComma":"all"} format 1`] = `
212277
====================================options=====================================
213278
parsers: ["typescript"]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []
2+
3+
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;

0 commit comments

Comments
 (0)