@@ -9,7 +9,10 @@ use crate::{
99 Formatter ,
1010 prelude:: * ,
1111 separated:: FormatSeparatedIter ,
12- trivia:: { FormatLeadingComments , FormatTrailingComments } ,
12+ trivia:: {
13+ DanglingIndentMode , FormatDanglingComments , FormatLeadingComments ,
14+ FormatTrailingComments ,
15+ } ,
1316 } ,
1417 generated:: ast_nodes:: { AstNode , AstNodes } ,
1518 write,
@@ -40,15 +43,27 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TryStatement<'a>> {
4043
4144impl < ' a > FormatWrite < ' a > for AstNode < ' a , CatchClause < ' a > > {
4245 fn write ( & self , f : & mut Formatter < ' _ , ' a > ) -> FormatResult < ( ) > {
43- // `try {} /* comment */ catch (e) {}`
44- // should be formatted like:
45- // `try {} catch (e) { /* comment */ }`
46- //
47- // Comments before the catch clause should be printed in the block statement.
48- // We cache them here to avoid the `params` printing them accidentally.
49- let printed_comments = f. intern ( & format_leading_comments ( self . span ) ) ;
50- if let Ok ( Some ( comments) ) = printed_comments {
51- f. context_mut ( ) . cache_element ( & self . span , comments) ;
46+ let comments = f. context ( ) . comments ( ) . comments_before ( self . span . start ) ;
47+ let has_line_comment = comments. iter ( ) . any ( |comment| {
48+ comment. is_line ( )
49+ || f. source_text ( ) . is_own_line_comment ( comment)
50+ || f. source_text ( ) . is_end_of_line_comment ( comment)
51+ } ) ;
52+
53+ if has_line_comment {
54+ // `try {} /* comment */\n catch (e) {}`
55+ // should be formatted like:
56+ // `try {} catch (e) { /* comment */ }`
57+ //
58+ // Comments before the catch clause should be printed in the block statement.
59+ // We cache them here to avoid the `params` printing them accidentally.
60+ let printed_comments = f. intern ( & FormatLeadingComments :: Comments ( comments) ) ;
61+ if let Ok ( Some ( comments) ) = printed_comments {
62+ f. context_mut ( ) . cache_element ( & self . span , comments) ;
63+ }
64+ } else if !comments. is_empty ( ) {
65+ // otherwise, print them before `catch`
66+ write ! ( f, [ FormatTrailingComments :: Comments ( comments) , space( ) ] ) ;
5267 }
5368
5469 write ! ( f, [ "catch" , space( ) , self . param( ) , space( ) ] ) ?;
0 commit comments