Skip to content

Commit 31860fd

Browse files
committed
refactor(transformer): add more specific methods to ModuleImportsStore
1 parent 5f5e3eb commit 31860fd

File tree

3 files changed

+46
-29
lines changed

3 files changed

+46
-29
lines changed

crates/oxc_transformer/src/common/helper_loader.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ use oxc_traverse::{BoundIdentifier, Traverse, TraverseCtx};
7373
use rustc_hash::FxHashMap;
7474
use serde::Deserialize;
7575

76-
use super::module_imports::ImportKind;
7776
use crate::TransformCtx;
7877

7978
/// Defines the mode for loading helper functions.
@@ -150,7 +149,7 @@ impl<'a, 'ctx> HelperLoader<'a, 'ctx> {
150149
fn add_imports(&self) {
151150
self.ctx.helper_loader.loaded_helpers.borrow_mut().drain().for_each(
152151
|(_, (source, import))| {
153-
self.ctx.module_imports.add_import(source, ImportKind::new_default(import), false);
152+
self.ctx.module_imports.add_default_import(source, import, false);
154153
},
155154
);
156155
}

crates/oxc_transformer/src/common/module_imports.rs

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {

crates/oxc_transformer/src/react/jsx.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ use oxc_syntax::{
100100
};
101101
use oxc_traverse::{BoundIdentifier, Traverse, TraverseCtx};
102102

103-
use crate::{common::module_imports::ImportKind, TransformCtx};
103+
use crate::TransformCtx;
104104

105105
use super::diagnostics;
106106
pub use super::{
@@ -198,8 +198,7 @@ impl<'a, 'ctx> AutomaticScriptBindings<'a, 'ctx> {
198198
) -> BoundIdentifier<'a> {
199199
let binding =
200200
ctx.generate_uid_in_root_scope(variable_name, SymbolFlags::FunctionScopedVariable);
201-
let import = ImportKind::new_default(binding.clone());
202-
self.ctx.module_imports.add_import(source, import, front);
201+
self.ctx.module_imports.add_default_import(source, binding.clone(), front);
203202
binding
204203
}
205204
}
@@ -297,8 +296,7 @@ impl<'a, 'ctx> AutomaticModuleBindings<'a, 'ctx> {
297296
ctx: &mut TraverseCtx<'a>,
298297
) -> BoundIdentifier<'a> {
299298
let binding = ctx.generate_uid_in_root_scope(name, SymbolFlags::Import);
300-
let import = ImportKind::new_named(Atom::from(name), binding.clone());
301-
self.ctx.module_imports.add_import(source, import, false);
299+
self.ctx.module_imports.add_named_import(source, Atom::from(name), binding.clone(), false);
302300
binding
303301
}
304302
}

0 commit comments

Comments
 (0)