Skip to content

Commit

Permalink
feat(transformer): pass TransformerCtx to async-to-generator plugin (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Oct 17, 2024
1 parent 2ff917f commit a01a5df
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
24 changes: 16 additions & 8 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -173,11 +184,8 @@ impl<'a> Traverse<'a> for AsyncToGenerator {
}
}

impl AsyncToGenerator {
fn get_helper_callee<'a>(
symbol_id: Option<SymbolId>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
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"),
Expand All @@ -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(
Expand Down
13 changes: 7 additions & 6 deletions crates/oxc_transformer/src/es2017/mod.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
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;

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);
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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>,
Expand Down

0 comments on commit a01a5df

Please sign in to comment.