@@ -7,15 +7,15 @@ use swc_common::{
7
7
DUMMY_SP ,
8
8
} ;
9
9
use swc_ecma_ast:: {
10
- BindingIdent , Decl , DefaultDecl , ExportDefaultExpr , Id , Ident , ImportSpecifier , ModuleDecl ,
10
+ BindingIdent , Decl , DefaultDecl , ExportDefaultExpr , Ident , ImportSpecifier , ModuleDecl ,
11
11
ModuleItem , NamedExport , Pat , Program , Script , Stmt , TsExportAssignment , VarDecl , VarDeclKind ,
12
12
VarDeclarator ,
13
13
} ;
14
14
use type_usage:: TypeUsageAnalyzer ;
15
15
use util:: {
16
16
ast_ext:: MemberPropExt , expando_function_collector:: ExpandoFunctionCollector , types:: type_ann,
17
17
} ;
18
- use visitors:: type_usage;
18
+ use visitors:: type_usage:: { self , SymbolFlags , UsedRefs } ;
19
19
20
20
use crate :: diagnostic:: { DtsIssue , SourceRange } ;
21
21
@@ -44,7 +44,7 @@ pub struct FastDts {
44
44
// states
45
45
id_counter : u32 ,
46
46
is_top_level : bool ,
47
- used_refs : FxHashSet < Id > ,
47
+ used_refs : UsedRefs ,
48
48
internal_annotations : Option < FxHashSet < BytePos > > ,
49
49
}
50
50
@@ -63,7 +63,7 @@ impl FastDts {
63
63
diagnostics : Vec :: new ( ) ,
64
64
id_counter : 0 ,
65
65
is_top_level : true ,
66
- used_refs : FxHashSet :: default ( ) ,
66
+ used_refs : UsedRefs :: default ( ) ,
67
67
internal_annotations,
68
68
}
69
69
}
@@ -241,7 +241,8 @@ impl FastDts {
241
241
242
242
let name_ident = Ident :: new_no_ctxt ( self . gen_unique_name ( "_default" ) , DUMMY_SP ) ;
243
243
let type_ann = self . infer_type_from_expr ( expr) . map ( type_ann) ;
244
- self . used_refs . insert ( name_ident. to_id ( ) ) ;
244
+ self . used_refs
245
+ . add_usage ( name_ident. to_id ( ) , SymbolFlags :: Value ) ;
245
246
246
247
if type_ann. is_none ( ) {
247
248
self . default_export_inferred ( expr. span ( ) ) ;
@@ -460,12 +461,12 @@ impl FastDts {
460
461
let used_refs = & self . used_refs ;
461
462
items. retain_mut ( |node| match node {
462
463
ModuleItem :: Stmt ( Stmt :: Decl ( decl) ) if !in_global_or_lit_module => match decl {
463
- Decl :: Class ( class_decl) => used_refs. contains ( & class_decl. ident . to_id ( ) ) ,
464
- Decl :: Fn ( fn_decl) => used_refs. contains ( & fn_decl. ident . to_id ( ) ) ,
464
+ Decl :: Class ( class_decl) => used_refs. used ( & class_decl. ident . to_id ( ) ) ,
465
+ Decl :: Fn ( fn_decl) => used_refs. used_as_value ( & fn_decl. ident . to_id ( ) ) ,
465
466
Decl :: Var ( var_decl) => {
466
467
var_decl. decls . retain ( |decl| {
467
468
if let Some ( ident) = decl. name . as_ident ( ) {
468
- used_refs. contains ( & ident. to_id ( ) )
469
+ used_refs. used_as_value ( & ident. to_id ( ) )
469
470
} else {
470
471
false
471
472
}
@@ -475,27 +476,27 @@ impl FastDts {
475
476
Decl :: Using ( using_decl) => {
476
477
using_decl. decls . retain ( |decl| {
477
478
if let Some ( ident) = decl. name . as_ident ( ) {
478
- used_refs. contains ( & ident. to_id ( ) )
479
+ used_refs. used_as_value ( & ident. to_id ( ) )
479
480
} else {
480
481
false
481
482
}
482
483
} ) ;
483
484
!using_decl. decls . is_empty ( )
484
485
}
485
486
Decl :: TsInterface ( ts_interface_decl) => {
486
- used_refs. contains ( & ts_interface_decl. id . to_id ( ) )
487
+ used_refs. used_as_type ( & ts_interface_decl. id . to_id ( ) )
487
488
}
488
489
Decl :: TsTypeAlias ( ts_type_alias_decl) => {
489
- used_refs. contains ( & ts_type_alias_decl. id . to_id ( ) )
490
+ used_refs. used_as_type ( & ts_type_alias_decl. id . to_id ( ) )
490
491
}
491
- Decl :: TsEnum ( ts_enum) => used_refs. contains ( & ts_enum. id . to_id ( ) ) ,
492
+ Decl :: TsEnum ( ts_enum) => used_refs. used ( & ts_enum. id . to_id ( ) ) ,
492
493
Decl :: TsModule ( ts_module_decl) => {
493
494
ts_module_decl. global
494
495
|| ts_module_decl. id . is_str ( )
495
496
|| ts_module_decl
496
497
. id
497
498
. as_ident ( )
498
- . map_or ( true , |ident| used_refs. contains ( & ident. to_id ( ) ) )
499
+ . map_or ( true , |ident| used_refs. used_as_type ( & ident. to_id ( ) ) )
499
500
}
500
501
} ,
501
502
ModuleItem :: ModuleDecl ( ModuleDecl :: Import ( import_decl) ) => {
@@ -504,21 +505,17 @@ impl FastDts {
504
505
}
505
506
506
507
import_decl. specifiers . retain ( |specifier| match specifier {
507
- ImportSpecifier :: Named ( specifier) => {
508
- used_refs. contains ( & specifier. local . to_id ( ) )
509
- }
510
- ImportSpecifier :: Default ( specifier) => {
511
- used_refs. contains ( & specifier. local . to_id ( ) )
512
- }
508
+ ImportSpecifier :: Named ( specifier) => used_refs. used ( & specifier. local . to_id ( ) ) ,
509
+ ImportSpecifier :: Default ( specifier) => used_refs. used ( & specifier. local . to_id ( ) ) ,
513
510
ImportSpecifier :: Namespace ( specifier) => {
514
- used_refs. contains ( & specifier. local . to_id ( ) )
511
+ used_refs. used ( & specifier. local . to_id ( ) )
515
512
}
516
513
} ) ;
517
514
518
515
!import_decl. specifiers . is_empty ( )
519
516
}
520
517
ModuleItem :: ModuleDecl ( ModuleDecl :: TsImportEquals ( ts_import_equals) ) => {
521
- used_refs. contains ( & ts_import_equals. id . to_id ( ) )
518
+ used_refs. used ( & ts_import_equals. id . to_id ( ) )
522
519
}
523
520
_ => true ,
524
521
} ) ;
0 commit comments