fix(es/minifier): Keep class with a static block (#8283)

**Related issue:**

 - Closes #8271.
This commit is contained in:
Austaras 2023-11-14 23:59:14 +08:00 committed by GitHub
parent 0c05f6e76e
commit 20fb5bab32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 70 additions and 1 deletions

View File

@ -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 {

View File

@ -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()

View 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);

View 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);

View File

@ -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;
}
}
_ => {}
}
}