Repository navigation
fix(tsrx-prettier-plugin): keep a comment in an empty @switch body - #878
Merged
Merged
Conversation
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]>
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR fixes
A comment in an empty
@switchbody moved into the parentheses (#835):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 aSwitchStatementand 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@ifand@try, so for@switchthe comment fell through to the generic rule, trailed the discriminant, and printed in the parentheses.@switchand@forare presented to the comment handlers as their statements, like@ifand@try. For@for, this fixes a line comment between@for (…)and its{, which moved into the body where Prettier'sforkeeps it before the{.switchdoes (printDanglingCommentswithindent). Prettier doesn't exportprintDanglingCommentsto plugins, so a small helper does the same with the exportedprintComment, as the plugin'sprintOwnCommentsdoes for leading and trailing comments.@finallyprinting andfinalizervisitor key go: TSRX has no@finally, and core never gives an@trya 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@forforms,@switch/@case/@default, and@try/@catch:(, and each clause of a classicforhead;)and{;@elseand@catch;@casetest, and before the first and after the last@case;@casewhose 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 fromcase (…):.Each must format as Prettier formats the same statement in TypeScript, on the first and the second format. All pass. Before this PR,
maindiffered 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'sindexandkeyclauses, before and in@empty, and before and in@pending. The comment stays in place and is stable. A comment before@pendingmoves into its block, as Prettier moves one beforefinally(its rule for a block aftertry). Some places are unstable in Prettier itself, where the plugin matches both of Prettier's formats: a line comment infor's parentheses, and a line comment around acasetest (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:checkand 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:
@switchand@forare now included inCOMMENT_STATEMENT_DIRECTIVES, so Prettier’s comment logic treats them likeswitch/for(e.g. comments in an empty@switchbody stay inside the braces instead of moving into the discriminant). Empty@switchbodies with only comments use a newprintDanglingCommentshelper, mirroring Prettier’s non-exported behavior.@finally/finalizersupport is removed from@tryprinting and visitor keys because TSRX does not use them.Tests: Adds focused cases for empty
@switchcomments 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.