diff --git a/crates/oxc_formatter/src/utils/member_chain/groups.rs b/crates/oxc_formatter/src/utils/member_chain/groups.rs index 7af5cb8c30b26..9829f2667ae8c 100644 --- a/crates/oxc_formatter/src/utils/member_chain/groups.rs +++ b/crates/oxc_formatter/src/utils/member_chain/groups.rs @@ -159,7 +159,7 @@ impl<'a, 'b> MemberChainGroup<'a, 'b> { let interned = f.intern(&FormatMemberChainGroup { group: self })?; if tail { - self.needs_empty_line.set(self.needs_empty_line_before(f)); + self.set_needs_empty_line(self.needs_empty_line_before(f)); } if let Some(interned) = interned { @@ -199,28 +199,21 @@ impl<'a, 'b> MemberChainGroup<'a, 'b> { // Check whether has more than 1 continuous new lines before the operator (`.`) let start = expression.object().span().end; let mut end = expression.property().span().start; - let mut comments = f.comments().comments_in_range(start, end).iter(); - let mut last_comment_span = comments.next_back(); - - while start < end { - // Skip comments that are after the operator (`.`) - if let Some(last_comment) = last_comment_span - && last_comment.span.end == end - { - end = last_comment.span.start - 1; - last_comment_span = comments.next_back(); - continue; - } else if matches!(source.byte_at(end), Some(b'.')) { - // Found the operator, stop the loop - break; - } - - end -= 1; - } - // Skip comments that are before the operator (`.`) - if let Some(comment) = comments.next() { - end = comment.span.start; + // We should found the real `end` position that is before comments and it is not after the operator `.` + if let Some(printed_comment) = f + .comments() + .printed_comments() + .iter() + .rev() + .take_while(|c| start <= c.span.start && c.span.end < end) + .last() + { + end = printed_comment.span.start; + } else if let Some(first_comment) = + f.comments().comments_before_character(start, b'.').first() + { + end = first_comment.span.start; } // Count the number of continuous new lines diff --git a/crates/oxc_formatter/tests/fixtures/js/member-chains/blank-line.js b/crates/oxc_formatter/tests/fixtures/js/member-chains/blank-line.js new file mode 100644 index 0000000000000..8fc1a0cc537a6 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/member-chains/blank-line.js @@ -0,0 +1,13 @@ +Promise.all(writeIconFiles) + // TO DO -- END + .then(() => writeRegistry()) + +Promise.all(writeIconFiles) + + // TO DO -- END + .then(() => writeRegistry()) + +Promise.all(writeIconFiles) + // TO DO -- END + + .then(() => writeRegistry()) diff --git a/crates/oxc_formatter/tests/fixtures/js/member-chains/blank-line.js.snap b/crates/oxc_formatter/tests/fixtures/js/member-chains/blank-line.js.snap new file mode 100644 index 0000000000000..4f130ec98aa48 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/member-chains/blank-line.js.snap @@ -0,0 +1,34 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +Promise.all(writeIconFiles) + // TO DO -- END + .then(() => writeRegistry()) + +Promise.all(writeIconFiles) + + // TO DO -- END + .then(() => writeRegistry()) + +Promise.all(writeIconFiles) + // TO DO -- END + + .then(() => writeRegistry()) + +==================== Output ==================== +Promise.all(writeIconFiles) + // TO DO -- END + .then(() => writeRegistry()); + +Promise.all(writeIconFiles) + + // TO DO -- END + .then(() => writeRegistry()); + +Promise.all(writeIconFiles) + // TO DO -- END + + .then(() => writeRegistry()); + +===================== End =====================