From 8d906b45e56d447792df59d5fdde5ee072e2bc80 Mon Sep 17 00:00:00 2001 From: HeYunfei Date: Thu, 24 Nov 2022 17:43:00 +0800 Subject: [PATCH] fix(es/minifier): Preserve classes with side effects in static fields (#6480) --- ...StaticBlock15(target=es2022).2.minified.js | 14 +- .../classStaticBlock23.2.minified.js | 7 +- ...sStaticBlock5(target=es2022).2.minified.js | 12 +- ...lassStaticBlockUseBeforeDef1.2.minified.js | 10 +- ...lassStaticBlockUseBeforeDef2.2.minified.js | 5 + ...PropertyName4(target=es2022).2.minified.js | 12 + ...taticMembers1(target=es2022).2.minified.js | 278 +++++++++++++++--- ...taticMembers1(target=es2022).2.minified.js | 54 +++- ...taticMembers2(target=es2022).2.minified.js | 54 +++- ...aticMembers10(target=es2022).2.minified.js | 24 +- ...aticMembers11(target=es2022).2.minified.js | 24 +- ...aticMembers12(target=es2022).2.minified.js | 8 +- ...aticMembers13(target=es2022).2.minified.js | 8 +- ...taticMembers3(target=es2022).2.minified.js | 10 +- ...taticMembers4(target=es2022).2.minified.js | 10 +- ...taticMembers7(target=es2022).2.minified.js | 10 +- ...taticMembers8(target=es2022).2.minified.js | 11 + ...taticMembers9(target=es2022).2.minified.js | 13 + .../uniqueSymbols.2.minified.js | 32 +- .../uniqueSymbolsDeclarations.2.minified.js | 32 +- ...niqueSymbolsDeclarationsInJs.2.minified.js | 9 +- .../src/compress/optimize/unused.rs | 25 +- .../tests/fixture/issues/5588/config.json | 4 + .../tests/fixture/issues/5588/input.js | 12 + .../tests/fixture/issues/5588/output.js | 12 + 25 files changed, 612 insertions(+), 78 deletions(-) create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/5588/config.json create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/5588/input.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/issues/5588/output.js diff --git a/crates/swc/tests/tsc-references/classStaticBlock15(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock15(target=es2022).2.minified.js index 72eb0e94e05..5d81bb8b8d1 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock15(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock15(target=es2022).2.minified.js @@ -1,2 +1,14 @@ //// [classStaticBlock15.ts] -console.log(void 0); +var _C__1; +class C { + static #_1 = 1; + static #_3 = 3; + static #_5 = 5; + static{} + static{} + static{} + static{} + static{} + static{} +} +console.log(_C__1); diff --git a/crates/swc/tests/tsc-references/classStaticBlock23.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock23.2.minified.js index 4e4f7b315b2..9c5a5115b29 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock23.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock23.2.minified.js @@ -1,6 +1,11 @@ //// [classStaticBlock23.ts] -[ +const nums = [ 1, 2, 3 ].map((n)=>Promise.resolve(n)); +class C { + static{ + for await (let nn of nums)console.log(nn); + } +} diff --git a/crates/swc/tests/tsc-references/classStaticBlock5(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock5(target=es2022).2.minified.js index 917a33244e3..0c3c8886bc6 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock5(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock5(target=es2022).2.minified.js @@ -1,2 +1,12 @@ //// [classStaticBlock5.ts] -super.a; +class B { + static a = 1; + static b = 2; +} +class C extends B { + static b = 3; + static c = super.a; + static{ + this.b, super.b, super.a; + } +} diff --git a/crates/swc/tests/tsc-references/classStaticBlockUseBeforeDef1.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlockUseBeforeDef1.2.minified.js index a1ad686d1e4..9fadd536d3d 100644 --- a/crates/swc/tests/tsc-references/classStaticBlockUseBeforeDef1.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlockUseBeforeDef1.2.minified.js @@ -1,2 +1,10 @@ //// [classStaticBlockUseBeforeDef1.ts] -this.x; +class C { + static{ + this.x = 1; + } + static y = this.x; + static{ + this.z = this.y; + } +} diff --git a/crates/swc/tests/tsc-references/classStaticBlockUseBeforeDef2.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlockUseBeforeDef2.2.minified.js index 911606dbe20..6b8024701eb 100644 --- a/crates/swc/tests/tsc-references/classStaticBlockUseBeforeDef2.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlockUseBeforeDef2.2.minified.js @@ -1 +1,6 @@ //// [classStaticBlockUseBeforeDef2.ts] +class C { + static{ + this.x = 1; + } +} diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName4(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName4(target=es2022).2.minified.js index 613598bdc6b..334fb01fafb 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName4(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName4(target=es2022).2.minified.js @@ -1 +1,13 @@ //// [privateNameComputedPropertyName4.ts] +class C1 { + static #qux = 42; + bar() {} +} +class C2 { + static #qux = 42; + static bar() {} +} +class C3 { + static #qux = 42; + static bar = "test"; +} 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 b48fd213d18..1b4d2c43c0a 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 @@ -7,150 +7,354 @@ Baz || (Baz = {}); export default class { } //// [locals.ts] -super.w(), (()=>{ - var { Reflect } = { - Reflect: null - }; - super.w(); -})(), (()=>{ - var [Reflect] = [ - null +class C extends B { + static _ = [ + void super.w(), + (()=>{ + var { Reflect } = { + Reflect: null + }; + super.w(); + })(), + (()=>{ + var [Reflect] = [ + null + ]; + super.w(); + })(), + void super.w(), + void super.w(), + (()=>{ + let Reflect; + Reflect || (Reflect = {}), super.w(); + })(), + (()=>{ + let Reflect; + Reflect || (Reflect = {}), super.w(); + })(), + void super.w(), + void super.w(), + void super.w(), + void super.w() ]; - super.w(); -})(), super.w(), super.w(), (()=>{ - let Reflect; - Reflect || (Reflect = {}), super.w(); -})(), (()=>{ - let Reflect; - Reflect || (Reflect = {}), super.w(); -})(), super.w(), super.w(), super.w(), super.w(); + static{ + var { Reflect } = { + Reflect: null + }; + super.w(); + } + static{ + var [Reflect1] = [ + null + ]; + super.w(); + } + static{ + super.w(); + } + static{ + super.w(); + } + static{ + super.w(); + } + static{ + let Reflect2; + Reflect2 || (Reflect2 = {}), super.w(); + } + static{ + let Reflect3; + Reflect3 || (Reflect3 = {}), super.w(); + } + static{ + super.w(); + } + static{ + super.w(); + } + static{ + super.w(); + } + static{ + super.w(); + } +} export { }; //// [varInContainingScopeStaticField1.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [varInContainingScopeStaticField2.ts] var { Reflect } = { Reflect: null }; -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [varInContainingScopeStaticField3.ts] var [Reflect] = [ null ]; -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [varInContainingScopeStaticBlock1.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [varInContainingScopeStaticBlock2.ts] var { Reflect } = { Reflect: null }; +class C extends B { + static{ + super.w(); + } +} export { }; //// [varInContainingScopeStaticBlock3.ts] var [Reflect] = [ null ]; +class C extends B { + static{ + super.w(); + } +} export { }; //// [classDeclInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [classDeclInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [funcDeclInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [funcDeclInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [valueNamespaceInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [valueNamespaceInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [enumInContainingScopeStaticField.ts] var Reflect; -Reflect || (Reflect = {}), super.w(); +Reflect || (Reflect = {}); +class C extends B { + static _ = super.w(); +} export { }; //// [enumInContainingScopeStaticBlock.ts] var Reflect; Reflect || (Reflect = {}); +class C extends B { + static{ + super.w(); + } +} export { }; //// [constEnumInContainingScopeStaticField.ts] var Reflect; -Reflect || (Reflect = {}), super.w(); +Reflect || (Reflect = {}); +class C extends B { + static _ = super.w(); +} export { }; //// [constEnumInContainingScopeStaticBlock.ts] var Reflect; Reflect || (Reflect = {}); +class C extends B { + static{ + super.w(); + } +} export { }; //// [namespaceImportInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [namespaceImportInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [namedImportInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [namedImportInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [namedImportOfInterfaceInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [namedImportOfInterfaceInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [namedImportOfUninstantiatedNamespaceInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [namedImportOfUninstantiatedNamespaceInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [namedImportOfConstEnumInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [namedImportOfConstEnumInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [typeOnlyNamedImportInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [typeOnlyNamedImportInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [defaultImportInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [defaultImportInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [typeOnlyDefaultImportInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [typeOnlyDefaultImportInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [typeInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [typeInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [interfaceInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [interfaceInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [uninstantiatedNamespaceInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [uninstantiatedNamespaceInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [classExprInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [classExprInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [inContainingClassExprStaticField.ts] export { }; //// [inContainingClassExprStaticBlock.ts] export { }; //// [funcExprInContainingScopeStaticField.ts] -super.w(); +class C extends B { + static _ = super.w(); +} export { }; //// [funcExprInContainingScopeStaticBlock.ts] +class C extends B { + static{ + super.w(); + } +} export { }; //// [inContainingFuncExprStaticField.ts] export { }; diff --git a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers1(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers1(target=es2022).2.minified.js index 80a07463c73..a87de050e3a 100644 --- a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers1(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers1(target=es2022).2.minified.js @@ -1,14 +1,42 @@ //// [thisAndSuperInStaticMembers1.ts] -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] = [ - 0 -], [super.a = 0] = [ - 0 -], [...super.a] = [ - 0 -], ({ x: super.a } = { - x: 0 -}), ({ x: super.a = 0 } = { - x: 0 -}), ({ ...super.a } = { - x: 0 -}), ++super.a, --super.a, ++super.a, super.a++, super.a``; +class C extends B { + static x = void 0; + static y1 = this.x; + static y2 = this.x(); + static y3 = this?.x(); + static y4 = this.x(); + static y5 = this?.x(); + static z1 = super.a; + static z2 = super.a; + static z3 = super.f(); + static z4 = super.f(); + static z5 = super.a = 0; + static z6 = super.a += 1; + static z7 = void (super.a = 0); + static z8 = [super.a] = [ + 0 + ]; + static z9 = [super.a = 0] = [ + 0 + ]; + static z10 = [...super.a] = [ + 0 + ]; + static z11 = { x: super.a } = { + x: 0 + }; + static z12 = { x: super.a = 0 } = { + x: 0 + }; + static z13 = { ...super.a } = { + x: 0 + }; + static z14 = ++super.a; + static z15 = --super.a; + static z16 = ++super.a; + static z17 = super.a++; + static z18 = super.a``; + x = 1; + y = this.x; + z = super.f(); +} diff --git a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2022).2.minified.js index c37c7af696e..9a71be67515 100644 --- a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2022).2.minified.js @@ -1,14 +1,42 @@ //// [thisAndSuperInStaticMembers2.ts] -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] = [ - 0 -], [super.a = 0] = [ - 0 -], [...super.a] = [ - 0 -], ({ x: super.a } = { - x: 0 -}), ({ x: super.a = 0 } = { - x: 0 -}), ({ ...super.a } = { - x: 0 -}), ++super.a, --super.a, ++super.a, super.a++, super.a``; +class C extends B { + static x = void 0; + static y1 = this.x; + static y2 = this.x(); + static y3 = this?.x(); + static y4 = this.x(); + static y5 = this?.x(); + static z1 = super.a; + static z2 = super.a; + static z3 = super.f(); + static z4 = super.f(); + static z5 = super.a = 0; + static z6 = super.a += 1; + static z7 = void (super.a = 0); + static z8 = [super.a] = [ + 0 + ]; + static z9 = [super.a = 0] = [ + 0 + ]; + static z10 = [...super.a] = [ + 0 + ]; + static z11 = { x: super.a } = { + x: 0 + }; + static z12 = { x: super.a = 0 } = { + x: 0 + }; + static z13 = { ...super.a } = { + x: 0 + }; + static z14 = ++super.a; + static z15 = --super.a; + static z16 = ++super.a; + static z17 = super.a++; + static z18 = super.a``; + x = 1; + y = this.x; + z = super.f(); +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers10(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers10(target=es2022).2.minified.js index 47f7073aad2..2877e858558 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers10(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers10(target=es2022).2.minified.js @@ -27,4 +27,26 @@ let D = class extends C { }; D = _ts_decorate([ foo -], D), this.a, this.c, super.a, this.c; +], D); +class CC { + static a = 1; + static b = this.a + 1; +} +class DD extends CC { + static c = 2; + static d = this.c + 1; + static e = super.a + this.c + 1; + static f = ()=>this.c + 1; + static ff = function() { + this.c; + }; + static foo() { + return this.c + 1; + } + static get fa() { + return this.c + 1; + } + static set fa(v) { + this.c = v + 1; + } +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers11(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers11(target=es2022).2.minified.js index b7fff23d855..f8f9f21a86e 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers11(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers11(target=es2022).2.minified.js @@ -27,4 +27,26 @@ let D = class extends C { }; D = _ts_decorate([ foo -], D), this.a, this.c, super.a, this.c; +], D); +class CC { + static a = 1; + static b = this.a + 1; +} +class DD extends CC { + static c = 2; + static d = this.c + 1; + static e = super.a + this.c + 1; + static f = ()=>this.c + 1; + static ff = function() { + this.c; + }; + static foo() { + return this.c + 1; + } + static get fa() { + return this.c + 1; + } + static set fa(v) { + this.c = v + 1; + } +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers12(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers12(target=es2022).2.minified.js index 8b99f31aacc..75c66bef271 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers12(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers12(target=es2022).2.minified.js @@ -1,2 +1,8 @@ //// [typeOfThisInStaticMembers12.ts] -this.c, this.c; +class C { + static c = "foo"; + static bar = class { + static [this.c] = 123; + [this.c] = 123; + }; +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers13(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers13(target=es2022).2.minified.js index 831200a1ac6..4a0502ac82d 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers13(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers13(target=es2022).2.minified.js @@ -1,2 +1,8 @@ //// [typeOfThisInStaticMembers13.ts] -this.c, this.c; +class C { + static c = "foo"; + static bar = class { + static [this.c] = 123; + [this.c] = 123; + }; +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers3(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers3(target=es2022).2.minified.js index 7ac9e061374..d96f656c2b0 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers3(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers3(target=es2022).2.minified.js @@ -1,2 +1,10 @@ //// [typeOfThisInStaticMembers3.ts] -this.a, this.c, super.a, this.c; +class C { + static a = 1; + static b = this.a + 1; +} +class D extends C { + static c = 2; + static d = this.c + 1; + static e = super.a + this.c + 1; +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers4(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers4(target=es2022).2.minified.js index 19486e195c1..36207940884 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers4(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers4(target=es2022).2.minified.js @@ -1,2 +1,10 @@ //// [typeOfThisInStaticMembers4.ts] -this.a, this.c, super.a, this.c; +class C { + static a = 1; + static b = this.a + 1; +} +class D extends C { + static c = 2; + static d = this.c + 1; + static e = super.a + this.c + 1; +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers7(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers7(target=es2022).2.minified.js index 7a9b38e04fc..8b1d1d1120b 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers7(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers7(target=es2022).2.minified.js @@ -1,2 +1,10 @@ //// [typeOfThisInStaticMembers7.ts] -this.a, this.c, super.a, this.c; +class C { + static a = 1; + static b = this.a + 1; +} +class D extends C { + static c = 2; + static d = this.c + 1; + static e = 1 + super.a + (this.c + 1) + 1; +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers8(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers8(target=es2022).2.minified.js index 39cf3c5f46c..91b16c03936 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers8(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers8(target=es2022).2.minified.js @@ -1 +1,12 @@ //// [typeOfThisInStaticMembers8.ts] +class C { + static f = 1; + static arrowFunctionBoundary = ()=>this.f + 1; + static functionExprBoundary = function() { + return this.f + 2; + }; + static classExprBoundary = class { + a = this.f + 3; + }; + static functionAndClassDeclBoundary = void 0; +} diff --git a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers9(target=es2022).2.minified.js b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers9(target=es2022).2.minified.js index b3f6ef4e7ee..59ac42d9b85 100644 --- a/crates/swc/tests/tsc-references/typeOfThisInStaticMembers9(target=es2022).2.minified.js +++ b/crates/swc/tests/tsc-references/typeOfThisInStaticMembers9(target=es2022).2.minified.js @@ -1 +1,14 @@ //// [typeOfThisInStaticMembers9.ts] +class C { + static f = 1; +} +class D extends C { + static arrowFunctionBoundary = ()=>super.f + 1; + static functionExprBoundary = function() { + return super.f + 2; + }; + static classExprBoundary = class { + a = super.f + 3; + }; + static functionAndClassDeclBoundary = void 0; +} diff --git a/crates/swc/tests/tsc-references/uniqueSymbols.2.minified.js b/crates/swc/tests/tsc-references/uniqueSymbols.2.minified.js index d421f8140c1..c33d8036b58 100644 --- a/crates/swc/tests/tsc-references/uniqueSymbols.2.minified.js +++ b/crates/swc/tests/tsc-references/uniqueSymbols.2.minified.js @@ -8,4 +8,34 @@ class C { readonlyCall = Symbol(); readwriteCall = Symbol(); } -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; +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; +class C0 { + static a = s; + static b = N.s; + static c = N.s; + static d = s; + static e = N.s; + static f = N.s; + a = s; + b = N.s; + c = N.s; + d = s; + e = N.s; + f = N.s; + method1() { + return s; + } + async method2() { + return s; + } + async *method3() { + yield s; + } + *method4() { + yield s; + } + method5(p = s) { + return p; + } +} +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; diff --git a/crates/swc/tests/tsc-references/uniqueSymbolsDeclarations.2.minified.js b/crates/swc/tests/tsc-references/uniqueSymbolsDeclarations.2.minified.js index d5a25a78433..11add4c804a 100644 --- a/crates/swc/tests/tsc-references/uniqueSymbolsDeclarations.2.minified.js +++ b/crates/swc/tests/tsc-references/uniqueSymbolsDeclarations.2.minified.js @@ -8,4 +8,34 @@ class C { readonlyCall = Symbol(); readwriteCall = Symbol(); } -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; +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; +class C0 { + static a = s; + static b = N.s; + static c = N.s; + static d = s; + static e = N.s; + static f = N.s; + a = s; + b = N.s; + c = N.s; + d = s; + e = N.s; + f = N.s; + method1() { + return s; + } + async method2() { + return s; + } + async *method3() { + yield s; + } + *method4() { + yield s; + } + method5(p = s) { + return p; + } +} +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; diff --git a/crates/swc/tests/tsc-references/uniqueSymbolsDeclarationsInJs.2.minified.js b/crates/swc/tests/tsc-references/uniqueSymbolsDeclarationsInJs.2.minified.js index 1bcb7296058..8628c54154f 100644 --- a/crates/swc/tests/tsc-references/uniqueSymbolsDeclarationsInJs.2.minified.js +++ b/crates/swc/tests/tsc-references/uniqueSymbolsDeclarationsInJs.2.minified.js @@ -1,2 +1,9 @@ //// [uniqueSymbolsDeclarationsInJs.js] -Symbol(), Symbol(), Symbol(), Symbol(); +class C { + static readonlyStaticCall = Symbol(); + static readonlyStaticTypeAndCall = Symbol(); + static readwriteStaticCall = Symbol(); + readonlyCall = Symbol(); + readwriteCall = Symbol(); +} +Symbol(); diff --git a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs index 1aa13c5dcd9..e09ffee52a2 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/unused.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/unused.rs @@ -473,10 +473,33 @@ where } match decl { - Decl::Class(ClassDecl { ident, .. }) => { + Decl::Class(ClassDecl { ident, class, .. }) => { if ident.sym == js_word!("arguments") { return; } + + // Fix https://github.com/swc-project/swc/issues/5588 + let may_have_side_effect = class.body.iter().any(|m| match m { + ClassMember::ClassProp(ClassProp { + is_static: true, + value: Some(_), + .. + }) + | ClassMember::PrivateProp(PrivateProp { + is_static: true, + value: Some(_), + .. + }) => true, + ClassMember::StaticBlock(StaticBlock { + body: BlockStmt { stmts, .. }, + .. + }) if !stmts.is_empty() => true, + _ => false, + }); + if may_have_side_effect { + return; + } + // If it is not used, drop it. if self .data diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/5588/config.json b/crates/swc_ecma_minifier/tests/fixture/issues/5588/config.json new file mode 100644 index 00000000000..b987119e94f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/5588/config.json @@ -0,0 +1,4 @@ +{ + "unused": true, + "toplevel": true +} \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/5588/input.js b/crates/swc_ecma_minifier/tests/fixture/issues/5588/input.js new file mode 100644 index 00000000000..0972a97da4f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/5588/input.js @@ -0,0 +1,12 @@ +"use strict"; +let getFoo; +let getFoo2 +class Foo { + static #foo = 42; + static #_ = getFoo2 = this.#foo + static { + getFoo = () => this.#foo; + } +} +expect(getFoo()).toBe(42); +expect(getFoo2()).toBe(42); \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/5588/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/5588/output.js new file mode 100644 index 00000000000..f70de3b7c4f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/issues/5588/output.js @@ -0,0 +1,12 @@ +"use strict"; +let getFoo; +let getFoo2; +class Foo { + static #foo = 42; + static #_ = getFoo2 = this.#foo; + static{ + getFoo = ()=>this.#foo; + } +} +expect(getFoo()).toBe(42); +expect(getFoo2()).toBe(42);