Skip to content

Commit

Permalink
refactor(transformer/class-properties): move creating temp var out of…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
overlookmotel committed Dec 3, 2024
1 parent ebd11fb commit 7bd6350
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
20 changes: 12 additions & 8 deletions crates/oxc_transformer/src/es2022/class_properties/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7bd6350

Please sign in to comment.