Skip to content

Commit 8d906b4

Browse files
authoredNov 24, 2022
fix(es/minifier): Preserve classes with side effects in static fields (#6480)
1 parent 6fd9797 commit 8d906b4

25 files changed

+612
-78
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
11
//// [classStaticBlock15.ts]
2-
console.log(void 0);
2+
var _C__1;
3+
class C {
4+
static #_1 = 1;
5+
static #_3 = 3;
6+
static #_5 = 5;
7+
static{}
8+
static{}
9+
static{}
10+
static{}
11+
static{}
12+
static{}
13+
}
14+
console.log(_C__1);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//// [classStaticBlock23.ts]
2-
[
2+
const nums = [
33
1,
44
2,
55
3
66
].map((n)=>Promise.resolve(n));
7+
class C {
8+
static{
9+
for await (let nn of nums)console.log(nn);
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,12 @@
11
//// [classStaticBlock5.ts]
2-
super.a;
2+
class B {
3+
static a = 1;
4+
static b = 2;
5+
}
6+
class C extends B {
7+
static b = 3;
8+
static c = super.a;
9+
static{
10+
this.b, super.b, super.a;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
//// [classStaticBlockUseBeforeDef1.ts]
2-
this.x;
2+
class C {
3+
static{
4+
this.x = 1;
5+
}
6+
static y = this.x;
7+
static{
8+
this.z = this.y;
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
//// [classStaticBlockUseBeforeDef2.ts]
2+
class C {
3+
static{
4+
this.x = 1;
5+
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
11
//// [privateNameComputedPropertyName4.ts]
2+
class C1 {
3+
static #qux = 42;
4+
bar() {}
5+
}
6+
class C2 {
7+
static #qux = 42;
8+
static bar() {}
9+
}
10+
class C3 {
11+
static #qux = 42;
12+
static bar = "test";
13+
}

‎crates/swc/tests/tsc-references/superInStaticMembers1(target=es2022).2.minified.js

+241-37
Original file line numberDiff line numberDiff line change
@@ -7,150 +7,354 @@ Baz || (Baz = {});
77
export default class {
88
}
99
//// [locals.ts]
10-
super.w(), (()=>{
11-
var { Reflect } = {
12-
Reflect: null
13-
};
14-
super.w();
15-
})(), (()=>{
16-
var [Reflect] = [
17-
null
10+
class C extends B {
11+
static _ = [
12+
void super.w(),
13+
(()=>{
14+
var { Reflect } = {
15+
Reflect: null
16+
};
17+
super.w();
18+
})(),
19+
(()=>{
20+
var [Reflect] = [
21+
null
22+
];
23+
super.w();
24+
})(),
25+
void super.w(),
26+
void super.w(),
27+
(()=>{
28+
let Reflect;
29+
Reflect || (Reflect = {}), super.w();
30+
})(),
31+
(()=>{
32+
let Reflect;
33+
Reflect || (Reflect = {}), super.w();
34+
})(),
35+
void super.w(),
36+
void super.w(),
37+
void super.w(),
38+
void super.w()
1839
];
19-
super.w();
20-
})(), super.w(), super.w(), (()=>{
21-
let Reflect;
22-
Reflect || (Reflect = {}), super.w();
23-
})(), (()=>{
24-
let Reflect;
25-
Reflect || (Reflect = {}), super.w();
26-
})(), super.w(), super.w(), super.w(), super.w();
40+
static{
41+
var { Reflect } = {
42+
Reflect: null
43+
};
44+
super.w();
45+
}
46+
static{
47+
var [Reflect1] = [
48+
null
49+
];
50+
super.w();
51+
}
52+
static{
53+
super.w();
54+
}
55+
static{
56+
super.w();
57+
}
58+
static{
59+
super.w();
60+
}
61+
static{
62+
let Reflect2;
63+
Reflect2 || (Reflect2 = {}), super.w();
64+
}
65+
static{
66+
let Reflect3;
67+
Reflect3 || (Reflect3 = {}), super.w();
68+
}
69+
static{
70+
super.w();
71+
}
72+
static{
73+
super.w();
74+
}
75+
static{
76+
super.w();
77+
}
78+
static{
79+
super.w();
80+
}
81+
}
2782
export { };
2883
//// [varInContainingScopeStaticField1.ts]
29-
super.w();
84+
class C extends B {
85+
static _ = super.w();
86+
}
3087
export { };
3188
//// [varInContainingScopeStaticField2.ts]
3289
var { Reflect } = {
3390
Reflect: null
3491
};
35-
super.w();
92+
class C extends B {
93+
static _ = super.w();
94+
}
3695
export { };
3796
//// [varInContainingScopeStaticField3.ts]
3897
var [Reflect] = [
3998
null
4099
];
41-
super.w();
100+
class C extends B {
101+
static _ = super.w();
102+
}
42103
export { };
43104
//// [varInContainingScopeStaticBlock1.ts]
105+
class C extends B {
106+
static{
107+
super.w();
108+
}
109+
}
44110
export { };
45111
//// [varInContainingScopeStaticBlock2.ts]
46112
var { Reflect } = {
47113
Reflect: null
48114
};
115+
class C extends B {
116+
static{
117+
super.w();
118+
}
119+
}
49120
export { };
50121
//// [varInContainingScopeStaticBlock3.ts]
51122
var [Reflect] = [
52123
null
53124
];
125+
class C extends B {
126+
static{
127+
super.w();
128+
}
129+
}
54130
export { };
55131
//// [classDeclInContainingScopeStaticField.ts]
56-
super.w();
132+
class C extends B {
133+
static _ = super.w();
134+
}
57135
export { };
58136
//// [classDeclInContainingScopeStaticBlock.ts]
137+
class C extends B {
138+
static{
139+
super.w();
140+
}
141+
}
59142
export { };
60143
//// [funcDeclInContainingScopeStaticField.ts]
61-
super.w();
144+
class C extends B {
145+
static _ = super.w();
146+
}
62147
export { };
63148
//// [funcDeclInContainingScopeStaticBlock.ts]
149+
class C extends B {
150+
static{
151+
super.w();
152+
}
153+
}
64154
export { };
65155
//// [valueNamespaceInContainingScopeStaticField.ts]
66-
super.w();
156+
class C extends B {
157+
static _ = super.w();
158+
}
67159
export { };
68160
//// [valueNamespaceInContainingScopeStaticBlock.ts]
161+
class C extends B {
162+
static{
163+
super.w();
164+
}
165+
}
69166
export { };
70167
//// [enumInContainingScopeStaticField.ts]
71168
var Reflect;
72-
Reflect || (Reflect = {}), super.w();
169+
Reflect || (Reflect = {});
170+
class C extends B {
171+
static _ = super.w();
172+
}
73173
export { };
74174
//// [enumInContainingScopeStaticBlock.ts]
75175
var Reflect;
76176
Reflect || (Reflect = {});
177+
class C extends B {
178+
static{
179+
super.w();
180+
}
181+
}
77182
export { };
78183
//// [constEnumInContainingScopeStaticField.ts]
79184
var Reflect;
80-
Reflect || (Reflect = {}), super.w();
185+
Reflect || (Reflect = {});
186+
class C extends B {
187+
static _ = super.w();
188+
}
81189
export { };
82190
//// [constEnumInContainingScopeStaticBlock.ts]
83191
var Reflect;
84192
Reflect || (Reflect = {});
193+
class C extends B {
194+
static{
195+
super.w();
196+
}
197+
}
85198
export { };
86199
//// [namespaceImportInContainingScopeStaticField.ts]
87-
super.w();
200+
class C extends B {
201+
static _ = super.w();
202+
}
88203
export { };
89204
//// [namespaceImportInContainingScopeStaticBlock.ts]
205+
class C extends B {
206+
static{
207+
super.w();
208+
}
209+
}
90210
export { };
91211
//// [namedImportInContainingScopeStaticField.ts]
92-
super.w();
212+
class C extends B {
213+
static _ = super.w();
214+
}
93215
export { };
94216
//// [namedImportInContainingScopeStaticBlock.ts]
217+
class C extends B {
218+
static{
219+
super.w();
220+
}
221+
}
95222
export { };
96223
//// [namedImportOfInterfaceInContainingScopeStaticField.ts]
97-
super.w();
224+
class C extends B {
225+
static _ = super.w();
226+
}
98227
export { };
99228
//// [namedImportOfInterfaceInContainingScopeStaticBlock.ts]
229+
class C extends B {
230+
static{
231+
super.w();
232+
}
233+
}
100234
export { };
101235
//// [namedImportOfUninstantiatedNamespaceInContainingScopeStaticField.ts]
102-
super.w();
236+
class C extends B {
237+
static _ = super.w();
238+
}
103239
export { };
104240
//// [namedImportOfUninstantiatedNamespaceInContainingScopeStaticBlock.ts]
241+
class C extends B {
242+
static{
243+
super.w();
244+
}
245+
}
105246
export { };
106247
//// [namedImportOfConstEnumInContainingScopeStaticField.ts]
107-
super.w();
248+
class C extends B {
249+
static _ = super.w();
250+
}
108251
export { };
109252
//// [namedImportOfConstEnumInContainingScopeStaticBlock.ts]
253+
class C extends B {
254+
static{
255+
super.w();
256+
}
257+
}
110258
export { };
111259
//// [typeOnlyNamedImportInContainingScopeStaticField.ts]
112-
super.w();
260+
class C extends B {
261+
static _ = super.w();
262+
}
113263
export { };
114264
//// [typeOnlyNamedImportInContainingScopeStaticBlock.ts]
265+
class C extends B {
266+
static{
267+
super.w();
268+
}
269+
}
115270
export { };
116271
//// [defaultImportInContainingScopeStaticField.ts]
117-
super.w();
272+
class C extends B {
273+
static _ = super.w();
274+
}
118275
export { };
119276
//// [defaultImportInContainingScopeStaticBlock.ts]
277+
class C extends B {
278+
static{
279+
super.w();
280+
}
281+
}
120282
export { };
121283
//// [typeOnlyDefaultImportInContainingScopeStaticField.ts]
122-
super.w();
284+
class C extends B {
285+
static _ = super.w();
286+
}
123287
export { };
124288
//// [typeOnlyDefaultImportInContainingScopeStaticBlock.ts]
289+
class C extends B {
290+
static{
291+
super.w();
292+
}
293+
}
125294
export { };
126295
//// [typeInContainingScopeStaticField.ts]
127-
super.w();
296+
class C extends B {
297+
static _ = super.w();
298+
}
128299
export { };
129300
//// [typeInContainingScopeStaticBlock.ts]
301+
class C extends B {
302+
static{
303+
super.w();
304+
}
305+
}
130306
export { };
131307
//// [interfaceInContainingScopeStaticField.ts]
132-
super.w();
308+
class C extends B {
309+
static _ = super.w();
310+
}
133311
export { };
134312
//// [interfaceInContainingScopeStaticBlock.ts]
313+
class C extends B {
314+
static{
315+
super.w();
316+
}
317+
}
135318
export { };
136319
//// [uninstantiatedNamespaceInContainingScopeStaticField.ts]
137-
super.w();
320+
class C extends B {
321+
static _ = super.w();
322+
}
138323
export { };
139324
//// [uninstantiatedNamespaceInContainingScopeStaticBlock.ts]
325+
class C extends B {
326+
static{
327+
super.w();
328+
}
329+
}
140330
export { };
141331
//// [classExprInContainingScopeStaticField.ts]
142-
super.w();
332+
class C extends B {
333+
static _ = super.w();
334+
}
143335
export { };
144336
//// [classExprInContainingScopeStaticBlock.ts]
337+
class C extends B {
338+
static{
339+
super.w();
340+
}
341+
}
145342
export { };
146343
//// [inContainingClassExprStaticField.ts]
147344
export { };
148345
//// [inContainingClassExprStaticBlock.ts]
149346
export { };
150347
//// [funcExprInContainingScopeStaticField.ts]
151-
super.w();
348+
class C extends B {
349+
static _ = super.w();
350+
}
152351
export { };
153352
//// [funcExprInContainingScopeStaticBlock.ts]
353+
class C extends B {
354+
static{
355+
super.w();
356+
}
357+
}
154358
export { };
155359
//// [inContainingFuncExprStaticField.ts]
156360
export { };
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
//// [thisAndSuperInStaticMembers1.ts]
2-
this.x, this.x(), this?.x(), this.x(), this?.x(), super.a, super.a, super.f(), super.f(), super.a = 0, super.a += 1, super.a = 0, [super.a] = [
3-
0
4-
], [super.a = 0] = [
5-
0
6-
], [...super.a] = [
7-
0
8-
], ({ x: super.a } = {
9-
x: 0
10-
}), ({ x: super.a = 0 } = {
11-
x: 0
12-
}), ({ ...super.a } = {
13-
x: 0
14-
}), ++super.a, --super.a, ++super.a, super.a++, super.a``;
2+
class C extends B {
3+
static x = void 0;
4+
static y1 = this.x;
5+
static y2 = this.x();
6+
static y3 = this?.x();
7+
static y4 = this.x();
8+
static y5 = this?.x();
9+
static z1 = super.a;
10+
static z2 = super.a;
11+
static z3 = super.f();
12+
static z4 = super.f();
13+
static z5 = super.a = 0;
14+
static z6 = super.a += 1;
15+
static z7 = void (super.a = 0);
16+
static z8 = [super.a] = [
17+
0
18+
];
19+
static z9 = [super.a = 0] = [
20+
0
21+
];
22+
static z10 = [...super.a] = [
23+
0
24+
];
25+
static z11 = { x: super.a } = {
26+
x: 0
27+
};
28+
static z12 = { x: super.a = 0 } = {
29+
x: 0
30+
};
31+
static z13 = { ...super.a } = {
32+
x: 0
33+
};
34+
static z14 = ++super.a;
35+
static z15 = --super.a;
36+
static z16 = ++super.a;
37+
static z17 = super.a++;
38+
static z18 = super.a``;
39+
x = 1;
40+
y = this.x;
41+
z = super.f();
42+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,42 @@
11
//// [thisAndSuperInStaticMembers2.ts]
2-
this.x, this.x(), this?.x(), this.x(), this?.x(), super.a, super.a, super.f(), super.f(), super.a = 0, super.a += 1, super.a = 0, [super.a] = [
3-
0
4-
], [super.a = 0] = [
5-
0
6-
], [...super.a] = [
7-
0
8-
], ({ x: super.a } = {
9-
x: 0
10-
}), ({ x: super.a = 0 } = {
11-
x: 0
12-
}), ({ ...super.a } = {
13-
x: 0
14-
}), ++super.a, --super.a, ++super.a, super.a++, super.a``;
2+
class C extends B {
3+
static x = void 0;
4+
static y1 = this.x;
5+
static y2 = this.x();
6+
static y3 = this?.x();
7+
static y4 = this.x();
8+
static y5 = this?.x();
9+
static z1 = super.a;
10+
static z2 = super.a;
11+
static z3 = super.f();
12+
static z4 = super.f();
13+
static z5 = super.a = 0;
14+
static z6 = super.a += 1;
15+
static z7 = void (super.a = 0);
16+
static z8 = [super.a] = [
17+
0
18+
];
19+
static z9 = [super.a = 0] = [
20+
0
21+
];
22+
static z10 = [...super.a] = [
23+
0
24+
];
25+
static z11 = { x: super.a } = {
26+
x: 0
27+
};
28+
static z12 = { x: super.a = 0 } = {
29+
x: 0
30+
};
31+
static z13 = { ...super.a } = {
32+
x: 0
33+
};
34+
static z14 = ++super.a;
35+
static z15 = --super.a;
36+
static z16 = ++super.a;
37+
static z17 = super.a++;
38+
static z18 = super.a``;
39+
x = 1;
40+
y = this.x;
41+
z = super.f();
42+
}

‎crates/swc/tests/tsc-references/typeOfThisInStaticMembers10(target=es2022).2.minified.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,26 @@ let D = class extends C {
2727
};
2828
D = _ts_decorate([
2929
foo
30-
], D), this.a, this.c, super.a, this.c;
30+
], D);
31+
class CC {
32+
static a = 1;
33+
static b = this.a + 1;
34+
}
35+
class DD extends CC {
36+
static c = 2;
37+
static d = this.c + 1;
38+
static e = super.a + this.c + 1;
39+
static f = ()=>this.c + 1;
40+
static ff = function() {
41+
this.c;
42+
};
43+
static foo() {
44+
return this.c + 1;
45+
}
46+
static get fa() {
47+
return this.c + 1;
48+
}
49+
static set fa(v) {
50+
this.c = v + 1;
51+
}
52+
}

‎crates/swc/tests/tsc-references/typeOfThisInStaticMembers11(target=es2022).2.minified.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,26 @@ let D = class extends C {
2727
};
2828
D = _ts_decorate([
2929
foo
30-
], D), this.a, this.c, super.a, this.c;
30+
], D);
31+
class CC {
32+
static a = 1;
33+
static b = this.a + 1;
34+
}
35+
class DD extends CC {
36+
static c = 2;
37+
static d = this.c + 1;
38+
static e = super.a + this.c + 1;
39+
static f = ()=>this.c + 1;
40+
static ff = function() {
41+
this.c;
42+
};
43+
static foo() {
44+
return this.c + 1;
45+
}
46+
static get fa() {
47+
return this.c + 1;
48+
}
49+
static set fa(v) {
50+
this.c = v + 1;
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
//// [typeOfThisInStaticMembers12.ts]
2-
this.c, this.c;
2+
class C {
3+
static c = "foo";
4+
static bar = class {
5+
static [this.c] = 123;
6+
[this.c] = 123;
7+
};
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
//// [typeOfThisInStaticMembers13.ts]
2-
this.c, this.c;
2+
class C {
3+
static c = "foo";
4+
static bar = class {
5+
static [this.c] = 123;
6+
[this.c] = 123;
7+
};
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
//// [typeOfThisInStaticMembers3.ts]
2-
this.a, this.c, super.a, this.c;
2+
class C {
3+
static a = 1;
4+
static b = this.a + 1;
5+
}
6+
class D extends C {
7+
static c = 2;
8+
static d = this.c + 1;
9+
static e = super.a + this.c + 1;
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
//// [typeOfThisInStaticMembers4.ts]
2-
this.a, this.c, super.a, this.c;
2+
class C {
3+
static a = 1;
4+
static b = this.a + 1;
5+
}
6+
class D extends C {
7+
static c = 2;
8+
static d = this.c + 1;
9+
static e = super.a + this.c + 1;
10+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
//// [typeOfThisInStaticMembers7.ts]
2-
this.a, this.c, super.a, this.c;
2+
class C {
3+
static a = 1;
4+
static b = this.a + 1;
5+
}
6+
class D extends C {
7+
static c = 2;
8+
static d = this.c + 1;
9+
static e = 1 + super.a + (this.c + 1) + 1;
10+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
11
//// [typeOfThisInStaticMembers8.ts]
2+
class C {
3+
static f = 1;
4+
static arrowFunctionBoundary = ()=>this.f + 1;
5+
static functionExprBoundary = function() {
6+
return this.f + 2;
7+
};
8+
static classExprBoundary = class {
9+
a = this.f + 3;
10+
};
11+
static functionAndClassDeclBoundary = void 0;
12+
}
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
11
//// [typeOfThisInStaticMembers9.ts]
2+
class C {
3+
static f = 1;
4+
}
5+
class D extends C {
6+
static arrowFunctionBoundary = ()=>super.f + 1;
7+
static functionExprBoundary = function() {
8+
return super.f + 2;
9+
};
10+
static classExprBoundary = class {
11+
a = super.f + 3;
12+
};
13+
static functionAndClassDeclBoundary = void 0;
14+
}

‎crates/swc/tests/tsc-references/uniqueSymbols.2.minified.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,34 @@ class C {
88
readonlyCall = Symbol();
99
readwriteCall = Symbol();
1010
}
11-
C.readonlyStaticCall, C.readonlyStaticType, C.readonlyStaticTypeAndCall, C.readwriteStaticCall, C.readonlyStaticCall, C.readonlyStaticType, C.readonlyStaticTypeAndCall, C.readwriteStaticCall, c.readonlyCall, c.readwriteCall, c.readonlyCall, c.readwriteCall, c.readonlyCall, c.readwriteCall, i.readonlyType, i.readonlyType, i.readonlyType, l.readonlyType, l.nested.readonlyNestedType, l.readonlyType, l.nested.readonlyNestedType, l.readonlyType, l.nested.readonlyNestedType, Promise.resolve(constCall), f(s), f(N.s), f(N.s), N.s, N.s, N.s, N.s, N.s, N.s, N.s, N.s, o[s], o[N.s], o[N.s], f(s), f(N.s), f(N.s), g(s), g(N.s), g(N.s), N.s, N.s, 2 * Math.random() && N.s, 2 * Math.random() && N.s, N.s;
11+
C.readonlyStaticCall, C.readonlyStaticType, C.readonlyStaticTypeAndCall, C.readwriteStaticCall, C.readonlyStaticCall, C.readonlyStaticType, C.readonlyStaticTypeAndCall, C.readwriteStaticCall, c.readonlyCall, c.readwriteCall, c.readonlyCall, c.readwriteCall, c.readonlyCall, c.readwriteCall, i.readonlyType, i.readonlyType, i.readonlyType, l.readonlyType, l.nested.readonlyNestedType, l.readonlyType, l.nested.readonlyNestedType, l.readonlyType, l.nested.readonlyNestedType, Promise.resolve(constCall), f(s), f(N.s), f(N.s), N.s, N.s, N.s, N.s, (p = s)=>p;
12+
class C0 {
13+
static a = s;
14+
static b = N.s;
15+
static c = N.s;
16+
static d = s;
17+
static e = N.s;
18+
static f = N.s;
19+
a = s;
20+
b = N.s;
21+
c = N.s;
22+
d = s;
23+
e = N.s;
24+
f = N.s;
25+
method1() {
26+
return s;
27+
}
28+
async method2() {
29+
return s;
30+
}
31+
async *method3() {
32+
yield s;
33+
}
34+
*method4() {
35+
yield s;
36+
}
37+
method5(p = s) {
38+
return p;
39+
}
40+
}
41+
o[s], o[N.s], o[N.s], f(s), f(N.s), f(N.s), g(s), g(N.s), g(N.s), N.s, N.s, 2 * Math.random() && N.s, 2 * Math.random() && N.s, N.s;

‎crates/swc/tests/tsc-references/uniqueSymbolsDeclarations.2.minified.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,34 @@ class C {
88
readonlyCall = Symbol();
99
readwriteCall = Symbol();
1010
}
11-
C.readonlyStaticCall, C.readonlyStaticType, C.readonlyStaticTypeAndCall, C.readwriteStaticCall, C.readonlyStaticCall, C.readonlyStaticType, C.readonlyStaticTypeAndCall, C.readwriteStaticCall, c.readonlyCall, c.readwriteCall, c.readonlyCall, c.readwriteCall, c.readonlyCall, c.readwriteCall, i.readonlyType, i.readonlyType, i.readonlyType, l.readonlyType, l.nested.readonlyNestedType, l.readonlyType, l.nested.readonlyNestedType, l.readonlyType, l.nested.readonlyNestedType, Promise.resolve(constCall), f(s), f(N.s), f(N.s), N.s, N.s, N.s, N.s, N.s, N.s, N.s, N.s, o[s], o[N.s], o[N.s], f(s), f(N.s), f(N.s), g(s), g(N.s), g(N.s), N.s, N.s, 2 * Math.random() && N.s, 2 * Math.random() && N.s, N.s;
11+
C.readonlyStaticCall, C.readonlyStaticType, C.readonlyStaticTypeAndCall, C.readwriteStaticCall, C.readonlyStaticCall, C.readonlyStaticType, C.readonlyStaticTypeAndCall, C.readwriteStaticCall, c.readonlyCall, c.readwriteCall, c.readonlyCall, c.readwriteCall, c.readonlyCall, c.readwriteCall, i.readonlyType, i.readonlyType, i.readonlyType, l.readonlyType, l.nested.readonlyNestedType, l.readonlyType, l.nested.readonlyNestedType, l.readonlyType, l.nested.readonlyNestedType, Promise.resolve(constCall), f(s), f(N.s), f(N.s), N.s, N.s, N.s, N.s, (p = s)=>p;
12+
class C0 {
13+
static a = s;
14+
static b = N.s;
15+
static c = N.s;
16+
static d = s;
17+
static e = N.s;
18+
static f = N.s;
19+
a = s;
20+
b = N.s;
21+
c = N.s;
22+
d = s;
23+
e = N.s;
24+
f = N.s;
25+
method1() {
26+
return s;
27+
}
28+
async method2() {
29+
return s;
30+
}
31+
async *method3() {
32+
yield s;
33+
}
34+
*method4() {
35+
yield s;
36+
}
37+
method5(p = s) {
38+
return p;
39+
}
40+
}
41+
o[s], o[N.s], o[N.s], f(s), f(N.s), f(N.s), g(s), g(N.s), g(N.s), N.s, N.s, 2 * Math.random() && N.s, 2 * Math.random() && N.s, N.s;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
//// [uniqueSymbolsDeclarationsInJs.js]
2-
Symbol(), Symbol(), Symbol(), Symbol();
2+
class C {
3+
static readonlyStaticCall = Symbol();
4+
static readonlyStaticTypeAndCall = Symbol();
5+
static readwriteStaticCall = Symbol();
6+
readonlyCall = Symbol();
7+
readwriteCall = Symbol();
8+
}
9+
Symbol();

‎crates/swc_ecma_minifier/src/compress/optimize/unused.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,33 @@ where
473473
}
474474

475475
match decl {
476-
Decl::Class(ClassDecl { ident, .. }) => {
476+
Decl::Class(ClassDecl { ident, class, .. }) => {
477477
if ident.sym == js_word!("arguments") {
478478
return;
479479
}
480+
481+
// Fix https://github.com/swc-project/swc/issues/5588
482+
let may_have_side_effect = class.body.iter().any(|m| match m {
483+
ClassMember::ClassProp(ClassProp {
484+
is_static: true,
485+
value: Some(_),
486+
..
487+
})
488+
| ClassMember::PrivateProp(PrivateProp {
489+
is_static: true,
490+
value: Some(_),
491+
..
492+
}) => true,
493+
ClassMember::StaticBlock(StaticBlock {
494+
body: BlockStmt { stmts, .. },
495+
..
496+
}) if !stmts.is_empty() => true,
497+
_ => false,
498+
});
499+
if may_have_side_effect {
500+
return;
501+
}
502+
480503
// If it is not used, drop it.
481504
if self
482505
.data
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"unused": true,
3+
"toplevel": true
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
let getFoo;
3+
let getFoo2
4+
class Foo {
5+
static #foo = 42;
6+
static #_ = getFoo2 = this.#foo
7+
static {
8+
getFoo = () => this.#foo;
9+
}
10+
}
11+
expect(getFoo()).toBe(42);
12+
expect(getFoo2()).toBe(42);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
let getFoo;
3+
let getFoo2;
4+
class Foo {
5+
static #foo = 42;
6+
static #_ = getFoo2 = this.#foo;
7+
static{
8+
getFoo = ()=>this.#foo;
9+
}
10+
}
11+
expect(getFoo()).toBe(42);
12+
expect(getFoo2()).toBe(42);

1 commit comments

Comments
 (1)

github-actions[bot] commented on Nov 24, 2022

@github-actions[bot]

Benchmark

Benchmark suite Current: 8d906b4 Previous: b3b5526 Ratio
es/full/bugs-1 339249 ns/iter (± 19114) 348064 ns/iter (± 21090) 0.97
es/full/minify/libraries/antd 1926569394 ns/iter (± 34703410) 1861224113 ns/iter (± 18958664) 1.04
es/full/minify/libraries/d3 435442912 ns/iter (± 7649321) 389988874 ns/iter (± 6870003) 1.12
es/full/minify/libraries/echarts 1624471008 ns/iter (± 28803665) 1577763775 ns/iter (± 16905140) 1.03
es/full/minify/libraries/jquery 102257399 ns/iter (± 8813358) 98646680 ns/iter (± 2204385) 1.04
es/full/minify/libraries/lodash 130135274 ns/iter (± 4286135) 116542380 ns/iter (± 1726326) 1.12
es/full/minify/libraries/moment 63941126 ns/iter (± 3293122) 59429737 ns/iter (± 2548244) 1.08
es/full/minify/libraries/react 21328940 ns/iter (± 1355804) 19875033 ns/iter (± 430279) 1.07
es/full/minify/libraries/terser 325367259 ns/iter (± 28015923) 295769946 ns/iter (± 4592453) 1.10
es/full/minify/libraries/three 578632415 ns/iter (± 18338390) 556747640 ns/iter (± 9272686) 1.04
es/full/minify/libraries/typescript 3437266357 ns/iter (± 47791599) 3380624220 ns/iter (± 30967521) 1.02
es/full/minify/libraries/victory 856744228 ns/iter (± 28800418) 823330763 ns/iter (± 8991560) 1.04
es/full/minify/libraries/vue 158569815 ns/iter (± 7575003) 149893326 ns/iter (± 3945108) 1.06
es/full/codegen/es3 34035 ns/iter (± 1014) 32325 ns/iter (± 575) 1.05
es/full/codegen/es5 34007 ns/iter (± 807) 32309 ns/iter (± 874) 1.05
es/full/codegen/es2015 34081 ns/iter (± 957) 32335 ns/iter (± 1130) 1.05
es/full/codegen/es2016 34016 ns/iter (± 674) 32269 ns/iter (± 916) 1.05
es/full/codegen/es2017 33999 ns/iter (± 743) 32664 ns/iter (± 1410) 1.04
es/full/codegen/es2018 34008 ns/iter (± 715) 32273 ns/iter (± 669) 1.05
es/full/codegen/es2019 34020 ns/iter (± 1942) 32184 ns/iter (± 737) 1.06
es/full/codegen/es2020 33906 ns/iter (± 842) 32149 ns/iter (± 495) 1.05
es/full/all/es3 200541905 ns/iter (± 8865910) 185618286 ns/iter (± 5086021) 1.08
es/full/all/es5 187712132 ns/iter (± 7367374) 187186209 ns/iter (± 20265378) 1.00
es/full/all/es2015 147288770 ns/iter (± 6736358) 147104205 ns/iter (± 5903807) 1.00
es/full/all/es2016 145597155 ns/iter (± 5191199) 161329879 ns/iter (± 15039864) 0.90
es/full/all/es2017 145379414 ns/iter (± 5165232) 144670050 ns/iter (± 9934192) 1.00
es/full/all/es2018 144724573 ns/iter (± 9131420) 142704662 ns/iter (± 4543639) 1.01
es/full/all/es2019 142600788 ns/iter (± 7385079) 140021092 ns/iter (± 5206629) 1.02
es/full/all/es2020 136994947 ns/iter (± 6100882) 133891968 ns/iter (± 4505645) 1.02
es/full/parser 717720 ns/iter (± 50162) 713844 ns/iter (± 21609) 1.01
es/full/base/fixer 26456 ns/iter (± 818) 26263 ns/iter (± 433) 1.01
es/full/base/resolver_and_hygiene 92926 ns/iter (± 3163) 92195 ns/iter (± 3278) 1.01
serialization of ast node 205 ns/iter (± 1) 205 ns/iter (± 4) 1
serialization of serde 212 ns/iter (± 4) 213 ns/iter (± 7) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.