From 2a503e265fc06d02e1f32c266ceb1ee88c15788f Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Fri, 6 May 2022 17:32:50 +0800 Subject: [PATCH] fix(rome_js_formatter): Put closing curly on new line for empty blocks. (#2540) Fix #2406 --- .../src/js/statements/switch_statement.rs | 21 ++--- .../specs/js/module/statement/empty_blocks.js | 30 +++++++ .../js/module/statement/empty_blocks.js.snap | 81 +++++++++++++++++++ .../tests/specs/js/script/with.js | 2 + .../tests/specs/js/script/with.js.snap | 5 ++ .../prettier/js/switch/empty_switch.js.snap | 6 +- .../specs/prettier/js/switch/switch.js.snap | 7 +- .../tests/specs/ts/statement/empty_block.ts | 2 + .../specs/ts/statement/empty_block.ts.snap | 19 +++++ 9 files changed, 159 insertions(+), 14 deletions(-) create mode 100644 crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js create mode 100644 crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js.snap create mode 100644 crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts create mode 100644 crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts.snap diff --git a/crates/rome_js_formatter/src/js/statements/switch_statement.rs b/crates/rome_js_formatter/src/js/statements/switch_statement.rs index 7fb5a4ea15a..0b4ad695363 100644 --- a/crates/rome_js_formatter/src/js/statements/switch_statement.rs +++ b/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 { @@ -30,13 +30,16 @@ impl FormatNode for JsSwitchStatement { space_token(), formatter.format_delimited_block_indent( &l_curly_token?, - join_elements_hard_line( - cases - .clone() - .into_iter() - .map(|node| node.syntax().clone()) - .zip(formatter.format_all(cases)?) - ), + if cases.is_empty() { + hard_line_break() + } else { + join_elements_hard_line( + cases + .iter() + .map(|node| node.syntax().clone()) + .zip(formatter.format_all(cases)?), + ) + }, &r_curly_token? )? ])) diff --git a/crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js b/crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js new file mode 100644 index 00000000000..b2f57e48b79 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js @@ -0,0 +1,30 @@ +// 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 diff --git a/crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js.snap b/crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js.snap new file mode 100644 index 00000000000..d8b1219c9de --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/js/module/statement/empty_blocks.js.snap @@ -0,0 +1,81 @@ +--- +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); + diff --git a/crates/rome_js_formatter/tests/specs/js/script/with.js b/crates/rome_js_formatter/tests/specs/js/script/with.js index 886941f0b25..e801c82b5db 100644 --- a/crates/rome_js_formatter/tests/specs/js/script/with.js +++ b/crates/rome_js_formatter/tests/specs/js/script/with.js @@ -3,3 +3,5 @@ with ( b) { 5 } + +with({}) {} \ No newline at end of file diff --git a/crates/rome_js_formatter/tests/specs/js/script/with.js.snap b/crates/rome_js_formatter/tests/specs/js/script/with.js.snap index ecce29770e5..5e94f343204 100644 --- a/crates/rome_js_formatter/tests/specs/js/script/with.js.snap +++ b/crates/rome_js_formatter/tests/specs/js/script/with.js.snap @@ -1,5 +1,6 @@ --- 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 ({}) { +} + diff --git a/crates/rome_js_formatter/tests/specs/prettier/js/switch/empty_switch.js.snap b/crates/rome_js_formatter/tests/specs/prettier/js/switch/empty_switch.js.snap index 64e0c3938ae..63b817b73be 100644 --- a/crates/rome_js_formatter/tests/specs/prettier/js/switch/empty_switch.js.snap +++ b/crates/rome_js_formatter/tests/specs/prettier/js/switch/empty_switch.js.snap @@ -1,8 +1,7 @@ --- 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) { +} ``` diff --git a/crates/rome_js_formatter/tests/specs/prettier/js/switch/switch.js.snap b/crates/rome_js_formatter/tests/specs/prettier/js/switch/switch.js.snap index 7eb68c31f5f..8f7767edf29 100644 --- a/crates/rome_js_formatter/tests/specs/prettier/js/switch/switch.js.snap +++ b/crates/rome_js_formatter/tests/specs/prettier/js/switch/switch.js.snap @@ -1,5 +1,6 @@ --- 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 -) {} +) { +} ``` diff --git a/crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts b/crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts new file mode 100644 index 00000000000..aaf7b4fdd22 --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts @@ -0,0 +1,2 @@ +interface X {} +type X = {}; \ No newline at end of file diff --git a/crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts.snap b/crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts.snap new file mode 100644 index 00000000000..c8495b5c9eb --- /dev/null +++ b/crates/rome_js_formatter/tests/specs/ts/statement/empty_block.ts.snap @@ -0,0 +1,19 @@ +--- +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 = {}; +