Skip to content

Commit

Permalink
refactor(transformer/typescript): insert assignments after super by `…
Browse files Browse the repository at this point in the history
…StatementInjector` (oxc-project#6654)
  • Loading branch information
Dunqing committed Oct 24, 2024
1 parent 60f487a commit 3a56d59
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
1 change: 0 additions & 1 deletion crates/oxc_transformer/src/common/statement_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ impl<'a> StatementInjectorStore<'a> {
}

/// 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.insertions.borrow_mut();
let adjacent_stmts = insertions.entry(target).or_default();
Expand Down
49 changes: 24 additions & 25 deletions crates/oxc_transformer/src/typescript/annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::cell::Cell;

use rustc_hash::FxHashSet;

use oxc_allocator::Vec as ArenaVec;
use oxc_allocator::{GetAddress, Vec as ArenaVec};
use oxc_ast::ast::*;
use oxc_diagnostics::OxcDiagnostic;
use oxc_semantic::SymbolFlags;
Expand Down Expand Up @@ -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(
&mut self,
stmts: &mut ArenaVec<'a, Statement<'a>>,
ctx: &mut TraverseCtx<'a>,
_ctx: &mut TraverseCtx<'a>,
) {
// Remove TS specific statements
stmts.retain(|stmt| match stmt {
Expand All @@ -417,29 +439,6 @@ impl<'a, 'ctx> Traverse<'a> for TypeScriptAnnotations<'a, 'ctx> {
// Ignore ModuleDeclaration as it's handled in the program
_ => 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
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_transformer/src/typescript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ impl<'a, 'ctx> Traverse<'a> for TypeScript<'a, '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>) {
self.r#enum.enter_statement(stmt, ctx);
}
Expand Down

0 comments on commit 3a56d59

Please sign in to comment.