From 7bd6350978f1cb0b0847cd13f1de92e7650b31ea Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 3 Dec 2024 07:46:01 +0000 Subject: [PATCH] refactor(transformer/class-properties): move creating temp var out of main loop (#7587) Small optimization. Move code out of the loop which determines if class property transform has nothing to do and can bail out early. This also clears the way for correcting the logic around when temp vars are/aren't created in #7516. --- .../src/es2022/class_properties/class.rs | 20 +++++++++++-------- .../snapshots/babel.snap.md | 4 ++-- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/crates/oxc_transformer/src/es2022/class_properties/class.rs b/crates/oxc_transformer/src/es2022/class_properties/class.rs index c3692456c8af0..2e9179e3f7ac5 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 ff5c56cb88ece..74acfeb5c1fb5 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