Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve formatting for empty tuple types #11884

Merged
merged 8 commits into from Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 32 additions & 0 deletions changelog_unreleased/typescript/11884.md
@@ -0,0 +1,32 @@
#### Improve formatting for empty tuple types (#11884 by @sosukesuzuki)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.


<!-- 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;