mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
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:
1 parent
d0dd140fcf
commit
c22a9b2742
3 files changed
+69
-1
No files matched your search
@@ -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())]);
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
class A {
|
||||
static {
|
||||
;
|
||||
}
|
||||
}
|
||||
class B {
|
||||
static {
|
||||
;
|
||||
// comment
|
||||
;
|
||||
}
|
||||
}
|
||||
class C {
|
||||
static {}
|
||||
}
|
||||
+52
@@ -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 =====================
|
||||
Reference in new issue
Block a user