@@ -13,7 +13,6 @@ use crate::{
1313 ast_nodes:: { AstNode , AstNodes } ,
1414 formatter:: { FormatResult , Formatter , trivia:: FormatTrailingComments } ,
1515 parentheses:: NeedsParentheses ,
16- utils:: format_node_without_trailing_comments:: FormatNodeWithoutTrailingComments ,
1716} ;
1817
1918use crate :: { format_args, formatter:: prelude:: * , write} ;
@@ -294,8 +293,6 @@ enum BinaryLeftOrRightSide<'a, 'b> {
294293 parent : BinaryLikeExpression < ' a , ' b > ,
295294 /// Is the parent the condition of a `if` / `while` / `do-while` / `for` statement?
296295 inside_condition : bool ,
297- /// It is the root of the expression.
298- root : bool ,
299296 } ,
300297}
301298
@@ -306,7 +303,6 @@ impl<'a> Format<'a> for BinaryLeftOrRightSide<'a, '_> {
306303 Self :: Right {
307304 parent : binary_like_expression,
308305 inside_condition : inside_parenthesis,
309- root,
310306 } => {
311307 let mut binary_like_expression = * binary_like_expression;
312308 // // It's only possible to suppress the formatting of the whole binary expression formatting OR
@@ -401,16 +397,7 @@ impl<'a> Format<'a> for BinaryLeftOrRightSide<'a, '_> {
401397 write ! ( f, [ soft_line_break_or_space( ) ] ) ?;
402398 }
403399
404- if * root {
405- write ! ( f, FormatNodeWithoutTrailingComments ( right) ) ?;
406- let comments = f
407- . context ( )
408- . comments ( )
409- . comments_before ( binary_like_expression. span ( ) . end ) ;
410- write ! ( f, FormatTrailingComments :: Comments ( comments) )
411- } else {
412- write ! ( f, right)
413- }
400+ write ! ( f, right)
414401 } ) ;
415402
416403 // Doesn't match prettier that only distinguishes between logical and binary
@@ -425,6 +412,16 @@ impl<'a> Format<'a> for BinaryLeftOrRightSide<'a, '_> {
425412 right. as_ast_nodes ( ) ,
426413 ) || ( * inside_parenthesis && logical_operator. is_some ( ) ) ) ;
427414
415+ match binary_like_expression. left ( ) . as_ast_nodes ( ) {
416+ AstNodes :: LogicalExpression ( logical) => {
417+ logical. format_trailing_comments ( f) ?;
418+ }
419+ AstNodes :: BinaryExpression ( binary) => {
420+ binary. format_trailing_comments ( f) ?;
421+ }
422+ _ => { }
423+ }
424+
428425 if should_group {
429426 // `left` side has printed before `right` side, so that trailing comments of `left` side has been printed,
430427 // so we need to find if there are any printed comments that are after the `left` side and it is line comment.
@@ -489,7 +486,6 @@ fn split_into_left_and_right_sides<'a, 'b>(
489486 inside_condition : bool ,
490487) -> Vec < BinaryLeftOrRightSide < ' a , ' b > > {
491488 fn split_into_left_and_right_sides_inner < ' a , ' b > (
492- is_root : bool ,
493489 binary : BinaryLikeExpression < ' a , ' b > ,
494490 inside_condition : bool ,
495491 items : & mut Vec < BinaryLeftOrRightSide < ' a , ' b > > ,
@@ -500,7 +496,6 @@ fn split_into_left_and_right_sides<'a, 'b>(
500496 // We can flatten the left hand side, so we need to check if we have a nested binary expression
501497 // that we can flatten.
502498 split_into_left_and_right_sides_inner (
503- false ,
504499 // SAFETY: `left` is guaranteed to be a valid binary like expression in `can_flatten()`.
505500 BinaryLikeExpression :: try_from ( left) . unwrap ( ) ,
506501 inside_condition,
@@ -510,19 +505,15 @@ fn split_into_left_and_right_sides<'a, 'b>(
510505 items. push ( BinaryLeftOrRightSide :: Left { parent : binary } ) ;
511506 }
512507
513- items. push ( BinaryLeftOrRightSide :: Right {
514- parent : binary,
515- inside_condition,
516- root : is_root,
517- } ) ;
508+ items. push ( BinaryLeftOrRightSide :: Right { parent : binary, inside_condition } ) ;
518509 }
519510
520511 // Stores the left and right parts of the binary expression in sequence (rather than nested as they
521512 // appear in the tree).
522513 // `with_capacity(2)` because we expect at least 2 items (left and right).
523514 let mut items = Vec :: with_capacity ( 2 ) ;
524515
525- split_into_left_and_right_sides_inner ( true , binary, inside_condition, & mut items) ;
516+ split_into_left_and_right_sides_inner ( binary, inside_condition, & mut items) ;
526517
527518 items
528519}
0 commit comments