fix(rome_js_formatter): Put closing curly on new line for empty blocks.
Fix
MODIFY
crates/rome_js_formatter/src/js/statements/switch_statement.rs
crates/rome_js_formatter/src/js/statements/switch_statement.rs
@@ -3,9 +3,9 @@ use crate::{
format_elements, join_elements_hard_line, space_token, Format, FormatElement, FormatNode,
Formatter, JsFormatter,
};
-use rome_formatter::FormatResult;
+use rome_formatter::{hard_line_break, FormatResult};
use rome_js_syntax::{JsSwitchStatement, JsSwitchStatementFields};
-use rome_rowan::AstNode;
+use rome_rowan::{AstNode, AstNodeList};
impl FormatNode for JsSwitchStatement {
fn format_fields(&self, formatter: &Formatter) -> FormatResult<FormatElement> {
@@ -30,13 +30,16 @@ impl FormatNode for JsSwitchStatement {
space_token(),
formatter.format_delimited_block_indent(
&l_curly_token?,
+ if cases.is_empty() {
+ hard_line_break()
+ } else {
join_elements_hard_line(
cases
- .clone()
- .into_iter()
+ .iter()
.map(|node| node.syntax().clone())
- .zip(formatter.format_all(cases)?)
- ),
+ .zip(formatter.format_all(cases)?),
+ )
+ },
&r_curly_token?
)?
]))

ADD
crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js
+// Line break before closing `}`
+if (true) {}
+if (true) {} else {}
+
+for (x in []) {}
+for (x of []) {}
+
+
+
+switch ("test") {}
+
+switch ("test") {
+ case "test": {}
+}
+
+test: {}
+
+try {
+} catch {
+} finally {
+}
+
+// No Line breaks
+class Test {}
+
+function test() {}
+
+for (;;) {}
+while (true) {}
+do {} while (true);
\ No newline at end of file

ADD
crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js.snap
+---
+source: crates/rome_js_formatter/tests/spec_test.rs
+assertion_line: 242
+expression: empty_blocks.js
+---
+# Input
+// Line break before closing `}`
+if (true) {}
+if (true) {} else {}
+
+for (x in []) {}
+for (x of []) {}
+
+
+
+switch ("test") {}
+
+switch ("test") {
+ case "test": {}
+}
+
+test: {}
+
+try {
+} catch {
+} finally {
+}
+
+// No Line breaks
+class Test {}
+
+function test() {}
+
+for (;;) {}
+while (true) {}
+do {} while (true);
+=============================
+# Outputs
+## Output 1
+-----
+Indent style: Tab
+Line width: 80
+Quote style: Double Quotes
+-----
+// Line break before closing `}`
+if (true) {
+}
+if (true) {
+} else {
+}
+
+for (x in []) {
+}
+for (x of []) {
+}
+
+switch ("test") {
+}
+
+switch ("test") {
+ case "test": {
+ }
+}
+
+test: {
+}
+
+try {
+} catch {
+} finally {
+}
+
+// No Line breaks
+class Test {}
+
+function test() {}
+
+for (;;) {}
+while (true) {}
+do {} while (true);
+

MODIFY
crates/rome_js_formatter/tests/specs/js/script/with.js
crates/rome_js_formatter/tests/specs/js/script/with.js
@@ -3,3 +3,5 @@ with ( b)
{
5
}
+
+with({}) {}
\ No newline at end of file

MODIFY
crates/rome_js_formatter/tests/specs/js/script/with.js.snap
crates/rome_js_formatter/tests/specs/js/script/with.js.snap
---
source: crates/rome_js_formatter/tests/spec_test.rs
+assertion_line: 242
expression: with.js
---
# Input
@@ -9,6 +10,7 @@ with ( b)
5
}
+with({}) {}
=============================
# Outputs
## Output 1
@@ -21,3 +23,6 @@ with (b) {
5;
}
+with ({}) {
+}
+

MODIFY
crates/rome_js_formatter/tests/specs/prettier/js/switch/empty_switch.js.snap
crates/rome_js_formatter/tests/specs/prettier/js/switch/empty_switch.js.snap
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
-assertion_line: 57
+assertion_line: 175
expression: empty_switch.js
-
---
# Input
```js
@@ -16,7 +15,8 @@ switch (1) {}
switch (1) {
default:
}
-switch (1) {}
+switch (1) {
+}
```

MODIFY
crates/rome_js_formatter/tests/specs/prettier/js/switch/switch.js.snap
crates/rome_js_formatter/tests/specs/prettier/js/switch/switch.js.snap
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
+assertion_line: 175
expression: switch.js
---
# Input
@@ -80,11 +81,13 @@ switch (
switch (
$veryLongAndVeryVerboseVariableName && $anotherVeryLongAndVeryVerboseVariableName
-) {}
+) {
+}
switch (
$longButSlightlyShorterVariableName && $anotherSlightlyShorterVariableName
-) {}
+) {
+}
```

ADD
crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts
+interface X {}
+type X = {};
\ No newline at end of file

ADD
crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts.snap
+---
+source: crates/rome_js_formatter/tests/spec_test.rs
+assertion_line: 242
+expression: empty_block.ts
+---
+# Input
+interface X {}
+type X = {};
+=============================
+# Outputs
+## Output 1
+-----
+Indent style: Tab
+Line width: 80
+Quote style: Double Quotes
+-----
+interface X {}
+type X = {};
+

fix(rome_js_formatter): Put closing curly on new line for empty blocks. (#2540)

Fix #2406