Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/minifier): Drop empty static blocks #8152

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class Foo {
foo = 2;
}
Loading