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
8 changes: 3 additions & 5 deletions crates/oxc_formatter/src/formatter/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl<'a> Comments<'a> {

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

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

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

/// Checks if there are leading or trailing comments around `current_span`.
Expand Down
44 changes: 22 additions & 22 deletions crates/oxc_formatter/src/utils/assignment_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,19 +314,19 @@ impl<'a> AssignmentLike<'a, '_> {
}

let right_expression = self.get_right_expression();
if let Some(expr) = right_expression {
if let Some(layout) = self.chain_formatting_layout(expr) {
return layout;
}

if let Some(layout) = right_expression.and_then(|expr| self.chain_formatting_layout(expr)) {
return layout;
}

if let Some(Expression::CallExpression(call_expression)) =
&right_expression.map(AsRef::as_ref)
&& call_expression
.callee
.get_identifier_reference()
.is_some_and(|ident| ident.name == "require")
{
return AssignmentLikeLayout::NeverBreakAfterOperator;
if let Expression::CallExpression(call_expression) = expr.as_ref()
&& call_expression
.callee
.get_identifier_reference()
.is_some_and(|ident| ident.name == "require")
{
return AssignmentLikeLayout::NeverBreakAfterOperator;
}
}

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

let is_poorly_breakable = match &right_expression {
Some(expression) => is_poorly_breakable_member_or_call_chain(expression, f),
None => false,
};
let is_poorly_breakable =
right_expression.is_some_and(|expr| is_poorly_breakable_member_or_call_chain(expr, f));

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

if is_jsx {
return false;
}

let comments = f.comments();
let source_text = f.source_text();
for comment in f.comments().comments_before(right.span().start) {
if !is_jsx
&& (source_text.lines_after(comment.span.end) > 0
|| source_text.is_own_line_comment(comment))
{
for comment in comments.comments_before(right.span().start) {
if source_text.lines_after(comment.span.end) > 0 {
return true;
}

// Needs to wrap a parenthesis for the node, so it won't break.
if f.comments().is_type_cast_comment(comment) {
if comments.is_type_cast_comment(comment) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var longlonglonglonglonglong = /*#__PURE__*/_interopDefaultLegacy(aaaaaaaaaaaaaaa);
var short = /*#__PURE__*/_interopDefaultLegacy(b);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
var longlonglonglonglonglong = /*#__PURE__*/_interopDefaultLegacy(aaaaaaaaaaaaaaa);
var short = /*#__PURE__*/_interopDefaultLegacy(b);

==================== Output ====================
var longlonglonglonglonglong =
/*#__PURE__*/ _interopDefaultLegacy(aaaaaaaaaaaaaaa);
var short = /*#__PURE__*/ _interopDefaultLegacy(b);

===================== End =====================
Loading