Skip to content

Commit e0ef9a8

Browse files
committed
refactor(transformer/class-properties): debug assert private_field_count is 0 at end of transform (#10457)
`private_field_count` is incremented when entering a class with private properties/methods and decremented when leaving it. Make sure this is working correctly by ensuring `private_field_count == 0` at the end of the transform. As suggested in #10447 (review).
1 parent 45f8cc0 commit e0ef9a8

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

crates/oxc_transformer/src/es2022/class_properties/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
323323
}
324324

325325
impl<'a> Traverse<'a> for ClassProperties<'a, '_> {
326+
#[expect(clippy::inline_always)]
327+
#[inline(always)] // Because this is a no-op in release mode
328+
fn exit_program(&mut self, _program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
329+
debug_assert_eq!(self.private_field_count, 0);
330+
}
331+
326332
fn enter_class_body(&mut self, body: &mut ClassBody<'a>, ctx: &mut TraverseCtx<'a>) {
327333
self.transform_class_body_on_entry(body, ctx);
328334
}

crates/oxc_transformer/src/es2022/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ impl<'a, 'ctx> ES2022<'a, 'ctx> {
3737
}
3838

3939
impl<'a> Traverse<'a> for ES2022<'a, '_> {
40+
#[inline] // Because this is a no-op in release mode
41+
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
42+
if let Some(class_properties) = &mut self.class_properties {
43+
class_properties.exit_program(program, ctx);
44+
}
45+
}
46+
4047
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
4148
if let Some(class_properties) = &mut self.class_properties {
4249
class_properties.enter_expression(expr, ctx);

crates/oxc_transformer/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ impl<'a> Traverse<'a> for TransformerImpl<'a, '_> {
201201
if let Some(typescript) = self.x0_typescript.as_mut() {
202202
typescript.exit_program(program, ctx);
203203
}
204+
self.x2_es2022.exit_program(program, ctx);
204205
self.x2_es2018.exit_program(program, ctx);
205206
self.common.exit_program(program, ctx);
206207
}

0 commit comments

Comments
 (0)