mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(transformer/async-to-generator): use helper_call_expr (#6634)
This commit is contained in:
parent
a01a5dfdaf
commit
390abca29a
2 changed files with 14 additions and 27 deletions
|
|
@ -137,12 +137,14 @@ fn default_as_module_name() -> Cow<'static, str> {
|
||||||
/// Available helpers.
|
/// Available helpers.
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
||||||
pub enum Helper {
|
pub enum Helper {
|
||||||
|
AsyncToGenerator,
|
||||||
ObjectSpread2,
|
ObjectSpread2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Helper {
|
impl Helper {
|
||||||
const fn name(self) -> &'static str {
|
const fn name(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
|
Self::AsyncToGenerator => "asyncToGenerator",
|
||||||
Self::ObjectSpread2 => "objectSpread2",
|
Self::ObjectSpread2 => "objectSpread2",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -187,9 +189,8 @@ impl<'a> TransformCtx<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as [`TransformCtx::helper_call`], but returns a `CallExpression` wrapped in an `Expression`.
|
/// Same as [`TransformCtx::helper_call`], but returns a `CallExpression` wrapped in an `Expression`.
|
||||||
#[expect(dead_code)]
|
|
||||||
pub fn helper_call_expr(
|
pub fn helper_call_expr(
|
||||||
&mut self,
|
&self,
|
||||||
helper: Helper,
|
helper: Helper,
|
||||||
arguments: Vec<'a, Argument<'a>>,
|
arguments: Vec<'a, Argument<'a>>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
|
|
|
||||||
|
|
@ -49,14 +49,12 @@ use oxc_ast::{
|
||||||
},
|
},
|
||||||
NONE,
|
NONE,
|
||||||
};
|
};
|
||||||
use oxc_span::{Atom, SPAN};
|
use oxc_span::SPAN;
|
||||||
use oxc_syntax::{reference::ReferenceFlags, symbol::SymbolId};
|
|
||||||
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};
|
use oxc_traverse::{Ancestor, Traverse, TraverseCtx};
|
||||||
|
|
||||||
use crate::context::TransformCtx;
|
use crate::{common::helper_loader::Helper, context::TransformCtx};
|
||||||
|
|
||||||
pub struct AsyncToGenerator<'a, 'ctx> {
|
pub struct AsyncToGenerator<'a, 'ctx> {
|
||||||
#[allow(dead_code)]
|
|
||||||
ctx: &'ctx TransformCtx<'a>,
|
ctx: &'ctx TransformCtx<'a>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,7 +100,7 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
|
||||||
if !func.r#async || func.generator {
|
if !func.r#async || func.generator {
|
||||||
return;
|
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);
|
*expr = ctx.ast.expression_from_function(new_function);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -112,7 +110,7 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
|
||||||
if !func.r#async || func.generator {
|
if !func.r#async || func.generator {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let new_function = Self::transform_function(func, ctx);
|
let new_function = self.transform_function(func, ctx);
|
||||||
if let Some(id) = func.id.take() {
|
if let Some(id) = func.id.take() {
|
||||||
*stmt = ctx.ast.statement_declaration(ctx.ast.declaration_variable(
|
*stmt = ctx.ast.statement_declaration(ctx.ast.declaration_variable(
|
||||||
SPAN,
|
SPAN,
|
||||||
|
|
@ -145,8 +143,6 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
|
||||||
if !arrow.r#async {
|
if !arrow.r#async {
|
||||||
return;
|
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(
|
let body = ctx.ast.function_body(
|
||||||
SPAN,
|
SPAN,
|
||||||
ctx.ast.move_vec(&mut arrow.body.directives),
|
ctx.ast.move_vec(&mut arrow.body.directives),
|
||||||
|
|
@ -172,7 +168,7 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
|
||||||
);
|
);
|
||||||
let parameters =
|
let parameters =
|
||||||
ctx.ast.vec1(ctx.ast.argument_expression(ctx.ast.expression_from_function(target)));
|
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(
|
let body = ctx.ast.function_body(
|
||||||
SPAN,
|
SPAN,
|
||||||
ctx.ast.vec(),
|
ctx.ast.vec(),
|
||||||
|
|
@ -185,21 +181,11 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
|
impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
|
||||||
fn get_helper_callee(symbol_id: Option<SymbolId>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
|
fn transform_function(
|
||||||
let ident = ctx.create_reference_id(
|
&self,
|
||||||
SPAN,
|
func: &mut Function<'a>,
|
||||||
Atom::from("babelHelpers"),
|
ctx: &mut TraverseCtx<'a>,
|
||||||
symbol_id,
|
) -> Function<'a> {
|
||||||
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);
|
|
||||||
let target = ctx.ast.function(
|
let target = ctx.ast.function(
|
||||||
func.r#type,
|
func.r#type,
|
||||||
SPAN,
|
SPAN,
|
||||||
|
|
@ -220,7 +206,7 @@ impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
|
||||||
);
|
);
|
||||||
let parameters =
|
let parameters =
|
||||||
ctx.ast.vec1(ctx.ast.argument_expression(ctx.ast.expression_from_function(target)));
|
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 returns = ctx.ast.return_statement(SPAN, Some(call));
|
||||||
let body = Statement::ReturnStatement(ctx.ast.alloc(returns));
|
let body = Statement::ReturnStatement(ctx.ast.alloc(returns));
|
||||||
let body = ctx.ast.function_body(SPAN, ctx.ast.vec(), ctx.ast.vec1(body));
|
let body = ctx.ast.function_body(SPAN, ctx.ast.vec(), ctx.ast.vec1(body));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue