@@ -656,7 +656,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, ForStatement<'a>> {
656656 [
657657 FormatDanglingComments :: Comments {
658658 comments,
659- indent: DanglingIndentMode :: None ,
659+ indent: DanglingIndentMode :: None
660660 } ,
661661 soft_line_break_or_space( )
662662 ]
@@ -777,8 +777,10 @@ impl<'a> FormatWrite<'a> for AstNode<'a, IfStatement<'a>> {
777777 }
778778
779779 if has_dangling_comments {
780- FormatDanglingComments :: Comments { comments, indent : DanglingIndentMode :: None }
781- . fmt ( f) ?;
780+ write ! (
781+ f,
782+ FormatDanglingComments :: Comments { comments, indent: DanglingIndentMode :: None }
783+ ) ?;
782784
783785 if has_line_comment {
784786 write ! ( f, hard_line_break( ) ) ?;
@@ -999,7 +1001,18 @@ impl<'a> FormatWrite<'a> for AstNode<'a, BindingPattern<'a>> {
9991001
10001002impl < ' a > FormatWrite < ' a > for AstNode < ' a , AssignmentPattern < ' a > > {
10011003 fn write ( & self , f : & mut Formatter < ' _ , ' a > ) -> FormatResult < ( ) > {
1002- write ! ( f, [ self . left( ) , space( ) , "=" , space( ) , self . right( ) ] )
1004+ let comments = f. context ( ) . comments ( ) . own_line_comments_before ( self . right . span ( ) . start ) ;
1005+ write ! (
1006+ f,
1007+ [
1008+ FormatLeadingComments :: Comments ( comments) ,
1009+ self . left( ) ,
1010+ space( ) ,
1011+ "=" ,
1012+ space( ) ,
1013+ self . right( ) ,
1014+ ]
1015+ )
10031016 }
10041017}
10051018
@@ -1077,6 +1090,11 @@ impl<'a> FormatWrite<'a> for AstNode<'a, BindingRestElement<'a>> {
10771090
10781091impl < ' a > FormatWrite < ' a > for AstNode < ' a , FormalParameters < ' a > > {
10791092 fn write ( & self , f : & mut Formatter < ' _ , ' a > ) -> FormatResult < ( ) > {
1093+ let comments = f. context ( ) . comments ( ) . comments_before ( self . span . start ) ;
1094+ if !comments. is_empty ( ) {
1095+ write ! ( f, [ space( ) , FormatTrailingComments :: Comments ( comments) ] ) ?;
1096+ }
1097+
10801098 let parentheses_not_needed = if let AstNodes :: ArrowFunctionExpression ( arrow) = self . parent {
10811099 can_avoid_parentheses ( arrow, f)
10821100 } else {
@@ -1259,15 +1277,24 @@ impl<'a> FormatWrite<'a> for AstNode<'a, MethodDefinition<'a>> {
12591277 if let Some ( type_parameters) = & self . value ( ) . type_parameters ( ) {
12601278 write ! ( f, type_parameters) ?;
12611279 }
1262- write ! ( f, group( & self . value( ) . params( ) ) ) ?;
1263- if let Some ( return_type) = & self . value ( ) . return_type ( ) {
1280+ let value = self . value ( ) ;
1281+ let comments = f. context ( ) . comments ( ) . comments_before ( value. params ( ) . span . start ) ;
1282+ if !comments. is_empty ( ) {
1283+ write ! ( f, [ space( ) , FormatTrailingComments :: Comments ( comments) ] ) ?;
1284+ }
1285+ write ! ( f, group( & value. params( ) ) ) ?;
1286+ if let Some ( return_type) = & value. return_type ( ) {
12641287 write ! ( f, return_type) ?;
12651288 }
1266- if let Some ( body) = & self . value ( ) . body ( ) {
1289+ if let Some ( body) = & value. body ( ) {
1290+ let comments = f. context ( ) . comments ( ) . block_comments_before ( body. span . start ) ;
1291+ if !comments. is_empty ( ) {
1292+ write ! ( f, [ space( ) , FormatLeadingComments :: Comments ( comments) ] ) ?;
1293+ }
12671294 write ! ( f, [ space( ) , body] ) ?;
12681295 }
12691296 if self . r#type ( ) . is_abstract ( )
1270- || matches ! ( self . value( ) . r#type( ) , FunctionType :: TSEmptyBodyFunctionExpression )
1297+ || matches ! ( value. r#type, FunctionType :: TSEmptyBodyFunctionExpression )
12711298 {
12721299 write ! ( f, OptionalSemicolon ) ?;
12731300 }
0 commit comments