Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions crates/oxc_formatter/src/write/member_expression.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use std::ops::Deref;

use oxc_ast::ast::*;
use oxc_span::GetSpan;

use crate::{
JsLabels, format_args,
formatter::{
Buffer, Format, FormatResult, Formatter, prelude::*, trivia::format_dangling_comments,
Buffer, Format, FormatResult, Formatter,
buffer::RemoveSoftLinesBuffer,
prelude::*,
trivia::{
DanglingIndentMode, FormatDanglingComments, FormatLeadingComments,
FormatTrailingComments, format_dangling_comments,
},
},
generated::ast_nodes::{AstNode, AstNodes},
options::Expand,
Expand All @@ -30,7 +37,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticMemberExpression<'a>> {
recording.stop().has_label(LabelId::of(JsLabels::MemberChain))
};

match layout(self, is_member_chain) {
match layout(self, is_member_chain, f) {
StaticMemberLayout::NoBreak => {
let format_no_break =
format_with(|f| write!(f, [operator_token(self.optional()), self.property()]));
Expand All @@ -46,6 +53,19 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticMemberExpression<'a>> {
f,
[group(&indent(&format_args!(
soft_line_break(),
&format_once(|f| {
if f.comments().has_leading_own_line_comment(self.property.span.start) {
let comments = f
.context()
.comments()
.comments_before(self.property.span.start);
write!(
f,
[FormatLeadingComments::Comments(comments), soft_line_break()]
)?;
}
Ok(())
}),
operator_token(self.optional()),
self.property(),
)))]
Expand All @@ -71,7 +91,12 @@ fn operator_token(optional: bool) -> &'static str {
fn layout<'a>(
node: &AstNode<'a, StaticMemberExpression<'a>>,
is_member_chain: bool,
f: &Formatter<'_, 'a>,
) -> StaticMemberLayout {
if f.comments().has_leading_own_line_comment(node.property.span.start) {
return StaticMemberLayout::BreakAfterObject;
}

// `a.b.c!` and `a.b?.c`
// `TSNonNullExpression` is a wrapper node for `!`, and `ChainExpression` is a wrapper node for `?.`,
// so we need to skip them to find the real parent node.
Expand Down
4 changes: 1 addition & 3 deletions tasks/coverage/snapshots/formatter_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@ commit: 261630d6

formatter_typescript Summary:
AST Parsed : 8816/8816 (100.00%)
Positive Passed: 8808/8816 (99.91%)
Positive Passed: 8809/8816 (99.92%)
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/arrayFromAsync.ts
`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
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/genericTypeAssertions3.ts
Unexpected token
Mismatch: tasks/coverage/typescript/tests/cases/compiler/propertyAccessExpressionInnerComments.ts

Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/classes/propertyMemberDeclarations/staticPropertyNameConflicts.ts
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
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts
Expand Down
Loading