mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
fix(transformer/private-methods): no temp var for class when unused private methods (#8360)
No temp var is required for class if it contains static private method which is not referenced within class. e.g.:
```js
let C = class {
static #method() {}
};
```
->
```js
let C = class {};
function _method() {}
```
This commit is contained in:
parent
f367a16364
commit
ab6142503f
5 changed files with 52 additions and 7 deletions
|
|
@ -70,11 +70,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;
|
||||
|
|
@ -104,7 +105,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;
|
||||
}
|
||||
}
|
||||
|
|
@ -115,7 +116,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;
|
||||
}
|
||||
|
|
@ -169,7 +170,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 {
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"plugins": [
|
||||
"transform-class-properties",
|
||||
"transform-private-methods"
|
||||
]
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
class C {
|
||||
#method() {}
|
||||
}
|
||||
|
||||
class C2 {
|
||||
static #method() {}
|
||||
}
|
||||
|
||||
const C3 = class {
|
||||
#method() {}
|
||||
};
|
||||
|
||||
const C4 = class {
|
||||
static #method() {}
|
||||
};
|
||||
|
|
@ -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() {}
|
||||
Loading…
Reference in a new issue