@@ -55,14 +55,29 @@ pub fn hoist(
55
55
Ok ( ( module, hoist. get_result ( ) , diagnostics) )
56
56
}
57
57
58
+ #[ derive( Debug , Serialize , Deserialize ) ]
59
+ struct ExportedSymbol {
60
+ local : JsWord ,
61
+ exported : JsWord ,
62
+ loc : SourceLocation ,
63
+ }
64
+
65
+ #[ derive( Debug , Serialize , Deserialize ) ]
66
+ struct ImportedSymbol {
67
+ source : JsWord ,
68
+ local : JsWord ,
69
+ imported : JsWord ,
70
+ loc : SourceLocation ,
71
+ }
72
+
58
73
struct Hoist < ' a > {
59
74
module_id : & ' a str ,
60
75
collect : & ' a Collect ,
61
76
module_items : Vec < ModuleItem > ,
62
77
export_decls : HashSet < JsWord > ,
63
- imported_symbols : HashMap < JsWord , ( JsWord , JsWord , SourceLocation ) > ,
64
- exported_symbols : HashMap < JsWord , ( JsWord , SourceLocation ) > ,
65
- re_exports : Vec < ( JsWord , JsWord , JsWord , SourceLocation ) > ,
78
+ imported_symbols : Vec < ImportedSymbol > ,
79
+ exported_symbols : Vec < ExportedSymbol > ,
80
+ re_exports : Vec < ImportedSymbol > ,
66
81
self_references : HashSet < JsWord > ,
67
82
dynamic_imports : HashMap < JsWord , JsWord > ,
68
83
in_function_scope : bool ,
@@ -71,9 +86,9 @@ struct Hoist<'a> {
71
86
72
87
#[ derive( Debug , Default , Serialize , Deserialize ) ]
73
88
pub struct HoistResult {
74
- imported_symbols : HashMap < JsWord , ( JsWord , JsWord , SourceLocation ) > ,
75
- exported_symbols : HashMap < JsWord , ( JsWord , SourceLocation ) > ,
76
- re_exports : Vec < ( JsWord , JsWord , JsWord , SourceLocation ) > ,
89
+ imported_symbols : Vec < ImportedSymbol > ,
90
+ exported_symbols : Vec < ExportedSymbol > ,
91
+ re_exports : Vec < ImportedSymbol > ,
77
92
self_references : HashSet < JsWord > ,
78
93
wrapped_requires : HashSet < JsWord > ,
79
94
dynamic_imports : HashMap < JsWord , JsWord > ,
@@ -90,8 +105,8 @@ impl<'a> Hoist<'a> {
90
105
collect,
91
106
module_items : vec ! [ ] ,
92
107
export_decls : HashSet :: new ( ) ,
93
- imported_symbols : HashMap :: new ( ) ,
94
- exported_symbols : HashMap :: new ( ) ,
108
+ imported_symbols : vec ! [ ] ,
109
+ exported_symbols : vec ! [ ] ,
95
110
re_exports : vec ! [ ] ,
96
111
self_references : HashSet :: new ( ) ,
97
112
dynamic_imports : HashMap :: new ( ) ,
@@ -206,28 +221,28 @@ impl<'a> Fold for Hoist<'a> {
206
221
Some ( exported) => exported. sym ,
207
222
None => named. orig . sym . clone ( ) ,
208
223
} ;
209
- self . re_exports . push ( (
210
- exported ,
211
- src . value . clone ( ) ,
212
- named. orig . sym ,
213
- SourceLocation :: from ( & self . collect . source_map , named. span ) ,
214
- ) ) ;
224
+ self . re_exports . push ( ImportedSymbol {
225
+ source : src . value . clone ( ) ,
226
+ local : exported ,
227
+ imported : named. orig . sym ,
228
+ loc : SourceLocation :: from ( & self . collect . source_map , named. span ) ,
229
+ } ) ;
215
230
}
216
231
ExportSpecifier :: Default ( default) => {
217
- self . re_exports . push ( (
218
- default . exported . sym ,
219
- src . value . clone ( ) ,
220
- js_word ! ( "default" ) ,
221
- SourceLocation :: from ( & self . collect . source_map , default. exported . span ) ,
222
- ) ) ;
232
+ self . re_exports . push ( ImportedSymbol {
233
+ source : src . value . clone ( ) ,
234
+ local : default . exported . sym ,
235
+ imported : js_word ! ( "default" ) ,
236
+ loc : SourceLocation :: from ( & self . collect . source_map , default. exported . span ) ,
237
+ } ) ;
223
238
}
224
239
ExportSpecifier :: Namespace ( namespace) => {
225
- self . re_exports . push ( (
226
- namespace . name . sym ,
227
- src . value . clone ( ) ,
228
- "*" . into ( ) ,
229
- SourceLocation :: from ( & self . collect . source_map , namespace. span ) ,
230
- ) ) ;
240
+ self . re_exports . push ( ImportedSymbol {
241
+ source : src . value . clone ( ) ,
242
+ local : namespace . name . sym ,
243
+ imported : "*" . into ( ) ,
244
+ loc : SourceLocation :: from ( & self . collect . source_map , namespace. span ) ,
245
+ } ) ;
231
246
}
232
247
}
233
248
}
@@ -243,12 +258,12 @@ impl<'a> Fold for Hoist<'a> {
243
258
source, specifier, ..
244
259
} ) = self . collect . imports . get ( & id)
245
260
{
246
- self . re_exports . push ( (
247
- exported ,
248
- source . clone ( ) ,
249
- specifier. clone ( ) ,
250
- SourceLocation :: from ( & self . collect . source_map , named. span ) ,
251
- ) ) ;
261
+ self . re_exports . push ( ImportedSymbol {
262
+ source : source . clone ( ) ,
263
+ local : exported ,
264
+ imported : specifier. clone ( ) ,
265
+ loc : SourceLocation :: from ( & self . collect . source_map , named. span ) ,
266
+ } ) ;
252
267
} else {
253
268
// A variable will appear only once in the `exports` mapping but
254
269
// could be exported multiple times with different names.
@@ -259,10 +274,11 @@ impl<'a> Fold for Hoist<'a> {
259
274
} else {
260
275
self . get_export_ident ( DUMMY_SP , orig_exported)
261
276
} ;
262
- self . exported_symbols . entry ( exported) . or_insert ( (
263
- id. sym ,
264
- SourceLocation :: from ( & self . collect . source_map , named. span ) ,
265
- ) ) ;
277
+ self . exported_symbols . push ( ExportedSymbol {
278
+ local : id. sym ,
279
+ exported,
280
+ loc : SourceLocation :: from ( & self . collect . source_map , named. span ) ,
281
+ } ) ;
266
282
}
267
283
}
268
284
}
@@ -281,12 +297,12 @@ impl<'a> Fold for Hoist<'a> {
281
297
} ,
282
298
type_only : false ,
283
299
} ) ) ) ;
284
- self . re_exports . push ( (
285
- "*" . into ( ) ,
286
- export . src . value ,
287
- "*" . into ( ) ,
288
- SourceLocation :: from ( & self . collect . source_map , export. span ) ,
289
- ) ) ;
300
+ self . re_exports . push ( ImportedSymbol {
301
+ source : export . src . value ,
302
+ local : "*" . into ( ) ,
303
+ imported : "*" . into ( ) ,
304
+ loc : SourceLocation :: from ( & self . collect . source_map , export. span ) ,
305
+ } ) ;
290
306
}
291
307
ModuleDecl :: ExportDefaultExpr ( export) => {
292
308
let ident = self . get_export_ident ( export. span , & "default" . into ( ) ) ;
@@ -582,14 +598,12 @@ impl<'a> Fold for Hoist<'a> {
582
598
hash!( key)
583
599
)
584
600
. into ( ) ;
585
- self . imported_symbols . insert (
586
- name,
587
- (
588
- source. clone ( ) ,
589
- key. clone ( ) ,
590
- SourceLocation :: from ( & self . collect . source_map , member. span ) ,
591
- ) ,
592
- ) ;
601
+ self . imported_symbols . push ( ImportedSymbol {
602
+ source : source. clone ( ) ,
603
+ local : name,
604
+ imported : key. clone ( ) ,
605
+ loc : SourceLocation :: from ( & self . collect . source_map , member. span ) ,
606
+ } ) ;
593
607
} else {
594
608
return Expr :: Ident ( self . get_import_ident (
595
609
member. span ,
@@ -676,14 +690,12 @@ impl<'a> Fold for Hoist<'a> {
676
690
let name: JsWord = format ! ( "${}$importAsync${:x}" , self . module_id, hash!( source) ) . into ( ) ;
677
691
self . dynamic_imports . insert ( name. clone ( ) , source. clone ( ) ) ;
678
692
if self . collect . non_static_requires . contains ( & source) || self . collect . should_wrap {
679
- self . imported_symbols . insert (
680
- name. clone ( ) ,
681
- (
682
- source,
683
- "*" . into ( ) ,
684
- SourceLocation :: from ( & self . collect . source_map , call. span ) ,
685
- ) ,
686
- ) ;
693
+ self . imported_symbols . push ( ImportedSymbol {
694
+ source : source,
695
+ local : name. clone ( ) ,
696
+ imported : "*" . into ( ) ,
697
+ loc : SourceLocation :: from ( & self . collect . source_map , call. span ) ,
698
+ } ) ;
687
699
}
688
700
return Expr :: Ident ( Ident :: new ( name, call. span ) ) ;
689
701
}
@@ -784,15 +796,21 @@ impl<'a> Fold for Hoist<'a> {
784
796
hash!( specifier)
785
797
)
786
798
. into ( ) ;
787
- self
788
- . imported_symbols
789
- . insert ( name, ( source. clone ( ) , specifier. clone ( ) , loc. clone ( ) ) ) ;
799
+ self . imported_symbols . push ( ImportedSymbol {
800
+ source : source. clone ( ) ,
801
+ local : name,
802
+ imported : specifier. clone ( ) ,
803
+ loc : loc. clone ( ) ,
804
+ } ) ;
790
805
} else if self . collect . non_static_access . contains_key ( & id ! ( node) ) {
791
806
let name: JsWord =
792
807
format ! ( "${}$importAsync${:x}" , self . module_id, hash!( source) ) . into ( ) ;
793
- self
794
- . imported_symbols
795
- . insert ( name, ( source. clone ( ) , "*" . into ( ) , loc. clone ( ) ) ) ;
808
+ self . imported_symbols . push ( ImportedSymbol {
809
+ source : source. clone ( ) ,
810
+ local : name,
811
+ imported : "*" . into ( ) ,
812
+ loc : loc. clone ( ) ,
813
+ } ) ;
796
814
}
797
815
} else {
798
816
// If this identifier is not constant, we cannot directly reference the imported
@@ -813,10 +831,11 @@ impl<'a> Fold for Hoist<'a> {
813
831
// If wrapped, mark the original symbol as exported.
814
832
// Otherwise replace with an export identifier.
815
833
if self . collect . should_wrap {
816
- self . exported_symbols . entry ( exported. clone ( ) ) . or_insert ( (
817
- node. sym . clone ( ) ,
818
- SourceLocation :: from ( & self . collect . source_map , node. span ) ,
819
- ) ) ;
834
+ self . exported_symbols . push ( ExportedSymbol {
835
+ local : node. sym . clone ( ) ,
836
+ exported : exported. clone ( ) ,
837
+ loc : SourceLocation :: from ( & self . collect . source_map , node. span ) ,
838
+ } ) ;
820
839
return node;
821
840
} else {
822
841
return self . get_export_ident ( node. span , exported) ;
@@ -1007,13 +1026,16 @@ impl<'a> Hoist<'a> {
1007
1026
& mut self ,
1008
1027
span : Span ,
1009
1028
source : & JsWord ,
1010
- local : & JsWord ,
1029
+ imported : & JsWord ,
1011
1030
loc : SourceLocation ,
1012
1031
) -> Ident {
1013
- let new_name = self . get_import_name ( source, local) ;
1014
- self
1015
- . imported_symbols
1016
- . insert ( new_name. clone ( ) , ( source. clone ( ) , local. clone ( ) , loc) ) ;
1032
+ let new_name = self . get_import_name ( source, imported) ;
1033
+ self . imported_symbols . push ( ImportedSymbol {
1034
+ source : source. clone ( ) ,
1035
+ local : new_name. clone ( ) ,
1036
+ imported : imported. clone ( ) ,
1037
+ loc : loc. clone ( ) ,
1038
+ } ) ;
1017
1039
Ident :: new ( new_name, span)
1018
1040
}
1019
1041
@@ -1031,10 +1053,11 @@ impl<'a> Hoist<'a> {
1031
1053
format ! ( "${}$export${:x}" , self . module_id, hash!( exported) ) . into ( )
1032
1054
} ;
1033
1055
1034
- self . exported_symbols . entry ( exported. clone ( ) ) . or_insert ( (
1035
- new_name. clone ( ) ,
1036
- SourceLocation :: from ( & self . collect . source_map , span) ,
1037
- ) ) ;
1056
+ self . exported_symbols . push ( ExportedSymbol {
1057
+ local : new_name. clone ( ) ,
1058
+ exported : exported. clone ( ) ,
1059
+ loc : SourceLocation :: from ( & self . collect . source_map , span) ,
1060
+ } ) ;
1038
1061
1039
1062
let mut span = span;
1040
1063
span. ctxt = SyntaxContext :: empty ( ) ;
@@ -2054,8 +2077,8 @@ mod tests {
2054
2077
macro_rules! assert_eq_imported_symbols {
2055
2078
( $m: expr, $match: expr) => { {
2056
2079
let mut map = HashMap :: new( ) ;
2057
- for ( key , val ) in $m {
2058
- map. insert( key , ( val . 0 , val . 1 ) ) ;
2080
+ for sym in $m {
2081
+ map. insert( sym . local , ( sym . source , sym . imported ) ) ;
2059
2082
}
2060
2083
assert_eq!( map, $match) ;
2061
2084
} } ;
0 commit comments