Skip to content

Commit 8fbb354

Browse files
committed
fix(formatter): correct printing of trailing comments after the semicolon for class properties
1 parent fa5087e commit 8fbb354

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

crates/oxc_formatter/src/write/class.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -567,8 +567,6 @@ impl<'a, 'b> FormatClassElementWithSemicolon<'a, 'b> {
567567

568568
impl<'a> Format<'a> for FormatClassElementWithSemicolon<'a, '_> {
569569
fn fmt(&self, f: &mut Formatter<'_, 'a>) {
570-
write!(f, [self.element]);
571-
572570
let needs_semi = matches!(
573571
self.element.as_ref(),
574572
ClassElement::PropertyDefinition(_) | ClassElement::AccessorProperty(_)
@@ -580,7 +578,23 @@ impl<'a> Format<'a> for FormatClassElementWithSemicolon<'a, '_> {
580578
Semicolons::AsNeeded => self.needs_semicolon(),
581579
};
582580

583-
write!(f, needs_semi.then_some(";"));
581+
if needs_semi {
582+
write!(f, [FormatNodeWithoutTrailingComments(self.element), ";"]);
583+
// Print trailing comments after the semicolon
584+
match self.element.as_ast_nodes() {
585+
AstNodes::PropertyDefinition(prop) => {
586+
prop.format_trailing_comments(f);
587+
}
588+
AstNodes::AccessorProperty(prop) => {
589+
prop.format_trailing_comments(f);
590+
}
591+
_ => {
592+
unreachable!("Only `PropertyDefinition` and `AccessorProperty` can reach here");
593+
}
594+
}
595+
} else {
596+
write!(f, self.element);
597+
}
584598
}
585599
}
586600

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class X {
2+
prop1 = "test"; /* comment */
3+
accessor prop2 = 1; /* comment */
4+
static prop3; /* comment */
5+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
class X {
6+
prop1 = "test"; /* comment */
7+
accessor prop2 = 1; /* comment */
8+
static prop3; /* comment */
9+
}
10+
11+
==================== Output ====================
12+
------------------------------
13+
{ printWidth: 80, semi: true }
14+
------------------------------
15+
class X {
16+
prop1 = "test"; /* comment */
17+
accessor prop2 = 1; /* comment */
18+
static prop3; /* comment */
19+
}
20+
21+
-------------------------------
22+
{ printWidth: 100, semi: true }
23+
-------------------------------
24+
class X {
25+
prop1 = "test"; /* comment */
26+
accessor prop2 = 1; /* comment */
27+
static prop3; /* comment */
28+
}
29+
30+
-------------------------------
31+
{ printWidth: 80, semi: false }
32+
-------------------------------
33+
class X {
34+
prop1 = "test" /* comment */
35+
accessor prop2 = 1 /* comment */
36+
static prop3 /* comment */
37+
}
38+
39+
--------------------------------
40+
{ printWidth: 100, semi: false }
41+
--------------------------------
42+
class X {
43+
prop1 = "test" /* comment */
44+
accessor prop2 = 1 /* comment */
45+
static prop3 /* comment */
46+
}
47+
48+
===================== End =====================

0 commit comments

Comments
 (0)