Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(transformer/class-properties): store temp_var_is_created on ClassBindings #7981

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/oxc_transformer/src/es2022/class_properties/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
let class_expr = ctx.ast.move_expression(expr);
if let Some(binding) = &class_details.bindings.temp {
// Insert `var _Class` statement, if it wasn't already in `transform_class`
if !self.temp_var_is_created {
if !class_details.bindings.temp_var_is_created {
self.ctx.var_declarations.insert_var(binding, ctx);
}

Expand Down Expand Up @@ -191,7 +191,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
// Binding for class name is required
if let Some(ident) = &class.id {
// Insert `var _Class` statement, if it wasn't already in `transform_class`
if !self.temp_var_is_created {
if !class_details.bindings.temp_var_is_created {
self.ctx.var_declarations.insert_var(temp_binding, ctx);
}

Expand Down Expand Up @@ -373,7 +373,6 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
let mut class_name_binding = class.id.as_ref().map(BoundIdentifier::from_binding_ident);

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);
Expand All @@ -393,7 +392,8 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
None
};

let class_bindings = ClassBindings::new(class_name_binding, class_temp_binding);
let class_bindings =
ClassBindings::new(class_name_binding, class_temp_binding, need_temp_var);

// Add entry to `classes_stack`
self.classes_stack.push(ClassDetails {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,23 @@ pub(super) struct ClassBindings<'a> {
/// `true` if should use temp binding for references to class in transpiled static private fields,
/// `false` if can use name binding
pub static_private_fields_use_temp: bool,
/// `true` if temp var for class has been inserted
pub temp_var_is_created: bool,
}

impl<'a> ClassBindings<'a> {
/// Create `ClassBindings`.
pub fn new(
name_binding: Option<BoundIdentifier<'a>>,
temp_binding: Option<BoundIdentifier<'a>>,
temp_var_is_created: bool,
) -> Self {
Self { name: name_binding, temp: temp_binding, static_private_fields_use_temp: true }
Self {
name: name_binding,
temp: temp_binding,
static_private_fields_use_temp: true,
temp_var_is_created,
}
}

/// Get `SymbolId` of name binding.
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` if temp var for class has been inserted
temp_var_is_created: bool,
/// Scope that instance init initializers will be inserted into
instance_inits_scope_id: ScopeId,
/// `ScopeId` of class constructor, if instance init initializers will be inserted into constructor.
Expand Down Expand Up @@ -256,7 +254,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
temp_var_is_created: false,
instance_inits_scope_id: ScopeId::new(0),
instance_inits_constructor_scope_id: None,
// `Vec`s and `FxHashMap`s which are reused for every class being transformed
Expand Down
Loading