Skip to content

Commit 380a0af

Browse files
committed
fix(formatter): incorrect printing of class binding trailing comments (#16234)
* close: #16174
1 parent fd02931 commit 380a0af

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

crates/oxc_formatter/src/write/class.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,16 +291,7 @@ impl<'a> Format<'a> for FormatClass<'a, '_> {
291291

292292
let head = format_with(|f| {
293293
if let Some(id) = self.id() {
294-
write!(f, [space()]);
295-
296-
if self.type_parameters.is_some()
297-
|| self.super_class.is_some()
298-
|| !self.implements.is_empty()
299-
{
300-
id.fmt(f);
301-
} else {
302-
id.write(f);
303-
}
294+
write!(f, [space(), id]);
304295
}
305296

306297
if let Some(type_parameters) = &type_parameters {
@@ -458,6 +449,11 @@ impl<'a> Format<'a> for FormatClass<'a, '_> {
458449
write!(f, [head, format_heritage_clauses, space()]);
459450
}
460451

452+
let leading_comments = f.context().comments().comments_before(self.body.span.start);
453+
if leading_comments.iter().any(|c| !c.is_line()) {
454+
write!(f, FormatLeadingComments::Comments(leading_comments));
455+
}
456+
461457
if body.body.is_empty() {
462458
write!(f, ["{", format_dangling_comments(self.span).with_block_indent(), "}"]);
463459
} else {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export class Cls /* YES */ {
2+
// class body comment
3+
}
4+
5+
6+
export class Mutliline /* Y
7+
E
8+
S */ {
9+
// class body comment
10+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
export class Cls /* YES */ {
6+
// class body comment
7+
}
8+
9+
10+
export class Mutliline /* Y
11+
E
12+
S */ {
13+
// class body comment
14+
}
15+
16+
==================== Output ====================
17+
------------------
18+
{ printWidth: 80 }
19+
------------------
20+
export class Cls /* YES */ {
21+
// class body comment
22+
}
23+
24+
export class Mutliline /* Y
25+
E
26+
S */ {
27+
// class body comment
28+
}
29+
30+
-------------------
31+
{ printWidth: 100 }
32+
-------------------
33+
export class Cls /* YES */ {
34+
// class body comment
35+
}
36+
37+
export class Mutliline /* Y
38+
E
39+
S */ {
40+
// class body comment
41+
}
42+
43+
===================== End =====================

0 commit comments

Comments
 (0)