Skip to content

Commit 4406be7

Browse files
authored
fix: else was moved to wrong if sometimes when formatting minified code (#633)
1 parent ea1a301 commit 4406be7

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

src/generation/generate.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8294,7 +8294,7 @@ fn gen_control_flow_separator(
82948294
}
82958295

82968296
struct GenHeaderWithConditionalBraceBodyOptions<'a> {
8297-
body_node: Node<'a>,
8297+
body_node: Stmt<'a>,
82988298
generated_header: PrintItems,
82998299
use_braces: UseBraces,
83008300
brace_position: BracePosition,
@@ -8324,8 +8324,12 @@ fn gen_header_with_conditional_brace_body<'a>(
83248324
items.push_info(end_header_ln);
83258325
let result = gen_conditional_brace_body(
83268326
GenConditionalBraceBodyOptions {
8327-
body_node: opts.body_node,
8328-
use_braces: opts.use_braces,
8327+
body_node: opts.body_node.into(),
8328+
use_braces: if force_use_braces_for_stmt(opts.body_node) {
8329+
UseBraces::Always
8330+
} else {
8331+
opts.use_braces
8332+
},
83298333
brace_position: opts.brace_position,
83308334
single_body_position: opts.single_body_position,
83318335
requires_braces_condition_ref: opts.requires_braces_condition_ref,
@@ -8343,6 +8347,32 @@ fn gen_header_with_conditional_brace_body<'a>(
83438347
}
83448348
}
83458349

8350+
fn force_use_braces_for_stmt(stmt: Stmt) -> bool {
8351+
match stmt {
8352+
Stmt::Block(block) => {
8353+
if block.stmts.len() != 1 {
8354+
true
8355+
} else {
8356+
force_use_braces_for_stmt(block.stmts[0])
8357+
}
8358+
}
8359+
// force braces for any children where no braces could be ambiguous
8360+
Stmt::Empty(_)
8361+
| Stmt::DoWhile(_)
8362+
| Stmt::For(_)
8363+
| Stmt::ForIn(_)
8364+
| Stmt::ForOf(_)
8365+
| Stmt::Decl(_)
8366+
| Stmt::If(_) // especially force for this as it may cause a bug
8367+
| Stmt::Labeled(_)
8368+
| Stmt::Switch(_)
8369+
| Stmt::Try(_)
8370+
| Stmt::While(_)
8371+
| Stmt::With(_) => true,
8372+
Stmt::Break(_) | Stmt::Continue(_) | Stmt::Debugger(_) | Stmt::Expr(_) | Stmt::Return(_) | Stmt::Throw(_) => false,
8373+
}
8374+
}
8375+
83468376
struct GenConditionalBraceBodyOptions<'a> {
83478377
body_node: Node<'a>,
83488378
use_braces: UseBraces,

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#![allow(clippy::if_same_then_else)]
55
#![allow(clippy::vec_init_then_push)]
66
#![allow(clippy::type_complexity)]
7-
#![allow(clippy::needless_lifetimes)] // todo: enable this in the future
7+
#![allow(clippy::needless_lifetimes)]
88
#![deny(clippy::disallowed_methods)]
99
#![deny(clippy::disallowed_types)]
1010
#![deny(clippy::print_stderr)]

tests/specs/issues/issue0632.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
== should maintain if stmt ==
2+
if(a){if(b){console.log(0);}}else if(d){console.log(1);}else{console.log(2);}
3+
4+
[expect]
5+
if (a) { if (b) console.log(0); }
6+
else if (d) console.log(1);
7+
else console.log(2);

0 commit comments

Comments
 (0)