Skip to content

Commit a9f0c45

Browse files
committed
fix(formatter): decorators and class method on the same line shouldn't be broken by a leading comment of the method (#15029)
1 parent 23660c9 commit a9f0c45

File tree

5 files changed

+27
-15
lines changed

5 files changed

+27
-15
lines changed

crates/oxc_formatter/src/utils/assignment_like.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ impl<'a> AssignmentLike<'a, '_> {
233233
}
234234
}
235235
AssignmentLike::PropertyDefinition(property) => {
236+
write!(f, [property.decorators()])?;
237+
236238
if property.declare {
237239
write!(f, ["declare", space()])?;
238240
}

crates/oxc_formatter/src/write/class.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,25 +54,14 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ClassElement<'a>>> {
5454

5555
impl<'a> Format<'a> for (&AstNode<'a, ClassElement<'a>>, Option<&AstNode<'a, ClassElement<'a>>>) {
5656
fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
57-
let decorators = match self.0.as_ast_nodes() {
58-
AstNodes::MethodDefinition(method) => {
59-
write!(f, [method.decorators(), method])
60-
}
61-
AstNodes::PropertyDefinition(property) => {
62-
write!(f, [property.decorators(), property])
63-
}
64-
AstNodes::AccessorProperty(accessor) => {
65-
write!(f, [accessor.decorators(), accessor])
66-
}
67-
_ => write!(f, self.0),
68-
};
69-
70-
write!(f, [ClassPropertySemicolon::new(self.0, self.1)])
57+
write!(f, [self.0, ClassPropertySemicolon::new(self.0, self.1)])
7158
}
7259
}
7360

7461
impl<'a> FormatWrite<'a> for AstNode<'a, MethodDefinition<'a>> {
7562
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
63+
write!(f, [self.decorators()])?;
64+
7665
if let Some(accessibility) = &self.accessibility {
7766
write!(f, [accessibility.as_str(), space()])?;
7867
}
@@ -160,6 +149,8 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticBlock<'a>> {
160149

161150
impl<'a> FormatWrite<'a> for AstNode<'a, AccessorProperty<'a>> {
162151
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
152+
write!(f, [self.decorators()])?;
153+
163154
if let Some(accessibility) = self.accessibility() {
164155
write!(f, [accessibility.as_str(), space()])?;
165156
}

crates/oxc_formatter/src/write/decorators.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ fn is_identifier_or_static_member_only(callee: &Expression) -> bool {
7373

7474
impl<'a> FormatWrite<'a> for AstNode<'a, Decorator<'a>> {
7575
fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> {
76-
self.format_leading_comments(f)?;
7776
write!(f, ["@"]);
7877

7978
// Determine if parentheses are required around decorator expressions
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class A {
2+
// comment shouldn't break the decorators grouping
3+
@memoize onContextMenu() { }
4+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: crates/oxc_formatter/tests/fixtures/mod.rs
3+
---
4+
==================== Input ====================
5+
class A {
6+
// comment shouldn't break the decorators grouping
7+
@memoize onContextMenu() { }
8+
}
9+
10+
==================== Output ====================
11+
class A {
12+
// comment shouldn't break the decorators grouping
13+
@memoize onContextMenu() {}
14+
}
15+
16+
===================== End =====================

0 commit comments

Comments
 (0)