Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-switch-case-comment-idempotence.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed [#2786](https://github.com/biomejs/biome/issues/2786): The formatter no longer produces different output on subsequent runs when a `case` clause has a trailing line comment followed by a single block statement.
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add a tiny before/after formatter diff in the changeset body.

Nice summary, but this formatter fix should include a concrete formatting diff so users can see the visible behaviour change at a glance.

Suggested changeset snippet
 Fixed [`#2786`](https://github.com/biomejs/biome/issues/2786): The formatter no longer produces different output on subsequent runs when a `case` clause has a trailing line comment followed by a single block statement.

+```diff
+-case 1337: {
+-  // ELITE
++case 1337: // ELITE
++{
+   console.log("x is cool");
+ }
+```

As per coding guidelines: for formatter changes in changesets, show formatting changes using diff code blocks.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
Fixed [#2786](https://github.com/biomejs/biome/issues/2786): The formatter no longer produces different output on subsequent runs when a `case` clause has a trailing line comment followed by a single block statement.
---
package: `@biomejs/biome`
type: patch
---
Fixed [`#2786`](https://github.com/biomejs/biome/issues/2786): The formatter no longer produces different output on subsequent runs when a `case` clause has a trailing line comment followed by a single block statement.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.changeset/fix-switch-case-comment-idempotence.md at line 5, Update the
changeset body in .changeset/fix-switch-case-comment-idempotence.md to include a
before/after formatter diff code block showing the concrete visible change (use
a unified diff-style snippet); insert a fenced code block (```diff) containing
the original lines (prefixed with -) and the formatted lines (prefixed with +) —
e.g., show the "-case 1337: {", "-  // ELITE", "-  console.log(\"x is cool\");",
"-}" and the corresponding "+case 1337: // ELITE", "+{", "+  console.log(\"x is
cool\");", "+}" lines — so users can see the exact formatting change.

12 changes: 11 additions & 1 deletion crates/biome_js_formatter/src/js/auxiliary/case_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,22 @@ impl FormatNodeRule<JsCaseClause> for FormatJsCaseClause {
// default:
// break;
// }
// If the case clause has a trailing line comment after the colon
// (e.g. `case 1337: // ELITE`), we must not hug the block statement on
// the same line. Doing so produces `case 1337: { // ELITE`, and on the
// next format pass the comment is reparsed as a leading comment inside
// the block, causing it to move to its own line — an idempotence bug.
// The comment is stored as a trailing comment of the `test` node.
let has_trailing_comment = test
.as_ref()
.is_ok_and(|test| f.comments().has_trailing_comments(test.syntax()));

if consequent.is_empty() {
// Print nothing to ensure that trailing comments on the same line
// are printed on the same line. The parent list formatter takes
// care of inserting a hard line break between cases.
Ok(())
} else if is_single_block_statement {
} else if is_single_block_statement && !has_trailing_comment {
write![f, [space(), consequent.format()]]
} else {
// no line break needed after because it is added by the indent in the switch statement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,21 @@ switch(x) {
a(); // ab
break;
}

// Trailing comment on a case clause followed by a single block statement.
// The comment must stay on the `case` line and not migrate into the block
// on subsequent format passes (idempotence, issue #2786).
function cool(x) {
switch (x) {
case 4: // guaranteed to be random
case 42: // classic
case 1337: // ELITE
{
console.log("x is cool");
break;
}
default: {
console.error("x is not cool");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ switch(x) {
break;
}

// Trailing comment on a case clause followed by a single block statement.
// The comment must stay on the `case` line and not migrate into the block
// on subsequent format passes (idempotence, issue #2786).
function cool(x) {
switch (x) {
case 4: // guaranteed to be random
case 42: // classic
case 1337: // ELITE
{
console.log("x is cool");
break;
}
default: {
console.error("x is not cool");
}
}
}

```


Expand Down Expand Up @@ -75,4 +93,22 @@ switch (x) {
break;
}

// Trailing comment on a case clause followed by a single block statement.
// The comment must stay on the `case` line and not migrate into the block
// on subsequent format passes (idempotence, issue #2786).
function cool(x) {
switch (x) {
case 4: // guaranteed to be random
case 42: // classic
case 1337: // ELITE
{
console.log("x is cool");
break;
}
default: {
console.error("x is not cool");
}
}
}

```
Loading