11use std:: ops:: Deref ;
22
33use oxc_ast:: ast:: * ;
4+ use oxc_span:: GetSpan ;
45
56use crate :: {
67 JsLabels , format_args,
78 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+ } ,
916 } ,
1017 generated:: ast_nodes:: { AstNode , AstNodes } ,
1118 options:: Expand ,
@@ -30,7 +37,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticMemberExpression<'a>> {
3037 recording. stop ( ) . has_label ( LabelId :: of ( JsLabels :: MemberChain ) )
3138 } ;
3239
33- match layout ( self , is_member_chain) {
40+ match layout ( self , is_member_chain, f ) {
3441 StaticMemberLayout :: NoBreak => {
3542 let format_no_break =
3643 format_with ( |f| write ! ( f, [ operator_token( self . optional( ) ) , self . property( ) ] ) ) ;
@@ -46,6 +53,19 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticMemberExpression<'a>> {
4653 f,
4754 [ group( & indent( & format_args!(
4855 soft_line_break( ) ,
56+ & format_once( |f| {
57+ if f. comments( ) . has_leading_own_line_comment( self . property. span. start) {
58+ let comments = f
59+ . context( )
60+ . comments( )
61+ . comments_before( self . property. span. start) ;
62+ write!(
63+ f,
64+ [ FormatLeadingComments :: Comments ( comments) , soft_line_break( ) ]
65+ ) ?;
66+ }
67+ Ok ( ( ) )
68+ } ) ,
4969 operator_token( self . optional( ) ) ,
5070 self . property( ) ,
5171 ) ) ) ]
@@ -71,7 +91,12 @@ fn operator_token(optional: bool) -> &'static str {
7191fn layout < ' a > (
7292 node : & AstNode < ' a , StaticMemberExpression < ' a > > ,
7393 is_member_chain : bool ,
94+ f : & Formatter < ' _ , ' a > ,
7495) -> StaticMemberLayout {
96+ if f. comments ( ) . has_leading_own_line_comment ( node. property . span . start ) {
97+ return StaticMemberLayout :: BreakAfterObject ;
98+ }
99+
75100 // `a.b.c!` and `a.b?.c`
76101 // `TSNonNullExpression` is a wrapper node for `!`, and `ChainExpression` is a wrapper node for `?.`,
77102 // so we need to skip them to find the real parent node.
0 commit comments