Skip to content

fix(fmt): surround template literal type unions with newlines when too long#34462

Open
divybot wants to merge 1 commit into
mainfrom
orch/divybot-258
Open

fix(fmt): surround template literal type unions with newlines when too long#34462
divybot wants to merge 1 commit into
mainfrom
orch/divybot-258

Conversation

@divybot

@divybot divybot commented May 28, 2026

Copy link
Copy Markdown
Contributor

Summary

A TsUnionType (or TsIntersectionType) inside a ${...} placeholder of a TsTplLitType was breaking only on the union separator (| / &) when the line exceeded the configured width, leaving the closing } glued onto the last union element's line.

Reported in #29868. Minimal repro:

export type SortSelector = `${typeof Descending | typeof Ascending}${SortItems}`;

formatted to

export type SortSelector = `${
  | typeof Descending
  | typeof Ascending}${SortItems}`;

instead of the expected

export type SortSelector = `${
  typeof Descending | typeof Ascending
}${SortItems}`;

Fix

The fix is in dprint-plugin-typescript (dprint/dprint-plugin-typescript#794): it adds TsUnionType and TsIntersectionType to the get_possible_surround_newlines match inside gen_template_literal, so the existing surround_with_newlines_indented_if_multi_line codepath handles them the same way it already handles BinExpr / CondExpr placeholders.

Until the next dprint-plugin-typescript release ships that fix, this PR consumes it via a [patch.crates-io] against the fork branch. Once upstream releases a new dprint-plugin-typescript version with #794 merged, the [patch.crates-io] block in Cargo.toml should be removed and the dprint-plugin-typescript workspace pin bumped to the released version.

A spec regression test in tests/specs/fmt/template_literal_type_union covers the case from the issue and asserts the expected formatted output.

Closes #29868
Closes denoland/divybot#258

Test plan

  • cargo test specs::template_literal_type_union
  • tools/format.js && tools/lint.js clean

…o long

A `TsUnionType` (or `TsIntersectionType`) inside a `${...}` placeholder
of a `TsTplLitType` would break only on the union separator (`|` / `&`)
when the line exceeded the configured width, leaving the closing `}`
glued onto the last union element's line. The minimal repro from #29868
is

```ts
export type SortSelector = `${typeof Descending | typeof Ascending}${SortItems}`;
```

which formatted to

```ts
export type SortSelector = `${
  | typeof Descending
  | typeof Ascending}${SortItems}`;
```

instead of the expected

```ts
export type SortSelector = `${
  typeof Descending | typeof Ascending
}${SortItems}`;
```

The fix is in `dprint-plugin-typescript`
(dprint/dprint-plugin-typescript#794), which adds `TsUnionType` and
`TsIntersectionType` to the `get_possible_surround_newlines` match
inside `gen_template_literal` so the existing
`surround_with_newlines_indented_if_multi_line` codepath handles them
the same way it already handles `BinExpr` / `CondExpr` placeholders.
Until the next dprint-plugin-typescript release ships that fix, this
PR consumes it via a `[patch.crates-io]` against a fork branch; the
patch should be removed and the version pin bumped once the upstream
release is available.

Adds a spec regression test in
`tests/specs/fmt/template_literal_type_union` covering the issue's
exact input and the expected formatted output.

Closes denoland/orchid#258

Co-Authored-By: Divy Srivastava <me@littledivy.com>
@littledivy

Copy link
Copy Markdown
Member

@divybot Add an AI disclaimer to the dprint PR

@bartlomieju bartlomieju left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Blocked on upstream plugin

@bartlomieju bartlomieju added the upstream Changes in upstream are required to solve these issues label Jun 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

upstream Changes in upstream are required to solve these issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unintended Behavior: Formatted Template Literal in Union

3 participants