Skip to content

Commit 57d0d1b

Browse files
authored
fix: keep braces on if & while statements when block body contains single decl (#513)
1 parent 7c81446 commit 57d0d1b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/generation/generate.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8435,7 +8435,9 @@ fn gen_conditional_brace_body<'a>(opts: GenConditionalBraceBodyOptions<'a>, cont
84358435

84368436
fn get_force_braces(body_node: Node) -> bool {
84378437
if let Node::BlockStmt(body_node) = body_node {
8438-
body_node.stmts.is_empty() || body_node.stmts.iter().all(|s| s.kind() == NodeKind::EmptyStmt)
8438+
body_node.stmts.is_empty()
8439+
|| body_node.stmts.iter().all(|s| s.kind() == NodeKind::EmptyStmt)
8440+
|| (body_node.stmts.len() == 1 && matches!(body_node.stmts[0], Stmt::Decl(_)))
84398441
} else {
84408442
false
84418443
}

tests/specs/issues/issue0510.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
== should not remove braces in an if/while statement when the body contains a declaration ==
2+
if (true) { const _err = 5; }
3+
while (true) { const _err = 5; }
4+
if (true) { class Test {} }
5+
while (true) { class Test {} }
6+
7+
[expect]
8+
if (true) { const _err = 5; }
9+
while (true) { const _err = 5; }
10+
if (true) { class Test {} }
11+
while (true) { class Test {} }

0 commit comments

Comments
 (0)