fix(rome_js_analyzer): add newline if statement has comments in `useBlockStatements`
MODIFY
crates/rome_js_analyze/src/analyzers/correctness/use_block_statements.rs
crates/rome_js_analyze/src/analyzers/correctness/use_block_statements.rs
@@ -216,8 +216,24 @@ impl Rule for UseBlockStatements {
r_curly_token.with_leading_trivia(leading_trivia)
} else {
+ let has_trailing_single_line_comments = stmt
+ .syntax()
+ .last_trailing_trivia()
+ .map(|trivia| {
+ trivia
+ .pieces()
+ .any(|trivia| trivia.kind() == TriviaPieceKind::SingleLineComment)
+ })
+ .unwrap_or(false);
+ // if the node we have to enclose has some trailing comments, then we add a new line
+ // to the leading trivia of the right curly brace
+ if !has_trailing_single_line_comments {
r_curly_token
.with_leading_trivia(iter::once((TriviaPieceKind::Whitespace, " ")))
+ } else {
+ r_curly_token
+ .with_leading_trivia(iter::once((TriviaPieceKind::Newline, "\n")))
+ }
};
mutation.replace_node_discard_trivia(

MODIFY
crates/rome_js_analyze/src/lib.rs
crates/rome_js_analyze/src/lib.rs
@@ -112,12 +112,10 @@ mod tests {
#[test]
fn quick_test() {
const SOURCE: &str = r#"
-import AwesomeReact, { Fragment as AwesomeFragment } from "react";
-
-<>
- <AwesomeFragment>foo</AwesomeFragment>
- <AwesomeReact.Fragment>foo</AwesomeReact.Fragment>
-</>
+if (true) {
+ console.log("true");
+} else
+ console.log("false"); // comment
"#;
@@ -131,13 +129,16 @@ import AwesomeReact, { Fragment as AwesomeFragment } from "react";
|signal| {
if let Some(diag) = signal.diagnostic() {
let diag = diag.into_diagnostic(Severity::Warning);
- dbg!(&diag);
let primary = diag.primary.as_ref().unwrap();
error_ranges.push(primary.span.range);
}
- dbg!(signal.action());
+ if let Some(action) = signal.action() {
+ let new_code = action.mutation.commit();
+
+ eprintln!("{new_code}");
+ }
ControlFlow::<Never>::Continue(())
},

MODIFY
crates/rome_js_analyze/tests/specs/correctness/useBlockStatements.js
crates/rome_js_analyze/tests/specs/correctness/useBlockStatements.js
@@ -38,3 +38,7 @@ else
bar
else
bar
+
+if (test) {
+ correct;
+} else console.log("false") // comment

fix(rome_js_analyzer): add newline if statement has comments in `useBlockStatements` (#3276)