From a01a5dfdafb9cd536cb87867697e3ae43b1990e6 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Thu, 17 Oct 2024 08:25:58 +0000 Subject: [PATCH] feat(transformer): pass TransformerCtx to async-to-generator plugin (#6633) --- .../src/es2017/async_to_generator.rs | 24 ++++++++++++------- crates/oxc_transformer/src/es2017/mod.rs | 13 +++++----- crates/oxc_transformer/src/lib.rs | 4 ++-- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/crates/oxc_transformer/src/es2017/async_to_generator.rs b/crates/oxc_transformer/src/es2017/async_to_generator.rs index a48c7248afdea..4ddc618f0500c 100644 --- a/crates/oxc_transformer/src/es2017/async_to_generator.rs +++ b/crates/oxc_transformer/src/es2017/async_to_generator.rs @@ -53,9 +53,20 @@ use oxc_span::{Atom, SPAN}; use oxc_syntax::{reference::ReferenceFlags, symbol::SymbolId}; use oxc_traverse::{Ancestor, Traverse, TraverseCtx}; -pub struct AsyncToGenerator; +use crate::context::TransformCtx; -impl<'a> Traverse<'a> for AsyncToGenerator { +pub struct AsyncToGenerator<'a, 'ctx> { + #[allow(dead_code)] + ctx: &'ctx TransformCtx<'a>, +} + +impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> { + pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self { + Self { ctx } + } +} + +impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> { fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { if let Expression::AwaitExpression(await_expr) = expr { // Do not transform top-level await, or in async generator functions. @@ -173,11 +184,8 @@ impl<'a> Traverse<'a> for AsyncToGenerator { } } -impl AsyncToGenerator { - fn get_helper_callee<'a>( - symbol_id: Option, - ctx: &mut TraverseCtx<'a>, - ) -> Expression<'a> { +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"), @@ -189,7 +197,7 @@ impl AsyncToGenerator { Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false)) } - fn transform_function<'a>(func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) -> Function<'a> { + 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( diff --git a/crates/oxc_transformer/src/es2017/mod.rs b/crates/oxc_transformer/src/es2017/mod.rs index f2f2442c1f20f..9ef2e1f3d6162 100644 --- a/crates/oxc_transformer/src/es2017/mod.rs +++ b/crates/oxc_transformer/src/es2017/mod.rs @@ -1,6 +1,7 @@ use oxc_ast::ast::{ArrowFunctionExpression, Expression, Statement}; use oxc_traverse::{Traverse, TraverseCtx}; +use crate::context::TransformCtx; use crate::es2017::async_to_generator::AsyncToGenerator; use crate::es2017::options::ES2017Options; @@ -8,20 +9,20 @@ mod async_to_generator; pub mod options; #[allow(dead_code)] -pub struct ES2017 { +pub struct ES2017<'a, 'ctx> { options: ES2017Options, // Plugins - async_to_generator: AsyncToGenerator, + async_to_generator: AsyncToGenerator<'a, 'ctx>, } -impl ES2017 { - pub fn new(options: ES2017Options) -> ES2017 { - ES2017 { async_to_generator: AsyncToGenerator, options } +impl<'a, 'ctx> ES2017<'a, 'ctx> { + pub fn new(options: ES2017Options, ctx: &'ctx TransformCtx<'a>) -> ES2017<'a, 'ctx> { + ES2017 { async_to_generator: AsyncToGenerator::new(ctx), options } } } -impl<'a> Traverse<'a> for ES2017 { +impl<'a, 'ctx> Traverse<'a> for ES2017<'a, 'ctx> { fn exit_expression(&mut self, node: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { if self.options.async_to_generator { self.async_to_generator.exit_expression(node, ctx); diff --git a/crates/oxc_transformer/src/lib.rs b/crates/oxc_transformer/src/lib.rs index 14b8c202cfca6..a0fdea9852eaf 100644 --- a/crates/oxc_transformer/src/lib.rs +++ b/crates/oxc_transformer/src/lib.rs @@ -98,7 +98,7 @@ impl<'a> Transformer<'a> { x2_es2019: ES2019::new(self.options.es2019), x2_es2018: ES2018::new(self.options.es2018, &self.ctx), x2_es2016: ES2016::new(self.options.es2016, &self.ctx), - x2_es2017: ES2017::new(self.options.es2017), + x2_es2017: ES2017::new(self.options.es2017, &self.ctx), x3_es2015: ES2015::new(self.options.es2015), x4_regexp: RegExp::new(self.options.regexp, &self.ctx), common: Common::new(&self.ctx), @@ -117,7 +117,7 @@ struct TransformerImpl<'a, 'ctx> { x2_es2020: ES2020<'a, 'ctx>, x2_es2019: ES2019, x2_es2018: ES2018<'a, 'ctx>, - x2_es2017: ES2017, + x2_es2017: ES2017<'a, 'ctx>, x2_es2016: ES2016<'a, 'ctx>, x3_es2015: ES2015<'a>, x4_regexp: RegExp<'a, 'ctx>,