feat(transformer): pass TransformerCtx to async-to-generator plugin (#6633)

This commit is contained in:
Dunqing 2024-10-17 08:25:58 +00:00
parent 2ff917fdac
commit a01a5dfdaf
3 changed files with 25 additions and 16 deletions

View file

@ -53,9 +53,20 @@ use oxc_span::{Atom, SPAN};
use oxc_syntax::{reference::ReferenceFlags, symbol::SymbolId}; use oxc_syntax::{reference::ReferenceFlags, symbol::SymbolId};
use oxc_traverse::{Ancestor, Traverse, TraverseCtx}; 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>) { fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Expression::AwaitExpression(await_expr) = expr { if let Expression::AwaitExpression(await_expr) = expr {
// Do not transform top-level await, or in async generator functions. // Do not transform top-level await, or in async generator functions.
@ -173,11 +184,8 @@ impl<'a> Traverse<'a> for AsyncToGenerator {
} }
} }
impl AsyncToGenerator { impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
fn get_helper_callee<'a>( fn get_helper_callee(symbol_id: Option<SymbolId>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
symbol_id: Option<SymbolId>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let ident = ctx.create_reference_id( let ident = ctx.create_reference_id(
SPAN, SPAN,
Atom::from("babelHelpers"), Atom::from("babelHelpers"),
@ -189,7 +197,7 @@ impl AsyncToGenerator {
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false)) 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 babel_helpers_id = ctx.scopes().find_binding(ctx.current_scope_id(), "babelHelpers");
let callee = Self::get_helper_callee(babel_helpers_id, ctx); let callee = Self::get_helper_callee(babel_helpers_id, ctx);
let target = ctx.ast.function( let target = ctx.ast.function(

View file

@ -1,6 +1,7 @@
use oxc_ast::ast::{ArrowFunctionExpression, Expression, Statement}; use oxc_ast::ast::{ArrowFunctionExpression, Expression, Statement};
use oxc_traverse::{Traverse, TraverseCtx}; use oxc_traverse::{Traverse, TraverseCtx};
use crate::context::TransformCtx;
use crate::es2017::async_to_generator::AsyncToGenerator; use crate::es2017::async_to_generator::AsyncToGenerator;
use crate::es2017::options::ES2017Options; use crate::es2017::options::ES2017Options;
@ -8,20 +9,20 @@ mod async_to_generator;
pub mod options; pub mod options;
#[allow(dead_code)] #[allow(dead_code)]
pub struct ES2017 { pub struct ES2017<'a, 'ctx> {
options: ES2017Options, options: ES2017Options,
// Plugins // Plugins
async_to_generator: AsyncToGenerator, async_to_generator: AsyncToGenerator<'a, 'ctx>,
} }
impl ES2017 { impl<'a, 'ctx> ES2017<'a, 'ctx> {
pub fn new(options: ES2017Options) -> ES2017 { pub fn new(options: ES2017Options, ctx: &'ctx TransformCtx<'a>) -> ES2017<'a, 'ctx> {
ES2017 { async_to_generator: AsyncToGenerator, options } 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>) { fn exit_expression(&mut self, node: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.async_to_generator { if self.options.async_to_generator {
self.async_to_generator.exit_expression(node, ctx); self.async_to_generator.exit_expression(node, ctx);

View file

@ -98,7 +98,7 @@ impl<'a> Transformer<'a> {
x2_es2019: ES2019::new(self.options.es2019), x2_es2019: ES2019::new(self.options.es2019),
x2_es2018: ES2018::new(self.options.es2018, &self.ctx), x2_es2018: ES2018::new(self.options.es2018, &self.ctx),
x2_es2016: ES2016::new(self.options.es2016, &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), x3_es2015: ES2015::new(self.options.es2015),
x4_regexp: RegExp::new(self.options.regexp, &self.ctx), x4_regexp: RegExp::new(self.options.regexp, &self.ctx),
common: Common::new(&self.ctx), common: Common::new(&self.ctx),
@ -117,7 +117,7 @@ struct TransformerImpl<'a, 'ctx> {
x2_es2020: ES2020<'a, 'ctx>, x2_es2020: ES2020<'a, 'ctx>,
x2_es2019: ES2019, x2_es2019: ES2019,
x2_es2018: ES2018<'a, 'ctx>, x2_es2018: ES2018<'a, 'ctx>,
x2_es2017: ES2017, x2_es2017: ES2017<'a, 'ctx>,
x2_es2016: ES2016<'a, 'ctx>, x2_es2016: ES2016<'a, 'ctx>,
x3_es2015: ES2015<'a>, x3_es2015: ES2015<'a>,
x4_regexp: RegExp<'a, 'ctx>, x4_regexp: RegExp<'a, 'ctx>,