mirror of
https://github.com/swc-project/swc.git
synced 2024-12-28 08:04:43 +03:00
fix(es/minifier): Keep class with a static block (#8283)
**Related issue:** - Closes #8271.
This commit is contained in:
parent
0c05f6e76e
commit
20fb5bab32
@ -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 {
|
||||
|
@ -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<Box<Expr>> =
|
||||
extract_class_side_effect(&self.expr_ctx, *cls.class.take())
|
||||
.into_iter()
|
||||
|
21
crates/swc_ecma_minifier/tests/fixture/issues/8271/input.js
Normal file
21
crates/swc_ecma_minifier/tests/fixture/issues/8271/input.js
Normal file
@ -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);
|
16
crates/swc_ecma_minifier/tests/fixture/issues/8271/output.js
Normal file
16
crates/swc_ecma_minifier/tests/fixture/issues/8271/output.js
Normal file
@ -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);
|
@ -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;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user