Skip to content

Commit

Permalink
feat(es/minifier): Drop empty static blocks (#8152)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #8136
  • Loading branch information
kdy1 authored Oct 19, 2023
1 parent 64bddc1 commit 8a461b8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
46 changes: 34 additions & 12 deletions crates/swc_ecma_minifier/src/compress/pure/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClassMember>) {
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);

Expand Down Expand Up @@ -560,6 +582,10 @@ impl VisitMut for Pure<'_> {
}
}

fn visit_mut_expr_or_spreads(&mut self, nodes: &mut Vec<ExprOrSpread>) {
self.visit_par(nodes);
}

fn visit_mut_expr_stmt(&mut self, s: &mut ExprStmt) {
s.visit_mut_children_with(self);

Expand All @@ -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<Option<ExprOrSpread>>) {
self.visit_par(nodes);
}

fn visit_mut_expr_or_spreads(&mut self, nodes: &mut Vec<ExprOrSpread>) {
self.visit_par(nodes);
}

fn visit_mut_var_declarators(&mut self, nodes: &mut Vec<VarDeclarator>) {
self.visit_par(nodes);
}

fn visit_mut_fn_decl(&mut self, n: &mut FnDecl) {
#[cfg(feature = "debug")]
let _tracing = tracing::span!(
Expand Down Expand Up @@ -727,6 +741,10 @@ impl VisitMut for Pure<'_> {
}
}

fn visit_mut_opt_vec_expr_or_spreads(&mut self, nodes: &mut Vec<Option<ExprOrSpread>>) {
self.visit_par(nodes);
}

fn visit_mut_pat_or_expr(&mut self, n: &mut PatOrExpr) {
n.visit_mut_children_with(self);

Expand Down Expand Up @@ -1039,6 +1057,10 @@ impl VisitMut for Pure<'_> {
}
}

fn visit_mut_var_declarators(&mut self, nodes: &mut Vec<VarDeclarator>) {
self.visit_par(nodes);
}

fn visit_mut_while_stmt(&mut self, s: &mut WhileStmt) {
s.visit_mut_children_with(self);

Expand Down
4 changes: 4 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8136/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class Foo {
static { }
foo = 2;
}
3 changes: 3 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/8136/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Foo {
foo = 2;
}

1 comment on commit 8a461b8

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 8a461b8 Previous: 35601e4 Ratio
es/full/bugs-1 288154 ns/iter (± 7076) 282381 ns/iter (± 8088) 1.02
es/full/minify/libraries/antd 1409778002 ns/iter (± 20907463) 1384735159 ns/iter (± 13929748) 1.02
es/full/minify/libraries/d3 294144198 ns/iter (± 5133288) 297732456 ns/iter (± 4893655) 0.99
es/full/minify/libraries/echarts 1103968098 ns/iter (± 5214811) 1107397736 ns/iter (± 6664232) 1.00
es/full/minify/libraries/jquery 88907836 ns/iter (± 174035) 88852079 ns/iter (± 1369713) 1.00
es/full/minify/libraries/lodash 103802079 ns/iter (± 325285) 104605158 ns/iter (± 377377) 0.99
es/full/minify/libraries/moment 52504903 ns/iter (± 77885) 52526592 ns/iter (± 101647) 1.00
es/full/minify/libraries/react 18892001 ns/iter (± 72257) 18968471 ns/iter (± 48704) 1.00
es/full/minify/libraries/terser 230555797 ns/iter (± 800847) 229655008 ns/iter (± 974438) 1.00
es/full/minify/libraries/three 416056252 ns/iter (± 15634512) 407616515 ns/iter (± 1589913) 1.02
es/full/minify/libraries/typescript 2753296394 ns/iter (± 21088717) 2746192461 ns/iter (± 16143038) 1.00
es/full/minify/libraries/victory 583551422 ns/iter (± 3443019) 585550951 ns/iter (± 5839114) 1.00
es/full/minify/libraries/vue 125823448 ns/iter (± 565239) 126001124 ns/iter (± 432774) 1.00
es/full/codegen/es3 34072 ns/iter (± 119) 34564 ns/iter (± 84) 0.99
es/full/codegen/es5 34244 ns/iter (± 474) 34588 ns/iter (± 81) 0.99
es/full/codegen/es2015 34094 ns/iter (± 71) 34602 ns/iter (± 88) 0.99
es/full/codegen/es2016 34032 ns/iter (± 58) 34708 ns/iter (± 103) 0.98
es/full/codegen/es2017 33997 ns/iter (± 102) 34658 ns/iter (± 56) 0.98
es/full/codegen/es2018 33963 ns/iter (± 61) 34533 ns/iter (± 53) 0.98
es/full/codegen/es2019 33977 ns/iter (± 442) 34592 ns/iter (± 60) 0.98
es/full/codegen/es2020 34152 ns/iter (± 104) 34703 ns/iter (± 79) 0.98
es/full/all/es3 178420785 ns/iter (± 1167576) 180175010 ns/iter (± 1267890) 0.99
es/full/all/es5 170393647 ns/iter (± 1336212) 172481674 ns/iter (± 1242607) 0.99
es/full/all/es2015 129336173 ns/iter (± 886361) 130495456 ns/iter (± 875574) 0.99
es/full/all/es2016 128495200 ns/iter (± 1276007) 128834342 ns/iter (± 1118609) 1.00
es/full/all/es2017 127979891 ns/iter (± 852747) 129391745 ns/iter (± 844362) 0.99
es/full/all/es2018 125439696 ns/iter (± 673989) 126918409 ns/iter (± 590606) 0.99
es/full/all/es2019 125224954 ns/iter (± 589663) 126524720 ns/iter (± 375931) 0.99
es/full/all/es2020 120701020 ns/iter (± 973700) 122035228 ns/iter (± 760583) 0.99
es/full/parser 562644 ns/iter (± 3565) 585561 ns/iter (± 2693) 0.96
es/full/base/fixer 19316 ns/iter (± 83) 20049 ns/iter (± 421) 0.96
es/full/base/resolver_and_hygiene 83363 ns/iter (± 436) 84265 ns/iter (± 257) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.