Skip to content

Commit 756fc9f

Browse files
committed
fix(formatter): correct checking comments between the operator and the right side for assignment like nodes
1 parent 096651f commit 756fc9f

File tree

3 files changed

+37
-22
lines changed

3 files changed

+37
-22
lines changed

crates/oxc_formatter/src/utils/assignment_like.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -314,19 +314,19 @@ impl<'a> AssignmentLike<'a, '_> {
314314
}
315315

316316
let right_expression = self.get_right_expression();
317+
if let Some(expr) = right_expression {
318+
if let Some(layout) = self.chain_formatting_layout(expr) {
319+
return layout;
320+
}
317321

318-
if let Some(layout) = right_expression.and_then(|expr| self.chain_formatting_layout(expr)) {
319-
return layout;
320-
}
321-
322-
if let Some(Expression::CallExpression(call_expression)) =
323-
&right_expression.map(AsRef::as_ref)
324-
&& call_expression
325-
.callee
326-
.get_identifier_reference()
327-
.is_some_and(|ident| ident.name == "require")
328-
{
329-
return AssignmentLikeLayout::NeverBreakAfterOperator;
322+
if let Expression::CallExpression(call_expression) = expr.as_ref()
323+
&& call_expression
324+
.callee
325+
.get_identifier_reference()
326+
.is_some_and(|ident| ident.name == "require")
327+
{
328+
return AssignmentLikeLayout::NeverBreakAfterOperator;
329+
}
330330
}
331331

332332
if self.should_break_after_operator(right_expression, f) {
@@ -359,10 +359,8 @@ impl<'a> AssignmentLike<'a, '_> {
359359
return AssignmentLikeLayout::BreakAfterOperator;
360360
}
361361

362-
let is_poorly_breakable = match &right_expression {
363-
Some(expression) => is_poorly_breakable_member_or_call_chain(expression, f),
364-
None => false,
365-
};
362+
let is_poorly_breakable =
363+
right_expression.is_some_and(|expr| is_poorly_breakable_member_or_call_chain(expr, f));
366364

367365
if is_poorly_breakable {
368366
return AssignmentLikeLayout::BreakAfterOperator;
@@ -581,17 +579,19 @@ fn should_break_after_operator<'a>(
581579
) -> bool {
582580
let is_jsx = matches!(right.as_ref(), Expression::JSXElement(_) | Expression::JSXFragment(_));
583581

582+
if is_jsx {
583+
return false;
584+
}
585+
586+
let comments = f.comments();
584587
let source_text = f.source_text();
585-
for comment in f.comments().comments_before(right.span().start) {
586-
if !is_jsx
587-
&& (source_text.lines_after(comment.span.end) > 0
588-
|| source_text.is_own_line_comment(comment))
589-
{
588+
for comment in comments.comments_before(right.span().start) {
589+
if source_text.lines_after(comment.span.end) > 0 {
590590
return true;
591591
}
592592

593593
// Needs to wrap a parenthesis for the node, so it won't break.
594-
if f.comments().is_type_cast_comment(comment) {
594+
if comments.is_type_cast_comment(comment) {
595595
return false;
596596
}
597597
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
var longlonglonglonglonglong = /*#__PURE__*/_interopDefaultLegacy(aaaaaaaaaaaaaaa);
2+
var short = /*#__PURE__*/_interopDefaultLegacy(b);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
var longlonglonglonglonglong = /*#__PURE__*/_interopDefaultLegacy(aaaaaaaaaaaaaaa);
6+
var short = /*#__PURE__*/_interopDefaultLegacy(b);
7+
8+
==================== Output ====================
9+
var longlonglonglonglonglong =
10+
/*#__PURE__*/ _interopDefaultLegacy(aaaaaaaaaaaaaaa);
11+
var short = /*#__PURE__*/ _interopDefaultLegacy(b);
12+
13+
===================== End =====================

0 commit comments

Comments
 (0)