From 407b72500e90ebdf5594b1e695fe80e316942897 Mon Sep 17 00:00:00 2001 From: Leonabcd123 <156839416+Leonabcd123@users.noreply.github.com> Date: Thu, 9 Apr 2026 06:27:45 +0300 Subject: [PATCH] fix(oxfmt): indent dangling comments in empty enum with block indent (#21163) Copied the dangling comment logic that we have for empty objects, empty functions and empty interfaces, and made it apply to empty enums as well. https://github.com/oxc-project/oxc/blob/a950f555c8258d5c3ff5eace062ed2dcaae01a7d/crates/oxc_formatter/src/print/object_like.rs#L109 Fixes #21145 --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- crates/oxc_formatter/src/print/mod.rs | 5 +--- .../tests/fixtures/ts/comments/enum.ts | 3 +++ .../tests/fixtures/ts/comments/enum.ts.snap | 24 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 crates/oxc_formatter/tests/fixtures/ts/comments/enum.ts create mode 100644 crates/oxc_formatter/tests/fixtures/ts/comments/enum.ts.snap diff --git a/crates/oxc_formatter/src/print/mod.rs b/crates/oxc_formatter/src/print/mod.rs index 799db4dd51..161a877f2f 100644 --- a/crates/oxc_formatter/src/print/mod.rs +++ b/crates/oxc_formatter/src/print/mod.rs @@ -1107,10 +1107,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSEnumDeclaration<'a>> { impl<'a> FormatWrite<'a> for AstNode<'a, TSEnumBody<'a>> { fn write(&self, f: &mut Formatter<'_, 'a>) { if self.members().is_empty() { - write!( - f, - group(&format_args!(format_dangling_comments(self.span()), soft_line_break())) - ); + write!(f, format_dangling_comments(self.span()).with_block_indent()); } else { write!(f, block_indent(self.members())); } diff --git a/crates/oxc_formatter/tests/fixtures/ts/comments/enum.ts b/crates/oxc_formatter/tests/fixtures/ts/comments/enum.ts new file mode 100644 index 0000000000..c7f6968d8b --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/comments/enum.ts @@ -0,0 +1,3 @@ +enum Foo { + // Comment +} diff --git a/crates/oxc_formatter/tests/fixtures/ts/comments/enum.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/comments/enum.ts.snap new file mode 100644 index 0000000000..b377e9a737 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/comments/enum.ts.snap @@ -0,0 +1,24 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +enum Foo { + // Comment +} + +==================== Output ==================== +------------------ +{ printWidth: 80 } +------------------ +enum Foo { + // Comment +} + +------------------- +{ printWidth: 100 } +------------------- +enum Foo { + // Comment +} + +===================== End =====================