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>
This commit is contained in:
Sosuke Suzukiandfisker Cheung authored and GitHub committed 2021-11-30 23:48:28 +09:00
1 parent 62ede8b710
commit e17404eeb1
4 files changed
+113 -7

No files matched your search

+32
View File
@@ -0,0 +1,32 @@
#### Improve formatting for empty tuple types (#11884 by @sosukesuzuki)
<!-- prettier-ignore -->
```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;
```
+13 -7
View File
@@ -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,15 +287,21 @@ 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 isNonEmptyTuple = isNonEmptyArray(types);
const hasRest = isNonEmptyTuple && getLast(types).type === "TSRestType";
const bracketsDelimiterLine = isNonEmptyTuple ? softline : "";
return group([
"[",
indent([softline, printArrayItems(path, options, typesField, print)]),
ifBreak(shouldPrintComma(options, "all") && !hasRest ? "," : ""),
indent([
bracketsDelimiterLine,
printArrayItems(path, options, typesField, print),
]),
ifBreak(
isNonEmptyTuple && shouldPrintComma(options, "all") && !hasRest ? "," : ""
),
printDanglingComments(path, options, /* sameIndent */ true),
softline,
bracketsDelimiterLine,
"]",
]);
}
@@ -208,6 +208,71 @@ 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 = []
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;
=====================================output=====================================
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong =
[];
type Foo =
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends []
? Foo3
: Foo4;
================================================================================
`;
exports[`trailing-comma-for-empty-tuples.ts - {"trailingComma":"none"} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
trailingComma: "none"
| printWidth
=====================================input======================================
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;
=====================================output=====================================
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong =
[];
type Foo =
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends []
? Foo3
: Foo4;
================================================================================
`;
exports[`trailing-comma-for-empty-tuples.ts format 1`] = `
====================================options=====================================
parsers: ["typescript"]
printWidth: 80
| printWidth
=====================================input======================================
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;
=====================================output=====================================
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong =
[];
type Foo =
Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends []
? Foo3
: Foo4;
================================================================================
`;
exports[`tuple.ts - {"trailingComma":"all"} format 1`] = `
====================================options=====================================
parsers: ["typescript"]
@@ -0,0 +1,3 @@
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []
type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;