@@ -17,6 +17,7 @@ class ReactDebugMacro
17
17
var pos = Context .currentPos ();
18
18
var propsType : Null <ComplexType > = macro :Dynamic ;
19
19
var stateType : Null <ComplexType > = macro :Dynamic ;
20
+ var hasState = false ;
20
21
21
22
switch (inClass .superClass )
22
23
{
@@ -26,13 +27,17 @@ class ReactDebugMacro
26
27
27
28
stateType = TypeTools .toComplexType (params [1 ]);
28
29
if (isEmpty (stateType )) stateType = null ;
30
+ else hasState = true ;
29
31
30
32
default :
31
33
}
32
34
33
35
if (! updateComponentUpdate (fields , inClass , propsType , stateType ))
34
36
addComponentUpdate (fields , inClass , propsType , stateType );
35
37
38
+ if (hasState && ! updateConstructor (fields , inClass , propsType , stateType ))
39
+ addConstructor (fields , inClass , propsType , stateType );
40
+
36
41
return fields ;
37
42
}
38
43
@@ -47,6 +52,83 @@ class ReactDebugMacro
47
52
};
48
53
}
49
54
55
+ static function updateConstructor (
56
+ fields : Array <Field >,
57
+ inClass : ClassType ,
58
+ propsType : Null <ComplexType >,
59
+ stateType : Null <ComplexType >
60
+ ) {
61
+ for (field in fields )
62
+ {
63
+ if (field .name == " new" )
64
+ {
65
+ switch (field .kind ) {
66
+ case FFun (f ):
67
+ f .expr = macro {
68
+ ${f .expr }
69
+ ${exprConstructor (inClass )}
70
+ };
71
+
72
+ return true ;
73
+ default :
74
+ }
75
+ }
76
+ }
77
+
78
+ return false ;
79
+ }
80
+
81
+ static function addConstructor (
82
+ fields : Array <Field >,
83
+ inClass : ClassType ,
84
+ propsType : Null <ComplexType >,
85
+ stateType : Null <ComplexType >
86
+ ) {
87
+ var constructor = {
88
+ args : [
89
+ {
90
+ meta : [],
91
+ name : " props" ,
92
+ type : propsType == null ? macro : react. Empty : propsType ,
93
+ opt : false ,
94
+ value : null
95
+ }
96
+ ],
97
+ ret : macro :Void ,
98
+ expr : macro {
99
+ super (props );
100
+ ${exprConstructor (inClass )};
101
+ }
102
+ }
103
+
104
+ fields .push ({
105
+ name : ' new' ,
106
+ access : [APublic ],
107
+ kind : FFun (constructor ),
108
+ pos : inClass .pos
109
+ });
110
+ }
111
+
112
+ static function exprConstructor (inClass : ClassType )
113
+ {
114
+ return macro {
115
+ if (state == null ) {
116
+ js. Browser .console .error (
117
+ ' Warning: component ${inClass .name } is stateful but its '
118
+ + ' `state` is not initialized inside its constructor. '
119
+
120
+ + ' Either add a `state = { ... }` statement to its constructor '
121
+ + ' or define this component as a `ReactComponentOfProps` '
122
+ + ' if it is only using `props`. '
123
+
124
+ + ' If it is using neither `props` nor `state`, you might '
125
+ + ' consider using `@:jsxStatic` to avoid unneeded lifecycle.'
126
+ // TODO: link to @:jsxStatic documentation when available
127
+ );
128
+ }
129
+ };
130
+ }
131
+
50
132
static function updateComponentUpdate (
51
133
fields : Array <Field >,
52
134
inClass : ClassType ,
0 commit comments