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): move creating temp var out of main loop #7587

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
20 changes: 12 additions & 8 deletions crates/oxc_transformer/src/es2022/class_properties/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,8 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {

// 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 @@ -306,11 +307,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
}

if prop.r#static {
// TODO(improve-on-babel): Even though private static properties may not access
// class name, Babel still creates a temp var for class. That's unnecessary.
self.initialize_class_name_binding(ctx);

has_static_prop_or_static_block = true;
has_static_prop = true;
} else {
instance_prop_count += 1;
}
Expand All @@ -320,7 +317,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 @@ -340,11 +337,18 @@ 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;
}

// Create temp var if class has any static props
if has_static_prop {
// TODO(improve-on-babel): Even though private static properties may not access
// class name, Babel still creates a temp var for class. That's unnecessary.
self.initialize_class_name_binding(ctx);
}

// Add entry to `private_props_stack`
if private_props.is_empty() {
self.private_props_stack.push(None);
Expand Down
4 changes: 2 additions & 2 deletions tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -943,10 +943,10 @@ Bindings mismatch:
after transform: ScopeId(2): ["_A", "_bar", "_i"]
rebuilt : ScopeId(2): ["_i"]
Symbol scope ID mismatch for "_A":
after transform: SymbolId(3): ScopeId(2)
after transform: SymbolId(4): ScopeId(2)
rebuilt : SymbolId(2): ScopeId(0)
Symbol scope ID mismatch for "_bar":
after transform: SymbolId(4): ScopeId(2)
after transform: SymbolId(3): ScopeId(2)
rebuilt : SymbolId(3): ScopeId(0)

* regression/T7364/input.mjs
Expand Down
Loading