Skip to content

Commit

Permalink
style: rename field
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Oct 21, 2024
1 parent 424407c commit 2270e72
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions crates/oxc_transformer/src/common/statement_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,20 @@ struct AdjacentStatement<'a> {

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

// Public methods
impl<'a> StatementInjectorStore<'a> {
/// Create new `StatementInjectorStore`.
pub fn new() -> Self {
Self { stmts: RefCell::new(FxHashMap::default()) }
Self { insertions: RefCell::new(FxHashMap::default()) }
}

/// Add a statement to be inserted immediately before the target statement.
#[expect(dead_code)]
pub fn insert_before(&self, target: Address, stmt: Statement<'a>) {
let mut insertions = self.stmts.borrow_mut();
let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default();
let index = adjacent_stmts
.iter()
Expand All @@ -80,15 +80,15 @@ impl<'a> StatementInjectorStore<'a> {
/// Add a statement to be inserted immediately after the target statement.
#[expect(dead_code)]
pub fn insert_after(&self, target: Address, stmt: Statement<'a>) {
let mut insertions = self.stmts.borrow_mut();
let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default();
adjacent_stmts.push(AdjacentStatement { stmt, direction: Direction::After });
}

/// Add multiple statements to be inserted immediately after the target statement.
#[expect(dead_code)]
pub fn insert_many_after(&self, target: Address, stmts: Vec<Statement<'a>>) {
let mut insertions = self.stmts.borrow_mut();
let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default();
adjacent_stmts.extend(
stmts.into_iter().map(|stmt| AdjacentStatement { stmt, direction: Direction::After }),
Expand All @@ -101,21 +101,23 @@ impl<'a> StatementInjectorStore<'a> {
statements: &mut OxcVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
let mut stmts = self.stmts.borrow_mut();
if stmts.is_empty() {
let mut insertions = self.insertions.borrow_mut();
if insertions.is_empty() {
return;
}

let new_statement_count =
statements.iter().filter_map(|s| stmts.get(&s.address()).map(Vec::len)).sum::<usize>();
let new_statement_count = statements
.iter()
.filter_map(|s| insertions.get(&s.address()).map(Vec::len))
.sum::<usize>();
if new_statement_count == 0 {
return;
}

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) = stmts.remove(&stmt.address()) {
if let Some(mut adjacent_stmts) = insertions.remove(&stmt.address()) {
let first_after_stmt_index = adjacent_stmts
.iter()
.position(|s| matches!(s.direction, Direction::After))
Expand Down

0 comments on commit 2270e72

Please sign in to comment.