1
- use std:: mem;
2
-
3
1
use swc_common:: collections:: { AHashMap , AHashSet } ;
4
2
use swc_ecma_ast:: * ;
5
3
use swc_ecma_utils:: stack_size:: maybe_grow_default;
@@ -8,7 +6,7 @@ use swc_ecma_visit::{noop_visit_type, Visit, VisitMut, VisitMutWith, VisitWith};
8
6
use crate :: { strip_type:: IsConcrete , ImportsNotUsedAsValues } ;
9
7
10
8
#[ derive( Debug , Default ) ]
11
- struct UsageCollect {
9
+ pub ( crate ) struct UsageCollect {
12
10
id_usage : AHashSet < Id > ,
13
11
import_chain : AHashMap < Id , Id > ,
14
12
}
@@ -145,7 +143,7 @@ fn get_module_ident(ts_entity_name: &TsEntityName) -> &Ident {
145
143
}
146
144
147
145
#[ derive( Debug , Default ) ]
148
- struct DeclareCollect {
146
+ pub ( crate ) struct DeclareCollect {
149
147
id_type : AHashSet < Id > ,
150
148
id_value : AHashSet < Id > ,
151
149
}
@@ -284,20 +282,22 @@ impl DeclareCollect {
284
282
/// import.
285
283
#[ derive( Default ) ]
286
284
pub ( crate ) struct StripImportExport {
287
- pub id_usage : AHashSet < Id > ,
288
285
pub import_not_used_as_values : ImportsNotUsedAsValues ,
286
+ pub usage_info : UsageCollect ,
287
+ pub declare_info : DeclareCollect ,
289
288
}
290
289
291
290
impl VisitMut for StripImportExport {
292
- fn visit_mut_module_items ( & mut self , n : & mut Vec < ModuleItem > ) {
293
- let mut usage_info = UsageCollect :: from ( mem :: take ( & mut self . id_usage ) ) ;
294
- let mut declare_info = DeclareCollect :: default ( ) ;
291
+ fn visit_mut_module ( & mut self , n : & mut Module ) {
292
+ n . visit_with ( & mut self . usage_info ) ;
293
+ n . visit_with ( & mut self . declare_info ) ;
295
294
296
- n. visit_with ( & mut usage_info) ;
297
- n. visit_with ( & mut declare_info) ;
295
+ self . usage_info . analyze_import_chain ( ) ;
298
296
299
- usage_info. analyze_import_chain ( ) ;
297
+ n. visit_mut_children_with ( self ) ;
298
+ }
300
299
300
+ fn visit_mut_module_items ( & mut self , n : & mut Vec < ModuleItem > ) {
301
301
let mut strip_ts_import_equals = StripTsImportEquals ;
302
302
303
303
n. retain_mut ( |module_item| match module_item {
@@ -317,29 +317,29 @@ impl VisitMut for StripImportExport {
317
317
318
318
let id = named. local . to_id ( ) ;
319
319
320
- if declare_info. has_value ( & id) {
320
+ if self . declare_info . has_value ( & id) {
321
321
return false ;
322
322
}
323
323
324
- usage_info. has_usage ( & id)
324
+ self . usage_info . has_usage ( & id)
325
325
}
326
326
ImportSpecifier :: Default ( default) => {
327
327
let id = default. local . to_id ( ) ;
328
328
329
- if declare_info. has_value ( & id) {
329
+ if self . declare_info . has_value ( & id) {
330
330
return false ;
331
331
}
332
332
333
- usage_info. has_usage ( & id)
333
+ self . usage_info . has_usage ( & id)
334
334
}
335
335
ImportSpecifier :: Namespace ( namespace) => {
336
336
let id = namespace. local . to_id ( ) ;
337
337
338
- if declare_info. has_value ( & id) {
338
+ if self . declare_info . has_value ( & id) {
339
339
return false ;
340
340
}
341
341
342
- usage_info. has_usage ( & id)
342
+ self . usage_info . has_usage ( & id)
343
343
}
344
344
} ) ;
345
345
@@ -363,7 +363,7 @@ impl VisitMut for StripImportExport {
363
363
} ) if src. is_none ( ) => {
364
364
let id = ident. to_id ( ) ;
365
365
366
- !declare_info. has_pure_type ( & id)
366
+ !self . declare_info . has_pure_type ( & id)
367
367
}
368
368
ExportSpecifier :: Named ( ExportNamedSpecifier { is_type_only, .. } ) => {
369
369
!is_type_only
@@ -383,7 +383,7 @@ impl VisitMut for StripImportExport {
383
383
. map ( |ident| {
384
384
let id = ident. to_id ( ) ;
385
385
386
- !declare_info. has_pure_type ( & id)
386
+ !self . declare_info . has_pure_type ( & id)
387
387
} )
388
388
. unwrap_or ( true ) ,
389
389
ModuleItem :: ModuleDecl ( ModuleDecl :: TsImportEquals ( ts_import_equals_decl) ) => {
@@ -395,7 +395,7 @@ impl VisitMut for StripImportExport {
395
395
return true ;
396
396
}
397
397
398
- usage_info. has_usage ( & ts_import_equals_decl. id . to_id ( ) )
398
+ self . usage_info . has_usage ( & ts_import_equals_decl. id . to_id ( ) )
399
399
}
400
400
ModuleItem :: Stmt ( Stmt :: Decl ( Decl :: TsModule ( ref ts_module) ) )
401
401
if ts_module. body . is_some ( ) =>
0 commit comments