Skip to content

Commit

Permalink
refactor(transformer/class-properties): store is_declaration only o…
Browse files Browse the repository at this point in the history
…n `ClassDetails` (#7980)

`is_declaration` was stored in 2 places. Only store it in `ClassDetails` struct.
  • Loading branch information
overlookmotel committed Dec 18, 2024
1 parent 70e2ba8 commit ffabc4a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
23 changes: 12 additions & 11 deletions crates/oxc_transformer/src/es2022/class_properties/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
return 0;
}

self.is_declaration = false;

self.transform_class(class, ctx);
self.transform_class(class, false, ctx);

// Return number of expressions to be inserted before/after the class
let mut expr_count = self.insert_before.len() + self.insert_after_exprs.len();
Expand Down Expand Up @@ -184,9 +182,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
return;
}

self.is_declaration = true;

self.transform_class(class, ctx);
self.transform_class(class, true, ctx);

// TODO: Run other transforms on inserted statements. How?

Expand Down Expand Up @@ -292,7 +288,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
}

/// Main guts of the transform.
fn transform_class(&mut self, class: &mut Class<'a>, ctx: &mut TraverseCtx<'a>) {
fn transform_class(
&mut self,
class: &mut Class<'a>,
is_declaration: bool,
ctx: &mut TraverseCtx<'a>,
) {
// TODO(improve-on-babel): If outer scope is sloppy mode, all code which is moved to outside
// the class should be wrapped in an IIFE with `'use strict'` directive. Babel doesn't do this.

Expand Down Expand Up @@ -371,12 +372,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// temp var for class. Static prop in class declaration doesn't.
let mut class_name_binding = class.id.as_ref().map(BoundIdentifier::from_binding_ident);

let need_temp_var = has_static_prop && (!self.is_declaration || class.id.is_none());
let need_temp_var = has_static_prop && (!is_declaration || class.id.is_none());
self.temp_var_is_created = need_temp_var;

let class_temp_binding = if need_temp_var {
let temp_binding = ClassBindings::create_temp_binding(class_name_binding.as_ref(), ctx);
if self.is_declaration {
if is_declaration {
// Anonymous `export default class {}`. Set class name binding to temp var.
// Actual class name will be set to this later.
class_name_binding = Some(temp_binding.clone());
Expand All @@ -396,7 +397,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {

// Add entry to `classes_stack`
self.classes_stack.push(ClassDetails {
is_declaration: self.is_declaration,
is_declaration,
private_props: if private_props.is_empty() { None } else { Some(private_props) },
bindings: class_bindings,
});
Expand Down Expand Up @@ -511,7 +512,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
expr: Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if self.is_declaration {
if self.current_class().is_declaration {
self.insert_after_stmts.push(ctx.ast.statement_expression(SPAN, expr));
} else {
self.insert_after_exprs.push(expr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// TODO: If static block transform is not enabled, it's possible to construct the class
// within the static block `class C { static { new C() } }` and that'd run before `_super`
// is defined. So it needs to go before the class, not after, in that case.
let init = if self.is_declaration {
let init = if self.current_class().is_declaration {
Some(super_func)
} else {
let assignment = create_assignment(super_binding, super_func, ctx);
Expand Down
3 changes: 0 additions & 3 deletions crates/oxc_transformer/src/es2022/class_properties/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,6 @@ pub struct ClassProperties<'a, 'ctx> {

// State during transform of class
//
/// `true` for class declaration, `false` for class expression
is_declaration: bool,
/// `true` if temp var for class has been inserted
temp_var_is_created: bool,
/// Scope that instance init initializers will be inserted into
Expand Down Expand Up @@ -258,7 +256,6 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
classes_stack: ClassesStack::default(),
class_expression_addresses_stack: NonEmptyStack::new(Address::DUMMY),
// Temporary values - overwritten when entering class
is_declaration: false,
temp_var_is_created: false,
instance_inits_scope_id: ScopeId::new(0),
instance_inits_constructor_scope_id: None,
Expand Down

0 comments on commit ffabc4a

Please sign in to comment.