@@ -11,8 +11,8 @@ use swc_ecma_ast::*;
11
11
use swc_ecma_transforms_optimization:: simplify:: {
12
12
dead_branch_remover, expr_simplifier, ExprSimplifierConfig ,
13
13
} ;
14
- use swc_ecma_usage_analyzer:: { analyzer :: UsageAnalyzer , marks:: Marks } ;
15
- use swc_ecma_visit:: { noop_visit_mut_type , visit_mut_pass, VisitMut , VisitMutWith , VisitWith } ;
14
+ use swc_ecma_usage_analyzer:: marks:: Marks ;
15
+ use swc_ecma_visit:: { visit_mut_pass, VisitMutWith , VisitWith } ;
16
16
use swc_timer:: timer;
17
17
use tracing:: { debug, error} ;
18
18
@@ -23,8 +23,8 @@ use crate::{
23
23
debug:: { dump, AssertValid } ,
24
24
mode:: Mode ,
25
25
option:: { CompressOptions , MangleOptions } ,
26
- program_data:: { analyze, ProgramData } ,
27
- util:: { now , unit :: CompileUnit } ,
26
+ program_data:: analyze,
27
+ util:: { force_dump_program , now } ,
28
28
} ;
29
29
30
30
mod hoist_decls;
@@ -37,7 +37,7 @@ pub(crate) fn compressor<'a, M>(
37
37
options : & ' a CompressOptions ,
38
38
mangle_options : Option < & ' a MangleOptions > ,
39
39
mode : & ' a M ,
40
- ) -> impl ' a + VisitMut
40
+ ) -> impl ' a + Pass
41
41
where
42
42
M : Mode ,
43
43
{
52
52
} ;
53
53
54
54
(
55
- visit_mut_pass ( compressor) ,
55
+ compressor,
56
56
Optional {
57
57
enabled : options. evaluate || options. side_effects ,
58
58
visitor : visit_mut_pass ( expr_simplifier (
@@ -81,14 +81,14 @@ impl CompilerPass for Compressor<'_> {
81
81
}
82
82
}
83
83
84
+ impl Pass for Compressor < ' _ > {
85
+ fn process ( & mut self , program : & mut Program ) {
86
+ self . optimize_unit_repeatedly ( program) ;
87
+ }
88
+ }
89
+
84
90
impl Compressor < ' _ > {
85
- fn optimize_unit_repeatedly < N > ( & mut self , n : & mut N )
86
- where
87
- N : CompileUnit
88
- + VisitWith < UsageAnalyzer < ProgramData > >
89
- + for < ' aa > VisitMutWith < Compressor < ' aa > >
90
- + VisitWith < AssertValid > ,
91
- {
91
+ fn optimize_unit_repeatedly ( & mut self , n : & mut Program ) {
92
92
trace_op ! (
93
93
"Optimizing a compile unit within `{:?}`" ,
94
94
thread:: current( ) . name( )
@@ -105,7 +105,7 @@ impl Compressor<'_> {
105
105
} ,
106
106
& data,
107
107
) ;
108
- n. apply ( & mut v) ;
108
+ n. visit_mut_with ( & mut v) ;
109
109
self . changed |= v. changed ( ) ;
110
110
}
111
111
@@ -129,13 +129,7 @@ impl Compressor<'_> {
129
129
}
130
130
131
131
/// Optimize a module. `N` can be [Module] or [FnExpr].
132
- fn optimize_unit < N > ( & mut self , n : & mut N )
133
- where
134
- N : CompileUnit
135
- + VisitWith < UsageAnalyzer < ProgramData > >
136
- + for < ' aa > VisitMutWith < Compressor < ' aa > >
137
- + VisitWith < AssertValid > ,
138
- {
132
+ fn optimize_unit ( & mut self , n : & mut Program ) {
139
133
let _timer = timer ! ( "optimize" , pass = self . pass) ;
140
134
141
135
if self . options . passes != 0 && self . options . passes < self . pass {
@@ -150,7 +144,7 @@ impl Compressor<'_> {
150
144
error ! ( "Seems like there's an infinite loop" ) ;
151
145
}
152
146
153
- let code = n . force_dump ( ) ;
147
+ let code = force_dump_program ( n ) ;
154
148
155
149
if self . dump_for_infinite_loop . contains ( & code) {
156
150
let mut msg = String :: new ( ) ;
@@ -189,7 +183,7 @@ impl Compressor<'_> {
189
183
let start = n. dump ( ) ;
190
184
191
185
let mut visitor = expr_simplifier ( self . marks . unresolved_mark , ExprSimplifierConfig { } ) ;
192
- n. apply ( & mut visitor) ;
186
+ n. visit_mut_with ( & mut visitor) ;
193
187
194
188
self . changed |= visitor. changed ( ) ;
195
189
if visitor. changed ( ) {
@@ -240,7 +234,7 @@ impl Compressor<'_> {
240
234
debug_infinite_loop : self . pass >= 20 ,
241
235
} ,
242
236
) ;
243
- n. apply ( & mut visitor) ;
237
+ n. visit_mut_with ( & mut visitor) ;
244
238
245
239
self . changed |= visitor. changed ( ) ;
246
240
@@ -276,7 +270,7 @@ impl Compressor<'_> {
276
270
self . mode ,
277
271
!self . dump_for_infinite_loop . is_empty ( ) ,
278
272
) ;
279
- n. apply ( & mut visitor) ;
273
+ n. visit_mut_with ( & mut visitor) ;
280
274
281
275
self . changed |= visitor. changed ( ) ;
282
276
@@ -291,7 +285,7 @@ impl Compressor<'_> {
291
285
let start_time = now ( ) ;
292
286
293
287
let mut v = dead_branch_remover ( self . marks . unresolved_mark ) ;
294
- n. apply ( & mut v) ;
288
+ n. visit_mut_with ( & mut v) ;
295
289
296
290
if let Some ( start_time) = start_time {
297
291
let end_time = Instant :: now ( ) ;
@@ -320,42 +314,6 @@ impl Compressor<'_> {
320
314
}
321
315
}
322
316
323
- impl VisitMut for Compressor < ' _ > {
324
- noop_visit_mut_type ! ( ) ;
325
-
326
- fn visit_mut_script ( & mut self , n : & mut Script ) {
327
- self . optimize_unit_repeatedly ( n) ;
328
- }
329
-
330
- fn visit_mut_module ( & mut self , n : & mut Module ) {
331
- self . optimize_unit_repeatedly ( n) ;
332
- }
333
-
334
- fn visit_mut_module_items ( & mut self , stmts : & mut Vec < ModuleItem > ) {
335
- stmts. retain ( |stmt| match stmt {
336
- ModuleItem :: Stmt ( Stmt :: Empty ( ..) ) => false ,
337
- ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( ExportDecl {
338
- decl : Decl :: Var ( v) ,
339
- ..
340
- } ) )
341
- | ModuleItem :: Stmt ( Stmt :: Decl ( Decl :: Var ( v) ) )
342
- if v. decls . is_empty ( ) =>
343
- {
344
- false
345
- }
346
- _ => true ,
347
- } ) ;
348
- }
349
-
350
- fn visit_mut_stmts ( & mut self , stmts : & mut Vec < Stmt > ) {
351
- stmts. retain ( |stmt| match stmt {
352
- Stmt :: Empty ( ..) => false ,
353
- Stmt :: Decl ( Decl :: Var ( v) ) if v. decls . is_empty ( ) => false ,
354
- _ => true ,
355
- } ) ;
356
- }
357
- }
358
-
359
317
#[ cfg( feature = "debug" ) ]
360
318
#[ derive( PartialEq , Eq ) ]
361
319
struct DebugUsingDisplay < ' a > ( pub & ' a str ) ;
0 commit comments