@@ -287,18 +287,18 @@ impl Transform {
287
287
}
288
288
289
289
let id = stmt. get_decl_id ( ) ;
290
- stmt = self . fold_stmt ( stmt) ;
290
+ stmt = self . fold_stmt ( stmt, false ) ;
291
291
decl_id_record. extend ( id) ;
292
292
if !stmt. is_empty ( ) {
293
293
n. push ( stmt) ;
294
294
}
295
295
}
296
296
}
297
297
298
- fn fold_stmt ( & mut self , n : Stmt ) -> Stmt {
298
+ fn fold_stmt ( & mut self , n : Stmt , is_export : bool ) -> Stmt {
299
299
match n {
300
300
Stmt :: Decl ( Decl :: TsModule ( ts_module) ) => self . transform_ts_module ( * ts_module, false ) ,
301
- Stmt :: Decl ( Decl :: TsEnum ( ts_enum) ) => self . transform_ts_enum ( * ts_enum, false ) ,
301
+ Stmt :: Decl ( Decl :: TsEnum ( ts_enum) ) => self . transform_ts_enum ( * ts_enum, is_export ) ,
302
302
stmt => stmt,
303
303
}
304
304
}
@@ -312,6 +312,8 @@ impl Transform {
312
312
return ;
313
313
}
314
314
315
+ let export_names = self . collect_named_export ( n) ;
316
+
315
317
let mut decl_id_record = AHashSet :: < Id > :: default ( ) ;
316
318
317
319
for mut module_item in n. take ( ) . drain ( ..) {
@@ -342,15 +344,19 @@ impl Transform {
342
344
}
343
345
344
346
let id = module_item. get_decl_id ( ) ;
345
- module_item = self . fold_module_item ( module_item) ;
347
+ let is_export = id
348
+ . as_ref ( )
349
+ . map ( |id| export_names. contains ( id) )
350
+ . unwrap_or_default ( ) ;
351
+ module_item = self . fold_module_item ( module_item, is_export) ;
346
352
decl_id_record. extend ( id) ;
347
353
if !matches ! ( module_item, ModuleItem :: Stmt ( Stmt :: Empty ( ..) ) ) {
348
354
n. push ( module_item) ;
349
355
}
350
356
}
351
357
}
352
358
353
- fn fold_module_item ( & mut self , n : ModuleItem ) -> ModuleItem {
359
+ fn fold_module_item ( & mut self , n : ModuleItem , is_export : bool ) -> ModuleItem {
354
360
match n {
355
361
ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDecl ( ExportDecl {
356
362
decl : Decl :: TsModule ( ts_module) ,
@@ -360,7 +366,7 @@ impl Transform {
360
366
decl : Decl :: TsEnum ( ts_enum) ,
361
367
..
362
368
} ) ) => self . transform_ts_enum ( * ts_enum, true ) . into ( ) ,
363
- ModuleItem :: Stmt ( stmt @ Stmt :: Decl ( ..) ) => self . fold_stmt ( stmt) . into ( ) ,
369
+ ModuleItem :: Stmt ( stmt @ Stmt :: Decl ( ..) ) => self . fold_stmt ( stmt, is_export ) . into ( ) ,
364
370
module_item => module_item,
365
371
}
366
372
}
@@ -879,6 +885,41 @@ impl Transform {
879
885
880
886
Factory :: function ( vec ! [ ident. into( ) ] , body) . as_call ( DUMMY_SP , vec ! [ init_arg. into( ) ] )
881
887
}
888
+
889
+ fn collect_named_export ( & self , n : & [ ModuleItem ] ) -> AHashSet < Id > {
890
+ let mut names = AHashSet :: default ( ) ;
891
+
892
+ if self . namespace_id . is_some ( ) {
893
+ return names;
894
+ }
895
+
896
+ for item in n {
897
+ match item {
898
+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportNamed ( NamedExport {
899
+ specifiers,
900
+ src : None ,
901
+ ..
902
+ } ) ) => {
903
+ names. extend ( specifiers. iter ( ) . map ( |s| match s {
904
+ ExportSpecifier :: Named ( ExportNamedSpecifier {
905
+ orig : ModuleExportName :: Ident ( export_id) ,
906
+ ..
907
+ } ) => export_id. to_id ( ) ,
908
+ _ => unreachable ! ( "only named export is allowed for src = None" ) ,
909
+ } ) ) ;
910
+ }
911
+ ModuleItem :: ModuleDecl ( ModuleDecl :: ExportDefaultExpr ( ExportDefaultExpr {
912
+ expr,
913
+ ..
914
+ } ) ) if expr. is_ident ( ) => {
915
+ names. extend ( expr. as_ident ( ) . map ( Ident :: to_id) ) ;
916
+ }
917
+ _ => { }
918
+ }
919
+ }
920
+
921
+ names
922
+ }
882
923
}
883
924
884
925
impl Transform {
0 commit comments