Skip to content

Commit ea8f9ed

Browse files
committed
fix(formatter): correct checking comments between the operator and the right side for assignment like nodes (#14482)
1 parent 4f19504 commit ea8f9ed

File tree

4 files changed

+40
-27
lines changed

4 files changed

+40
-27
lines changed

crates/oxc_formatter/src/formatter/comments.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'a> Comments<'a> {
236236

237237
/// Checks if there are any comments between the given positions.
238238
pub fn has_comment_in_range(&self, start: u32, end: u32) -> bool {
239-
self.comments_before_iter(end).any(|comment| comment.span.end >= start)
239+
self.comments_before_iter(end).any(|comment| comment.span.end > start)
240240
}
241241

242242
/// Checks if there are any comments within the given span.
@@ -253,10 +253,8 @@ impl<'a> Comments<'a> {
253253

254254
/// Checks if there are any leading own-line comments before the given position.
255255
pub fn has_leading_own_line_comment(&self, start: u32) -> bool {
256-
self.comments_before_iter(start).any(|comment| {
257-
self.source_text.is_own_line_comment(comment)
258-
|| self.source_text.lines_after(comment.span.end) > 0
259-
})
256+
self.comments_before_iter(start)
257+
.any(|comment| self.source_text.lines_after(comment.span.end) > 0)
260258
}
261259

262260
/// Checks if there are leading or trailing comments around `current_span`.

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)