Skip to content

Commit

Permalink
feat(transformer/async-to-generator): handle this within arrows function
Browse files Browse the repository at this point in the history
  • Loading branch information
Dunqing committed Nov 1, 2024
1 parent 1674cf5 commit 0d94534
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 368 deletions.
400 changes: 400 additions & 0 deletions crates/oxc_transformer/src/common/arrow_function_to_expression.rs

Large diffs are not rendered by default.

53 changes: 53 additions & 0 deletions crates/oxc_transformer/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ use oxc_traverse::{Traverse, TraverseCtx};

use crate::TransformCtx;

pub mod arrow_function_to_expression;
pub mod helper_loader;
pub mod module_imports;
pub mod statement_injector;
pub mod top_level_statements;
pub mod var_declarations;

use arrow_function_to_expression::ArrowFunctionToExpression;
use module_imports::ModuleImports;
use statement_injector::StatementInjector;
use top_level_statements::TopLevelStatements;
Expand All @@ -22,6 +24,7 @@ pub struct Common<'a, 'ctx> {
var_declarations: VarDeclarations<'a, 'ctx>,
statement_injector: StatementInjector<'a, 'ctx>,
top_level_statements: TopLevelStatements<'a, 'ctx>,
arrow_function_to_expression: ArrowFunctionToExpression<'a, 'ctx>,
}

impl<'a, 'ctx> Common<'a, 'ctx> {
Expand All @@ -31,13 +34,15 @@ impl<'a, 'ctx> Common<'a, 'ctx> {
var_declarations: VarDeclarations::new(ctx),
statement_injector: StatementInjector::new(ctx),
top_level_statements: TopLevelStatements::new(ctx),
arrow_function_to_expression: ArrowFunctionToExpression::new(ctx),
}
}
}

impl<'a, 'ctx> Traverse<'a> for Common<'a, 'ctx> {
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.module_imports.exit_program(program, ctx);
self.arrow_function_to_expression.exit_program(program, ctx);
self.var_declarations.exit_program(program, ctx);
self.top_level_statements.exit_program(program, ctx);
}
Expand All @@ -58,4 +63,52 @@ impl<'a, 'ctx> Traverse<'a> for Common<'a, 'ctx> {
self.var_declarations.exit_statements(stmts, ctx);
self.statement_injector.exit_statements(stmts, ctx);
}

fn enter_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
self.arrow_function_to_expression.enter_function(func, ctx);
}

fn exit_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
self.arrow_function_to_expression.exit_function(func, ctx);
}

fn exit_method_definition(
&mut self,
node: &mut MethodDefinition<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.arrow_function_to_expression.exit_method_definition(node, ctx);
}

fn enter_static_block(&mut self, block: &mut StaticBlock<'a>, ctx: &mut TraverseCtx<'a>) {
self.arrow_function_to_expression.enter_static_block(block, ctx);
}

fn exit_static_block(&mut self, block: &mut StaticBlock<'a>, ctx: &mut TraverseCtx<'a>) {
self.arrow_function_to_expression.exit_static_block(block, ctx);
}

fn enter_jsx_element_name(
&mut self,
element_name: &mut JSXElementName<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.arrow_function_to_expression.enter_jsx_element_name(element_name, ctx);
}

fn enter_jsx_member_expression_object(
&mut self,
object: &mut JSXMemberExpressionObject<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.arrow_function_to_expression.enter_jsx_member_expression_object(object, ctx);
}

fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.arrow_function_to_expression.enter_expression(expr, ctx);
}

fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
self.arrow_function_to_expression.exit_expression(expr, ctx);
}
}
4 changes: 4 additions & 0 deletions crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use oxc_span::SourceType;

use crate::{
common::{
arrow_function_to_expression::ArrowFunctionToExpressionStore,
helper_loader::HelperLoaderStore, module_imports::ModuleImportsStore,
statement_injector::StatementInjectorStore, top_level_statements::TopLevelStatementsStore,
var_declarations::VarDeclarationsStore,
Expand Down Expand Up @@ -40,6 +41,8 @@ pub struct TransformCtx<'a> {
pub statement_injector: StatementInjectorStore<'a>,
/// Manage inserting statements at top of program globally
pub top_level_statements: TopLevelStatementsStore<'a>,
/// Manage converting arrow functions to expressions
pub arrow_function_to_expression: ArrowFunctionToExpressionStore,
}

impl<'a> TransformCtx<'a> {
Expand All @@ -63,6 +66,7 @@ impl<'a> TransformCtx<'a> {
var_declarations: VarDeclarationsStore::new(),
statement_injector: StatementInjectorStore::new(),
top_level_statements: TopLevelStatementsStore::new(),
arrow_function_to_expression: ArrowFunctionToExpressionStore::new(),
}
}

Expand Down
Loading

0 comments on commit 0d94534

Please sign in to comment.