Skip to content

Commit

Permalink
feat(transformer/object-rest-spread): using helper loader (#6449)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Oct 14, 2024
1 parent c0e9d7e commit 41c8675
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 39 deletions.
1 change: 0 additions & 1 deletion crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,6 @@ impl<'a> HelperLoaderStore<'a> {
}

/// Load a helper function and return the callee expression.
#[allow(dead_code)]
pub fn load(&self, helper_name: Atom<'a>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
match self.mode {
HelperLoaderMode::Runtime => self.transform_for_runtime_helper(&helper_name, ctx),
Expand Down
13 changes: 8 additions & 5 deletions crates/oxc_transformer/src/es2018/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,28 @@ pub use options::ES2018Options;
use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};

pub struct ES2018 {
use crate::context::TransformCtx;

pub struct ES2018<'a, 'ctx> {
options: ES2018Options,

// Plugins
object_rest_spread: ObjectRestSpread,
object_rest_spread: ObjectRestSpread<'a, 'ctx>,
}

impl ES2018 {
pub fn new(options: ES2018Options) -> Self {
impl<'a, 'ctx> ES2018<'a, 'ctx> {
pub fn new(options: ES2018Options, ctx: &'ctx TransformCtx<'a>) -> Self {
Self {
object_rest_spread: ObjectRestSpread::new(
options.object_rest_spread.unwrap_or_default(),
ctx,
),
options,
}
}
}

impl<'a> Traverse<'a> for ES2018 {
impl<'a, 'ctx> Traverse<'a> for ES2018<'a, 'ctx> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.object_rest_spread.is_some() {
self.object_rest_spread.enter_expression(expr, ctx);
Expand Down
14 changes: 8 additions & 6 deletions crates/oxc_transformer/src/es2018/object_rest_spread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use oxc_ast::ast::*;
use oxc_traverse::{Traverse, TraverseCtx};
use serde::Deserialize;

use crate::context::TransformCtx;

mod object_rest;
mod object_spread;

Expand All @@ -43,27 +45,27 @@ pub struct ObjectRestSpreadOptions {
pub(crate) use_built_ins: bool,
}

pub struct ObjectRestSpread {
pub struct ObjectRestSpread<'a, 'ctx> {
#[allow(dead_code)]
options: ObjectRestSpreadOptions,

// Plugins
object_spread: ObjectSpread,
object_spread: ObjectSpread<'a, 'ctx>,
#[allow(dead_code)]
object_rest: ObjectRest,
}

impl ObjectRestSpread {
pub fn new(options: ObjectRestSpreadOptions) -> Self {
impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
pub fn new(options: ObjectRestSpreadOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
Self {
object_spread: ObjectSpread::new(options),
object_spread: ObjectSpread::new(options, ctx),
object_rest: ObjectRest::new(options),
options,
}
}
}

impl<'a> Traverse<'a> for ObjectRestSpread {
impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.object_spread.enter_expression(expr, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,21 @@ use oxc_semantic::{ReferenceFlags, SymbolId};
use oxc_span::SPAN;
use oxc_traverse::{Traverse, TraverseCtx};

use crate::context::TransformCtx;

use super::ObjectRestSpreadOptions;

pub struct ObjectSpread {
pub struct ObjectSpread<'a, 'ctx> {
options: ObjectRestSpreadOptions,
ctx: &'ctx TransformCtx<'a>,
}

impl ObjectSpread {
pub fn new(options: ObjectRestSpreadOptions) -> Self {
Self { options }
impl<'a, 'ctx> ObjectSpread<'a, 'ctx> {
pub fn new(options: ObjectRestSpreadOptions, ctx: &'ctx TransformCtx<'a>) -> Self {
Self { options, ctx }
}
}
impl<'a> Traverse<'a> for ObjectSpread {
impl<'a, 'ctx> Traverse<'a> for ObjectSpread<'a, 'ctx> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let Expression::ObjectExpression(obj_expr) = expr else {
return;
Expand Down Expand Up @@ -77,9 +80,8 @@ impl<'a> Traverse<'a> for ObjectSpread {
arguments.push(Argument::from(ctx.ast.move_expression(&mut spread_prop.argument)));

let object_id = ctx.scopes().find_binding(ctx.current_scope_id(), "Object");
let babel_helpers_id = ctx.scopes().find_binding(ctx.current_scope_id(), "babelHelpers");

let callee = self.get_extend_object_callee(object_id, babel_helpers_id, ctx);
let callee = self.get_extend_object_callee(object_id, ctx);

// ({ ...x }) => _objectSpread({}, x)
*expr = ctx.ast.expression_call(SPAN, callee, NONE, arguments, false);
Expand All @@ -91,14 +93,14 @@ impl<'a> Traverse<'a> for ObjectSpread {
arguments.push(Argument::from(ctx.ast.move_expression(expr)));
arguments.push(Argument::from(ctx.ast.expression_object(SPAN, obj_prop_list, None)));

let callee = self.get_extend_object_callee(object_id, babel_helpers_id, ctx);
let callee = self.get_extend_object_callee(object_id, ctx);

*expr = ctx.ast.expression_call(SPAN, callee, NONE, arguments, false);
}
}
}

impl<'a> ObjectSpread {
impl<'a, 'ctx> ObjectSpread<'a, 'ctx> {
fn object_assign(symbol_id: Option<SymbolId>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
let ident =
ctx.create_reference_id(SPAN, Atom::from("Object"), symbol_id, ReferenceFlags::Read);
Expand All @@ -108,32 +110,19 @@ impl<'a> ObjectSpread {
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
}

fn babel_external_helper(
symbol_id: Option<SymbolId>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let ident = ctx.create_reference_id(
SPAN,
Atom::from("babelHelpers"),
symbol_id,
ReferenceFlags::Read,
);
let object = ctx.ast.expression_from_identifier_reference(ident);
let property = ctx.ast.identifier_name(SPAN, Atom::from("objectSpread2"));

Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
fn babel_external_helper(&self, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
self.ctx.helper_loader.load(Atom::from("objectSpread2"), ctx)
}

fn get_extend_object_callee(
&mut self,
object_id: Option<SymbolId>,
babel_helpers_id: Option<SymbolId>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
if self.options.set_spread_properties {
Self::object_assign(object_id, ctx)
} else {
Self::babel_external_helper(babel_helpers_id, ctx)
self.babel_external_helper(ctx)
}
}
}
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<'a> Transformer<'a> {
x2_es2021: ES2021::new(self.options.es2021, &self.ctx),
x2_es2020: ES2020::new(self.options.es2020, &self.ctx),
x2_es2019: ES2019::new(self.options.es2019),
x2_es2018: ES2018::new(self.options.es2018),
x2_es2018: ES2018::new(self.options.es2018, &self.ctx),
x2_es2016: ES2016::new(self.options.es2016, &self.ctx),
x3_es2015: ES2015::new(self.options.es2015),
x4_regexp: RegExp::new(self.options.regexp, &self.ctx),
Expand All @@ -112,7 +112,7 @@ struct TransformerImpl<'a, 'ctx> {
x2_es2021: ES2021<'a, 'ctx>,
x2_es2020: ES2020<'a, 'ctx>,
x2_es2019: ES2019,
x2_es2018: ES2018,
x2_es2018: ES2018<'a, 'ctx>,
x2_es2016: ES2016<'a, 'ctx>,
x3_es2015: ES2015<'a>,
x4_regexp: RegExp<'a, 'ctx>,
Expand Down

0 comments on commit 41c8675

Please sign in to comment.