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

Commit

Permalink
fix(rome_js_formatter): Put closing curly on new line for empty block…
Browse files Browse the repository at this point in the history
…s. (#2540)

Fix #2406
  • Loading branch information
IWANABETHATGUY authored May 6, 2022
1 parent 7a24b21 commit 2a503e2
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 14 deletions.
21 changes: 12 additions & 9 deletions crates/rome_js_formatter/src/js/statements/switch_statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -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?
)?
]))
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);
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({}) {}
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 (
$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 = {};
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 = {};

0 comments on commit 2a503e2

Please sign in to comment.