Skip to content

Commit

Permalink
Correct classes_stack when nothing to transform
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 18, 2024
1 parent a69f792 commit f4313c5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
37 changes: 28 additions & 9 deletions crates/oxc_transformer/src/es2022/class_properties/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,24 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
}
}

// Exit if nothing to transform
if instance_prop_count == 0 && !has_static_prop && !has_static_block {
self.classes_stack.push(ClassDetails::default());
return;
}

// Determine if is a class declaration
let is_declaration = match ctx.ancestor(1) {
Ancestor::ExportDefaultDeclarationDeclaration(_)
| Ancestor::ExportNamedDeclarationDeclaration(_) => true,
grandparent => grandparent.is_via_statement(),
};

// Exit if nothing to transform
if instance_prop_count == 0 && !has_static_prop && !has_static_block {
self.classes_stack.push(ClassDetails {
is_declaration,
transform_required: false,
private_props: None,
bindings: ClassBindings::default(),
});
return;
}

// Initialize class binding vars.
// Static prop in class expression or anonymous `export default class {}` always require
// temp var for class. Static prop in class declaration doesn't.
Expand Down Expand Up @@ -152,6 +157,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// Add entry to `classes_stack`
self.classes_stack.push(ClassDetails {
is_declaration,
transform_required: true,
private_props: if private_props.is_empty() { None } else { Some(private_props) },
bindings: class_bindings,
});
Expand Down Expand Up @@ -267,6 +273,13 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
return;
}

// TODO: Refactor
if !class_details.transform_required {
debug_assert!(class_details.bindings.temp.is_none());
self.classes_stack.pop();
return;
}

// TODO: Comment explaining why
class_details.bindings.static_private_fields_use_temp = true;

Expand Down Expand Up @@ -367,6 +380,13 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
return;
}

let class_details = self.current_class();
if !class_details.transform_required {
debug_assert!(class_details.bindings.temp.is_none());
self.classes_stack.pop();
return;
}

// Transform static properties, remove static and instance properties, and move computed keys
// to before class
self.transform_class_on_exit_impl(class, ctx);
Expand All @@ -393,9 +413,8 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
expr_count += private_props.len();
}

if expr_count == 0 {
return;
}
// TODO: Why is this assertion failing sometimes?
// debug_assert!(expr_count > 0);

expr_count += 1 + usize::from(class_details.bindings.temp.is_some());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ use super::{ClassBindings, ClassProperties, FxIndexMap};
pub(super) struct ClassDetails<'a> {
/// `true` for class declaration, `false` for class expression
pub is_declaration: bool,
/// `true` if class requires no transformation
pub transform_required: bool,
/// Private properties.
/// Mapping private prop name to binding for temp var.
/// This is then used as lookup when transforming e.g. `this.#x`.
Expand Down

0 comments on commit f4313c5

Please sign in to comment.