@@ -52,29 +52,33 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> {
5252 union_type_at_top = parent;
5353 }
5454
55- let should_indent = {
55+ let should_indent = !has_leading_comments && {
5656 let parent = union_type_at_top. parent ;
5757
5858 // These parents have indent for their content, so we don't need to indent here
59- !match parent {
60- AstNodes :: TSTypeAliasDeclaration ( _) => has_leading_comments,
59+ match parent {
60+ AstNodes :: TSTypeAliasDeclaration ( alias) => {
61+ !f. comments ( ) . printed_comments ( ) . last ( ) . is_some_and ( |comment| {
62+ comment. span . start
63+ > alias. type_parameters ( ) . map_or ( alias. id . span . end , |tp| tp. span . end )
64+ && f. comments ( ) . is_end_of_line_comment ( comment)
65+ } )
66+ }
6167 AstNodes :: TSTypeAssertion ( _)
6268 | AstNodes :: TSTupleType ( _)
63- | AstNodes :: TSTypeParameterInstantiation ( _) => true ,
64- _ => false ,
69+ | AstNodes :: TSTypeParameterInstantiation ( _) => false ,
70+ _ => true ,
6571 }
6672 } ;
6773
6874 let types = format_with ( |f| {
69- let suppressed_node_span = if f. comments ( ) . is_suppressed ( self . span . start ) {
70- self . types . first ( ) . unwrap ( ) . span ( )
71- } else {
72- Span :: default ( )
73- } ;
75+ let is_suppressed = leading_comments
76+ . iter ( )
77+ . rev ( )
78+ . any ( |comment| f. comments ( ) . is_suppression_comment ( comment) ) ;
7479
75- if has_leading_comments {
76- write ! ( f, FormatLeadingComments :: Comments ( leading_comments) ) ;
77- }
80+ let suppressed_node_span =
81+ if is_suppressed { self . types . first ( ) . unwrap ( ) . span ( ) } else { Span :: default ( ) } ;
7882
7983 let leading_soft_line_break_or_space = should_indent && !has_leading_comments;
8084
@@ -123,7 +127,24 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> {
123127 }
124128 } ) ;
125129
126- write ! ( f, [ group( & content) ] ) ;
130+ if has_leading_comments {
131+ let has_own_line_leading_comment = union_type_at_top. types . len ( ) > 1
132+ && leading_comments. iter ( ) . any ( |comment| f. comments ( ) . is_own_line_comment ( comment) ) ;
133+ let is_end_of_line_comment = leading_comments
134+ . last ( )
135+ . is_some_and ( |comment| f. comments ( ) . is_end_of_line_comment ( comment) ) ;
136+ write ! (
137+ f,
138+ [ group( & indent( & format_args!(
139+ has_own_line_leading_comment. then( soft_line_break) ,
140+ FormatLeadingComments :: Comments ( leading_comments) ,
141+ ( !is_end_of_line_comment) . then( soft_line_break) ,
142+ group( & content)
143+ ) ) ) ]
144+ ) ;
145+ } else {
146+ write ! ( f, [ group( & content) ] ) ;
147+ }
127148 }
128149}
129150
0 commit comments