Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions crates/oxc_formatter/src/write/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,6 @@ impl<'a, 'b> FormatClassElementWithSemicolon<'a, 'b> {

impl<'a> Format<'a> for FormatClassElementWithSemicolon<'a, '_> {
fn fmt(&self, f: &mut Formatter<'_, 'a>) {
write!(f, [self.element]);

let needs_semi = matches!(
self.element.as_ref(),
ClassElement::PropertyDefinition(_) | ClassElement::AccessorProperty(_)
Expand All @@ -580,7 +578,23 @@ impl<'a> Format<'a> for FormatClassElementWithSemicolon<'a, '_> {
Semicolons::AsNeeded => self.needs_semicolon(),
};

write!(f, needs_semi.then_some(";"));
if needs_semi {
write!(f, [FormatNodeWithoutTrailingComments(self.element), ";"]);
// Print trailing comments after the semicolon
match self.element.as_ast_nodes() {
AstNodes::PropertyDefinition(prop) => {
prop.format_trailing_comments(f);
}
AstNodes::AccessorProperty(prop) => {
prop.format_trailing_comments(f);
}
_ => {
unreachable!("Only `PropertyDefinition` and `AccessorProperty` can reach here");
}
}
} else {
write!(f, self.element);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class X {
prop1 = "test"; /* comment */
accessor prop2 = 1; /* comment */
static prop3; /* comment */
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
class X {
prop1 = "test"; /* comment */
accessor prop2 = 1; /* comment */
static prop3; /* comment */
}

==================== Output ====================
------------------------------
{ printWidth: 80, semi: true }
------------------------------
class X {
prop1 = "test"; /* comment */
accessor prop2 = 1; /* comment */
static prop3; /* comment */
}

-------------------------------
{ printWidth: 100, semi: true }
-------------------------------
class X {
prop1 = "test"; /* comment */
accessor prop2 = 1; /* comment */
static prop3; /* comment */
}

-------------------------------
{ printWidth: 80, semi: false }
-------------------------------
class X {
prop1 = "test" /* comment */
accessor prop2 = 1 /* comment */
static prop3 /* comment */
}

--------------------------------
{ printWidth: 100, semi: false }
--------------------------------
class X {
prop1 = "test" /* comment */
accessor prop2 = 1 /* comment */
static prop3 /* comment */
}

===================== End =====================
Loading