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.
This commit is contained in:
leaysgur committed 2026-09-25 07:10:10 +00:00
1 parent 56d188008f
commit 50be18e6ef
4 files changed
+45 -20

No files matched your search

+4 -14
View File
@@ -78,9 +78,8 @@
//! — see [`DanglingIndentMode`] for which variant an empty container takes
//! 3. Preserves comment relationships and spacing
//! 4. Advances cursor for processed comments
use oxc_allocator::ArenaStringBuilder;
use oxc_ast::{Comment, CommentContent, CommentKind};
use oxc_formatter_core::SourceText;
use oxc_formatter_core::{LINE_TERMINATORS, SourceText, arena_cow_str, normalize_newlines};
use oxc_span::Span;
use oxc_syntax::line_terminator::LineTerminatorSplitter;
@@ -560,8 +559,8 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatCommentText<'_> {
} else {
let content = f.source_text().text_for(&comment.span);
if comment.is_multiline_block() {
let mut lines = LineTerminatorSplitter::new(content);
if is_alignable_comment(content) {
let mut lines = LineTerminatorSplitter::new(content);
// `unwrap` is safe because `content` contains at least one line.
let first_line = lines.next().unwrap();
write!(f, [text(first_line.trim_end())]);
@@ -578,17 +577,8 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatCommentText<'_> {
}
}
} else {
// Normalize line endings `\r\n` to `\n`
let mut string =
ArenaStringBuilder::with_capacity_in(content.len(), f.allocator());
// `unwrap` is safe because `content` contains at least one line.
string.push_str(lines.next().unwrap().trim_end());
for str in lines {
string.push('\n');
string.push_str(str);
}
write!(f, [text(string.into_str())]);
let normalized = normalize_newlines(content, LINE_TERMINATORS);
write!(f, [text(arena_cow_str(&normalized, f))]);
}
} else {
write!(f, [text(content.trim_end())]);
@@ -0,0 +1,5 @@
/* See ->
this ^
trailing spaces stay
*/
function plain() {}
@@ -0,0 +1,30 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
/* See ->
this ^
trailing spaces stay
*/
function plain() {}
==================== Output ====================
------------------
{ printWidth: 80 }
------------------
/* See ->
this ^
trailing spaces stay
*/
function plain() {}
-------------------
{ printWidth: 100 }
-------------------
/* See ->
this ^
trailing spaces stay
*/
function plain() {}
===================== End =====================
@@ -1,8 +1,8 @@
checker.ts:
file size: 2922154 # 2.92 MB
sys allocs: 19383
sys reallocs: 1166
sys deallocs: 19382
sys allocs: 19384
sys reallocs: 1169
sys deallocs: 19383
sys alloc bytes: 6767781 # 6.77 MB
sys peak growth: 2973674 # 2.97 MB
arena allocs: 1070124
@@ -16,7 +16,7 @@ App.tsx:
sys deallocs: 5191
sys alloc bytes: 1564670 # 1.56 MB
sys peak growth: 435358 # 435.36 kB
arena allocs: 210975
arena allocs: 210972
arena reallocs: 364
arena size: 16925816 # 16.93 MB
@@ -49,9 +49,9 @@ antd.js:
sys deallocs: 90042
sys alloc bytes: 76472316 # 76.47 MB
sys peak growth: 50737424 # 50.74 MB
arena allocs: 2558365
arena allocs: 2555858
arena reallocs: 1889
arena size: 246661408 # 246.66 MB
arena size: 246149128 # 246.15 MB
binder.ts:
file size: 193077 # 193.08 kB