Skip to content

Commit

Permalink
Move creating class temp var out of 1st pass loop
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 2, 2024
1 parent 7e1b93a commit 4ff10e6
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions crates/oxc_transformer/src/es2022/class_properties/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,15 +277,10 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// Maybe force transform of static blocks if any static properties?
// Or alternatively could insert static property initializers into static blocks.

// Initialize class binding vars
self.class_bindings = ClassBindings {
name: class.id.as_ref().map(BoundIdentifier::from_binding_ident),
temp: None,
};

// 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 @@ -309,18 +304,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
}

if prop.r#static {
// Static prop in class expression or anonymous `export default class {}`
// always requires a temp var. Static prop in class declaration doesn't.
// TODO: Move this out of the loop.
if !self.is_declaration || self.class_bindings.name.is_none() {
self.class_bindings.get_or_init_temp_binding(
self.is_declaration,
self.ctx,
ctx,
);
}

has_static_prop_or_static_block = true;
has_static_prop = true;
} else {
instance_prop_count += 1;
}
Expand All @@ -330,7 +314,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 @@ -350,11 +334,23 @@ 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;
}

// Initialize class binding vars
self.class_bindings = ClassBindings {
name: class.id.as_ref().map(BoundIdentifier::from_binding_ident),
temp: None,
};

// Static prop in class expression or anonymous `export default class {}`
// always requires a temp var. Static prop in class declaration doesn't.
if has_static_prop && (!self.is_declaration || self.class_bindings.name.is_none()) {
self.class_bindings.get_or_init_temp_binding(self.is_declaration, self.ctx, ctx);
}

// Add entry to `private_props_stack`
if private_props.is_empty() {
self.private_props_stack.push(None);
Expand Down

0 comments on commit 4ff10e6

Please sign in to comment.