@@ -62,27 +62,16 @@ impl<'a, 'ctx> Traverse<'a> for ModuleImports<'a, 'ctx> {
6262 }
6363}
6464
65- #[ derive( Clone ) ]
66- pub struct NamedImport < ' a > {
65+ struct NamedImport < ' a > {
6766 imported : Atom < ' a > ,
6867 local : BoundIdentifier < ' a > ,
6968}
7069
71- pub enum ImportKind < ' a > {
70+ enum ImportKind < ' a > {
7271 Named ( NamedImport < ' a > ) ,
7372 Default ( BoundIdentifier < ' a > ) ,
7473}
7574
76- impl < ' a > ImportKind < ' a > {
77- pub fn new_named ( imported : Atom < ' a > , local : BoundIdentifier < ' a > ) -> Self {
78- Self :: Named ( NamedImport { imported, local } )
79- }
80-
81- pub fn new_default ( local : BoundIdentifier < ' a > ) -> Self {
82- Self :: Default ( local)
83- }
84- }
85-
8675/// Store for `import` / `require` statements to be added at top of program.
8776///
8877/// TODO(improve-on-babel): Insertion order does not matter. We only have to use `IndexMap`
@@ -99,17 +88,56 @@ impl<'a> ModuleImportsStore<'a> {
9988 Self { imports : RefCell :: new ( IndexMap :: default ( ) ) }
10089 }
10190
91+ /// Add default `import` or `require` to top of program.
92+ ///
93+ /// Which it will be depends on the source type.
94+ ///
95+ /// * `import named_import from 'source';` or
96+ /// * `var named_import = require('source');`
97+ ///
98+ /// If `front` is `true`, `import`/`require` is added to front of the `import`s/`require`s.
99+ pub fn add_default_import ( & self , source : Atom < ' a > , local : BoundIdentifier < ' a > , front : bool ) {
100+ self . add_import ( source, ImportKind :: Default ( local) , front) ;
101+ }
102+
103+ /// Add named `import` to top of program.
104+ ///
105+ /// `import { named_import } from 'source';`
106+ ///
107+ /// If `front` is `true`, `import` is added to front of the `import`s.
108+ ///
109+ /// Adding named `require`s is not supported, and will cause a panic later on.
110+ pub fn add_named_import (
111+ & self ,
112+ source : Atom < ' a > ,
113+ imported : Atom < ' a > ,
114+ local : BoundIdentifier < ' a > ,
115+ front : bool ,
116+ ) {
117+ self . add_import ( source, ImportKind :: Named ( NamedImport { imported, local } ) , front) ;
118+ }
119+
120+ /// Returns `true` if no imports have been scheduled for insertion.
121+ pub fn is_empty ( & self ) -> bool {
122+ self . imports . borrow ( ) . is_empty ( )
123+ }
124+ }
125+
126+ // Internal methods
127+ impl < ' a > ModuleImportsStore < ' a > {
102128 /// Add `import` or `require` to top of program.
103129 ///
104130 /// Which it will be depends on the source type.
105131 ///
106132 /// * `import { named_import } from 'source';` or
107133 /// * `var named_import = require('source');`
108134 ///
135+ /// Adding a named `require` is not supported, and will cause a panic later on.
136+ ///
109137 /// If `front` is `true`, `import`/`require` is added to front of the `import`s/`require`s.
110138 /// TODO(improve-on-babel): `front` option is only required to pass one of Babel's tests. Output
111139 /// without it is still valid. Remove this once our output doesn't need to match Babel exactly.
112- pub fn add_import ( & self , source : Atom < ' a > , import : ImportKind < ' a > , front : bool ) {
140+ fn add_import ( & self , source : Atom < ' a > , import : ImportKind < ' a > , front : bool ) {
113141 match self . imports . borrow_mut ( ) . entry ( source) {
114142 IndexMapEntry :: Occupied ( mut entry) => {
115143 entry. get_mut ( ) . push ( import) ;
@@ -128,14 +156,6 @@ impl<'a> ModuleImportsStore<'a> {
128156 }
129157 }
130158
131- /// Returns `true` if no imports have been scheduled for insertion.
132- pub fn is_empty ( & self ) -> bool {
133- self . imports . borrow ( ) . is_empty ( )
134- }
135- }
136-
137- // Internal methods
138- impl < ' a > ModuleImportsStore < ' a > {
139159 /// Insert `import` / `require` statements at top of program.
140160 fn insert_into_program ( & self , transform_ctx : & TransformCtx < ' a > , ctx : & mut TraverseCtx < ' a > ) {
141161 if transform_ctx. source_type . is_script ( ) {
0 commit comments