diff --git a/crates/oxc_transformer/src/es2022/class_properties/class.rs b/crates/oxc_transformer/src/es2022/class_properties/class.rs index c3692456c8af0b..2e9179e3f7ac5c 100644 --- a/crates/oxc_transformer/src/es2022/class_properties/class.rs +++ b/crates/oxc_transformer/src/es2022/class_properties/class.rs @@ -282,7 +282,8 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { // Check if class has any properties and get index of constructor (if class has one) let mut instance_prop_count = 0; - let mut has_static_prop_or_static_block = false; + let mut has_static_prop = false; + let mut has_static_block = false; // TODO: Store `FxIndexMap`s in a pool and re-use them let mut private_props = FxIndexMap::default(); let mut constructor_index = None; @@ -306,11 +307,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { } if prop.r#static { - // TODO(improve-on-babel): Even though private static properties may not access - // class name, Babel still creates a temp var for class. That's unnecessary. - self.initialize_class_name_binding(ctx); - - has_static_prop_or_static_block = true; + has_static_prop = true; } else { instance_prop_count += 1; } @@ -320,7 +317,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { ClassElement::StaticBlock(_) => { // Static block only necessitates transforming class if it's being transformed if self.transform_static_blocks { - has_static_prop_or_static_block = true; + has_static_block = true; continue; } } @@ -340,11 +337,18 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { } // Exit if nothing to transform - if instance_prop_count == 0 && !has_static_prop_or_static_block { + if instance_prop_count == 0 && !has_static_prop && !has_static_block { self.private_props_stack.push(None); return; } + // Create temp var if class has any static props + if has_static_prop { + // TODO(improve-on-babel): Even though private static properties may not access + // class name, Babel still creates a temp var for class. That's unnecessary. + self.initialize_class_name_binding(ctx); + } + // Add entry to `private_props_stack` if private_props.is_empty() { self.private_props_stack.push(None); diff --git a/tasks/transform_conformance/snapshots/babel.snap.md b/tasks/transform_conformance/snapshots/babel.snap.md index ff5c56cb88ece4..74acfeb5c1fb5d 100644 --- a/tasks/transform_conformance/snapshots/babel.snap.md +++ b/tasks/transform_conformance/snapshots/babel.snap.md @@ -943,10 +943,10 @@ Bindings mismatch: after transform: ScopeId(2): ["_A", "_bar", "_i"] rebuilt : ScopeId(2): ["_i"] Symbol scope ID mismatch for "_A": -after transform: SymbolId(3): ScopeId(2) +after transform: SymbolId(4): ScopeId(2) rebuilt : SymbolId(2): ScopeId(0) Symbol scope ID mismatch for "_bar": -after transform: SymbolId(4): ScopeId(2) +after transform: SymbolId(3): ScopeId(2) rebuilt : SymbolId(3): ScopeId(0) * regression/T7364/input.mjs