@@ -10,13 +10,14 @@ use swc_common::{util::take::Take, Mark, SyntaxContext, DUMMY_SP};
1010use swc_ecma_ast:: * ;
1111use swc_ecma_transforms_base:: perf:: { Parallel , ParallelExt } ;
1212use swc_ecma_utils:: {
13- collect_decls, contains_this_expr, prop_name_from_ident, ExprCtx , ExprExt , Remapper ,
13+ collect_decls, contains_this_expr, prop_name_from_ident, ExprCtx , ExprExt , IdentUsageFinder ,
14+ Remapper ,
1415} ;
1516use swc_ecma_visit:: { noop_visit_mut_type, VisitMut , VisitMutWith } ;
1617use tracing:: debug;
1718
1819use super :: { Ctx , Optimizer } ;
19- use crate :: HEAVY_TASK_PARALLELS ;
20+ use crate :: { compress :: util :: contains_super , HEAVY_TASK_PARALLELS } ;
2021
2122impl < ' b > Optimizer < ' b > {
2223 pub ( super ) fn normalize_expr ( & mut self , e : & mut Expr ) {
@@ -164,10 +165,11 @@ impl Drop for WithCtx<'_, '_> {
164165 }
165166}
166167
167- pub ( crate ) fn extract_class_side_effect (
168+ pub ( crate ) fn extract_class_side_effect < ' a > (
168169 expr_ctx : ExprCtx ,
169- c : & mut Class ,
170- ) -> Option < Vec < & mut Box < Expr > > > {
170+ ident : Option < & ' a Ident > ,
171+ c : & ' a mut Class ,
172+ ) -> Option < Vec < & ' a mut Box < Expr > > > {
171173 let mut res = Vec :: new ( ) ;
172174 if let Some ( e) = & mut c. super_class {
173175 if e. may_have_side_effects ( expr_ctx) {
@@ -195,9 +197,16 @@ pub(crate) fn extract_class_side_effect(
195197
196198 if let Some ( v) = & mut p. value {
197199 if p. is_static && v. may_have_side_effects ( expr_ctx) {
198- if contains_this_expr ( v) {
200+ if contains_this_expr ( v) || contains_super ( v ) {
199201 return None ;
200202 }
203+
204+ if let Some ( id) = ident {
205+ if IdentUsageFinder :: find ( id, v) {
206+ return None ;
207+ }
208+ }
209+
201210 res. push ( v) ;
202211 }
203212 }
@@ -208,12 +217,44 @@ pub(crate) fn extract_class_side_effect(
208217 ..
209218 } ) => {
210219 if v. may_have_side_effects ( expr_ctx) {
211- if contains_this_expr ( v) {
220+ if contains_this_expr ( v) || contains_super ( v ) {
212221 return None ;
213222 }
223+
224+ if let Some ( id) = ident {
225+ if IdentUsageFinder :: find ( id, v) {
226+ return None ;
227+ }
228+ }
229+
214230 res. push ( v) ;
215231 }
216232 }
233+ ClassMember :: StaticBlock ( s) => {
234+ if s. body . stmts . len ( ) > 1 {
235+ return None ;
236+ }
237+
238+ let first = if let Some ( stmt) = s. body . stmts . get_mut ( 0 ) {
239+ & mut stmt. as_mut_expr ( ) ?. expr
240+ } else {
241+ continue ;
242+ } ;
243+
244+ if first. may_have_side_effects ( expr_ctx) {
245+ if contains_this_expr ( first) || contains_super ( first) {
246+ return None ;
247+ }
248+
249+ if let Some ( id) = ident {
250+ if IdentUsageFinder :: find ( id, first) {
251+ return None ;
252+ }
253+ }
254+
255+ res. push ( first) ;
256+ }
257+ }
217258
218259 _ => { }
219260 }
0 commit comments