From 6a768edb143ec8d971723b916e89ec933aa3dc70 Mon Sep 17 00:00:00 2001 From: mdm317 Date: Fri, 26 Jan 2024 02:05:01 +0900 Subject: [PATCH] fix(js_formatter): fix comment with single instruction within a control flow body (#1655) --- CHANGELOG.md | 2 + crates/biome_js_formatter/src/comments.rs | 8 +- ...ntinue-and-break-comment-without-blocks.js | 17 +- ...ak-comment-without-blocks.js.prettier-snap | 2 +- ...e-and-break-comment-without-blocks.js.snap | 216 ------------------ .../src/content/docs/internals/changelog.mdx | 2 + 6 files changed, 19 insertions(+), 228 deletions(-) delete mode 100644 crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js.snap diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b21a710a685..ae46ab65605c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom - Fix [#1508](https://github.com/biomejs/biome/issues/1508) by excluding deleted files from being processed. Contributed by @ematipico +- Fix [#1173](https://github.com/biomejs/biome/issues/1173). Fix the formatting of a single instruction with commented in a control flow body to ensure consistency. Contributed by @mdm317 + ### Configuration ### Editors diff --git a/crates/biome_js_formatter/src/comments.rs b/crates/biome_js_formatter/src/comments.rs index 3016bc9d1038..9e93585f31c7 100644 --- a/crates/biome_js_formatter/src/comments.rs +++ b/crates/biome_js_formatter/src/comments.rs @@ -280,8 +280,12 @@ fn handle_continue_break_comment( let enclosing = comment.enclosing_node(); - if let Some(preceding) = comment.preceding_node() { - if preceding.kind() == JsSyntaxKind::JS_LABEL { + if let (Some(preceding), Some(parent)) = + (comment.preceding_node(), comment.enclosing_node().parent()) + { + if preceding.kind() == JsSyntaxKind::JS_LABEL + && parent.kind() != JsSyntaxKind::JS_FOR_STATEMENT + { return CommentPlacement::trailing(preceding.clone(), comment); } } diff --git a/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js b/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js index d7c99fb7ee3d..d68bba16e79c 100644 --- a/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js +++ b/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js @@ -85,14 +85,13 @@ for (const f in {}) break label1: for (;;) continue label1 /* comment */ ; -// FIXME: TODO: reformat issue -// label1: for (;;) continue label1 -// /* comment */ -// ; +label1: for (;;) continue label1 +/* comment */ +; -// label1: for (;;) continue label1 // comment -// ; +label1: for (;;) continue label1 // comment +; -// label1: for (;;) continue label1 -// // comment -// ; +label1: for (;;) continue label1 +// comment +; diff --git a/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js.prettier-snap b/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js.prettier-snap index 1eae371a4024..39ec64beac9f 100644 --- a/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js.prettier-snap +++ b/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js.prettier-snap @@ -58,7 +58,7 @@ for (const f in {}) continue; for (const f in {}) break; /* comment */ -label1: for (;;) continue label1 /* comment */; +label1: for (;;) continue label1; /* comment */ label1: for (;;) continue label1; /* comment */ diff --git a/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js.snap b/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js.snap deleted file mode 100644 index c0cfde75a4f9..000000000000 --- a/crates/biome_js_formatter/tests/specs/prettier/js/for/continue-and-break-comment-without-blocks.js.snap +++ /dev/null @@ -1,216 +0,0 @@ ---- -source: crates/biome_formatter_test/src/snapshot_builder.rs -info: js/for/continue-and-break-comment-without-blocks.js ---- - -# Input - -```js -for(;;) continue -// comment -; - -for (;;) break -// comment -; - -for (const f of []) continue -// comment -; - -for (const f of []) break -// comment -; - -for (const f in {}) continue -// comment -; - -for (const f in {}) break -// comment -; - -for(;;) continue // comment -; - -for (;;) break // comment -; - -for (const f of []) continue // comment -; - -for (const f of []) break // comment -; - -for (const f in {}) continue // comment -; - -for (const f in {}) break // comment -; - -for(;;) continue /* comment */ -; - -for (;;) break /* comment */ -; - -for (const f of []) continue /* comment */ -; - -for (const f of []) break /* comment */ -; - -for (const f in {}) continue /* comment */ -; - -for (const f in {}) break /* comment */ -; - -for(;;) continue -/* comment */ -; - -for (;;) break -/* comment */ -; - -for (const f of []) continue -/* comment */ -; - -for (const f of []) break -/* comment */ -; - -for (const f in {}) continue -/* comment */ -; - -for (const f in {}) break -/* comment */ -; - -label1: for (;;) continue label1 /* comment */ -; - -// FIXME: TODO: reformat issue -// label1: for (;;) continue label1 -// /* comment */ -// ; - -// label1: for (;;) continue label1 // comment -// ; - -// label1: for (;;) continue label1 -// // comment -// ; - -``` - - -# Prettier differences - -```diff ---- Prettier -+++ Biome -@@ -60,10 +60,14 @@ - - label1: for (;;) continue label1 /* comment */; - --label1: for (;;) continue label1; --/* comment */ -+// FIXME: TODO: reformat issue -+// label1: for (;;) continue label1 -+// /* comment */ -+// ; - --label1: for (;;) continue label1; // comment -+// label1: for (;;) continue label1 // comment -+// ; - --label1: for (;;) continue label1; --// comment -+// label1: for (;;) continue label1 -+// // comment -+// ; -``` - -# Output - -```js -for (;;) continue; -// comment - -for (;;) break; -// comment - -for (const f of []) continue; -// comment - -for (const f of []) break; -// comment - -for (const f in {}) continue; -// comment - -for (const f in {}) break; -// comment - -for (;;) continue; // comment - -for (;;) break; // comment - -for (const f of []) continue; // comment - -for (const f of []) break; // comment - -for (const f in {}) continue; // comment - -for (const f in {}) break; // comment - -for (;;) continue; /* comment */ - -for (;;) break; /* comment */ - -for (const f of []) continue; /* comment */ - -for (const f of []) break; /* comment */ - -for (const f in {}) continue; /* comment */ - -for (const f in {}) break; /* comment */ - -for (;;) continue; -/* comment */ - -for (;;) break; -/* comment */ - -for (const f of []) continue; -/* comment */ - -for (const f of []) break; -/* comment */ - -for (const f in {}) continue; -/* comment */ - -for (const f in {}) break; -/* comment */ - -label1: for (;;) continue label1 /* comment */; - -// FIXME: TODO: reformat issue -// label1: for (;;) continue label1 -// /* comment */ -// ; - -// label1: for (;;) continue label1 // comment -// ; - -// label1: for (;;) continue label1 -// // comment -// ; -``` - - diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index 9bb2ef04b9bf..5f4f7b668bb3 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -24,6 +24,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom - Fix [#1508](https://github.com/biomejs/biome/issues/1508) by excluding deleted files from being processed. Contributed by @ematipico +- Fix [#1173](https://github.com/biomejs/biome/issues/1173). Fix the formatting of a single instruction with commented in a control flow body to ensure consistency. Contributed by @mdm317 + ### Configuration ### Editors