refactor(transformer): StatementInjectorStore methods take &Statement as target (#6858)

This makes the use of `Address` an internal implementation detail of `StatementInjectorStore`. The caller shouldn't need to care about how `StatementInjectorStore` achieves its work. In future it might use `NodeId`, for instance.

We could broaden this in future so these methods take a `&A where A: GetAddress` if we need to, but for now taking a `&Statement` seems OK.
This commit is contained in:
overlookmotel 2024-10-24 12:38:57 +00:00
parent c19996c2e7
commit 333b758b49
2 changed files with 10 additions and 10 deletions

View file

@ -67,9 +67,9 @@ impl<'a> StatementInjectorStore<'a> {
/// Add a statement to be inserted immediately before the target statement. /// Add a statement to be inserted immediately before the target statement.
#[expect(dead_code)] #[expect(dead_code)]
pub fn insert_before(&self, target: Address, stmt: Statement<'a>) { pub fn insert_before(&self, target: &Statement<'a>, stmt: Statement<'a>) {
let mut insertions = self.insertions.borrow_mut(); let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default(); let adjacent_stmts = insertions.entry(target.address()).or_default();
let index = adjacent_stmts let index = adjacent_stmts
.iter() .iter()
.position(|s| matches!(s.direction, Direction::After)) .position(|s| matches!(s.direction, Direction::After))
@ -79,20 +79,20 @@ impl<'a> StatementInjectorStore<'a> {
/// Add a statement to be inserted immediately after the target statement. /// Add a statement to be inserted immediately after the target statement.
#[expect(dead_code)] #[expect(dead_code)]
pub fn insert_after(&self, target: Address, stmt: Statement<'a>) { pub fn insert_after(&self, target: &Statement<'a>, stmt: Statement<'a>) {
let mut insertions = self.insertions.borrow_mut(); let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default(); let adjacent_stmts = insertions.entry(target.address()).or_default();
adjacent_stmts.push(AdjacentStatement { stmt, direction: Direction::After }); adjacent_stmts.push(AdjacentStatement { stmt, direction: Direction::After });
} }
/// Add multiple statements to be inserted immediately before the target statement. /// Add multiple statements to be inserted immediately before the target statement.
#[expect(dead_code)] #[expect(dead_code)]
pub fn insert_many_before<S>(&self, target: Address, stmts: S) pub fn insert_many_before<S>(&self, target: &Statement<'a>, stmts: S)
where where
S: IntoIterator<Item = Statement<'a>>, S: IntoIterator<Item = Statement<'a>>,
{ {
let mut insertions = self.insertions.borrow_mut(); let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default(); let adjacent_stmts = insertions.entry(target.address()).or_default();
adjacent_stmts.splice( adjacent_stmts.splice(
0..0, 0..0,
stmts.into_iter().map(|stmt| AdjacentStatement { stmt, direction: Direction::Before }), stmts.into_iter().map(|stmt| AdjacentStatement { stmt, direction: Direction::Before }),
@ -100,12 +100,12 @@ impl<'a> StatementInjectorStore<'a> {
} }
/// Add multiple statements to be inserted immediately after the target statement. /// Add multiple statements to be inserted immediately after the target statement.
pub fn insert_many_after<S>(&self, target: Address, stmts: S) pub fn insert_many_after<S>(&self, target: &Statement<'a>, stmts: S)
where where
S: IntoIterator<Item = Statement<'a>>, S: IntoIterator<Item = Statement<'a>>,
{ {
let mut insertions = self.insertions.borrow_mut(); let mut insertions = self.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default(); let adjacent_stmts = insertions.entry(target.address()).or_default();
adjacent_stmts.extend( adjacent_stmts.extend(
stmts.into_iter().map(|stmt| AdjacentStatement { stmt, direction: Direction::After }), stmts.into_iter().map(|stmt| AdjacentStatement { stmt, direction: Direction::After }),
); );

View file

@ -4,7 +4,7 @@ use std::cell::Cell;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use oxc_allocator::{GetAddress, Vec as ArenaVec}; use oxc_allocator::Vec as ArenaVec;
use oxc_ast::ast::*; use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic; use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::SymbolFlags; use oxc_semantic::SymbolFlags;
@ -414,7 +414,7 @@ impl<'a, 'ctx> Traverse<'a> for TypeScriptAnnotations<'a, 'ctx> {
// Add assignments after super calls // Add assignments after super calls
self.ctx.statement_injector.insert_many_after( self.ctx.statement_injector.insert_many_after(
stmt.address(), stmt,
self.assignments self.assignments
.iter() .iter()
.map(|assignment| assignment.create_this_property_assignment(ctx)), .map(|assignment| assignment.create_this_property_assignment(ctx)),