Skip to content

Commit 3289721

Browse files
committed
feat(transformer/statement-injector): add an assertion to check if it 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.
1 parent c2101ad commit 3289721

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

crates/oxc_transformer/src/common/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ impl<'a> Traverse<'a> for Common<'a, '_> {
4646
self.var_declarations.exit_program(program, ctx);
4747
self.top_level_statements.exit_program(program, ctx);
4848
self.arrow_function_converter.exit_program(program, ctx);
49+
self.statement_injector.exit_program(program, ctx);
4950
}
5051

5152
fn enter_statements(

crates/oxc_transformer/src/common/statement_injector.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ impl<'a> Traverse<'a> for StatementInjector<'a, '_> {
4141
) {
4242
self.ctx.statement_injector.insert_into_statements(statements, ctx);
4343
}
44+
45+
#[inline]
46+
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
47+
self.ctx.statement_injector.assert_no_insertions_remaining();
48+
}
4449
}
4550

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

196201
*statements = new_statements;
197202
}
203+
204+
// Assertion for checking if no remaining insertions are left.
205+
// `#[inline(always)]` because this is a no-op in release mode
206+
#[expect(clippy::inline_always)]
207+
#[inline(always)]
208+
fn assert_no_insertions_remaining(&self) {
209+
debug_assert!(self.insertions.borrow().is_empty());
210+
}
198211
}

0 commit comments

Comments
 (0)