mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(transformer/typescript): insert assignments after super by StatementInjector (#6654)
This commit is contained in:
parent
60f487a203
commit
3a56d59dce
3 changed files with 28 additions and 26 deletions
|
|
@ -86,7 +86,6 @@ 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.
|
||||||
#[expect(dead_code)]
|
|
||||||
pub fn insert_many_after(&self, target: Address, stmts: Vec<Statement<'a>>) {
|
pub fn insert_many_after(&self, target: Address, stmts: Vec<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).or_default();
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ use std::cell::Cell;
|
||||||
|
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use oxc_allocator::Vec as ArenaVec;
|
use oxc_allocator::{GetAddress, 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;
|
||||||
|
|
@ -403,10 +403,32 @@ impl<'a, 'ctx> Traverse<'a> for TypeScriptAnnotations<'a, 'ctx> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
|
// Add assignments after super calls
|
||||||
|
if self.assignments.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let has_super_call = matches!(stmt, Statement::ExpressionStatement(stmt) if stmt.expression.is_super_call_expression());
|
||||||
|
if !has_super_call {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add assignments after super calls
|
||||||
|
self.ctx.statement_injector.insert_many_after(
|
||||||
|
stmt.address(),
|
||||||
|
self.assignments
|
||||||
|
.iter()
|
||||||
|
.map(|assignment| assignment.create_this_property_assignment(ctx))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
);
|
||||||
|
self.has_super_call = true;
|
||||||
|
}
|
||||||
|
|
||||||
fn exit_statements(
|
fn exit_statements(
|
||||||
&mut self,
|
&mut self,
|
||||||
stmts: &mut ArenaVec<'a, Statement<'a>>,
|
stmts: &mut ArenaVec<'a, Statement<'a>>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
_ctx: &mut TraverseCtx<'a>,
|
||||||
) {
|
) {
|
||||||
// Remove TS specific statements
|
// Remove TS specific statements
|
||||||
stmts.retain(|stmt| match stmt {
|
stmts.retain(|stmt| match stmt {
|
||||||
|
|
@ -417,29 +439,6 @@ impl<'a, 'ctx> Traverse<'a> for TypeScriptAnnotations<'a, 'ctx> {
|
||||||
// Ignore ModuleDeclaration as it's handled in the program
|
// Ignore ModuleDeclaration as it's handled in the program
|
||||||
_ => true,
|
_ => true,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Add assignments after super calls
|
|
||||||
if !self.assignments.is_empty() {
|
|
||||||
let has_super_call = stmts.iter().any(|stmt| {
|
|
||||||
matches!(stmt, Statement::ExpressionStatement(stmt) if stmt.expression.is_super_call_expression())
|
|
||||||
});
|
|
||||||
if has_super_call {
|
|
||||||
let mut new_stmts = ctx.ast.vec();
|
|
||||||
for stmt in stmts.drain(..) {
|
|
||||||
let is_super_call = matches!(stmt, Statement::ExpressionStatement(ref stmt) if stmt.expression.is_super_call_expression());
|
|
||||||
new_stmts.push(stmt);
|
|
||||||
if is_super_call {
|
|
||||||
new_stmts.extend(
|
|
||||||
self.assignments
|
|
||||||
.iter()
|
|
||||||
.map(|assignment| assignment.create_this_property_assignment(ctx)),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.has_super_call = true;
|
|
||||||
*stmts = new_stmts;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transform if statement's consequent and alternate to block statements if they are super calls
|
/// Transform if statement's consequent and alternate to block statements if they are super calls
|
||||||
|
|
|
||||||
|
|
@ -205,6 +205,10 @@ impl<'a, 'ctx> Traverse<'a> for TypeScript<'a, 'ctx> {
|
||||||
self.annotations.exit_statements(stmts, ctx);
|
self.annotations.exit_statements(stmts, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
|
self.annotations.exit_statement(stmt, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn enter_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
self.r#enum.enter_statement(stmt, ctx);
|
self.r#enum.enter_statement(stmt, ctx);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue