Skip to content

Commit f05221d

Browse files
committed
feat(es/decorators): decorator support private prop
1 parent 6b5dbc6 commit f05221d

File tree

43 files changed

+551
-455
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+551
-455
lines changed

crates/swc_ecma_transforms_proposal/src/decorator_impl.rs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -820,42 +820,9 @@ impl VisitMut for DecoratorPass {
820820
args: vec![ThisExpr { span: DUMMY_SP }.as_arg()],
821821
..Default::default()
822822
};
823-
let mut proto_inited = false;
824-
for member in n.body.iter_mut() {
825-
if let ClassMember::ClassProp(prop) = member {
826-
if prop.is_static {
827-
continue;
828-
}
829-
if let Some(value) = prop.value.clone() {
830-
prop.value = Some(Expr::from_exprs(vec![
831-
init_proto_expr.clone().into(),
832-
value,
833-
]));
834-
835-
proto_inited = true;
836-
break;
837-
}
838-
} else if let ClassMember::PrivateProp(prop) = member {
839-
if prop.is_static {
840-
continue;
841-
}
842-
if let Some(value) = prop.value.clone() {
843-
prop.value = Some(Expr::from_exprs(vec![
844-
init_proto_expr.clone().into(),
845-
value,
846-
]));
847-
848-
proto_inited = true;
849-
break;
850-
}
851-
}
852-
}
823+
let c = self.ensure_constructor(n);
853824

854-
if !proto_inited {
855-
let c = self.ensure_constructor(n);
856-
857-
inject_after_super(c, vec![Box::new(init_proto_expr.into())])
858-
}
825+
inject_after_super(c, vec![Box::new(init_proto_expr.into())])
859826
}
860827

861828
self.consume_inits();
@@ -1780,6 +1747,9 @@ impl VisitMut for DecoratorPass {
17801747
self.state.init_static_args.push(Some(initialize_init));
17811748
} else {
17821749
self.state.proto_lhs.push(init);
1750+
self.state
1751+
.init_proto
1752+
.get_or_insert_with(|| private_ident!("_initProto"));
17831753
self.state.init_proto_args.push(Some(initialize_init));
17841754
}
17851755
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-accessors--to-es2015/private/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ class Foo {
1313
});
1414
_class_private_field_init(this, ___a_1, {
1515
writable: true,
16-
value: (_initProto(this), _init_a(this))
16+
value: _init_a(this)
1717
});
1818
_class_private_field_init(this, ___b_2, {
1919
writable: true,
2020
value: _init_b(this, 123)
2121
});
22+
_initProto(this);
2223
}
2324
}
2425
({ e: [_init_a, _get___a, _set___a, _init_b, _get___b, _set___b, _initProto] } = _apply_decs_2203_r(Foo, [

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-accessors--to-es2015/public/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Foo {
2525
constructor(){
2626
_class_private_field_init(this, ____private_a_1, {
2727
writable: true,
28-
value: (_initProto(this), _init_a(this))
28+
value: _init_a(this)
2929
});
3030
_class_private_field_init(this, ____private_b_2, {
3131
writable: true,
@@ -35,6 +35,7 @@ class Foo {
3535
writable: true,
3636
value: _init_computedKey(this, 456)
3737
});
38+
_initProto(this);
3839
}
3940
}
4041
({ e: [_init_a, _init_b, _init_computedKey, _initProto] } = _apply_decs_2203_r(Foo, [

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-accessors/private/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ class Foo {
2727
]
2828
], []));
2929
}
30-
#__a_1 = (_initProto(this), _init_a(this));
30+
constructor(){
31+
_initProto(this);
32+
}
33+
#__a_1 = _init_a(this);
3134
get #a() {
3235
return _get___a(this);
3336
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-accessors/public/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class Foo {
2121
]
2222
], []));
2323
}
24-
#___private_a_1 = (_initProto(this), _init_a(this));
24+
constructor(){
25+
_initProto(this);
26+
}
27+
#___private_a_1 = _init_a(this);
2528
get a() {
2629
return this.#___private_a_1;
2730
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-duplicated-keys--to-es2015/.method-and-field/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ var _init_a, _initProto;
22
const dec = () => { };
33
class Foo {
44
constructor() {
5-
defineProperty(this, "a", (_initProto(this), _init_a(this, 123)));
5+
defineProperty(this, "a", _init_a(this, 123));
6+
_initProto(this);
67
}
78
a() {
89
return 1;

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-duplicated-keys/method-and-field/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ class Foo {
1515
]
1616
], []));
1717
}
18-
a = (_initProto(this), _init_a(this, 123));
18+
constructor(){
19+
_initProto(this);
20+
}
21+
a = _init_a(this, 123);
1922
a() {
2023
return 1;
2124
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-fields--to-es2015/private/output.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var _init_a, _init_b;
1+
var _init_a, _init_b, _initProto;
22
const dec = ()=>{};
33
var _a = /*#__PURE__*/ new WeakMap(), _b = /*#__PURE__*/ new WeakMap();
44
class Foo {
@@ -11,9 +11,10 @@ class Foo {
1111
writable: true,
1212
value: _init_b(this, 123)
1313
});
14+
_initProto(this);
1415
}
1516
}
16-
({ e: [_init_a, _init_b] } = _apply_decs_2203_r(Foo, [
17+
({ e: [_init_a, _init_b, _initProto] } = _apply_decs_2203_r(Foo, [
1718
[
1819
dec,
1920
0,

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-fields/private/output.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
var _init_a, _init_b;
1+
var _init_a, _init_b, _initProto;
22
const dec = ()=>{};
33
class Foo {
44
static{
5-
({ e: [_init_a, _init_b] } = _apply_decs_2203_r(this, [
5+
({ e: [_init_a, _init_b, _initProto] } = _apply_decs_2203_r(this, [
66
[
77
dec,
88
0,
@@ -27,6 +27,9 @@ class Foo {
2727
]
2828
], []));
2929
}
30+
constructor(){
31+
_initProto(this);
32+
}
3033
#a = _init_a(this);
3134
#b = _init_b(this, 123);
3235
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-getters--to-es2015/private/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class Foo {
1010
get: get_a,
1111
set: void 0
1212
});
13-
_define_property(this, "value", (_initProto(this), 1));
13+
_define_property(this, "value", 1);
14+
_initProto(this);
1415
}
1516
}
1617
({ e: [_call_a, _initProto] } = _apply_decs_2203_r(Foo, [

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-getters--to-es2015/public/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class Foo {
1010
return this.value;
1111
}
1212
constructor(){
13-
_define_property(this, "value", (_initProto(this), 1));
13+
_define_property(this, "value", 1);
14+
_initProto(this);
1415
}
1516
}
1617
({ e: [_initProto] } = _apply_decs_2203_r(Foo, [

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-getters-and-setters--to-es2015/private/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class Foo {
1313
get: get_a,
1414
set: set_a
1515
});
16-
_define_property(this, "value", (_initProto(this), 1));
16+
_define_property(this, "value", 1);
17+
_initProto(this);
1718
}
1819
}
1920
({ e: [_call_a, _call_a1, _initProto] } = _apply_decs_2203_r(Foo, [

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-getters-and-setters--to-es2015/public/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class Foo {
1616
this.value = v;
1717
}
1818
constructor(){
19-
_define_property(this, "value", (_initProto(this), 1));
19+
_define_property(this, "value", 1);
20+
_initProto(this);
2021
}
2122
}
2223
({ e: [_initProto] } = _apply_decs_2203_r(Foo, [

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-getters-and-setters/private/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ class Foo {
2121
]
2222
], []));
2323
}
24-
value = (_initProto(this), 1);
24+
constructor(){
25+
_initProto(this);
26+
}
27+
value = 1;
2528
get #a() {
2629
return _call_a(this);
2730
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-getters-and-setters/public/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class Foo {
2626
]
2727
], []));
2828
}
29-
value = (_initProto(this), 1);
29+
constructor(){
30+
_initProto(this);
31+
}
32+
value = 1;
3033
get a() {
3134
return this.value;
3235
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-getters/private/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class Foo {
1313
]
1414
], []));
1515
}
16-
value = (_initProto(this), 1);
16+
constructor(){
17+
_initProto(this);
18+
}
19+
value = 1;
1720
get #a() {
1821
return _call_a(this);
1922
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-getters/public/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class Foo {
1616
]
1717
], []));
1818
}
19-
value = (_initProto(this), 1);
19+
constructor(){
20+
_initProto(this);
21+
}
22+
value = 1;
2023
get a() {
2124
return this.value;
2225
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-methods--to-es2015/private/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class Foo {
1010
get: get_a,
1111
set: void 0
1212
});
13-
_define_property(this, "value", (_initProto(this), 1));
13+
_define_property(this, "value", 1);
14+
_initProto(this);
1415
}
1516
}
1617
({ e: [_call_a, _initProto] } = _apply_decs_2203_r(Foo, [

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-methods--to-es2015/public/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class Foo {
1010
return this.value;
1111
}
1212
constructor(){
13-
_define_property(this, "value", (_initProto(this), 1));
13+
_define_property(this, "value", 1);
14+
_initProto(this);
1415
}
1516
}
1617
({ e: [_initProto] } = _apply_decs_2203_r(Foo, [

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-methods/private/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ class Foo {
1313
]
1414
], []));
1515
}
16-
value = (_initProto(this), 1);
16+
constructor(){
17+
_initProto(this);
18+
}
19+
value = 1;
1720
get #a() {
1821
return _call_a;
1922
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-methods/public/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class Foo {
1616
]
1717
], []));
1818
}
19-
value = (_initProto(this), 1);
19+
constructor(){
20+
_initProto(this);
21+
}
22+
value = 1;
2023
a() {
2124
return this.value;
2225
}

crates/swc_ecma_transforms_proposal/tests/decorators/2022-03-misc/all-decorators/output.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,10 @@ new class extends _identity {
144144
]));
145145
_initStatic(this);
146146
}
147-
a = (_initProto(this), _init_a(this));
147+
constructor(){
148+
_initProto(this);
149+
}
150+
a = _init_a(this);
148151
b() {}
149152
get c() {}
150153
set c(v) {}

0 commit comments

Comments
 (0)