Skip to content

Commit 212d8bc

Browse files
authored
fix(es/minifier): Fix cycle detection (#10950)
**Description:** The super class expression of a class declaration should be analyzed from the outside context of the class declarartions.
1 parent b5e5e8c commit 212d8bc

File tree

7 files changed

+115
-5
lines changed

7 files changed

+115
-5
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
swc_ecma_utils: patch
3+
swc_core: patch
4+
swc_ecma_lexer: patch
5+
swc_ecma_parser: patch
6+
swc_ecma_usage_analyzer: patch
7+
---
8+
9+
fix(es/minifier): Fix cycle detection
Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
//// [privateNamesConstructorChain-1.ts]
2-
import "@swc/helpers/_/_class_private_field_get";
3-
import "@swc/helpers/_/_class_private_field_init";
4-
import "@swc/helpers/_/_class_private_field_set";
5-
import "@swc/helpers/_/_class_static_private_field_spec_get";
2+
import { _ as _class_private_field_get } from "@swc/helpers/_/_class_private_field_get";
3+
import { _ as _class_private_field_init } from "@swc/helpers/_/_class_private_field_init";
4+
import { _ as _class_private_field_set } from "@swc/helpers/_/_class_private_field_set";
5+
import { _ as _class_static_private_field_spec_get } from "@swc/helpers/_/_class_static_private_field_spec_get";
6+
var _foo = /*#__PURE__*/ new WeakMap();
7+
class Parent {
8+
accessChildProps() {
9+
_class_private_field_get(new Child(), _foo), _class_static_private_field_spec_get(Child, Parent, _bar);
10+
}
11+
constructor(){
12+
_class_private_field_init(this, _foo, {
13+
writable: !0,
14+
value: void 0
15+
}), _class_private_field_set(this, _foo, 3);
16+
}
17+
}
18+
var _bar = {
19+
writable: !0,
20+
value: 5
21+
}, _foo1 = /*#__PURE__*/ new WeakMap(), _bar1 = /*#__PURE__*/ new WeakMap();
22+
class Child extends Parent {
23+
constructor(...args){
24+
super(...args), _class_private_field_init(this, _foo1, {
25+
writable: !0,
26+
value: void 0
27+
}), _class_private_field_init(this, _bar1, {
28+
writable: !0,
29+
value: void 0
30+
}), _class_private_field_set(this, _foo1, "foo"), _class_private_field_set(this, _bar1, "bar");
31+
}
32+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
(() => {
2+
class A {
3+
cycle() {
4+
return B;
5+
}
6+
}
7+
class B {
8+
cycle() {
9+
return A
10+
}
11+
}
12+
13+
class ExtendsA1 extends sideEffectWith(A) {}
14+
class Unused1 {
15+
constructor() {
16+
ExtendsA1
17+
}
18+
}
19+
class ExtendsA2 extends sideEffectWith(A) {}
20+
class Unused2 {
21+
async put() {
22+
ExtendsA2
23+
}
24+
}
25+
})();
26+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class A {
2+
cycle() {
3+
return B;
4+
}
5+
}
6+
class B {
7+
cycle() {
8+
return A;
9+
}
10+
}
11+
sideEffectWith(A), sideEffectWith(A);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
(() => {
3+
class C {
4+
cycle() {
5+
return D;
6+
}
7+
}
8+
class D {
9+
cycle() {
10+
return C
11+
}
12+
}
13+
14+
class ExtendsC extends sideEffectWith(C) {}
15+
class Unused {
16+
constructor() {
17+
ExtendsC
18+
}
19+
}
20+
})();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class C {
2+
cycle() {
3+
return D;
4+
}
5+
}
6+
class D {
7+
cycle() {
8+
return C;
9+
}
10+
}
11+
sideEffectWith(C);

crates/swc_ecma_transforms_optimization/src/simplify/dce/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,16 @@ impl Visit for Analyzer<'_> {
471471
}
472472

473473
fn visit_class_decl(&mut self, n: &ClassDecl) {
474+
if let Some(super_class) = &n.class.super_class {
475+
super_class.visit_with(self);
476+
}
477+
474478
self.with_ast_path(vec![n.ident.to_id()], |v| {
475479
let old = v.cur_class_id.take();
476480
v.cur_class_id = Some(n.ident.to_id());
477-
n.visit_children_with(v);
481+
n.ident.visit_with(v);
482+
n.class.decorators.visit_with(v);
483+
n.class.body.visit_with(v);
478484
v.cur_class_id = old;
479485

480486
if !n.class.decorators.is_empty() {

0 commit comments

Comments
 (0)