1
- use swc_core:: ecma:: {
2
- ast:: Program ,
3
- visit:: { as_folder, FoldWith , VisitMut } ,
1
+ use std:: collections:: HashSet ;
2
+ use swc_core:: common:: util:: take:: Take ;
3
+ use swc_core:: ecma:: atoms:: JsWord ;
4
+ use swc_core:: ecma:: ast:: {
5
+ Id ,
6
+ Program ,
7
+ ImportDecl ,
8
+ ImportSpecifier ,
9
+ ImportDefaultSpecifier ,
10
+ ImportStarAsSpecifier ,
11
+ ImportNamedSpecifier ,
12
+ ModuleItem ,
13
+ ModuleDecl ,
14
+ } ;
15
+ use swc_core:: ecma:: visit:: {
16
+ as_folder,
17
+ FoldWith ,
18
+ VisitMut , VisitMutWith
4
19
} ;
5
20
use swc_core:: plugin:: { plugin_transform, proxies:: TransformPluginProgramMetadata } ;
6
21
7
- pub struct TransformVisitor ;
22
+ pub struct TransformVisitor {
23
+ target_variables : HashSet < Id > ,
24
+ target_modules : HashSet < JsWord > ,
25
+ }
26
+
27
+ impl Default for TransformVisitor {
28
+ fn default ( ) -> Self {
29
+ Self {
30
+ target_variables : HashSet :: new ( ) ,
31
+ target_modules : [
32
+ "node:assert" ,
33
+ "node:assert/strict" ,
34
+ "assert" ,
35
+ "assert/strict" ,
36
+ ] . iter ( ) . map ( |s| JsWord :: from ( * s) ) . collect ( ) ,
37
+ }
38
+ }
39
+ }
8
40
9
41
impl VisitMut for TransformVisitor {
10
- // Implement necessary visit_mut_* methods for actual custom transform.
11
- // A comprehensive list of possible visitor methods can be found here:
12
- // https://rustdoc.swc.rs/swc_ecma_visit/trait.VisitMut.html
42
+ fn visit_mut_import_decl ( & mut self , n : & mut ImportDecl ) {
43
+ if self . target_modules . contains ( & n. src . value ) {
44
+ for s in & mut n. specifiers {
45
+ match s {
46
+ ImportSpecifier :: Default ( ImportDefaultSpecifier { local, .. } ) => {
47
+ self . target_variables . insert ( local. to_id ( ) ) ;
48
+ } ,
49
+ ImportSpecifier :: Namespace ( ImportStarAsSpecifier { local, .. } ) => {
50
+ self . target_variables . insert ( local. to_id ( ) ) ;
51
+ } ,
52
+ ImportSpecifier :: Named ( ImportNamedSpecifier { local, .. } ) => {
53
+ self . target_variables . insert ( local. to_id ( ) ) ;
54
+ } ,
55
+ }
56
+ }
57
+ n. take ( ) ;
58
+ } else {
59
+ n. visit_mut_children_with ( self ) ;
60
+ }
61
+ }
62
+
63
+ fn visit_mut_module_items ( & mut self , n : & mut Vec < ModuleItem > ) {
64
+ n. visit_mut_children_with ( self ) ;
65
+ n. retain ( |s| {
66
+ match s {
67
+ ModuleItem :: ModuleDecl ( ModuleDecl :: Import ( decl) ) => {
68
+ * decl != ImportDecl :: dummy ( )
69
+ } ,
70
+ _ => true ,
71
+ }
72
+ } ) ;
73
+ }
13
74
}
14
75
15
76
/// An example plugin function with macro support.
@@ -29,7 +90,7 @@ impl VisitMut for TransformVisitor {
29
90
/// Refer swc_plugin_macro to see how does it work internally.
30
91
#[ plugin_transform]
31
92
pub fn process_transform ( program : Program , _metadata : TransformPluginProgramMetadata ) -> Program {
32
- program. fold_with ( & mut as_folder ( TransformVisitor ) )
93
+ program. fold_with ( & mut as_folder ( TransformVisitor :: default ( ) ) )
33
94
}
34
95
35
96
#[ cfg( test) ]
@@ -47,7 +108,7 @@ mod tests {
47
108
test_fixture (
48
109
Syntax :: Es ( EsConfig :: default ( ) ) ,
49
110
& |_t| {
50
- as_folder ( TransformVisitor { } )
111
+ as_folder ( TransformVisitor :: default ( ) )
51
112
} ,
52
113
& input,
53
114
& output,
0 commit comments