Skip to content

Commit

Permalink
refactor(transformer/async-to-generator): using helper_call_expr
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Oct 17, 2024
1 parent fddcb1f commit 04485fe
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 27 deletions.
5 changes: 3 additions & 2 deletions crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,14 @@ fn default_as_module_name() -> Cow<'static, str> {
/// Available helpers.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub enum Helper {
AsyncToGenerator,
ObjectSpread2,
}

impl Helper {
const fn name(self) -> &'static str {
match self {
Self::AsyncToGenerator => "asyncToGenerator",
Self::ObjectSpread2 => "objectSpread2",
}
}
Expand Down Expand Up @@ -187,9 +189,8 @@ impl<'a> TransformCtx<'a> {
}

/// Same as [`TransformCtx::helper_call`], but returns a `CallExpression` wrapped in an `Expression`.
#[expect(dead_code)]
pub fn helper_call_expr(
&mut self,
&self,
helper: Helper,
arguments: Vec<'a, Argument<'a>>,
ctx: &mut TraverseCtx<'a>,
Expand Down
36 changes: 11 additions & 25 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,12 @@ use oxc_ast::{
},
NONE,
};
use oxc_span::{Atom, SPAN};
use oxc_syntax::{reference::ReferenceFlags, symbol::SymbolId};
use oxc_span::SPAN;
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};

use crate::context::TransformCtx;
use crate::{common::helper_loader::Helper, context::TransformCtx};

pub struct AsyncToGenerator<'a, 'ctx> {
#[allow(dead_code)]
ctx: &'ctx TransformCtx<'a>,
}

Expand Down Expand Up @@ -102,7 +100,7 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
if !func.r#async || func.generator {
return;
}
let new_function = Self::transform_function(func, ctx);
let new_function = self.transform_function(func, ctx);
*expr = ctx.ast.expression_from_function(new_function);
}
}
Expand All @@ -112,7 +110,7 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
if !func.r#async || func.generator {
return;
}
let new_function = Self::transform_function(func, ctx);
let new_function = self.transform_function(func, ctx);
if let Some(id) = func.id.take() {
*stmt = ctx.ast.statement_declaration(ctx.ast.declaration_variable(
SPAN,
Expand Down Expand Up @@ -145,8 +143,6 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
if !arrow.r#async {
return;
}
let babel_helpers_id = ctx.scopes().find_binding(ctx.current_scope_id(), "babelHelpers");
let callee = Self::get_helper_callee(babel_helpers_id, ctx);
let body = ctx.ast.function_body(
SPAN,
ctx.ast.move_vec(&mut arrow.body.directives),
Expand All @@ -172,7 +168,7 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
);
let parameters =
ctx.ast.vec1(ctx.ast.argument_expression(ctx.ast.expression_from_function(target)));
let call = ctx.ast.expression_call(SPAN, callee, NONE, parameters, false);
let call = self.ctx.helper_call_expr(Helper::AsyncToGenerator, parameters, ctx);
let body = ctx.ast.function_body(
SPAN,
ctx.ast.vec(),
Expand All @@ -185,21 +181,11 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
}

impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
fn get_helper_callee(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("asyncToGenerator"));
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false))
}

fn transform_function(func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) -> Function<'a> {
let babel_helpers_id = ctx.scopes().find_binding(ctx.current_scope_id(), "babelHelpers");
let callee = Self::get_helper_callee(babel_helpers_id, ctx);
fn transform_function(
&self,
func: &mut Function<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Function<'a> {
let target = ctx.ast.function(
func.r#type,
SPAN,
Expand All @@ -220,7 +206,7 @@ impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
);
let parameters =
ctx.ast.vec1(ctx.ast.argument_expression(ctx.ast.expression_from_function(target)));
let call = ctx.ast.expression_call(SPAN, callee, NONE, parameters, false);
let call = self.ctx.helper_call_expr(Helper::AsyncToGenerator, parameters, ctx);
let returns = ctx.ast.return_statement(SPAN, Some(call));
let body = Statement::ReturnStatement(ctx.ast.alloc(returns));
let body = ctx.ast.function_body(SPAN, ctx.ast.vec(), ctx.ast.vec1(body));
Expand Down

0 comments on commit 04485fe

Please sign in to comment.