1
1
use std:: ops:: Deref ;
2
2
3
3
use oxc_ast:: ast:: * ;
4
+ use oxc_span:: GetSpan ;
4
5
5
6
use crate :: {
6
7
JsLabels , format_args,
7
8
formatter:: {
8
- Buffer , Format , FormatResult , Formatter , prelude:: * , trivia:: format_dangling_comments,
9
+ Buffer , Format , FormatResult , Formatter ,
10
+ buffer:: RemoveSoftLinesBuffer ,
11
+ prelude:: * ,
12
+ trivia:: {
13
+ DanglingIndentMode , FormatDanglingComments , FormatLeadingComments ,
14
+ FormatTrailingComments , format_dangling_comments,
15
+ } ,
9
16
} ,
10
17
generated:: ast_nodes:: { AstNode , AstNodes } ,
11
18
options:: Expand ,
@@ -30,7 +37,14 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticMemberExpression<'a>> {
30
37
recording. stop ( ) . has_label ( LabelId :: of ( JsLabels :: MemberChain ) )
31
38
} ;
32
39
33
- match layout ( self , is_member_chain) {
40
+ let comments = f. context ( ) . comments ( ) . block_comments_before ( self . property . span . start ) ;
41
+ if !comments. is_empty ( ) {
42
+ write ! ( f, [ space( ) ] ) ?;
43
+ f. context_mut ( ) . comments_mut ( ) . increase_printed_count_by ( comments. len ( ) ) ;
44
+ f. join_with ( space ( ) ) . entries ( comments. iter ( ) ) . finish ( ) ?;
45
+ }
46
+
47
+ match layout ( self , is_member_chain, f) {
34
48
StaticMemberLayout :: NoBreak => {
35
49
let format_no_break =
36
50
format_with ( |f| write ! ( f, [ operator_token( self . optional( ) ) , self . property( ) ] ) ) ;
@@ -46,6 +60,17 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticMemberExpression<'a>> {
46
60
f,
47
61
[ group( & indent( & format_args!(
48
62
soft_line_break( ) ,
63
+ & format_once( |f| {
64
+ let comments =
65
+ f. context( ) . comments( ) . comments_before( self . property. span. start) ;
66
+ if !comments. is_empty( ) {
67
+ write!(
68
+ f,
69
+ [ FormatLeadingComments :: Comments ( comments) , soft_line_break( ) ]
70
+ ) ?;
71
+ }
72
+ Ok ( ( ) )
73
+ } ) ,
49
74
operator_token( self . optional( ) ) ,
50
75
self . property( ) ,
51
76
) ) ) ]
@@ -71,7 +96,15 @@ fn operator_token(optional: bool) -> &'static str {
71
96
fn layout < ' a > (
72
97
node : & AstNode < ' a , StaticMemberExpression < ' a > > ,
73
98
is_member_chain : bool ,
99
+ f : & Formatter < ' _ , ' a > ,
74
100
) -> StaticMemberLayout {
101
+ if f. comments ( )
102
+ . comments_before_iter ( node. property . span . start )
103
+ . any ( |c| f. source_text ( ) . is_own_line_comment ( c) )
104
+ {
105
+ return StaticMemberLayout :: BreakAfterObject ;
106
+ }
107
+
75
108
// `a.b.c!` and `a.b?.c`
76
109
// `TSNonNullExpression` is a wrapper node for `!`, and `ChainExpression` is a wrapper node for `?.`,
77
110
// so we need to skip them to find the real parent node.
0 commit comments