Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(js_formatter): match default clause block handling with case clauses #1148

Merged
merged 1 commit into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions crates/biome_js_formatter/src/js/auxiliary/case_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ impl FormatNodeRule<JsCaseClause> for FormatJsCaseClause {
)?;

// Whether the first statement in the clause is a BlockStatement, and
// there are no other non-empty statements. Empties may show up
// depending on whether the input code includes certain newlines.
// there are no other non-empty statements. Empties may show up when
// parsing depending on if the input code includes certain newlines.
let is_single_block_statement = matches!(
consequent.iter().next(),
Some(AnyJsStatement::JsBlockStatement(_))
Expand Down
15 changes: 12 additions & 3 deletions crates/biome_js_formatter/src/js/auxiliary/default_clause.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ impl FormatNodeRule<JsDefaultClause> for FormatJsDefaultClause {
consequent,
} = node.as_fields();

let first_child_is_block_stmt = matches!(
// Whether the first statement in the clause is a BlockStatement, and
// there are no other non-empty statements. Empties may show up when
// parsing depending on if the input code includes certain newlines.
//
// See the comments in `case_clause.rs` for a detailed example.
let is_single_block_statement = matches!(
consequent.iter().next(),
Some(AnyJsStatement::JsBlockStatement(_))
);
) && consequent
.iter()
.filter(|statement| !matches!(statement, AnyJsStatement::JsEmptyStatement(_)))
.count()
== 1;

write!(f, [default_token.format(), colon_token.format()])?;

Expand All @@ -30,7 +39,7 @@ impl FormatNodeRule<JsDefaultClause> for FormatJsDefaultClause {
return Ok(());
}

if first_child_is_block_stmt {
if is_single_block_statement {
write!(f, [space(), consequent.format()])
} else {
// no line break needed after because it is added by the indent in the switch statement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,21 @@ switch (key) {
const a = 1;
}
break;
}


switch (key) {
default: {
const a = 1;
break;
}
}

switch (key) {
// The block is not the only statement in the case body,
// so it doesn't hug the same line as the case here.
default: {
const a = 1;
}
break;
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ switch (key) {
}
break;
}


switch (key) {
default: {
const a = 1;
break;
}
}

switch (key) {
// The block is not the only statement in the case body,
// so it doesn't hug the same line as the case here.
default: {
const a = 1;
}
break;
}
```


Expand Down Expand Up @@ -101,6 +118,23 @@ switch (key) {
}
break;
}

switch (key) {
default: {
const a = 1;
break;
}
}

switch (key) {
// The block is not the only statement in the case body,
// so it doesn't hug the same line as the case here.
default:
{
const a = 1;
}
break;
}
```