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

fix(js_formatter): placement of comments inside default switch clause #1505

Merged
merged 3 commits into from
Jan 10, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

#### Bug fixes

- Fix [#1170](https://github.com/biomejs/biome/issues/1170). Fix placement of comments inside default switch clause. Now all line comments that has a preceding node will keep their position. Contributed by @kalleep

### JavaScript APIs

### Linter
Expand Down
41 changes: 29 additions & 12 deletions crates/biome_js_formatter/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,22 +330,39 @@ fn handle_continue_break_comment(
/// }
/// ```
///
/// All other same line comments become `Dangling` comments that are handled inside of the default case
/// formatting
/// All other same line comments will use `Default` placement if they have a perceding node.
/// ```javascript
/// switch(x) {
/// default:
/// a(); // asd
/// break;
/// }
/// ```
///
/// All other comments become `Dangling` comments that are handled inside of the default case
/// formatting.
fn handle_switch_default_case_comment(
comment: DecoratedComment<JsLanguage>,
) -> CommentPlacement<JsLanguage> {
match (comment.enclosing_node().kind(), comment.following_node()) {
(JsSyntaxKind::JS_DEFAULT_CLAUSE, Some(following)) => {
match JsBlockStatement::cast_ref(following) {
Some(block) if comment.kind().is_line() => {
place_block_statement_comment(block, comment)
}
_ => CommentPlacement::dangling(comment.enclosing_node().clone(), comment),
}
}
_ => CommentPlacement::Default(comment),
if comment.enclosing_node().kind() != JsSyntaxKind::JS_DEFAULT_CLAUSE {
return CommentPlacement::Default(comment);
}

if !comment.kind().is_line() {
return CommentPlacement::dangling(comment.enclosing_node().clone(), comment);
}

let Some(block) = comment
.following_node()
.and_then(JsBlockStatement::cast_ref)
else {
if comment.preceding_node().is_some() {
return CommentPlacement::Default(comment);
}
return CommentPlacement::dangling(comment.enclosing_node().clone(), comment);
};

place_block_statement_comment(block, comment)
}

fn handle_labelled_statement_comment(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
switch(5){default: // comment5
// comment5a
foo();bar();//comment5b
break;// comment5c
}

switch(x) {
default:
a(); // asd
break;
}

switch(x) {
default:
// a
a(); // ab
break;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
source: crates/biome_formatter_test/src/snapshot_builder.rs
info: js/module/statement/switch_comment.js
---

# Input

```js
switch(5){default: // comment5
// comment5a
foo();bar();//comment5b
break;// comment5c
}

switch(x) {
default:
a(); // asd
break;
}

switch(x) {
default:
// a
a(); // ab
break;
}

```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Indent width: 2
Line ending: LF
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
Bracket spacing: true
Bracket same line: false
-----

```js
switch (5) {
default: // comment5
// comment5a
foo();
bar(); //comment5b
break; // comment5c
}

switch (x) {
default:
a(); // asd
break;
}

switch (x) {
default:
// a
a(); // ab
break;
}
```


Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ switch(4){default: // comment4
break;// comment4b
}

// FIXME
// TODO: reformat issue
// switch(5){default: // comment5
// // comment5a
// foo();bar();//comment5b
// break;// comment5c
// }
switch(5){default: // comment5
// comment5a
foo();bar();//comment5b
break;// comment5c
}

This file was deleted.

2 changes: 2 additions & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

#### Bug fixes

- Fix [#1170](https://github.com/biomejs/biome/issues/1170). Fix placement of comments inside default switch clause. Now all line comments that has a preceding node will keep their position. Contributed by @kalleep

### JavaScript APIs

### Linter
Expand Down