You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/backend/controllers/statePropExtractors.ts
+41-6Lines changed: 41 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -83,7 +83,9 @@ export function getHooksStateAndUpdateMethod(
83
83
84
84
// ---------------------GET STATE VAR NAME & HOOK NAME--------------------------
85
85
/**
86
-
* This function receive a string representation of a functional component. This function then use JSX parser to traverse through the function string, and extract the state variable name and its corresponding setState method.
86
+
* This function receive a string representation of a functional component.
87
+
* This function then uses JSX parser to traverse through the function string,
88
+
* and extract the state variable name and its corresponding setState method.
87
89
* @param elementType - The string representation of a functional component
88
90
* @returns - An array of objects with key: hookName (the name of setState method) | value: varName (the state variable name)
declarations[0]?.init?.callee?.expressions||//work for browser
118
-
declarations[0]?.init?.arguments[0]?.callee?.expressions;//work for jest test
140
+
//Mark's notes: so this was where the app was breaking. ES6 functions (e.g. const handleClick = () => {}) inside functional components were hitting this line and crashing when it tried to access arguments[0] and arguments didn't exist.
141
+
declarations[0]?.init?.arguments?.[0]?.callee?.expressions;//work for jest test;
142
+
143
+
console.log('looked for expression, found:',expression);
144
+
//Mark's Note: for a functional definition that isn't a hook, it won't have the callee being searched for above. This line will cause this forEach execution to stop here in this case.
145
+
if(expression===undefined)return;
119
146
letreactHook: string;
120
147
reactHook=expression[1].property?.name;
121
148
if(reactHook==='useState'){
122
149
// Obtain the variable being set:
150
+
//Mark's note: changed to point to second to last element of declarations because webpack adds an extra variable when converting files that use ES6, so the previous pointer wasn't working for this case
123
151
letvarName: string=
124
-
declarations[1]?.id?.name||// work react application
152
+
declarations[declarations.length-2]?.id?.name||// work react application;
125
153
(Array.isArray(declarations[0]?.id?.elements)
126
154
? declarations[0]?.id?.elements[0]?.name
127
155
: undefined);//work for nextJS application
128
156
// Obtain the setState method:
157
+
//Mark's note: changed to point to last element of declarations because webpack adds an extra variable when converting files that use ES6, so the previous pointer wasn't working for this case
129
158
lethookName: string=
130
-
declarations[2]?.id?.name||// work react application
159
+
declarations[declarations.length-1]?.id?.name||// work react application;
131
160
(Array.isArray(declarations[0]?.id?.elements)
132
161
? declarations[0]?.id?.elements[0]?.name
133
162
: undefined);//work for nextJS & Remix
134
163
// Push reactHook & varName to statements array
164
+
/**
165
+
* Mark's notes, I'd like to alter the structure of the data
166
+
* to pass on the reactHook 'useState'. That way the user will
167
+
* eventually be able to view the difference between variables
0 commit comments