1
1
use swc_common:: { util:: take:: Take , DUMMY_SP } ;
2
2
use swc_ecma_ast:: * ;
3
3
use swc_ecma_visit:: { as_folder, noop_visit_mut_type, Fold , VisitMut , VisitMutWith } ;
4
- use swc_trace_macro:: swc_trace;
5
4
6
5
/// babel: `@babel/plugin-transform-reserved-words`
7
6
///
@@ -27,39 +26,85 @@ struct ReservedWord {
27
26
pub preserve_import : bool ,
28
27
}
29
28
30
- #[ swc_trace]
31
29
impl VisitMut for ReservedWord {
32
30
noop_visit_mut_type ! ( ) ;
33
31
34
32
fn visit_mut_module_items ( & mut self , n : & mut Vec < ModuleItem > ) {
35
33
let mut extra_exports = vec ! [ ] ;
36
34
37
35
n. iter_mut ( ) . for_each ( |module_item| {
38
- if let Some ( ( ident, decl) ) = match module_item {
39
- ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( ExportDecl { decl, .. } ) ) => {
40
- let ident = decl
41
- . as_fn_decl ( )
42
- . filter ( |fn_decl| fn_decl. ident . is_reserved_in_es3 ( ) )
43
- . map ( |fn_decl| fn_decl. ident . clone ( ) ) ;
44
-
45
- ident. map ( |ident| ( ident, decl. take ( ) ) )
36
+ match module_item {
37
+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( ExportDecl {
38
+ decl : decl @ Decl :: Fn ( ..) | decl @ Decl :: Class ( ..) ,
39
+ ..
40
+ } ) ) => {
41
+ let ident = match decl {
42
+ Decl :: Class ( d) => d. ident . clone ( ) ,
43
+ Decl :: Fn ( d) => d. ident . clone ( ) ,
44
+ _ => {
45
+ unreachable ! ( )
46
+ }
47
+ } ;
48
+
49
+ if !ident. is_reserved_in_es3 ( ) {
50
+ return ;
51
+ }
52
+
53
+ * module_item = ModuleItem :: Stmt ( decl. take ( ) . into ( ) ) ;
54
+
55
+ let mut orig = ident. clone ( ) ;
56
+ orig. visit_mut_with ( self ) ;
57
+
58
+ extra_exports. push (
59
+ ExportNamedSpecifier {
60
+ span : DUMMY_SP ,
61
+ orig : orig. into ( ) ,
62
+ exported : Some ( ident. into ( ) ) ,
63
+ is_type_only : false ,
64
+ }
65
+ . into ( ) ,
66
+ ) ;
46
67
}
47
- _ => None ,
48
- } {
49
- * module_item = ModuleItem :: Stmt ( decl . into ( ) ) ;
50
-
51
- let mut orig = ident . clone ( ) ;
52
- orig . visit_mut_with ( self ) ;
53
-
54
- extra_exports . push (
55
- ExportNamedSpecifier {
56
- span : DUMMY_SP ,
57
- orig : orig . into ( ) ,
58
- exported : Some ( ident . into ( ) ) ,
59
- is_type_only : false ,
68
+
69
+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( ExportDecl {
70
+ decl : Decl :: Var ( var ) ,
71
+ ..
72
+ } ) ) => {
73
+ if var . decls . iter ( ) . all ( |var| {
74
+ if let Pat :: Ident ( i ) = & var . name {
75
+ !i . id . sym . is_reserved_in_es3 ( )
76
+ } else {
77
+ true
78
+ }
79
+ } ) {
80
+ return ;
60
81
}
61
- . into ( ) ,
62
- ) ;
82
+
83
+ for var in & var. decls {
84
+ let ident = var. name . clone ( ) . expect_ident ( ) . id ;
85
+
86
+ if !ident. is_reserved_in_es3 ( ) {
87
+ return ;
88
+ }
89
+
90
+ let mut orig = ident. clone ( ) ;
91
+ orig. visit_mut_with ( self ) ;
92
+
93
+ extra_exports. push (
94
+ ExportNamedSpecifier {
95
+ span : DUMMY_SP ,
96
+ orig : orig. into ( ) ,
97
+ exported : Some ( ident. into ( ) ) ,
98
+ is_type_only : false ,
99
+ }
100
+ . into ( ) ,
101
+ ) ;
102
+ }
103
+
104
+ * module_item = ModuleItem :: Stmt ( Decl :: Var ( var. take ( ) ) . into ( ) ) ;
105
+ }
106
+
107
+ _ => { }
63
108
}
64
109
65
110
module_item. visit_mut_with ( self ) ;
0 commit comments