diff --git a/crates/oxc_formatter/src/utils/assignment_like.rs b/crates/oxc_formatter/src/utils/assignment_like.rs index 40cfdc95122a2..5443de2a92bcb 100644 --- a/crates/oxc_formatter/src/utils/assignment_like.rs +++ b/crates/oxc_formatter/src/utils/assignment_like.rs @@ -233,6 +233,8 @@ impl<'a> AssignmentLike<'a, '_> { } } AssignmentLike::PropertyDefinition(property) => { + write!(f, [property.decorators()])?; + if property.declare { write!(f, ["declare", space()])?; } diff --git a/crates/oxc_formatter/src/write/class.rs b/crates/oxc_formatter/src/write/class.rs index 518b8bf15c354..d768f75c2b6e4 100644 --- a/crates/oxc_formatter/src/write/class.rs +++ b/crates/oxc_formatter/src/write/class.rs @@ -54,25 +54,14 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, ClassElement<'a>>> { impl<'a> Format<'a> for (&AstNode<'a, ClassElement<'a>>, Option<&AstNode<'a, ClassElement<'a>>>) { fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - let decorators = match self.0.as_ast_nodes() { - AstNodes::MethodDefinition(method) => { - write!(f, [method.decorators(), method]) - } - AstNodes::PropertyDefinition(property) => { - write!(f, [property.decorators(), property]) - } - AstNodes::AccessorProperty(accessor) => { - write!(f, [accessor.decorators(), accessor]) - } - _ => write!(f, self.0), - }; - - write!(f, [ClassPropertySemicolon::new(self.0, self.1)]) + write!(f, [self.0, ClassPropertySemicolon::new(self.0, self.1)]) } } impl<'a> FormatWrite<'a> for AstNode<'a, MethodDefinition<'a>> { fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { + write!(f, [self.decorators()])?; + if let Some(accessibility) = &self.accessibility { write!(f, [accessibility.as_str(), space()])?; } @@ -160,6 +149,8 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticBlock<'a>> { impl<'a> FormatWrite<'a> for AstNode<'a, AccessorProperty<'a>> { fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { + write!(f, [self.decorators()])?; + if let Some(accessibility) = self.accessibility() { write!(f, [accessibility.as_str(), space()])?; } diff --git a/crates/oxc_formatter/src/write/decorators.rs b/crates/oxc_formatter/src/write/decorators.rs index 05e9b7f776df0..7de4ee2c165fa 100644 --- a/crates/oxc_formatter/src/write/decorators.rs +++ b/crates/oxc_formatter/src/write/decorators.rs @@ -73,7 +73,6 @@ fn is_identifier_or_static_member_only(callee: &Expression) -> bool { impl<'a> FormatWrite<'a> for AstNode<'a, Decorator<'a>> { fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - self.format_leading_comments(f)?; write!(f, ["@"]); // Determine if parentheses are required around decorator expressions diff --git a/crates/oxc_formatter/tests/fixtures/ts/class/decorators.ts b/crates/oxc_formatter/tests/fixtures/ts/class/decorators.ts new file mode 100644 index 0000000000000..1fcc3717fcf2d --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/class/decorators.ts @@ -0,0 +1,4 @@ +class A { + // comment shouldn't break the decorators grouping + @memoize onContextMenu() { } +} diff --git a/crates/oxc_formatter/tests/fixtures/ts/class/decorators.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/class/decorators.ts.snap new file mode 100644 index 0000000000000..b1650537a742b --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/class/decorators.ts.snap @@ -0,0 +1,16 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +class A { + // comment shouldn't break the decorators grouping + @memoize onContextMenu() { } +} + +==================== Output ==================== +class A { + // comment shouldn't break the decorators grouping + @memoize onContextMenu() {} +} + +===================== End =====================