fix(formatter): treat static block with only empty statements as empty (#27945)

Fixed panic on `{ ; }`.

`EmptyStatement` is removed, then `group` for `StaticBlock` failed to get its content.
This commit is contained in:
leaysgur committed 2026-10-19 01:42:24 +00:00
1 parent d0dd140fcf
commit c22a9b2742
3 files changed
+69 -1

No files matched your search

+2 -1
View File
@@ -18,6 +18,7 @@ use crate::{
},
parentheses::NeedsParentheses,
print::{
block_statement::is_empty_block,
function::{
function_content_end, function_signature_end, should_group_function_parameters,
},
@@ -174,7 +175,7 @@ impl<'a> FormatWrite<'a> for AstNode<'a, StaticBlock<'a>> {
fn write(&self, f: &mut JsFormatter<'_, 'a>) {
write!(f, ["static", space(), "{"]);
if self.body.is_empty() {
if is_empty_block(&self.body) {
write!(f, [format_dangling_comments(self.span).with_block_indent()]);
} else {
write!(f, [block_indent(&self.body())]);
@@ -0,0 +1,15 @@
class A {
static {
;
}
}
class B {
static {
;
// comment
;
}
}
class C {
static {}
}
@@ -0,0 +1,52 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
class A {
static {
;
}
}
class B {
static {
;
// comment
;
}
}
class C {
static {}
}
==================== Output ====================
------------------
{ printWidth: 80 }
------------------
class A {
static {}
}
class B {
static {
// comment
}
}
class C {
static {}
}
-------------------
{ printWidth: 100 }
-------------------
class A {
static {}
}
class B {
static {
// comment
}
}
class C {
static {}
}
===================== End =====================