Skip to content

fix(tsrx-prettier-plugin): keep a comment in an empty @switch body - #878

Merged
leonidaz merged 3 commits into
mainfrom
fix/np-empty-switch-comment-835
Sep 27, 2026
Merged

leonidaz merged 3 commits into
mainfrom
fix/np-empty-switch-comment-835

Conversation

@leonidaz

@leonidaz leonidaz commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

What this PR fixes

A comment in an empty @switch body moved into the parentheses (#835):

input before now (= Prettier's switch)
@switch (a) {, // c, } @switch (, a, // c, ) {, } @switch (a) {, // c, }
@switch (a) { /* c */ } @switch (a /* c */) {, } @switch (a) {, /* c */, }

Cause and change

Not a Prettier bug: Prettier keeps these comments in a plain switch. Its comment handlers look for a SwitchStatement and make the comment a dangling comment of the switch, which its printer prints in the braces. The plugin presents a directive to those handlers as its statement only for @if and @try, so for @switch the comment fell through to the generic rule, trailed the discriminant, and printed in the parentheses.

  • @switch and @for are presented to the comment handlers as their statements, like @if and @try. For @for, this fixes a line comment between @for (…) and its {, which moved into the body where Prettier's for keeps it before the {.
  • Its printer prints dangling comments in an empty body as Prettier's switch does (printDanglingComments with indent). Prettier doesn't export printDanglingComments to plugins, so a small helper does the same with the exported printComment, as the plugin's printOwnComments does for leading and trailing comments.
  • The @finally printing and finalizer visitor key go: TSRX has no @finally, and core never gives an @try a finalizer.

Every directive checked

A new test puts a line and a block comment in each place of each directive, 74 cases across @if/@else if/@else, the three @for forms, @switch/@case/@default, and @try/@catch:

  • an empty body, and before the first and after the last child;
  • the parentheses, right after (, and each clause of a classic for head;
  • between ) and {;
  • before @else and @catch;
  • after an @case test, and before the first and after the last @case;
  • an @case whose test is in parentheses (@case (admin + "_1"): { … }): alone in its block, before its child, after the :, between ) and :, and first, in the middle and last in the parentheses. The parentheses go, as Prettier drops them from case (…):.

Each must format as Prettier formats the same statement in TypeScript, on the first and the second format. All pass. Before this PR, main differed in three: a comment in an empty @switch (line and block), and a line comment between @for (…) and {.

The TSRX-only places, with no statement to compare with, are pinned too (8 cases): @for's index and key clauses, before and in @empty, and before and in @pending. The comment stays in place and is stable. A comment before @pending moves into its block, as Prettier moves one before finally (its rule for a block after try). Some places are unstable in Prettier itself, where the plugin matches both of Prettier's formats: a line comment in for's parentheses, and a line comment around a case test (after the :, between ) and :, or last in the test's parentheses). They go on #852's list of Prettier bugs.

Verification

  • tsrx-prettier-plugin: 2,230 tests pass (17 skipped); pnpm format:check and the plugin's typecheck pass.

Closes #835.

🤖 Generated with Claude Code


Note

Low Risk
Formatter-only changes in the Prettier plugin with broad regression tests; no runtime or security impact.

Overview
Fixes comment placement in TSRX @switch, @for, and related directives so formatting matches Prettier’s equivalent TypeScript statements (closes #835).

Printer: @switch and @for are now included in COMMENT_STATEMENT_DIRECTIVES, so Prettier’s comment logic treats them like switch/for (e.g. comments in an empty @switch body stay inside the braces instead of moving into the discriminant). Empty @switch bodies with only comments use a new printDanglingComments helper, mirroring Prettier’s non-exported behavior. @finally / finalizer support is removed from @try printing and visitor keys because TSRX does not use them.

Tests: Adds focused cases for empty @switch comments plus a large matrix (~74 scenarios) comparing directive comment positions to TypeScript, plus eight TSRX-only spots (@empty, index/key, @pending).

Reviewed by Cursor Bugbot for commit 789f177. Bugbot is set up for automated code reviews on this repo. Configure here.

Prettier keeps a comment in an empty `switch` as a dangling comment of
the switch, which it prints in the braces. Its comment handlers look for
a `SwitchStatement`, but the plugin presented only `@if` and `@try` as
their statements, so the comment fell to the discriminant and printed in
the parentheses (#835). `@switch` is now presented as its `switch` too,
and its printer prints dangling comments in an empty body as Prettier's
`switch` does.

A test now checks a line and a block comment in each place of each
directive against Prettier's output for the same statement.

Also removes the `@finally` printing: TSRX has no `@finally`, and core
never gives an `@try` a finalizer.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leonidaz and others added 2 commits September 27, 2026 15:59
…them in `for`

A line comment between `@for (…)` and its `{` moved into the body,
where Prettier's `for` keeps it before the `{`: `@for`, like `@switch`,
wasn't presented to Prettier's comment handlers as its statement. It is
now, with `@if`, `@switch` and `@try`.

The directive test now covers more of `@for` (between `)` and `{`,
after `(`, around the body's children, `for…in`'s parentheses, and each
clause of a classic head), and pins the TSRX-only places: `@for`'s
`index` and `key` clauses, `@empty` and `@pending`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rentheses

An `@case` whose test is in parentheses, with a line or a block comment
alone in its block, before its child, after the `:`, between `)` and
`:`, and first, in the middle and last in the parentheses. Each formats
as Prettier formats the same `case`, on both formats.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@leonidaz
leonidaz merged commit c551934 into main Sep 27, 2026
8 checks passed
@leonidaz
leonidaz deleted the fix/np-empty-switch-comment-835 branch September 27, 2026 20:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: tsrx-prettier-plugin: A comment in an empty @switch body moves into the parentheses

1 participant