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): share replace_class_name_with_temp_var in class_properties #8105

Draft
wants to merge 1 commit into
base: 12-25-refactor_transformer_class-properties_remove_all__if_super_methods_in_static_block_and_prop_init
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,35 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {

ctx.ast.move_expression(expr)
}

/// Replace reference to class name with reference to temp var for class.
fn replace_class_name_with_temp_var(
&mut self,
ident: &mut IdentifierReference<'a>,
ctx: &mut TraverseCtx<'a>,
) {
// Check identifier is reference to class name
let class_details = self.current_class_mut();
let class_name_symbol_id = class_details.bindings.name_symbol_id();
let Some(class_name_symbol_id) = class_name_symbol_id else { return };

let reference_id = ident.reference_id();
let reference = ctx.symbols().get_reference(reference_id);
let Some(symbol_id) = reference.symbol_id() else { return };

if symbol_id != class_name_symbol_id {
return;
}

// Identifier is reference to class name. Rename it.
let temp_binding = class_details.bindings.get_or_init_static_binding(ctx);
ident.name = temp_binding.name.clone();

let symbols = ctx.symbols_mut();
symbols.get_reference_mut(reference_id).set_symbol_id(temp_binding.symbol_id);
symbols.delete_resolved_reference(symbol_id, reference_id);
symbols.add_resolved_reference(temp_binding.symbol_id, reference_id);
}
}

/// Visitor to transform:
Expand Down Expand Up @@ -268,7 +297,7 @@ impl<'a, 'ctx, 'v> VisitMut<'a> for StaticVisitor<'a, 'ctx, 'v> {

/// Transform reference to class name to temp var
fn visit_identifier_reference(&mut self, ident: &mut IdentifierReference<'a>) {
self.replace_class_name_with_temp_var(ident);
self.super_converter.class_properties.replace_class_name_with_temp_var(ident, self.ctx);
}

/// Convert scope to sloppy mode if `self.make_sloppy_mode == true`.
Expand Down Expand Up @@ -498,31 +527,6 @@ impl<'a, 'ctx, 'v> StaticVisitor<'a, 'ctx, 'v> {
}
}

/// Replace reference to class name with reference to temp var for class.
fn replace_class_name_with_temp_var(&mut self, ident: &mut IdentifierReference<'a>) {
// Check identifier is reference to class name
let class_details = self.super_converter.class_properties.current_class_mut();
let class_name_symbol_id = class_details.bindings.name_symbol_id();
let Some(class_name_symbol_id) = class_name_symbol_id else { return };

let reference_id = ident.reference_id();
let reference = self.ctx.symbols().get_reference(reference_id);
let Some(symbol_id) = reference.symbol_id() else { return };

if symbol_id != class_name_symbol_id {
return;
}

// Identifier is reference to class name. Rename it.
let temp_binding = class_details.bindings.get_or_init_static_binding(self.ctx);
ident.name = temp_binding.name.clone();

let symbols = self.ctx.symbols_mut();
symbols.get_reference_mut(reference_id).set_symbol_id(temp_binding.symbol_id);
symbols.delete_resolved_reference(symbol_id, reference_id);
symbols.add_resolved_reference(temp_binding.symbol_id, reference_id);
}

/// Replace `delete this` with `true`.
fn replace_delete_this_with_true(&self, expr: &mut Expression<'a>, span: Span) {
if self.this_depth == 0 {
Expand Down
Loading