@@ -51,7 +51,6 @@ struct ClassState {
51
51
/// If not empty, `initProto` should be injected to the constructor.
52
52
init_proto : Option < Ident > ,
53
53
init_proto_args : Vec < Option < ExprOrSpread > > ,
54
- is_init_proto_called : bool ,
55
54
56
55
init_static : Option < Ident > ,
57
56
init_static_args : Vec < Option < ExprOrSpread > > ,
@@ -718,10 +717,10 @@ impl DecoratorPass {
718
717
}
719
718
}
720
719
body. visit_mut_with ( self ) ;
721
- c. visit_mut_with ( self ) ;
722
720
c. ident = preserved_class_name. clone ( ) ;
723
721
replace_ident ( & mut c. class , c. ident . to_id ( ) , & preserved_class_name) ;
724
722
c. class . body . extend ( body) ;
723
+ c. visit_mut_with ( self ) ;
725
724
c. class . body . push ( ClassMember :: StaticBlock ( StaticBlock {
726
725
span : DUMMY_SP ,
727
726
body : BlockStmt {
@@ -814,19 +813,48 @@ impl VisitMut for DecoratorPass {
814
813
815
814
n. visit_mut_children_with ( self ) ;
816
815
817
- if !self . state . is_init_proto_called {
818
- if let Some ( init_proto) = self . state . init_proto . clone ( ) {
816
+ if let Some ( init_proto) = self . state . init_proto . clone ( ) {
817
+ let init_proto_expr = CallExpr {
818
+ span : DUMMY_SP ,
819
+ callee : init_proto. clone ( ) . as_callee ( ) ,
820
+ args : vec ! [ ThisExpr { span: DUMMY_SP } . as_arg( ) ] ,
821
+ ..Default :: default ( )
822
+ } ;
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
+ }
853
+
854
+ if !proto_inited {
819
855
let c = self . ensure_constructor ( n) ;
820
856
821
- inject_after_super (
822
- c,
823
- vec ! [ Box :: new( Expr :: Call ( CallExpr {
824
- span: DUMMY_SP ,
825
- callee: init_proto. as_callee( ) ,
826
- args: vec![ ThisExpr { span: DUMMY_SP } . as_arg( ) ] ,
827
- ..Default :: default ( )
828
- } ) ) ] ,
829
- )
857
+ inject_after_super ( c, vec ! [ Box :: new( init_proto_expr. into( ) ) ] )
830
858
}
831
859
}
832
860
@@ -847,7 +875,6 @@ impl VisitMut for DecoratorPass {
847
875
}
848
876
849
877
self . state . init_proto = None ;
850
- self . state . is_init_proto_called = false ;
851
878
852
879
self . state . extra_stmts = old_stmts;
853
880
}
@@ -1045,29 +1072,6 @@ impl VisitMut for DecoratorPass {
1045
1072
value : if accessor. decorators . is_empty ( ) {
1046
1073
accessor. value
1047
1074
} else {
1048
- let init_proto =
1049
- if self . state . is_init_proto_called || accessor. is_static {
1050
- None
1051
- } else {
1052
- self . state . is_init_proto_called = true ;
1053
-
1054
- let init_proto = self
1055
- . state
1056
- . init_proto
1057
- . get_or_insert_with ( || private_ident ! ( "_initProto" ) )
1058
- . clone ( ) ;
1059
-
1060
- Some (
1061
- CallExpr {
1062
- span : DUMMY_SP ,
1063
- callee : init_proto. clone ( ) . as_callee ( ) ,
1064
- args : vec ! [ ThisExpr { span: DUMMY_SP } . as_arg( ) ] ,
1065
- ..Default :: default ( )
1066
- }
1067
- . into ( ) ,
1068
- )
1069
- } ;
1070
-
1071
1075
let init_call = CallExpr {
1072
1076
span : DUMMY_SP ,
1073
1077
callee : init. clone ( ) . as_callee ( ) ,
@@ -1078,9 +1082,7 @@ impl VisitMut for DecoratorPass {
1078
1082
}
1079
1083
. into ( ) ;
1080
1084
1081
- Some ( Expr :: from_exprs (
1082
- init_proto. into_iter ( ) . chain ( once ( init_call) ) . collect ( ) ,
1083
- ) )
1085
+ Some ( init_call)
1084
1086
} ,
1085
1087
type_ann : None ,
1086
1088
is_static : accessor. is_static ,
0 commit comments