fix(rome_js_formatter): empty statements in `if/else` and `do/while`
MODIFY
crates/rome_js_formatter/src/js/statements/do_while_statement.rs
crates/rome_js_formatter/src/js/statements/do_while_statement.rs
use crate::prelude::*;
use crate::FormatNodeFields;
-use rome_js_syntax::JsDoWhileStatement;
use rome_js_syntax::JsDoWhileStatementFields;
+use rome_js_syntax::{JsAnyStatement, JsDoWhileStatement};
impl FormatNodeFields<JsDoWhileStatement> for FormatNodeRule<JsDoWhileStatement> {
fn format_fields(
@@ -19,7 +19,7 @@ impl FormatNodeFields<JsDoWhileStatement> for FormatNodeRule<JsDoWhileStatement>
semicolon_token,
} = node.as_fields();
- let head = formatted![formatter, [do_token.format(), space_token(),]]?;
+ let head = formatted![formatter, [do_token.format()]]?;
let tail = formatted![
formatter,
@@ -39,6 +39,11 @@ impl FormatNodeFields<JsDoWhileStatement> for FormatNodeRule<JsDoWhileStatement>
]
]?;
- formatted![formatter, [head, body.format(), tail,]]
+ let body = body?;
+ if matches!(body, JsAnyStatement::JsEmptyStatement(_)) {
+ formatted![formatter, [head, body.format(), hard_line_break(), tail,]]
+ } else {
+ formatted![formatter, [head, space_token(), body.format(), tail,]]
+ }
}
}

MODIFY
crates/rome_js_formatter/src/js/statements/empty_statement.rs
crates/rome_js_formatter/src/js/statements/empty_statement.rs
use crate::prelude::*;
use crate::FormatNodeFields;
-use rome_js_syntax::JsEmptyStatement;
-use rome_js_syntax::JsEmptyStatementFields;
+use rome_js_syntax::{JsEmptyStatement, JsEmptyStatementFields, JsSyntaxKind};
+use rome_rowan::AstNode;
impl FormatNodeFields<JsEmptyStatement> for FormatNodeRule<JsEmptyStatement> {
fn format_fields(
@@ -10,7 +10,16 @@ impl FormatNodeFields<JsEmptyStatement> for FormatNodeRule<JsEmptyStatement> {
formatter: &Formatter<JsFormatOptions>,
) -> FormatResult<FormatElement> {
let JsEmptyStatementFields { semicolon_token } = node.as_fields();
-
+ let parent_kind = node.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)
+ ) {
+ formatted![formatter, [semicolon_token.format()]]
+ } else {
Ok(formatter.format_replaced(&semicolon_token?, empty_element()))
}
}
+}

MODIFY
crates/rome_js_formatter/src/js/statements/if_statement.rs
crates/rome_js_formatter/src/js/statements/if_statement.rs
@@ -32,7 +32,6 @@ impl FormatNodeFields<JsIfStatement> for FormatNodeRule<JsIfStatement> {
[
space_token(),
else_token.format(),
- space_token(),
into_block(formatter, alternate)?,
]
]?);
@@ -76,7 +75,6 @@ fn format_if_element(
)
.soft_block_indent()
.finish()?,
- space_token(),
into_block(formatter, consequent?)?,
]
]?;
@@ -90,21 +88,19 @@ fn into_block(
stmt: JsAnyStatement,
) -> FormatResult<FormatElement> {
if matches!(stmt, JsAnyStatement::JsBlockStatement(_)) {
- return formatted![formatter, [stmt.format()]];
+ return formatted![formatter, [space_token(), stmt.format()]];
}
// If the body is an empty statement, force a line break to ensure behavior
// is coherent with `is_non_collapsable_empty_block`
if matches!(stmt, JsAnyStatement::JsEmptyStatement(_)) {
- return formatted![
- formatter,
- [token("{"), stmt.format(), hard_line_break(), token("}")]
- ];
+ return formatted![formatter, [stmt.format(), hard_line_break()]];
}
formatted![
formatter,
[
+ space_token(),
token("{"),
block_indent(formatted![formatter, [stmt.format()]]?),
token("}"),

MODIFY
crates/rome_js_formatter/tests/specs/js/module/statement/do_while.js
crates/rome_js_formatter/tests/specs/js/module/statement/do_while.js
@@ -10,3 +10,6 @@ do { // trailing
}
while (something)
+
+
+do; while(true);
\ No newline at end of file

MODIFY
crates/rome_js_formatter/tests/specs/js/module/statement/do_while.js.snap
crates/rome_js_formatter/tests/specs/js/module/statement/do_while.js.snap
---
source: crates/rome_js_formatter/tests/spec_test.rs
+assertion_line: 242
expression: do_while.js
+
---
# Input
do {
@@ -15,6 +17,9 @@ do { // trailing
}
while (something)
+
+
+do; while(true);
=============================
# Outputs
## Output 1
@@ -32,3 +37,6 @@ do {
var foo = 4;
} while (something);
+do;
+while (true);
+

MODIFY
crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js
crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js
+if(a);
+if(a); else ;
+
if (Math.random() > 0.5) {
console.log(1)
} else if (Math.random() > 0.5) {

MODIFY
crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js.snap
crates/rome_js_formatter/tests/specs/js/module/statement/if_else.js.snap
---
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,10 @@ 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) {
@@ -122,6 +131,5 @@ if (true) {
if (true) {
that();
-} else {
-}
+} else;

MODIFY
crates/rome_js_formatter/tests/specs/prettier/js/empty-statement/body.js.snap
crates/rome_js_formatter/tests/specs/prettier/js/empty-statement/body.js.snap
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
-assertion_line: 144
+assertion_line: 175
expression: body.js
---
@@ -19,15 +19,15 @@ do; while(1);
# Output
```js
with (a);
-if (1) {
-} else if (2) {
-} else {
-}
+if (1);
+else if (2);
+else;
for (;;);
while (1);
for (var i in o);
for (var i of o);
-do while (1);
+do;
+while (1);
```

fix(rome_js_formatter): empty statements in `if/else` and `do/while` (#2549)