Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

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

Merged
merged 6 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions crates/rome_js_formatter/src/js/statements/switch_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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;

Expand All @@ -19,6 +19,7 @@ impl FormatNode for JsSwitchStatement {
r_curly_token,
} = self.as_fields();

let is_cases_empty = cases.clone().into_iter().len() == 0;
IWANABETHATGUY marked this conversation as resolved.
Show resolved Hide resolved
Ok(hard_group_elements(format_elements![
switch_token.format(formatter)?,
space_token(),
Expand All @@ -30,13 +31,17 @@ 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 is_cases_empty {
hard_line_break()
} else {
join_elements_hard_line(
cases
.clone()
.into_iter()
.map(|node| node.syntax().clone())
.zip(formatter.format_all(cases)?),
IWANABETHATGUY marked this conversation as resolved.
Show resolved Hide resolved
)
},
&r_curly_token?
)?
]))
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
IWANABETHATGUY marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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);

2 changes: 2 additions & 0 deletions crates/rome_js_formatter/tests/specs/js/script/with.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ with ( b)
{
5
}

with({}) {}
IWANABETHATGUY marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 5 additions & 0 deletions crates/rome_js_formatter/tests/specs/js/script/with.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rome_js_formatter/tests/spec_test.rs
assertion_line: 242
expression: with.js
---
# Input
Expand All @@ -9,6 +10,7 @@ with ( b)
5
}

with({}) {}
=============================
# Outputs
## Output 1
Expand All @@ -21,3 +23,6 @@ with (b) {
5;
}

with ({}) {
}

Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,7 +15,8 @@ switch (1) {}
switch (1) {
default:
}
switch (1) {}
switch (1) {
}

```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rome_js_formatter/tests/prettier_tests.rs
assertion_line: 175
expression: switch.js
---
# Input
Expand Down Expand Up @@ -80,11 +81,13 @@ switch (

switch (
IWANABETHATGUY marked this conversation as resolved.
Show resolved Hide resolved
$veryLongAndVeryVerboseVariableName && $anotherVeryLongAndVeryVerboseVariableName
) {}
) {
}

switch (
$longButSlightlyShorterVariableName && $anotherSlightlyShorterVariableName
) {}
) {
}

```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
interface X {}
type X = {};
IWANABETHATGUY marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -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 = {};