diff --git a/crates/swc/tests/tsc-references/classStaticBlock15(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock15(target=es2022).2.minified.js index 5d81bb8b8d10..5547fe278b87 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock15(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock15(target=es2022).2.minified.js @@ -4,11 +4,5 @@ class C { static #_1 = 1; static #_3 = 3; static #_5 = 5; - static{} - static{} - static{} - static{} - static{} - static{} } console.log(_C__1); diff --git a/crates/swc_ecma_minifier/src/compress/pure/mod.rs b/crates/swc_ecma_minifier/src/compress/pure/mod.rs index 88cc235051f3..374e1196c7fe 100644 --- a/crates/swc_ecma_minifier/src/compress/pure/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/pure/mod.rs @@ -303,6 +303,28 @@ impl VisitMut for Pure<'_> { self.drop_arguments_of_symbol_call(e); } + fn visit_mut_class_member(&mut self, m: &mut ClassMember) { + m.visit_mut_children_with(self); + + if let ClassMember::StaticBlock(sb) = m { + if sb.body.stmts.is_empty() { + *m = ClassMember::Empty(EmptyStmt { span: DUMMY_SP }); + } + } + } + + fn visit_mut_class_members(&mut self, m: &mut Vec) { + m.visit_mut_children_with(self); + + m.retain(|m| { + if let ClassMember::Empty(..) = m { + return false; + } + + true + }); + } + fn visit_mut_cond_expr(&mut self, e: &mut CondExpr) { e.visit_mut_children_with(self); @@ -560,6 +582,10 @@ impl VisitMut for Pure<'_> { } } + fn visit_mut_expr_or_spreads(&mut self, nodes: &mut Vec) { + self.visit_par(nodes); + } + fn visit_mut_expr_stmt(&mut self, s: &mut ExprStmt) { s.visit_mut_children_with(self); @@ -585,18 +611,6 @@ impl VisitMut for Pure<'_> { self.visit_par(nodes); } - fn visit_mut_opt_vec_expr_or_spreads(&mut self, nodes: &mut Vec>) { - self.visit_par(nodes); - } - - fn visit_mut_expr_or_spreads(&mut self, nodes: &mut Vec) { - self.visit_par(nodes); - } - - fn visit_mut_var_declarators(&mut self, nodes: &mut Vec) { - self.visit_par(nodes); - } - fn visit_mut_fn_decl(&mut self, n: &mut FnDecl) { #[cfg(feature = "debug")] let _tracing = tracing::span!( @@ -727,6 +741,10 @@ impl VisitMut for Pure<'_> { } } + fn visit_mut_opt_vec_expr_or_spreads(&mut self, nodes: &mut Vec>) { + self.visit_par(nodes); + } + fn visit_mut_pat_or_expr(&mut self, n: &mut PatOrExpr) { n.visit_mut_children_with(self); @@ -1039,6 +1057,10 @@ impl VisitMut for Pure<'_> { } } + fn visit_mut_var_declarators(&mut self, nodes: &mut Vec) { + self.visit_par(nodes); + } + fn visit_mut_while_stmt(&mut self, s: &mut WhileStmt) { s.visit_mut_children_with(self); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8136/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/8136/input.js new file mode 100644 index 000000000000..de3ec3840ddb --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8136/input.js @@ -0,0 +1,4 @@ +export class Foo { + static { } + foo = 2; +} \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8136/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/8136/output.js new file mode 100644 index 000000000000..925a572f2301 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8136/output.js @@ -0,0 +1,3 @@ +export class Foo { + foo = 2; +}