Skip to content

Commit

Permalink
Improve formatting for empty tuple types (#11884)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
sosukesuzuki and fisker committed Nov 30, 2021
1 parent 62ede8b commit e17404e
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 7 deletions.
32 changes: 32 additions & 0 deletions changelog_unreleased/typescript/11884.md
@@ -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;

```
20 changes: 13 additions & 7 deletions src/language-js/print/type-annotation.js
Expand Up @@ -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");
Expand Down Expand Up @@ -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,
"]",
]);
}
Expand Down
65 changes: 65 additions & 0 deletions tests/format/typescript/tuple/__snapshots__/jsfmt.spec.js.snap
Expand Up @@ -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"]
Expand Down
@@ -0,0 +1,3 @@
type Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong = []

type Foo = Foooooooooooooooooooooooooooooooooooooooooooooooooooooooooo extends [] ? Foo3 : Foo4;

0 comments on commit e17404e

Please sign in to comment.