diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c79810f2727..b16bdc97a364 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/crates/biome_js_formatter/src/comments.rs b/crates/biome_js_formatter/src/comments.rs index 9fab161b6ac1..10dc33511702 100644 --- a/crates/biome_js_formatter/src/comments.rs +++ b/crates/biome_js_formatter/src/comments.rs @@ -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, ) -> CommentPlacement { - 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( diff --git a/crates/biome_js_formatter/tests/specs/js/module/statement/switch_comment.js b/crates/biome_js_formatter/tests/specs/js/module/statement/switch_comment.js new file mode 100644 index 000000000000..2c580066140f --- /dev/null +++ b/crates/biome_js_formatter/tests/specs/js/module/statement/switch_comment.js @@ -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; +} diff --git a/crates/biome_js_formatter/tests/specs/js/module/statement/switch_comment.js.snap b/crates/biome_js_formatter/tests/specs/js/module/statement/switch_comment.js.snap new file mode 100644 index 000000000000..6aa678527093 --- /dev/null +++ b/crates/biome_js_formatter/tests/specs/js/module/statement/switch_comment.js.snap @@ -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; +} +``` + + diff --git a/crates/biome_js_formatter/tests/specs/prettier/js/switch/comments2.js b/crates/biome_js_formatter/tests/specs/prettier/js/switch/comments2.js index c6b7876ea52f..226a1248e1ed 100644 --- a/crates/biome_js_formatter/tests/specs/prettier/js/switch/comments2.js +++ b/crates/biome_js_formatter/tests/specs/prettier/js/switch/comments2.js @@ -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 +} diff --git a/crates/biome_js_formatter/tests/specs/prettier/js/switch/comments2.js.snap b/crates/biome_js_formatter/tests/specs/prettier/js/switch/comments2.js.snap deleted file mode 100644 index a87d4e220a80..000000000000 --- a/crates/biome_js_formatter/tests/specs/prettier/js/switch/comments2.js.snap +++ /dev/null @@ -1,93 +0,0 @@ ---- -source: crates/biome_formatter_test/src/snapshot_builder.rs -info: js/switch/comments2.js ---- - -# Input - -```js -switch(1){default: // comment1 -} - -switch(2){default: // comment2 -//comment2a -} - -switch(3){default: // comment3 -break;// comment3a -} - -switch(4){default: // comment4 -// comment4a -break;// comment4b -} - -// FIXME -// TODO: reformat issue -// switch(5){default: // comment5 -// // comment5a -// foo();bar();//comment5b -// break;// comment5c -// } - -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Biome -@@ -18,10 +18,10 @@ - break; // comment4b - } - --switch (5) { -- default: // comment5 -- // comment5a -- foo(); -- bar(); //comment5b -- break; // comment5c --} -+// FIXME -+// TODO: reformat issue -+// switch(5){default: // comment5 -+// // comment5a -+// foo();bar();//comment5b -+// break;// comment5c -+// } -``` - -# Output - -```js -switch (1) { - default: // comment1 -} - -switch (2) { - default: // comment2 - //comment2a -} - -switch (3) { - default: // comment3 - break; // comment3a -} - -switch (4) { - default: // comment4 - // comment4a - break; // comment4b -} - -// FIXME -// TODO: reformat issue -// switch(5){default: // comment5 -// // comment5a -// foo();bar();//comment5b -// break;// comment5c -// } -``` - - diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index 170d9bf61491..f4f0660cb72b 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -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