Skip to content

Commit

Permalink
feat(transformer/statement-injector): add an assertion to check if it…
Browse files Browse the repository at this point in the history
… still contains statements that don't inject (oxc-project#9063)

When there are still statements that aren't inserted after the visitor ends, it means statements inserted with an incorrect address or outdated address (e.g., the original statement has mutated to a new statement).

This PR adds an assertion to expose this potential bug as soon as possible on the development side.
  • Loading branch information
Dunqing committed Feb 18, 2025
1 parent c2101ad commit 3289721
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/oxc_transformer/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl<'a> Traverse<'a> for Common<'a, '_> {
self.var_declarations.exit_program(program, ctx);
self.top_level_statements.exit_program(program, ctx);
self.arrow_function_converter.exit_program(program, ctx);
self.statement_injector.exit_program(program, ctx);
}

fn enter_statements(
Expand Down
13 changes: 13 additions & 0 deletions crates/oxc_transformer/src/common/statement_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ impl<'a> Traverse<'a> for StatementInjector<'a, '_> {
) {
self.ctx.statement_injector.insert_into_statements(statements, ctx);
}

#[inline]
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
self.ctx.statement_injector.assert_no_insertions_remaining();
}
}

#[derive(Debug)]
Expand Down Expand Up @@ -195,4 +200,12 @@ impl<'a> StatementInjectorStore<'a> {

*statements = new_statements;
}

// Assertion for checking if no remaining insertions are left.
// `#[inline(always)]` because this is a no-op in release mode
#[expect(clippy::inline_always)]
#[inline(always)]
fn assert_no_insertions_remaining(&self) {
debug_assert!(self.insertions.borrow().is_empty());
}
}

0 comments on commit 3289721

Please sign in to comment.