feat(es/compat): Skip empty class static blocks (#8138)

**Description:**

And update the `static_block_private_id` generate rules to be consistent with Babel.

**Related issue:**

 - #8136
This commit is contained in:
magic-akari 2023-10-18 12:58:02 -05:00 committed by GitHub
parent 34b01672ad
commit e186b80ddc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 61 additions and 94 deletions

View File

@ -13,9 +13,3 @@ var __5 = {
writable: true,
value: 1
};
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();

View File

@ -14,10 +14,4 @@ var __5 = {
writable: true,
value: 5
};
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
(()=>{})();
console.log(_C__1);

View File

@ -5,8 +5,6 @@ function foo() {
}, _class.foo = 1, (()=>{
var _class;
const c = (_class = class {
}, _class.bar = 2, (()=>{
// do
})(), _class);
}, _class.bar = 2, _class);
})(), _class;
}

View File

@ -10,8 +10,6 @@ function foo() {
var c = (_class = function _class() {
"use strict";
_class_call_check(this, _class);
}, _class.bar = 2, function() {
// do
}(), _class);
}, _class.bar = 2, _class);
}(), _class;
}

View File

@ -4,6 +4,3 @@ var C = function C() {
"use strict";
_class_call_check(this, C);
};
(function() {
// something
})();

View File

@ -15,7 +15,7 @@ pub fn static_blocks(mark: Mark) -> impl Fold + VisitMut {
#[swc_trace]
impl ClassStaticBlock {
fn visit_mut_static_block(
fn transform_static_block(
&mut self,
mut static_block: StaticBlock,
private_id: JsWord,
@ -86,22 +86,36 @@ impl VisitMut for ClassStaticBlock {
}
}
let mut count = 0;
for member in class.body.iter_mut() {
if let ClassMember::StaticBlock(static_block) = member {
let static_block_private_id: JsWord = {
let mut id_value: JsWord = "_".into();
let mut count = 0;
while private_names.contains(&id_value) {
count += 1;
id_value = format!("_{}", count).into();
}
private_names.insert(id_value.clone());
id_value
};
*member = ClassMember::PrivateProp(
self.visit_mut_static_block(static_block.take(), static_block_private_id),
)
if static_block.body.stmts.is_empty() {
*member = ClassMember::dummy();
continue;
}
let static_block_private_id = generate_uid(&private_names, &mut count);
*member = self
.transform_static_block(static_block.take(), static_block_private_id)
.into();
};
}
}
}
fn generate_uid(deny_list: &AHashSet<JsWord>, i: &mut u32) -> JsWord {
*i += 1;
let mut uid: JsWord = if *i == 1 {
"_".to_string()
} else {
format!("_{i}")
}
.into();
while deny_list.contains(&uid) {
*i += 1;
uid = format!("_{i}").into();
}
uid
}

View File

@ -1,4 +1,3 @@
export default class Foo {
#foo;
static #_ = (()=>{})();
}

View File

@ -5,5 +5,5 @@ class Foo {
this.qux1 = this.qux;
})();
static qux = 21;
static #_1 = this.qux2 = this.qux;
static #_2 = this.qux2 = this.qux;
}

View File

@ -1,4 +1,4 @@
class Foo {
static #_ = 42;
static #_1 = this.foo = this.#_;
static #_2 = this.foo = this.#_;
}

View File

@ -23,7 +23,7 @@ var __ = {
classDec
])
};
var __1 = {
var __2 = {
writable: true,
value: _initClass()
};

View File

@ -24,7 +24,7 @@ var __ = {
classDec
])
};
var __1 = {
var __2 = {
writable: true,
value: _initClass()
};

View File

@ -1,4 +1,4 @@
var _A, __, __1, __2, _C, __3, __11, __21, _class, __4, __12, __22, _class1, __5, __13, __23, _G, __6, __14, __24, _class2, __7, __15, __25, _class3, __8, __16, __26, _K, __9, __17, __27;
var _A, __, __2, _C, __1, __21, _class, __3, __22, _class1, __4, __23, _G, __5, __24, _class2, __6, __25, _class3, __7, __26, _K, __8, __27;
var _initClass, _A1, _initClass1, _C1, _initClass2, _class4, _initClass3, _class5, _initClass4, _G1, _initClass5, _class6, _initClass6, _class7, _I, _initClass7, _K1, _L;
const dec = ()=>{};
const A = ((_A = class A {
@ -7,108 +7,84 @@ const A = ((_A = class A {
value: { c: [_A1, _initClass] } = _apply_decs_2203_r(_A, [], [
dec
])
}, __1 = {
writable: true,
value: (()=>{})()
}, __2 = {
writable: true,
value: _initClass()
}, _A), _A1);
const B = ((_C = class C {
}, __3 = {
}, __1 = {
writable: true,
value: { c: [_C1, _initClass1] } = _apply_decs_2203_r(_C, [], [
dec
])
}, __11 = {
writable: true,
value: (()=>{})()
}, __21 = {
writable: true,
value: _initClass1()
}, _C), _C1);
const D = ((_class = class {
}, __4 = {
}, __3 = {
writable: true,
value: { c: [_class4, _initClass2] } = _apply_decs_2203_r(_class, [], [
dec
])
}, __12 = {
writable: true,
value: (()=>{})()
}, __22 = {
writable: true,
value: _initClass2()
}, _class), _class4);
const E = (((_class1 = class {
}, __5 = {
}, __4 = {
writable: true,
value: { c: [_class5, _initClass3] } = _apply_decs_2203_r(_class1, [], [
dec
])
}, __13 = {
writable: true,
value: (()=>{})()
}, __23 = {
writable: true,
value: _initClass3()
}, _class1), _class5), 123);
const F = [
((_G = class G {
}, __6 = {
}, __5 = {
writable: true,
value: { c: [_G1, _initClass4] } = _apply_decs_2203_r(_G, [], [
dec
])
}, __14 = {
writable: true,
value: (()=>{})()
}, __24 = {
writable: true,
value: _initClass4()
}, _G), _G1),
((_class2 = class {
}, __7 = {
}, __6 = {
writable: true,
value: { c: [_class6, _initClass5] } = _apply_decs_2203_r(_class2, [], [
dec
])
}, __15 = {
writable: true,
value: (()=>{})()
}, __25 = {
writable: true,
value: _initClass5()
}, _class2), _class6)
];
const H = ((_class3 = class extends (_I = I) {
}, __8 = {
}, __7 = {
writable: true,
value: { c: [_class7, _initClass6] } = _apply_decs_2203_r(_class3, [], [
dec
], _I)
}, __16 = {
writable: true,
value: (()=>{})()
}, __26 = {
writable: true,
value: _initClass6()
}, _class3), _class7);
const J = ((_K = class K extends (_L = L) {
}, __9 = {
}, __8 = {
writable: true,
value: { c: [_K1, _initClass7] } = _apply_decs_2203_r(_K, [], [
dec
], _L)
}, __17 = {
writable: true,
value: (()=>{})()
}, __27 = {
writable: true,
value: _initClass7()
}, _K), _K1);
function classFactory() {
var _class, __, __1, __2;
var _class, __, __2;
var _initClass, _class1;
return _class = class {
}, __ = {
@ -116,9 +92,6 @@ function classFactory() {
value: { c: [_class1, _initClass] } = _apply_decs_2203_r(_class, [], [
dec
])
}, __1 = {
writable: true,
value: (()=>{})()
}, __2 = {
writable: true,
value: _initClass()

View File

@ -1,4 +1,4 @@
var _A, __, __1, _C, __2, __11, _class, __3, __12, _class1, __4, __13, _G, __5, __14, _class2, __6, __15, _class3, __7, __16, _K, __8, __17;
var _A, __, __2, _C, __1, __21, _class, __3, __22, _class1, __4, __23, _G, __5, __24, _class2, __6, __25, _class3, __7, __26, _K, __8, __27;
var _initClass, _A1, _initClass1, _C1, _initClass2, _class4, _initClass3, _class5, _initClass4, _G1, _initClass5, _class6, _initClass6, _class7, _I, _initClass7, _K1, _L;
const dec = ()=>{};
const A = ((_A = class A {
@ -7,17 +7,17 @@ const A = ((_A = class A {
value: { c: [_A1, _initClass] } = _apply_decs_2203_r(_A, [], [
dec
])
}, __1 = {
}, __2 = {
writable: true,
value: _initClass()
}, _A), _A1);
const B = ((_C = class C {
}, __2 = {
}, __1 = {
writable: true,
value: { c: [_C1, _initClass1] } = _apply_decs_2203_r(_C, [], [
dec
])
}, __11 = {
}, __21 = {
writable: true,
value: _initClass1()
}, _C), _C1);
@ -27,7 +27,7 @@ const D = ((_class = class {
value: { c: [_class4, _initClass2] } = _apply_decs_2203_r(_class, [], [
dec
])
}, __12 = {
}, __22 = {
writable: true,
value: _initClass2()
}, _class), _class4);
@ -37,7 +37,7 @@ const E = (((_class1 = class {
value: { c: [_class5, _initClass3] } = _apply_decs_2203_r(_class1, [], [
dec
])
}, __13 = {
}, __23 = {
writable: true,
value: _initClass3()
}, _class1), _class5), 123);
@ -48,7 +48,7 @@ const F = [
value: { c: [_G1, _initClass4] } = _apply_decs_2203_r(_G, [], [
dec
])
}, __14 = {
}, __24 = {
writable: true,
value: _initClass4()
}, _G), _G1),
@ -58,7 +58,7 @@ const F = [
value: { c: [_class6, _initClass5] } = _apply_decs_2203_r(_class2, [], [
dec
])
}, __15 = {
}, __25 = {
writable: true,
value: _initClass5()
}, _class2), _class6)
@ -69,7 +69,7 @@ const H = ((_class3 = class extends (_I = I) {
value: { c: [_class7, _initClass6] } = _apply_decs_2203_r(_class3, [], [
dec
], _I)
}, __16 = {
}, __26 = {
writable: true,
value: _initClass6()
}, _class3), _class7);
@ -79,12 +79,12 @@ const J = ((_K = class K extends (_L = L) {
value: { c: [_K1, _initClass7] } = _apply_decs_2203_r(_K, [], [
dec
], _L)
}, __17 = {
}, __27 = {
writable: true,
value: _initClass7()
}, _K), _K1);
function classFactory() {
var _class, __, __1;
var _class, __, __2;
var _initClass, _class1;
return _class = class {
}, __ = {
@ -92,7 +92,7 @@ function classFactory() {
value: { c: [_class1, _initClass] } = _apply_decs_2203_r(_class, [], [
dec
])
}, __1 = {
}, __2 = {
writable: true,
value: _initClass()
}, _class, _class1;

View File

@ -10,20 +10,20 @@ var __ = {
dec1
])
};
var __1 = {
var __2 = {
writable: true,
value: _initClass()
};
let _Foo;
class Foo extends (_Bar = _Bar1) {
}
var __2 = {
var __1 = {
writable: true,
value: { c: [_Foo, _initClass1] } = _apply_decs_2203_r(Foo, [], [
dec2
], _Bar)
};
var __11 = {
var __21 = {
writable: true,
value: _initClass1()
};

View File

@ -1,4 +1,4 @@
var _Bar, __, __1;
var _Bar, __, __2;
var _initClass, _Bar1;
const dec = ()=>{};
const Foo = ((_Bar = class Bar {
@ -10,7 +10,7 @@ const Foo = ((_Bar = class Bar {
value: { c: [_Bar1, _initClass] } = _apply_decs_2203_r(_Bar, [], [
dec
])
}, __1 = {
}, __2 = {
writable: true,
value: _initClass()
}, _Bar), _Bar1);

View File

@ -54,7 +54,7 @@ var __ = {
_dec3
])
};
var __1 = {
var __2 = {
writable: true,
value: _initClass()
};