Skip to content

Commit 191b50b

Browse files
committed
fix(formatter): correct printing comments that are around the StaticMemberExpression
1 parent 1b77734 commit 191b50b

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

crates/oxc_formatter/src/write/member_expression.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
use std::ops::Deref;
22

33
use oxc_ast::ast::*;
4+
use oxc_span::GetSpan;
45

56
use 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 {
7191
fn 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.

tasks/coverage/snapshots/formatter_typescript.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ commit: 261630d6
22

33
formatter_typescript Summary:
44
AST Parsed : 8816/8816 (100.00%)
5-
Positive Passed: 8808/8816 (99.91%)
5+
Positive Passed: 8809/8816 (99.92%)
66
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
77

88
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts
99
`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules`await` is only allowed within async functions and at the top levels of modules
1010
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/genericTypeAssertions3.ts
1111
Unexpected token
12-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/propertyAccessExpressionInnerComments.ts
13-
1412
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts
1513
Classes may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototypeClasses may not have a static property named prototype
1614
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts

0 commit comments

Comments
 (0)