diff --git a/crates/rome_js_formatter/src/js/statements/do_while_statement.rs b/crates/rome_js_formatter/src/js/statements/do_while_statement.rs index d088db9a660..4770827299c 100644 --- a/crates/rome_js_formatter/src/js/statements/do_while_statement.rs +++ b/crates/rome_js_formatter/src/js/statements/do_while_statement.rs @@ -1,4 +1,4 @@ -use crate::format_traits::FormatOptional; +use crate::format_traits::{FormatOptional, FormatWith}; use rome_formatter::FormatResult; use crate::{ @@ -21,7 +21,7 @@ impl FormatNode for JsDoWhileStatement { semicolon_token, } = self.as_fields(); - let head = format_elements![do_token.format(formatter)?, space_token(),]; + let head = do_token.format_with(formatter, |e| format_elements![e, space_token()])?; let tail = format_elements![ space_token(), @@ -42,6 +42,11 @@ impl FormatNode for JsDoWhileStatement { body.format(formatter)?, tail, ])) + } else if matches!(body, JsAnyStatement::JsEmptyStatement(_)) { + Ok(format_elements![ + hard_group_elements(format_elements![head, body.format(formatter)?,]), + tail, + ]) } else { Ok(format_elements![ hard_group_elements(head), diff --git a/crates/rome_js_formatter/src/js/statements/empty_statement.rs b/crates/rome_js_formatter/src/js/statements/empty_statement.rs index ac2344f8164..586747f5542 100644 --- a/crates/rome_js_formatter/src/js/statements/empty_statement.rs +++ b/crates/rome_js_formatter/src/js/statements/empty_statement.rs @@ -1,13 +1,23 @@ use crate::{empty_element, FormatElement, FormatNode, Formatter}; -use rome_formatter::FormatResult; - -use rome_js_syntax::JsEmptyStatement; +use rome_formatter::{format_elements, hard_line_break, token, FormatResult}; use rome_js_syntax::JsEmptyStatementFields; +use rome_js_syntax::{JsEmptyStatement, JsSyntaxKind}; +use rome_rowan::AstNode; impl FormatNode for JsEmptyStatement { fn format_fields(&self, formatter: &Formatter) -> FormatResult { let JsEmptyStatementFields { semicolon_token } = self.as_fields(); - - Ok(formatter.format_replaced(&semicolon_token?, empty_element())) + let parent_kind = self.syntax().parent().map(|p| p.kind()); + if matches!( + parent_kind, + Some(JsSyntaxKind::JS_DO_WHILE_STATEMENT) + | Some(JsSyntaxKind::JS_IF_STATEMENT) + | Some(JsSyntaxKind::JS_ELSE_CLAUSE) + ) { + let new_body = format_elements![token("{"), hard_line_break(), token("}")]; + Ok(formatter.format_replaced(&semicolon_token?, new_body)) + } else { + Ok(formatter.format_replaced(&semicolon_token?, empty_element())) + } } } diff --git a/crates/rome_js_formatter/src/js/statements/if_statement.rs b/crates/rome_js_formatter/src/js/statements/if_statement.rs index e3783338807..a87d6c76e7b 100644 --- a/crates/rome_js_formatter/src/js/statements/if_statement.rs +++ b/crates/rome_js_formatter/src/js/statements/if_statement.rs @@ -1,12 +1,7 @@ use crate::format_traits::FormatOptional; -use crate::{ - block_indent, concat_elements, group_elements, hard_group_elements, hard_line_break, token, - Format, -}; -use rome_formatter::FormatResult; - +use crate::{block_indent, concat_elements, group_elements, hard_group_elements, token, Format}; use crate::{format_elements, space_token, FormatElement, FormatNode, Formatter}; - +use rome_formatter::FormatResult; use rome_js_syntax::JsSyntaxToken; use rome_js_syntax::{JsAnyStatement, JsElseClauseFields, JsIfStatement}; use rome_js_syntax::{JsElseClause, JsIfStatementFields}; @@ -88,12 +83,7 @@ fn into_block(formatter: &Formatter, stmt: JsAnyStatement) -> FormatResult 0.5) { console.log(1) } else if (Math.random() > 0.5) { diff --git a/crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js.snap b/crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js.snap index bfce207d498..397f1cbe02f 100644 --- a/crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js.snap +++ b/crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js.snap @@ -1,8 +1,13 @@ --- source: crates/rome_js_formatter/tests/spec_test.rs +assertion_line: 242 expression: if_else.js + --- # Input +if(a); +if(a); else ; + if (Math.random() > 0.5) { console.log(1) } else if (Math.random() > 0.5) { @@ -71,6 +76,12 @@ Indent style: Tab Line width: 80 Quote style: Double Quotes ----- +if (a) { +} +if (a) { +} else { +} + if (Math.random() > 0.5) { console.log(1); } else if (Math.random() > 0.5) { diff --git a/crates/rome_js_formatter/tests/specs/prettier/js/empty-statement/body.js.snap b/crates/rome_js_formatter/tests/specs/prettier/js/empty-statement/body.js.snap index 4010b708008..67e58f31b0b 100644 --- a/crates/rome_js_formatter/tests/specs/prettier/js/empty-statement/body.js.snap +++ b/crates/rome_js_formatter/tests/specs/prettier/js/empty-statement/body.js.snap @@ -1,6 +1,6 @@ --- source: crates/rome_js_formatter/tests/prettier_tests.rs -assertion_line: 144 +assertion_line: 175 expression: body.js --- @@ -27,7 +27,8 @@ for (;;); while (1); for (var i in o); for (var i of o); -do while (1); +do { +} while (1); ```