Skip to content

Commit

Permalink
fix(es/minifier): should preserve classes with side effects in static…
Browse files Browse the repository at this point in the history
… fields
  • Loading branch information
hyf0 committed Nov 19, 2022
1 parent a4ed624 commit bf066e1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/unused.rs
Expand Up @@ -473,10 +473,22 @@ where
}

match decl {
Decl::Class(ClassDecl { ident, .. }) => {
Decl::Class(ClassDecl { ident, class, .. }) => {
if ident.sym == js_word!("arguments") {
return;
}

// Fix https://github.com/swc-project/swc/issues/5588
let may_have_side_effect = class.body.iter().any(|m| match m {
ClassMember::ClassProp(p) => p.is_static && p.value.is_some(),
ClassMember::PrivateProp(p) => p.is_static && p.value.is_some(),
ClassMember::StaticBlock(_) => true,
_ => true,
});
if may_have_side_effect {
return;
}

// If it is not used, drop it.
if self
.data
Expand Down
@@ -0,0 +1,4 @@
{
"unused": true,
"toplevel": true
}
12 changes: 12 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/5588/input.js
@@ -0,0 +1,12 @@
"use strict";
let getFoo;
let getFoo2
class Foo {
static #foo = 42;
static #_ = (getFoo2 = this.#foo)
static{
getFoo = ()=>this.#foo;
}
}
expect(getFoo()).toBe(42);
expect(getFoo2()).toBe(42);
12 changes: 12 additions & 0 deletions crates/swc_ecma_minifier/tests/fixture/issues/5588/output.js
@@ -0,0 +1,12 @@
"use strict";
let getFoo;
let getFoo2;
class Foo {
static #foo = 42;
static #_ = getFoo2 = this.#foo;
static{
getFoo = ()=>this.#foo;
}
}
expect(getFoo()).toBe(42);
expect(getFoo2()).toBe(42);

0 comments on commit bf066e1

Please sign in to comment.