Skip to content

Commit

Permalink
fix(transformer/private-methods): no temp var for class when unused p…
Browse files Browse the repository at this point in the history
…rivate methods
  • Loading branch information
overlookmotel committed Jan 9, 2025
1 parent e4d66e4 commit 13894e9
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 7 deletions.
13 changes: 7 additions & 6 deletions crates/oxc_transformer/src/es2022/class_properties/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,12 @@ impl<'a> ClassProperties<'a, '_> {
let class_scope_id = class.scope_id().get().unwrap();
let has_super_class = class.super_class().is_some();

// Check if class has any properties or statick blocks, and locate constructor (if class has one)
// Check if class has any properties, private methods, or static blocks.
// Locate constructor (if class has one).
let mut instance_prop_count = 0;
let mut has_instance_private_method = false;
let mut has_static_prop = false;
let mut has_static_block = false;
let mut has_instance_private_method = false;
let mut has_static_private_method_or_static_block = false;
// TODO: Store `FxIndexMap`s in a pool and re-use them
let mut private_props = FxIndexMap::default();
let mut constructor = None;
Expand Down Expand Up @@ -109,7 +110,7 @@ impl<'a> ClassProperties<'a, '_> {
ClassElement::StaticBlock(_) => {
// Static block only necessitates transforming class if it's being transformed
if self.transform_static_blocks {
has_static_block = true;
has_static_private_method_or_static_block = true;
continue;
}
}
Expand All @@ -120,7 +121,7 @@ impl<'a> ClassProperties<'a, '_> {
}
} else if let PropertyKey::PrivateIdentifier(ident) = &method.key {
if method.r#static {
has_static_prop = true;
has_static_private_method_or_static_block = true;
} else {
has_instance_private_method = true;
}
Expand Down Expand Up @@ -174,7 +175,7 @@ impl<'a> ClassProperties<'a, '_> {
// Exit if nothing to transform
if instance_prop_count == 0
&& !has_static_prop
&& !has_static_block
&& !has_static_private_method_or_static_block
&& !has_instance_private_method
{
self.classes_stack.push(ClassDetails {
Expand Down
3 changes: 2 additions & 1 deletion tasks/transform_conformance/snapshots/oxc.snap.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
commit: 54a8389f

Passed: 123/141
Passed: 124/142

# All Passed:
* babel-plugin-transform-class-static-block
* babel-plugin-transform-private-methods
* babel-plugin-transform-logical-assignment-operators
* babel-plugin-transform-nullish-coalescing-operator
* babel-plugin-transform-optional-catch-binding
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": [
"transform-class-properties",
"transform-private-methods"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class C {
#method() {}
}

class C2 {
static #method() {}
}

const C3 = class {
#method() {}
};

const C4 = class {
static #method() {}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var _Class_brand;

var _C_brand = new WeakSet();
class C {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _C_brand);
}
}
function _method() {}

class C2 {}
function _method2() {}

const C3 = (_Class_brand = new WeakSet(), class {
constructor() {
babelHelpers.classPrivateMethodInitSpec(this, _Class_brand);
}
});
function _method3() {}

const C4 = class {};
function _method4() {}

0 comments on commit 13894e9

Please sign in to comment.