Skip to content

Commit bf20cf5

Browse files
committed
fix(formatter): CRLF issue in the member chain (#15764)
* Fixes: #15559 Use `SourceText::lines_before` to check the line break, which has already handled `CRLF` and is well-tested. Thought: Whether we can remove all `\r` checks to avoid this problem, only counting `\n` as a line break. `CR` is very rare now, and we have a few places where we are just checking `\n`.
1 parent 5d688a0 commit bf20cf5

File tree

1 file changed

+4
-11
lines changed
  • crates/oxc_formatter/src/utils/member_chain

1 file changed

+4
-11
lines changed

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,10 @@ impl<'a, 'b> MemberChainGroup<'a, 'b> {
218218

219219
// Count the number of continuous new lines
220220
let mut new_lines_count = 0;
221-
for &b in source.bytes_range(start, end) {
222-
if matches!(b, b'\n' | b'\r') {
223-
new_lines_count += 1;
224-
// If there are more than 1 continuous new lines, return true
225-
if new_lines_count > 1 {
226-
return true;
227-
}
228-
} else if !b.is_ascii_whitespace() {
229-
// Reset the counter if there is a non-new-line character,
230-
// because the new lines are not continuous
231-
new_lines_count = 0;
221+
for (idx, &b) in source.bytes_range(start, end).iter().enumerate() {
222+
#[expect(clippy::cast_possible_truncation)]
223+
if matches!(b, b'\n' | b'\r') && source.lines_after(start + idx as u32) > 1 {
224+
return true;
232225
}
233226
}
234227

0 commit comments

Comments
 (0)