Skip to content

Commit

Permalink
feat(transformer): support replace and remove operation in StatementM…
Browse files Browse the repository at this point in the history
…anipulator
  • Loading branch information
Dunqing committed Oct 30, 2024
1 parent 0ae4cd6 commit 030572c
Showing 1 changed file with 36 additions and 7 deletions.
43 changes: 36 additions & 7 deletions crates/oxc_transformer/src/common/statement_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,36 @@ struct AdjacentStatement<'a> {
direction: Direction,
}

#[derive(Debug, Eq, PartialEq, Hash)]
enum Action {
Insert,
Replace,
Delete,
}

#[derive(Debug, Eq, PartialEq, Hash)]
struct ActionAddress {
action: Action,
address: Address,
}

impl ActionAddress {
fn insert(address: Address) -> Self {
Self { action: Action::Insert, address }
}

fn replace(address: Address) -> Self {
Self { action: Action::Replace, address }
}

fn delete(address: Address) -> Self {
Self { action: Action::Delete, address }
}
}

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

impl<'a> StatementInjectorStore<'a> {
Expand All @@ -83,7 +110,7 @@ impl<'a> StatementInjectorStore<'a> {

fn insert_before_address(&self, target: Address, stmt: Statement<'a>) {
let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default();
let adjacent_stmts = insertions.entry(ActionAddress::insert(target)).or_default();
let index = adjacent_stmts
.iter()
.position(|s| matches!(s.direction, Direction::After))
Expand All @@ -99,7 +126,7 @@ impl<'a> StatementInjectorStore<'a> {

fn insert_after_address(&self, target: Address, stmt: Statement<'a>) {
let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default();
let adjacent_stmts = insertions.entry(ActionAddress::insert(target)).or_default();
adjacent_stmts.push(AdjacentStatement { stmt, direction: Direction::After });
}

Expand All @@ -119,7 +146,7 @@ impl<'a> StatementInjectorStore<'a> {
S: IntoIterator<Item = Statement<'a>>,
{
let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default();
let adjacent_stmts = insertions.entry(ActionAddress::insert(target)).or_default();
adjacent_stmts.splice(
0..0,
stmts.into_iter().map(|stmt| AdjacentStatement { stmt, direction: Direction::Before }),
Expand All @@ -141,7 +168,7 @@ impl<'a> StatementInjectorStore<'a> {
S: IntoIterator<Item = Statement<'a>>,
{
let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default();
let adjacent_stmts = insertions.entry(ActionAddress::insert(target)).or_default();
adjacent_stmts.extend(
stmts.into_iter().map(|stmt| AdjacentStatement { stmt, direction: Direction::After }),
);
Expand All @@ -160,7 +187,7 @@ impl<'a> StatementInjectorStore<'a> {

let new_statement_count = statements
.iter()
.filter_map(|s| insertions.get(&s.address()).map(Vec::len))
.filter_map(|s| insertions.get(&ActionAddress::insert(s.address())).map(Vec::len))
.sum::<usize>();
if new_statement_count == 0 {
return;
Expand All @@ -169,7 +196,9 @@ impl<'a> StatementInjectorStore<'a> {
let mut new_statements = ctx.ast.vec_with_capacity(statements.len() + new_statement_count);

for stmt in statements.drain(..) {
if let Some(mut adjacent_stmts) = insertions.remove(&stmt.address()) {
if let Some(mut adjacent_stmts) =
insertions.remove(&ActionAddress::insert(stmt.address()))
{
let first_after_stmt_index = adjacent_stmts
.iter()
.position(|s| matches!(s.direction, Direction::After))
Expand Down

0 comments on commit 030572c

Please sign in to comment.