Skip to content

Commit 50be18e

Browse files
committed
fix(formatter): keep trailing spaces on normal block comments (#27037)
Closes #17786 --- I've decided to keep trailing spaces in normal block comments as belonging to the user. If the aim is to clean up unnecessary whitespace, we should presumably remove it from the other lines as well, but the current behavior only trims the first line, it is inconsistent. Furthermore, this doesn't match the behavior of CSS block comments.
1 parent 56d1880 commit 50be18e

4 files changed

Lines changed: 45 additions & 20 deletions

File tree

‎crates/oxc_formatter/src/formatter/trivia.rs‎

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,8 @@
7878
//! — see [`DanglingIndentMode`] for which variant an empty container takes
7979
//! 3. Preserves comment relationships and spacing
8080
//! 4. Advances cursor for processed comments
81-
use oxc_allocator::ArenaStringBuilder;
8281
use oxc_ast::{Comment, CommentContent, CommentKind};
83-
use oxc_formatter_core::SourceText;
82+
use oxc_formatter_core::{LINE_TERMINATORS, SourceText, arena_cow_str, normalize_newlines};
8483
use oxc_span::Span;
8584
use oxc_syntax::line_terminator::LineTerminatorSplitter;
8685

@@ -560,8 +559,8 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatCommentText<'_> {
560559
} else {
561560
let content = f.source_text().text_for(&comment.span);
562561
if comment.is_multiline_block() {
563-
let mut lines = LineTerminatorSplitter::new(content);
564562
if is_alignable_comment(content) {
563+
let mut lines = LineTerminatorSplitter::new(content);
565564
// `unwrap` is safe because `content` contains at least one line.
566565
let first_line = lines.next().unwrap();
567566
write!(f, [text(first_line.trim_end())]);
@@ -578,17 +577,8 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatCommentText<'_> {
578577
}
579578
}
580579
} else {
581-
// Normalize line endings `\r\n` to `\n`
582-
let mut string =
583-
ArenaStringBuilder::with_capacity_in(content.len(), f.allocator());
584-
// `unwrap` is safe because `content` contains at least one line.
585-
string.push_str(lines.next().unwrap().trim_end());
586-
587-
for str in lines {
588-
string.push('\n');
589-
string.push_str(str);
590-
}
591-
write!(f, [text(string.into_str())]);
580+
let normalized = normalize_newlines(content, LINE_TERMINATORS);
581+
write!(f, [text(arena_cow_str(&normalized, f))]);
592582
}
593583
} else {
594584
write!(f, [text(content.trim_end())]);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/* See ->
2+
this ^
3+
trailing spaces stay
4+
*/
5+
function plain() {}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
/* See ->
6+
this ^
7+
trailing spaces stay
8+
*/
9+
function plain() {}
10+
11+
==================== Output ====================
12+
------------------
13+
{ printWidth: 80 }
14+
------------------
15+
/* See ->
16+
this ^
17+
trailing spaces stay
18+
*/
19+
function plain() {}
20+
21+
-------------------
22+
{ printWidth: 100 }
23+
-------------------
24+
/* See ->
25+
this ^
26+
trailing spaces stay
27+
*/
28+
function plain() {}
29+
30+
===================== End =====================

‎tasks/track_memory_allocations/allocs_formatter.yaml‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
checker.ts:
22
file size: 2922154 # 2.92 MB
3-
sys allocs: 19383
4-
sys reallocs: 1166
5-
sys deallocs: 19382
3+
sys allocs: 19384
4+
sys reallocs: 1169
5+
sys deallocs: 19383
66
sys alloc bytes: 6767781 # 6.77 MB
77
sys peak growth: 2973674 # 2.97 MB
88
arena allocs: 1070124
@@ -16,7 +16,7 @@ App.tsx:
1616
sys deallocs: 5191
1717
sys alloc bytes: 1564670 # 1.56 MB
1818
sys peak growth: 435358 # 435.36 kB
19-
arena allocs: 210975
19+
arena allocs: 210972
2020
arena reallocs: 364
2121
arena size: 16925816 # 16.93 MB
2222

@@ -49,9 +49,9 @@ antd.js:
4949
sys deallocs: 90042
5050
sys alloc bytes: 76472316 # 76.47 MB
5151
sys peak growth: 50737424 # 50.74 MB
52-
arena allocs: 2558365
52+
arena allocs: 2555858
5353
arena reallocs: 1889
54-
arena size: 246661408 # 246.66 MB
54+
arena size: 246149128 # 246.15 MB
5555

5656
binder.ts:
5757
file size: 193077 # 193.08 kB

0 commit comments

Comments
 (0)