Skip to content

Commit 4cc3b10

Browse files
committed
fix(formatter): improve handling of new lines between comments in MemberChain (#14759)
1 parent e6bce8e commit 4cc3b10

File tree

3 files changed

+62
-22
lines changed

3 files changed

+62
-22
lines changed

crates/oxc_formatter/src/utils/member_chain/groups.rs

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'a, 'b> MemberChainGroup<'a, 'b> {
159159
let interned = f.intern(&FormatMemberChainGroup { group: self })?;
160160

161161
if tail {
162-
self.needs_empty_line.set(self.needs_empty_line_before(f));
162+
self.set_needs_empty_line(self.needs_empty_line_before(f));
163163
}
164164

165165
if let Some(interned) = interned {
@@ -199,28 +199,21 @@ impl<'a, 'b> MemberChainGroup<'a, 'b> {
199199
// Check whether has more than 1 continuous new lines before the operator (`.`)
200200
let start = expression.object().span().end;
201201
let mut end = expression.property().span().start;
202-
let mut comments = f.comments().comments_in_range(start, end).iter();
203-
let mut last_comment_span = comments.next_back();
204-
205-
while start < end {
206-
// Skip comments that are after the operator (`.`)
207-
if let Some(last_comment) = last_comment_span
208-
&& last_comment.span.end == end
209-
{
210-
end = last_comment.span.start - 1;
211-
last_comment_span = comments.next_back();
212-
continue;
213-
} else if matches!(source.byte_at(end), Some(b'.')) {
214-
// Found the operator, stop the loop
215-
break;
216-
}
217-
218-
end -= 1;
219-
}
220202

221-
// Skip comments that are before the operator (`.`)
222-
if let Some(comment) = comments.next() {
223-
end = comment.span.start;
203+
// We should found the real `end` position that is before comments and it is not after the operator `.`
204+
if let Some(printed_comment) = f
205+
.comments()
206+
.printed_comments()
207+
.iter()
208+
.rev()
209+
.take_while(|c| start <= c.span.start && c.span.end < end)
210+
.last()
211+
{
212+
end = printed_comment.span.start;
213+
} else if let Some(first_comment) =
214+
f.comments().comments_before_character(start, b'.').first()
215+
{
216+
end = first_comment.span.start;
224217
}
225218

226219
// Count the number of continuous new lines
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Promise.all(writeIconFiles)
2+
// TO DO -- END
3+
.then(() => writeRegistry())
4+
5+
Promise.all(writeIconFiles)
6+
7+
// TO DO -- END
8+
.then(() => writeRegistry())
9+
10+
Promise.all(writeIconFiles)
11+
// TO DO -- END
12+
13+
.then(() => writeRegistry())
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
Promise.all(writeIconFiles)
6+
// TO DO -- END
7+
.then(() => writeRegistry())
8+
9+
Promise.all(writeIconFiles)
10+
11+
// TO DO -- END
12+
.then(() => writeRegistry())
13+
14+
Promise.all(writeIconFiles)
15+
// TO DO -- END
16+
17+
.then(() => writeRegistry())
18+
19+
==================== Output ====================
20+
Promise.all(writeIconFiles)
21+
// TO DO -- END
22+
.then(() => writeRegistry());
23+
24+
Promise.all(writeIconFiles)
25+
26+
// TO DO -- END
27+
.then(() => writeRegistry());
28+
29+
Promise.all(writeIconFiles)
30+
// TO DO -- END
31+
32+
.then(() => writeRegistry());
33+
34+
===================== End =====================

0 commit comments

Comments
 (0)