@@ -13,11 +13,11 @@ use swc_ecma_utils::{
13
13
use swc_ecma_visit:: { as_folder, noop_visit_mut_type, Fold , VisitMut , VisitMutWith } ;
14
14
15
15
pub fn decorator_2022_03 ( ) -> impl VisitMut + Fold {
16
- as_folder ( Decorator202203 :: default ( ) )
16
+ as_folder ( Decorator2022_03 :: default ( ) )
17
17
}
18
18
19
19
#[ derive( Default ) ]
20
- struct Decorator202203 {
20
+ struct Decorator2022_03 {
21
21
/// Variables without initializer.
22
22
extra_vars : Vec < VarDeclarator > ,
23
23
@@ -35,6 +35,8 @@ struct Decorator202203 {
35
35
36
36
#[ derive( Default ) ]
37
37
struct ClassState {
38
+ private_id_index : u32 ,
39
+
38
40
static_lhs : Vec < Ident > ,
39
41
proto_lhs : Vec < Ident > ,
40
42
@@ -55,7 +57,7 @@ struct ClassState {
55
57
super_class : Option < Ident > ,
56
58
}
57
59
58
- impl Decorator202203 {
60
+ impl Decorator2022_03 {
59
61
fn preserve_side_effect_of_decorators (
60
62
& mut self ,
61
63
decorators : Vec < Decorator > ,
@@ -463,6 +465,7 @@ impl Decorator202203 {
463
465
let has_static_member = body. iter ( ) . any ( |m| match m {
464
466
ClassMember :: Method ( m) => m. is_static ,
465
467
ClassMember :: PrivateMethod ( m) => m. is_static ,
468
+ ClassMember :: AutoAccessor ( m) => m. is_static ,
466
469
ClassMember :: ClassProp ( ClassProp { is_static, .. } )
467
470
| ClassMember :: PrivateProp ( PrivateProp { is_static, .. } ) => * is_static,
468
471
ClassMember :: StaticBlock ( _) => true ,
@@ -527,7 +530,9 @@ impl Decorator202203 {
527
530
528
531
for m in body. iter_mut ( ) {
529
532
match m {
530
- ClassMember :: ClassProp ( ..) | ClassMember :: PrivateProp ( ..) => {
533
+ ClassMember :: ClassProp ( ..)
534
+ | ClassMember :: PrivateProp ( ..)
535
+ | ClassMember :: AutoAccessor ( ..) => {
531
536
replace_ident ( m, c. ident . to_id ( ) , & new_class_name) ;
532
537
}
533
538
@@ -568,6 +573,13 @@ impl Decorator202203 {
568
573
p. is_static = false ;
569
574
}
570
575
}
576
+
577
+ ClassMember :: AutoAccessor ( p) => {
578
+ if p. is_static {
579
+ should_move = true ;
580
+ p. is_static = false ;
581
+ }
582
+ }
571
583
_ => ( ) ,
572
584
}
573
585
@@ -753,7 +765,7 @@ impl Decorator202203 {
753
765
}
754
766
}
755
767
756
- impl VisitMut for Decorator202203 {
768
+ impl VisitMut for Decorator2022_03 {
757
769
noop_visit_mut_type ! ( ) ;
758
770
759
771
fn visit_mut_class ( & mut self , n : & mut Class ) {
@@ -932,6 +944,8 @@ impl VisitMut for Decorator202203 {
932
944
for mut m in members. take ( ) {
933
945
match m {
934
946
ClassMember :: AutoAccessor ( mut accessor) => {
947
+ accessor. value . visit_mut_with ( self ) ;
948
+
935
949
let name;
936
950
let init;
937
951
let field_name_like: JsWord ;
@@ -947,9 +961,14 @@ impl VisitMut for Decorator202203 {
947
961
init = private_ident ! ( format!( "_init_{}" , k. id. sym) ) ;
948
962
field_name_like = format ! ( "__{}" , k. id. sym) . into ( ) ;
949
963
964
+ self . state . private_id_index += 1 ;
950
965
PrivateName {
951
966
span : k. span ,
952
- id : Ident :: new ( format ! ( "__{}" , k. id. sym) . into ( ) , k. id . span ) ,
967
+ id : Ident :: new (
968
+ format ! ( "__{}_{}" , k. id. sym, self . state. private_id_index)
969
+ . into ( ) ,
970
+ k. id . span ,
971
+ ) ,
953
972
}
954
973
}
955
974
Key :: Public ( k) => {
@@ -958,10 +977,16 @@ impl VisitMut for Decorator202203 {
958
977
. replacen ( "init" , "private" , 1 )
959
978
. into ( ) ;
960
979
980
+ self . state . private_id_index += 1 ;
981
+
961
982
PrivateName {
962
983
span : init. span . with_ctxt ( SyntaxContext :: empty ( ) ) ,
963
984
id : Ident :: new (
964
- field_name_like. clone ( ) ,
985
+ format ! (
986
+ "{field_name_like}_{}" ,
987
+ self . state. private_id_index
988
+ )
989
+ . into ( ) ,
965
990
init. span . with_ctxt ( SyntaxContext :: empty ( ) ) ,
966
991
) ,
967
992
}
@@ -1310,7 +1335,9 @@ impl VisitMut for Decorator202203 {
1310
1335
1311
1336
for mut m in new. take ( ) {
1312
1337
match m {
1313
- ClassMember :: Method ( ..) | ClassMember :: PrivateMethod ( ..) => { }
1338
+ ClassMember :: Method ( ..)
1339
+ | ClassMember :: PrivateMethod ( ..)
1340
+ | ClassMember :: AutoAccessor ( ..) => { }
1314
1341
1315
1342
_ => {
1316
1343
if !m. span ( ) . is_dummy ( ) {
0 commit comments