diff --git a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).2.minified.js index 1939d0deffb1..a56d6182ef52 100644 --- a/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).2.minified.js @@ -373,8 +373,26 @@ class C extends B { } export { }; //// [inContainingClassExprStaticField.ts] +(class { + static{ + class C extends B { + static{ + this._ = super.w(); + } + } + } +}); export { }; //// [inContainingClassExprStaticBlock.ts] +(class { + static{ + class C extends B { + static{ + super.w(); + } + } + } +}); export { }; //// [funcExprInContainingScopeStaticField.ts] class C extends B { diff --git a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs index f709ee7f07c6..9875d3875496 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/mod.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/mod.rs @@ -698,6 +698,16 @@ impl Optimizer<'_> { } Expr::Class(cls) => { + if cls + .class + .body + .iter() + .any(|m| m.as_static_block().iter().any(|s| !s.body.is_empty())) + { + // there's nothing we can do about it + return Some(Expr::Class(cls.take())); + } + let exprs: Vec> = extract_class_side_effect(&self.expr_ctx, *cls.class.take()) .into_iter() diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8271/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/8271/input.js new file mode 100644 index 000000000000..caa5b683dbb1 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8271/input.js @@ -0,0 +1,21 @@ +let $eb2fd35624c84372$var$A = (() => { + let _classDecorators = [$eb2fd35624c84372$var$CustomElement("component-a")]; + let _classDescriptor; + let _classExtraInitializers = []; + let _classThis; + let _classSuper = HTMLElement; + var A = class extends _classSuper { + static { + _classThis = this; + } + static { + console.log(123); + } + constructor() { + super(); + this.innerHTML = "Component A is working"; + } + }; + return (A = _classThis); +})(); +console.log(new $eb2fd35624c84372$var$A().tagName); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/8271/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/8271/output.js new file mode 100644 index 000000000000..ea8930a74c45 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/8271/output.js @@ -0,0 +1,16 @@ +console.log(new ((()=>{ + let _classThis; + $eb2fd35624c84372$var$CustomElement("component-a"); + let _classSuper = HTMLElement; + return class extends _classSuper { + static{ + _classThis = this; + } + static{ + console.log(123); + } + constructor(){ + super(), this.innerHTML = "Component A is working"; + } + }, _classThis; +})())().tagName); diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs index 8bd3d8fa1f12..efe0c85978fe 100644 --- a/crates/swc_ecma_utils/src/lib.rs +++ b/crates/swc_ecma_utils/src/lib.rs @@ -1523,7 +1523,11 @@ pub fn class_has_side_effect(expr_ctx: &ExprCtx, c: &Class) -> bool { } } } - + ClassMember::StaticBlock(s) => { + if !s.body.stmts.is_empty() { + return true; + } + } _ => {} } }