Skip to content

Commit 78f5ca4

Browse files
committed
refactor(transformer): HelperLoader common transform: Helper enum
1 parent 389d261 commit 78f5ca4

File tree

3 files changed

+34
-21
lines changed

3 files changed

+34
-21
lines changed

crates/oxc_transformer/src/common/helper_loader.rs

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,21 @@ fn default_as_module_name() -> Cow<'static, str> {
128128
Cow::Borrowed("@babel/runtime")
129129
}
130130

131+
/// Available helpers.
132+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
133+
pub enum Helper {
134+
ObjectSpread2,
135+
}
136+
137+
impl Helper {
138+
const fn name(self) -> &'static str {
139+
match self {
140+
Self::ObjectSpread2 => "objectSpread2",
141+
}
142+
}
143+
}
144+
145+
/// Helper loader transform.
131146
pub struct HelperLoader<'a, 'ctx> {
132147
ctx: &'ctx TransformCtx<'a>,
133148
}
@@ -144,7 +159,7 @@ impl<'a, 'ctx> Traverse<'a> for HelperLoader<'a, 'ctx> {
144159
}
145160
}
146161

147-
struct Helper<'a> {
162+
struct LoadedHelper<'a> {
148163
source: Atom<'a>,
149164
local: BoundIdentifier<'a>,
150165
}
@@ -154,7 +169,7 @@ pub struct HelperLoaderStore<'a> {
154169
module_name: Cow<'static, str>,
155170
mode: HelperLoaderMode,
156171
/// Loaded helpers, determined what helpers are loaded and what imports should be added.
157-
loaded_helpers: RefCell<FxHashMap<Atom<'a>, Helper<'a>>>,
172+
loaded_helpers: RefCell<FxHashMap<Helper, LoadedHelper<'a>>>,
158173
}
159174

160175
// Public methods
@@ -171,11 +186,11 @@ impl<'a> HelperLoaderStore<'a> {
171186
#[expect(dead_code)]
172187
pub fn call(
173188
&mut self,
174-
helper_name: Atom<'a>,
189+
helper: Helper,
175190
arguments: Vec<'a, Argument<'a>>,
176191
ctx: &mut TraverseCtx<'a>,
177192
) -> CallExpression<'a> {
178-
let callee = self.load(helper_name, ctx);
193+
let callee = self.load(helper, ctx);
179194
ctx.ast.call_expression(
180195
SPAN,
181196
callee,
@@ -189,11 +204,11 @@ impl<'a> HelperLoaderStore<'a> {
189204
#[expect(dead_code)]
190205
pub fn call_expr(
191206
&mut self,
192-
helper_name: Atom<'a>,
207+
helper: Helper,
193208
arguments: Vec<'a, Argument<'a>>,
194209
ctx: &mut TraverseCtx<'a>,
195210
) -> Expression<'a> {
196-
let callee = self.load(helper_name, ctx);
211+
let callee = self.load(helper, ctx);
197212
ctx.ast.expression_call(
198213
SPAN,
199214
callee,
@@ -204,10 +219,10 @@ impl<'a> HelperLoaderStore<'a> {
204219
}
205220

206221
/// Load a helper function and return the callee expression.
207-
pub fn load(&self, helper_name: Atom<'a>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
222+
pub fn load(&self, helper: Helper, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
208223
match self.mode {
209-
HelperLoaderMode::Runtime => self.transform_for_runtime_helper(helper_name, ctx),
210-
HelperLoaderMode::External => Self::transform_for_external_helper(helper_name, ctx),
224+
HelperLoaderMode::Runtime => self.transform_for_runtime_helper(helper, ctx),
225+
HelperLoaderMode::External => Self::transform_for_external_helper(helper, ctx),
211226
HelperLoaderMode::Inline => {
212227
unreachable!("Inline helpers are not supported yet");
213228
}
@@ -219,29 +234,27 @@ impl<'a> HelperLoaderStore<'a> {
219234
impl<'a> HelperLoaderStore<'a> {
220235
fn transform_for_runtime_helper(
221236
&self,
222-
helper_name: Atom<'a>,
237+
helper: Helper,
223238
ctx: &mut TraverseCtx<'a>,
224239
) -> Expression<'a> {
225240
let mut loaded_helpers = self.loaded_helpers.borrow_mut();
226-
let helper = loaded_helpers.entry(helper_name).or_insert_with_key(|helper_name| {
241+
let loaded_helper = loaded_helpers.entry(helper).or_insert_with(|| {
242+
let helper_name = helper.name();
227243
let source = ctx.ast.atom(&format!("{}/helpers/{helper_name}", self.module_name));
228244
let local = ctx.generate_uid_in_root_scope(helper_name, SymbolFlags::Import);
229-
Helper { source, local }
245+
LoadedHelper { source, local }
230246
});
231-
helper.local.create_read_expression(ctx)
247+
loaded_helper.local.create_read_expression(ctx)
232248
}
233249

234-
fn transform_for_external_helper(
235-
helper_name: Atom<'a>,
236-
ctx: &mut TraverseCtx<'a>,
237-
) -> Expression<'a> {
250+
fn transform_for_external_helper(helper: Helper, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
238251
static HELPER_VAR: &str = "babelHelpers";
239252

240253
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), HELPER_VAR);
241254
let ident =
242255
ctx.create_reference_id(SPAN, Atom::from(HELPER_VAR), symbol_id, ReferenceFlags::Read);
243256
let object = ctx.ast.expression_from_identifier_reference(ident);
244-
let property = ctx.ast.identifier_name(SPAN, helper_name);
257+
let property = ctx.ast.identifier_name(SPAN, Atom::from(helper.name()));
245258
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
246259
}
247260

crates/oxc_transformer/src/common/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Utility transforms which are in common between other transforms.
22
3-
use helper_loader::HelperLoader;
43
use oxc_allocator::Vec;
54
use oxc_ast::ast::*;
65
use oxc_traverse::{Traverse, TraverseCtx};
@@ -12,6 +11,7 @@ pub mod module_imports;
1211
pub mod top_level_statements;
1312
pub mod var_declarations;
1413

14+
use helper_loader::HelperLoader;
1515
use module_imports::ModuleImports;
1616
use top_level_statements::TopLevelStatements;
1717
use var_declarations::VarDeclarations;

crates/oxc_transformer/src/es2018/object_rest_spread/object_spread.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use oxc_semantic::{ReferenceFlags, SymbolId};
3131
use oxc_span::SPAN;
3232
use oxc_traverse::{Traverse, TraverseCtx};
3333

34-
use crate::context::TransformCtx;
34+
use crate::{common::helper_loader::Helper, TransformCtx};
3535

3636
use super::ObjectRestSpreadOptions;
3737

@@ -132,6 +132,6 @@ impl<'a, 'ctx> ObjectSpread<'a, 'ctx> {
132132
}
133133

134134
fn babel_external_helper(&self, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
135-
self.ctx.helper_loader.load(Atom::from("objectSpread2"), ctx)
135+
self.ctx.helper_loader.load(Helper::ObjectSpread2, ctx)
136136
}
137137
}

0 commit comments

Comments
 (0)