Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/oxc_transformer/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ impl<'a> Traverse<'a> for Common<'a, '_> {
self.statement_injector.exit_statements(stmts, ctx);
}

#[inline]
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.statement_injector.enter_statement(stmt, ctx);
}

#[inline]
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.statement_injector.exit_statement(stmt, ctx);
}
Comment on lines +74 to +76
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this can be correct. When exiting a statement, don't you need to restore the address of the parent statement? e.g.:

const x = () => {
  let y;
};

After exiting the statement let y, the current statement is const x = ... again.

I guess would need a stack to achieve this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, This big problem I've overlooked. Thanks for pointing it out


fn enter_function(&mut self, func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
self.arrow_function_converter.enter_function(func, ctx);
}
Expand Down
60 changes: 58 additions & 2 deletions crates/oxc_transformer/src/common/statement_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! self.ctx.statement_injector.insert_many_after(address, statements);
//! ```

use std::cell::RefCell;
use std::cell::{Cell, RefCell};

use rustc_hash::FxHashMap;

Expand All @@ -34,6 +34,14 @@ impl<'a, 'ctx> StatementInjector<'a, 'ctx> {
}

impl<'a> Traverse<'a> for StatementInjector<'a, '_> {
fn enter_statement(&mut self, stmt: &mut Statement<'a>, _ctx: &mut TraverseCtx<'a>) {
self.ctx.statement_injector.set_current_statement_address(stmt);
}

fn exit_statement(&mut self, stmt: &mut Statement<'a>, _ctx: &mut TraverseCtx<'a>) {
self.ctx.statement_injector.set_current_statement_address(stmt);
}

fn exit_statements(
&mut self,
statements: &mut ArenaVec<'a, Statement<'a>>,
Expand All @@ -57,14 +65,18 @@ struct AdjacentStatement<'a> {

/// Store for statements to be added to the statements.
pub struct StatementInjectorStore<'a> {
current_statement_address: Cell<Address>,
insertions: RefCell<FxHashMap<Address, Vec<AdjacentStatement<'a>>>>,
}

// Public methods
impl StatementInjectorStore<'_> {
/// Create new `StatementInjectorStore`.
pub fn new() -> Self {
Self { insertions: RefCell::new(FxHashMap::default()) }
Self {
current_statement_address: Cell::new(Address::DUMMY),
insertions: RefCell::new(FxHashMap::default()),
}
}
}

Expand Down Expand Up @@ -148,6 +160,44 @@ impl<'a> StatementInjectorStore<'a> {
stmts.into_iter().map(|stmt| AdjacentStatement { stmt, direction: Direction::After }),
);
}

/// Add a statement to be inserted immediately before the current statement.
#[expect(unused)]
#[inline]
pub fn insert_before_current_statement(&self, stmt: Statement<'a>) {
debug_assert_ne!(self.current_statement_address.get(), Address::DUMMY);
self.insert_before_address(self.current_statement_address.get(), stmt);
}

/// Add a statement to be inserted immediately after the current statement.
#[expect(unused)]
#[inline]
pub fn insert_after_current_statement(&self, stmt: Statement<'a>) {
debug_assert_ne!(self.current_statement_address.get(), Address::DUMMY);
self.insert_after_address(self.current_statement_address.get(), stmt);
}

/// Add multiple statements to be inserted immediately before the current statement.
#[expect(unused)]
#[inline]
pub fn insert_many_before_current_statement<S>(&self, stmts: S)
where
S: IntoIterator<Item = Statement<'a>>,
{
debug_assert_ne!(self.current_statement_address.get(), Address::DUMMY);
self.insert_many_before_address(self.current_statement_address.get(), stmts);
}

/// Add multiple statements to be inserted immediately after the current statement.
#[expect(unused)]
#[inline]
pub fn insert_many_after_current_statement<S>(&self, stmts: S)
where
S: IntoIterator<Item = Statement<'a>>,
{
debug_assert_ne!(self.current_statement_address.get(), Address::DUMMY);
self.insert_many_after_address(self.current_statement_address.get(), stmts);
}
}

// Internal methods
Expand All @@ -167,6 +217,7 @@ impl<'a> StatementInjectorStore<'a> {
.iter()
.filter_map(|s| insertions.get(&s.address()).map(Vec::len))
.sum::<usize>();

if new_statement_count == 0 {
return;
}
Expand Down Expand Up @@ -195,4 +246,9 @@ impl<'a> StatementInjectorStore<'a> {

*statements = new_statements;
}

#[inline]
fn set_current_statement_address(&self, stmt: &Statement<'a>) {
self.current_statement_address.set(stmt.address());
}
}
2 changes: 2 additions & 0 deletions crates/oxc_transformer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ impl<'a> Traverse<'a> for TransformerImpl<'a, '_> {
}

fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.common.exit_statement(stmt, ctx);
if let Some(typescript) = self.x0_typescript.as_mut() {
typescript.exit_statement(stmt, ctx);
}
Expand All @@ -549,6 +550,7 @@ impl<'a> Traverse<'a> for TransformerImpl<'a, '_> {
}

fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
self.common.enter_statement(stmt, ctx);
if let Some(typescript) = self.x0_typescript.as_mut() {
typescript.enter_statement(stmt, ctx);
}
Expand Down