@@ -79,44 +79,121 @@ function _p5CodeAstAnalyzer(_cm) {
7979 const className = node . id . name ;
8080 const classInfo = {
8181 const : new Set ( ) ,
82- methods : [ ]
82+ fields : new Set ( ) ,
83+ methods : [ ] ,
84+ constructor_params : [ ] ,
85+ methodVars : { }
8386 } ;
8487
8588 node . body . body . forEach ( ( element ) => {
8689 if ( element . type === 'ClassMethod' ) {
8790 const methodName = element . key . name ;
8891
8992 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'
94100 ) {
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 ;
96156 if (
97157 expr . left . type === 'MemberExpression' &&
98158 expr . left . object . type === 'ThisExpression' &&
99159 expr . left . property . type === 'Identifier'
100160 ) {
101161 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 ) ;
103175 }
104176 }
105- } ) ;
106- } else {
107- classInfo . methods . push ( methodName ) ;
108- }
177+ } ,
178+ path . scope ,
179+ path
180+ ) ;
109181 }
110182 } ) ;
111183
112184 userDefinedClassMetadata [ className ] = {
113185 const : Array . from ( classInfo . const ) ,
186+ fields : Array . from ( classInfo . fields ) ,
114187 methods : classInfo . methods ,
115- initializer : ''
188+ constructor_params : classInfo . constructor_params ,
189+ initializer : '' ,
190+ methodVars : classInfo . methodVars
116191 } ;
117192 }
118- } ,
193+ }
194+ } ) ;
119195
196+ traverse ( ast , {
120197 AssignmentExpression ( path ) {
121198 const node = path . node ;
122199 if (
@@ -127,7 +204,6 @@ function _p5CodeAstAnalyzer(_cm) {
127204 ) {
128205 const varName = node . left . name ;
129206 const fnName = node . right . callee . name ;
130-
131207 const cls = functionToClass [ fnName ] ;
132208 const userCls = userDefinedClassMetadata [ fnName ] ;
133209 if ( userCls ) {
@@ -211,6 +287,7 @@ function _p5CodeAstAnalyzer(_cm) {
211287 } ;
212288
213289 lastValidResult = result ;
290+ console . log ( result ) ;
214291 return result ;
215292}
216293
0 commit comments