From 390abca29ae8e3c14b33ecbf5f20e92d2bb0e4d8 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Thu, 17 Oct 2024 08:37:05 +0000 Subject: [PATCH] refactor(transformer/async-to-generator): use `helper_call_expr` (#6634) --- .../src/common/helper_loader.rs | 5 +-- .../src/es2017/async_to_generator.rs | 36 ++++++------------- 2 files changed, 14 insertions(+), 27 deletions(-) diff --git a/crates/oxc_transformer/src/common/helper_loader.rs b/crates/oxc_transformer/src/common/helper_loader.rs index 1f9c909074cba..7169223514624 100644 --- a/crates/oxc_transformer/src/common/helper_loader.rs +++ b/crates/oxc_transformer/src/common/helper_loader.rs @@ -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", } } @@ -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>, diff --git a/crates/oxc_transformer/src/es2017/async_to_generator.rs b/crates/oxc_transformer/src/es2017/async_to_generator.rs index 4ddc618f0500c..fb54abac457e5 100644 --- a/crates/oxc_transformer/src/es2017/async_to_generator.rs +++ b/crates/oxc_transformer/src/es2017/async_to_generator.rs @@ -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>, } @@ -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); } } @@ -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, @@ -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), @@ -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(), @@ -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, 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, @@ -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));