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.
This commit is contained in:
overlookmotel 2024-12-03 07:46:01 +00:00
parent ebd11fb5e9
commit 7bd6350978
2 changed files with 14 additions and 10 deletions

View file

@ -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);

View file

@ -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