@@ -8294,7 +8294,7 @@ fn gen_control_flow_separator(
8294
8294
}
8295
8295
8296
8296
struct GenHeaderWithConditionalBraceBodyOptions < ' a > {
8297
- body_node : Node < ' a > ,
8297
+ body_node : Stmt < ' a > ,
8298
8298
generated_header : PrintItems ,
8299
8299
use_braces : UseBraces ,
8300
8300
brace_position : BracePosition ,
@@ -8324,8 +8324,12 @@ fn gen_header_with_conditional_brace_body<'a>(
8324
8324
items. push_info ( end_header_ln) ;
8325
8325
let result = gen_conditional_brace_body (
8326
8326
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
+ } ,
8329
8333
brace_position : opts. brace_position ,
8330
8334
single_body_position : opts. single_body_position ,
8331
8335
requires_braces_condition_ref : opts. requires_braces_condition_ref ,
@@ -8343,6 +8347,32 @@ fn gen_header_with_conditional_brace_body<'a>(
8343
8347
}
8344
8348
}
8345
8349
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
+
8346
8376
struct GenConditionalBraceBodyOptions < ' a > {
8347
8377
body_node : Node < ' a > ,
8348
8378
use_braces : UseBraces ,
0 commit comments