Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions crates/oxc_minifier/src/peephole/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl<'a> PeepholeOptimizations {
Statement::TryStatement(s) => Self::try_fold_try(s, ctx),
Statement::LabeledStatement(s) => Self::try_fold_labeled(s, ctx),
Statement::FunctionDeclaration(f) => Self::remove_unused_function_declaration(f, ctx),
Statement::ClassDeclaration(c) => Self::remove_unused_class_declaration(c, ctx),
_ => None,
} {
*stmt = new_stmt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,51 @@ impl<'a> PeepholeOptimizations {
}
let id = f.id.as_ref()?;
let symbol_id = id.symbol_id.get()?;
if ctx.scoping().symbol_is_unused(symbol_id) {
return Some(ctx.ast.statement_empty(f.span));
if !ctx.scoping().symbol_is_unused(symbol_id) {
return None;
}
Some(ctx.ast.statement_empty(f.span))
}

pub fn remove_unused_class_declaration(
c: &Class<'a>,
ctx: &mut Ctx<'a, '_>,
) -> Option<Statement<'a>> {
if ctx.state.options.unused == CompressOptionsUnused::Keep {
return None;
}
if Self::keep_top_level_var_in_script_mode(ctx) {
return None;
}
let id = c.id.as_ref()?;
let symbol_id = id.symbol_id.get()?;
if !ctx.scoping().symbol_is_unused(symbol_id) {
return None;
}
// TODO: keep side effectful expressions and remove the class.
if c.super_class.is_some() {
return None;
}
for e in &c.body.body {
match e {
ClassElement::StaticBlock(e) => {
if !e.body.is_empty() {
return None;
}
}
ClassElement::MethodDefinition(d) => {
if d.computed {
return None;
}
}
ClassElement::PropertyDefinition(_)
| ClassElement::AccessorProperty(_)
| ClassElement::TSIndexSignature(_) => {
return None;
}
}
}
None
Some(ctx.ast.statement_empty(c.span))
}

pub fn remove_unused_assignment_expression(
Expand Down Expand Up @@ -119,6 +160,26 @@ mod test {
test_same_options("export function foo() {} foo()", &options);
}

#[test]
fn remove_unused_class_declaration() {
let options = CompressOptions::smallest();
test_options("class C {}", "", &options);
test_same_options("class C extends Foo { }", &options);

test_options("class C { static {} }", "", &options);
test_same_options("class C { static { foo } }", &options);

test_options("class C { foo() {} }", "", &options);
test_same_options("class C { [foo]() {} }", &options);

test_same_options("class C { [foo] }", &options);
test_same_options("class C { foo = bar() }", &options);
test_same_options("class C { foo = 1 }", &options);
test_same_options("class C { static foo = bar }", &options);

test_same_options("class C { accessor foo = 1}", &options);
}

#[test]
#[ignore]
fn remove_unused_assignment_expression() {
Expand All @@ -139,21 +200,26 @@ mod test {
"function foo(t) { return x(); } foo();",
&options,
);
}

#[test]
#[ignore]
fn keep_in_script_mode() {
let options = CompressOptions::smallest();
let source_type = SourceType::cjs();
test_same_options_source_type("var x = 1; x = 2;", source_type, &options);
test_same_options_source_type("var x = 1; x = 2, foo(x)", source_type, &options);

test_options_source_type(
"function foo() { var x = 1; x = 2; bar() } foo()",
"function foo() { bar() } foo()",
source_type,
&options,
);
}

#[test]
fn keep_in_script_mode() {
let options = CompressOptions::smallest();
let source_type = SourceType::cjs();
test_same_options_source_type("var x = 1; x = 2;", source_type, &options);
test_same_options_source_type("var x = 1; x = 2, foo(x)", source_type, &options);

test_options_source_type("class C {}", "class C {}", source_type, &options);
}
}
Loading