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
11 changes: 11 additions & 0 deletions crates/oxc_formatter/src/ast_nodes/impls/ast_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,15 @@ impl<'a> AstNodes<'a> {
if matches!(node, AstNodes::Program(_)) { None } else { Some(node.parent()) }
})
}

/// If the node is a ChainExpression, recursively skip to its parent until a non-ChainExpression node is found.
/// This is useful for analyses that want to ignore the presence of ChainExpressions in the AST.
pub fn without_chain_expression(&self) -> &AstNodes<'a> {
match self {
AstNodes::ChainExpression(chain_expression) => {
chain_expression.parent.without_chain_expression()
}
_ => self,
}
}
}
21 changes: 11 additions & 10 deletions crates/oxc_formatter/src/utils/member_chain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl<'a, 'b> MemberChain<'a, 'b> {
call_expression: &'b AstNode<'a, CallExpression<'a>>,
f: &Formatter<'_, 'a>,
) -> Self {
let parent = &call_expression.parent;
let mut chain_members = chain_members_iter(call_expression, f).collect::<Vec<_>>();
chain_members.reverse();

Expand All @@ -54,7 +53,7 @@ impl<'a, 'b> MemberChain<'a, 'b> {

// Here we check if the first element of Groups::groups can be moved inside the head.
// If so, then we extract it and concatenate it together with the head.
member_chain.maybe_merge_with_first_group(parent, f);
member_chain.maybe_merge_with_first_group(call_expression.parent, f);

member_chain
}
Expand Down Expand Up @@ -93,14 +92,16 @@ impl<'a, 'b> MemberChain<'a, 'b> {
if self.head.members().len() == 1
&& let ChainMember::Node(node) = self.head.members()[0]
{
if let Expression::Identifier(identifier) = node.as_ref() {
has_computed_property ||
is_factory(&identifier.name) ||
// If an identifier has a name that is shorter than the tab with, then we join it with the "head"
(matches!(parent, AstNodes::ExpressionStatement(stmt) if !stmt.is_arrow_function_body())
&& has_short_name(&identifier.name, f.options().indent_width.value()))
} else {
matches!(node.as_ref(), Expression::ThisExpression(_))
match node.as_ref() {
Expression::Identifier(identifier) => {
has_computed_property ||
is_factory(&identifier.name) ||
// If an identifier has a name that is shorter than the tab width, then we join it with the "head"
(matches!(parent.without_chain_expression(), AstNodes::ExpressionStatement(stmt) if !stmt.is_arrow_function_body())
&& has_short_name(&identifier.name, f.options().indent_width.value()))
}
Expression::ThisExpression(_) => true,
_ => false,
}
} else if let Some(ChainMember::StaticMember(expression)) = self.head.members().last() {
has_computed_property || is_factory(&expression.property().name)
Expand Down
3 changes: 1 addition & 2 deletions tasks/prettier_conformance/snapshots/prettier.js.snap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
js compatibility: 684/749 (91.32%)
js compatibility: 685/749 (91.46%)

# Failed

Expand Down Expand Up @@ -42,7 +42,6 @@ js compatibility: 684/749 (91.32%)
| js/label/empty_label.js | 💥 | 66.67% |
| js/last-argument-expansion/dangling-comment-in-arrow-function.js | 💥 | 22.22% |
| js/logical-expressions/multiple-comments/17192.js | 💥 | 60.00% |
| js/method-chain/issue-17457.js | 💥 | 0.00% |
| js/object-multiline/multiline.js | 💥✨ | 22.22% |
| js/quote-props/classes.js | 💥💥✨✨ | 47.06% |
| js/quote-props/objects.js | 💥💥✨✨ | 45.10% |
Expand Down
Loading