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
12 changes: 5 additions & 7 deletions crates/oxc_minifier/src/peephole/remove_unused_declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,23 +130,21 @@ mod test {
test_options("class C { foo }", "", &options);
test_options("class C { foo = bar }", "", &options);
test_options("class C { foo = 1 }", "", &options);
test_options("class C { static foo = bar }", "bar", &options);
// TODO: would be nice if this is removed but the one with `this` is kept.
test_same_options("class C { static foo = bar }", &options);
test_same_options("class C { static foo = this.bar = {} }", &options);
test_options("class C { static foo = 1 }", "", &options);
test_options("class C { [foo] = bar }", "foo", &options);
test_options("class C { [foo] = 1 }", "foo", &options);
test_options("class C { static [foo] = bar }", "foo, bar", &options);
test_same_options("class C { static [foo] = bar }", &options);
test_options("class C { static [foo] = 1 }", "foo", &options);

// accessor
test_options("class C { accessor foo = 1 }", "", &options);
test_options("class C { accessor [foo] = 1 }", "foo", &options);

// order
test_options(
"class _ extends A { static [B] = C; static [D]() {} }",
"A, B, C, D",
&options,
);
test_options("class _ extends A { [B] = C; [D]() {} }", "A, B, D", &options);

// decorators
test_same_options("class C { @dec foo() {} }", &options);
Expand Down
36 changes: 21 additions & 15 deletions crates/oxc_minifier/src/peephole/remove_unused_expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,20 +674,29 @@ impl<'a> PeepholeOptimizations {
{
return None;
}
// Keep the entire class if non-empty static block exists.
// Keep the entire class if there are class level side effects.
for e in &c.body.body {
match e {
e if e.has_decorator() => return None,
ClassElement::TSIndexSignature(_) => return None,
ClassElement::StaticBlock(block) => {
if !block.body.is_empty() {
return None;
}
ClassElement::StaticBlock(block) if !block.body.is_empty() => return None,
ClassElement::PropertyDefinition(prop)
if prop.r#static
&& prop.value.as_ref().is_some_and(|v| v.may_have_side_effects(ctx)) =>
{
return None;
}
ClassElement::AccessorProperty(prop)
if prop.r#static
&& prop.value.as_ref().is_some_and(|v| v.may_have_side_effects(ctx)) =>
{
return None;
}
_ => {}
}
}

// Otherwise extract the expressions.
let mut exprs = ctx.ast.vec();

if let Some(e) = &mut c.super_class {
Expand Down Expand Up @@ -721,9 +730,8 @@ impl<'a> PeepholeOptimizations {
ClassElement::PropertyDefinition(def) => def.value.take(),
ClassElement::AccessorProperty(def) => def.value.take(),
} {
if init.may_have_side_effects(ctx) {
exprs.push(init);
}
// Already checked side effects above.
exprs.push(init);
}
}
}
Expand Down Expand Up @@ -1115,23 +1123,21 @@ mod test {
test_options("(class { foo })", "", &options);
test_options("(class { foo = bar })", "", &options);
test_options("(class { foo = 1 })", "", &options);
test_options("(class { static foo = bar })", "bar", &options);
// TODO: would be nice if this is removed but the one with `this` is kept.
test_same_options("(class { static foo = bar })", &options);
test_same_options("(class { static foo = this.bar = {} })", &options);
test_options("(class { static foo = 1 })", "", &options);
test_options("(class { [foo] = bar })", "foo", &options);
test_options("(class { [foo] = 1 })", "foo", &options);
test_options("(class { static [foo] = bar })", "foo, bar", &options);
test_same_options("(class { static [foo] = bar })", &options);
test_options("(class { static [foo] = 1 })", "foo", &options);

// accessor
test_options("(class { accessor foo = 1 })", "", &options);
test_options("(class { accessor [foo] = 1 })", "foo", &options);

// order
test_options(
"(class extends A { static [B] = C; static [D]() {} })",
"A, B, C, D",
&options,
);
test_options("(class extends A { [B] = C; [D]() {} })", "A, B, D", &options);

// decorators
test_same_options("(class { @dec foo() {} })", &options);
Expand Down
Loading