@@ -79,44 +79,121 @@ function _p5CodeAstAnalyzer(_cm) {
79
79
const className = node . id . name ;
80
80
const classInfo = {
81
81
const : new Set ( ) ,
82
- methods : [ ]
82
+ fields : new Set ( ) ,
83
+ methods : [ ] ,
84
+ constructor_params : [ ] ,
85
+ methodVars : { }
83
86
} ;
84
87
85
88
node . body . body . forEach ( ( element ) => {
86
89
if ( element . type === 'ClassMethod' ) {
87
90
const methodName = element . key . name ;
88
91
89
92
if ( element . kind === 'constructor' ) {
90
- element . body . body . forEach ( ( stmt ) => {
91
- if (
92
- stmt . type === 'ExpressionStatement' &&
93
- stmt . expression . type === 'AssignmentExpression'
93
+ // constructor params
94
+ element . params . forEach ( ( param ) => {
95
+ if ( param . type === 'Identifier' ) {
96
+ classInfo . constructor_params . push ( param . name ) ;
97
+ } else if (
98
+ param . type === 'AssignmentPattern' &&
99
+ param . left . type === 'Identifier'
94
100
) {
95
- const expr = stmt . expression ;
101
+ classInfo . constructor_params . push ( param . left . name ) ;
102
+ } else if ( param . type === 'ObjectPattern' ) {
103
+ param . properties . forEach ( ( prop ) => {
104
+ if ( prop . key && prop . key . type === 'Identifier' ) {
105
+ classInfo . constructor_params . push ( prop . key . name ) ;
106
+ }
107
+ } ) ;
108
+ } else if ( param . type === 'ArrayPattern' ) {
109
+ param . elements . forEach ( ( el ) => {
110
+ if ( el && el . type === 'Identifier' ) {
111
+ classInfo . constructor_params . push ( el . name ) ;
112
+ }
113
+ } ) ;
114
+ }
115
+ } ) ;
116
+
117
+ // collect constructor locals
118
+ traverse (
119
+ element ,
120
+ {
121
+ VariableDeclaration ( innerPath ) {
122
+ innerPath . node . declarations . forEach ( ( decl ) => {
123
+ if ( decl . id . type === 'Identifier' ) {
124
+ classInfo . const . add ( decl . id . name ) ;
125
+ }
126
+ } ) ;
127
+ }
128
+ } ,
129
+ path . scope ,
130
+ path
131
+ ) ;
132
+ } else {
133
+ classInfo . methods . push ( methodName ) ;
134
+
135
+ // collect local vars inside method
136
+ const localVars = [ ] ;
137
+ element . body . body . forEach ( ( stmt ) => {
138
+ if ( stmt . type === 'VariableDeclaration' ) {
139
+ stmt . declarations . forEach ( ( decl ) => {
140
+ if ( decl . id . type === 'Identifier' ) {
141
+ localVars . push ( decl . id . name ) ;
142
+ }
143
+ } ) ;
144
+ }
145
+ } ) ;
146
+
147
+ classInfo . methodVars [ methodName ] = localVars ;
148
+ }
149
+
150
+ // ✅ Collect this.* assignments and this.* calls in *all* methods (incl constructor)
151
+ traverse (
152
+ element ,
153
+ {
154
+ AssignmentExpression ( innerPath ) {
155
+ const expr = innerPath . node ;
96
156
if (
97
157
expr . left . type === 'MemberExpression' &&
98
158
expr . left . object . type === 'ThisExpression' &&
99
159
expr . left . property . type === 'Identifier'
100
160
) {
101
161
const propName = expr . left . property . name ;
102
- classInfo . const . add ( propName ) ;
162
+ classInfo . fields . add ( propName ) ;
163
+ }
164
+ } ,
165
+
166
+ CallExpression ( innerPath ) {
167
+ const callee = innerPath . node . callee ;
168
+ if (
169
+ callee . type === 'MemberExpression' &&
170
+ callee . object . type === 'ThisExpression' &&
171
+ callee . property . type === 'Identifier'
172
+ ) {
173
+ const methodName = callee . property . name ;
174
+ classInfo . fields . add ( methodName ) ;
103
175
}
104
176
}
105
- } ) ;
106
- } else {
107
- classInfo . methods . push ( methodName ) ;
108
- }
177
+ } ,
178
+ path . scope ,
179
+ path
180
+ ) ;
109
181
}
110
182
} ) ;
111
183
112
184
userDefinedClassMetadata [ className ] = {
113
185
const : Array . from ( classInfo . const ) ,
186
+ fields : Array . from ( classInfo . fields ) ,
114
187
methods : classInfo . methods ,
115
- initializer : ''
188
+ constructor_params : classInfo . constructor_params ,
189
+ initializer : '' ,
190
+ methodVars : classInfo . methodVars
116
191
} ;
117
192
}
118
- } ,
193
+ }
194
+ } ) ;
119
195
196
+ traverse ( ast , {
120
197
AssignmentExpression ( path ) {
121
198
const node = path . node ;
122
199
if (
@@ -127,7 +204,6 @@ function _p5CodeAstAnalyzer(_cm) {
127
204
) {
128
205
const varName = node . left . name ;
129
206
const fnName = node . right . callee . name ;
130
-
131
207
const cls = functionToClass [ fnName ] ;
132
208
const userCls = userDefinedClassMetadata [ fnName ] ;
133
209
if ( userCls ) {
@@ -211,6 +287,7 @@ function _p5CodeAstAnalyzer(_cm) {
211
287
} ;
212
288
213
289
lastValidResult = result ;
290
+ console . log ( result ) ;
214
291
return result ;
215
292
}
216
293
0 commit comments