diff --git a/crates/oxc_transformer/src/es2022/class_properties/class.rs b/crates/oxc_transformer/src/es2022/class_properties/class.rs index 50d1bbfeb9afd4..0ec5dab0cb0e41 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/class.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/class.rs @@ -56,9 +56,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { return 0; } - self.is_declaration = false; - - self.transform_class(class, ctx); + self.transform_class(class, false, ctx); // Return number of expressions to be inserted before/after the class let mut expr_count = self.insert_before.len() + self.insert_after_exprs.len(); @@ -184,9 +182,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { return; } - self.is_declaration = true; - - self.transform_class(class, ctx); + self.transform_class(class, true, ctx); // TODO: Run other transforms on inserted statements. How? @@ -292,7 +288,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { } /// Main guts of the transform. - fn transform_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) { + fn transform_class( + &mut self, + class: &mut Class<'a>, + is_declaration: bool, + ctx: &mut TraverseCtx<'a>, + ) { // TODO(improve-on-babel): If outer scope is sloppy mode, all code which is moved to outside // the class should be wrapped in an IIFE with `'use strict'` directive. Babel doesn't do this. @@ -371,12 +372,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { // temp var for class. Static prop in class declaration doesn't. let mut class_name_binding = class.id.as_ref().map(BoundIdentifier::from_binding_ident); - let need_temp_var = has_static_prop && (!self.is_declaration || class.id.is_none()); + let need_temp_var = has_static_prop && (!is_declaration || class.id.is_none()); self.temp_var_is_created = need_temp_var; let class_temp_binding = if need_temp_var { let temp_binding = ClassBindings::create_temp_binding(class_name_binding.as_ref(), ctx); - if self.is_declaration { + if is_declaration { // Anonymous `export default class {}`. Set class name binding to temp var. // Actual class name will be set to this later. class_name_binding = Some(temp_binding.clone()); @@ -396,7 +397,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { // Add entry to `classes_stack` self.classes_stack.push(ClassDetails { - is_declaration: self.is_declaration, + is_declaration, private_props: if private_props.is_empty() { None } else { Some(private_props) }, bindings: class_bindings, }); @@ -511,7 +512,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { expr: Expression<'a>, ctx: &mut TraverseCtx<'a>, ) { - if self.is_declaration { + if self.current_class().is_declaration { self.insert_after_stmts.push(ctx.ast.statement_expression(SPAN, expr)); } else { self.insert_after_exprs.push(expr); diff --git a/crates/oxc_transformer/src/es2022/class_properties/constructor.rs b/crates/oxc_transformer/src/es2022/class_properties/constructor.rs index 179991dd226606..aac84b1917ddb5 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/constructor.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/constructor.rs @@ -455,7 +455,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { // TODO: If static block transform is not enabled, it's possible to construct the class // within the static block `class C { static { new C() } }` and that'd run before `_super` // is defined. So it needs to go before the class, not after, in that case. - let init = if self.is_declaration { + let init = if self.current_class().is_declaration { Some(super_func) } else { let assignment = create_assignment(super_binding, super_func, ctx); diff --git a/crates/oxc_transformer/src/es2022/class_properties/mod.rs b/crates/oxc_transformer/src/es2022/class_properties/mod.rs index cf50b9c1ed57b2..fa64ee4e145433 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/mod.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/mod.rs @@ -218,8 +218,6 @@ pub struct ClassProperties<'a, 'ctx> { // State during transform of class // - /// `true` for class declaration, `false` for class expression - is_declaration: bool, /// `true` if temp var for class has been inserted temp_var_is_created: bool, /// Scope that instance init initializers will be inserted into @@ -258,7 +256,6 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { classes_stack: ClassesStack::default(), class_expression_addresses_stack: NonEmptyStack::new(Address::DUMMY), // Temporary values - overwritten when entering class - is_declaration: false, temp_var_is_created: false, instance_inits_scope_id: ScopeId::new(0), instance_inits_constructor_scope_id: None,