@@ -8294,7 +8294,7 @@ fn gen_control_flow_separator(
82948294}
82958295
82968296struct 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+
83468376struct GenConditionalBraceBodyOptions < ' a > {
83478377 body_node : Node < ' a > ,
83488378 use_braces : UseBraces ,
0 commit comments