Skip to content

Commit db14f1f

Browse files
committed
copy and paste from dev
1 parent 22868bf commit db14f1f

File tree

1 file changed

+0
-32
lines changed

1 file changed

+0
-32
lines changed

src/backend/controllers/statePropExtractors.ts

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,11 @@ export function getHooksStateAndUpdateMethod(
7878
}
7979
memoizedState = memoizedState.next;
8080
}
81-
// console.log('hooksStates from getHooksStateAndUpdateMethod');
82-
// console.log(hooksStates);
83-
//confirmed Sudoku home data successfully in hooksStates via console log
8481
return hooksStates;
8582
}
8683

8784
// ---------------------GET STATE VAR NAME & HOOK NAME--------------------------
8885
/**
89-
* This function receive a string representation of a functional component.
90-
* This function then uses JSX parser to traverse through the function string,
91-
* and extract the state variable name and its corresponding setState method.
9286
* 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.
9387
* @param elementType - The string representation of a functional component
9488
* @returns - An array of objects with key: hookName (the name of setState method) | value: varName (the state variable name)
@@ -112,62 +106,36 @@ export function getHooksNames(elementType: string): { hookName: string; varName:
112106
// check if functionDec.expression.body exists, then set declarationBody to functionDec's body
113107
else declarationBody = functionDec.body?.body ?? [];
114108
// Traverse through the function's funcDecs and Expression Statements
115-
console.log('declarationBody');
116-
console.log(declarationBody);
117109
declarationBody.forEach((elem: any) => {
118110
// Hooks will always be contained in a variable declaration
119111
if (elem.type === 'VariableDeclaration') {
120112
// Obtain the declarations array from elem.
121113
const { declarations } = elem;
122114
// Obtain the reactHook:
123115
// Due to difference in babel transpilation in browser vs for jest test, expression is stored in differen location
124-
console.log('Found variable declaration, elem declarations:', declarations);
125-
// console.log('dec[0]', declarations[0]);
126-
// console.log('init',declarations[0]?.init);
127-
// console.log('callee',declarations[0]?.init?.callee);
128-
// console.log('expressions', declarations[0]?.init?.callee?.expressions);
129-
// console.log('Line 2!');
130-
// console.log('args', declarations[0]?.init?.arguments);
131-
// console.log('args[0]', declarations[0]?.init?.arguments?.arguments[0]);
132-
// console.log('callee', declarations[0]?.init?.arguments?.arguments[0]?.callee);
133-
// console.log('expression', declarations[0]?.init?.arguments?.arguments[0]?.callee?.expressions);
134116
const expression =
135117
declarations[0]?.init?.callee?.expressions || //work for browser
136-
//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.
137118
declarations[0]?.init?.arguments?.[0]?.callee?.expressions; //work for jest test;
138-
139-
console.log('looked for expression, found:', expression);
140-
//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.
141-
if (expression === undefined) return;
142-
declarations[0]?.init?.arguments?.[0]?.callee?.expressions; //work for jest test;
143119
// A functional declaration within a component that isn't a hook won't have the callee being searched for above. This line will cause this forEach execution to stop here in this case.
144120
if (expression === undefined) return;
145121
let reactHook: string;
146122
reactHook = expression[1].property?.name;
147123
if (reactHook === 'useState') {
148124
// Obtain the variable being set:
149125
let varName: string =
150-
declarations[declarations.length - 2]?.id?.name || // work react application;
151126
// Points to second to last element of declarations because webpack adds an extra variable when converting files that use ES6
152127
declarations[declarations.length - 2]?.id?.name || // work react application;
153128
(Array.isArray(declarations[0]?.id?.elements)
154129
? declarations[0]?.id?.elements[0]?.name
155130
: undefined); //work for nextJS application
156131
// Obtain the setState method:
157132
let hookName: string =
158-
declarations[declarations.length - 1]?.id?.name || // work react application;
159133
//Points to last element of declarations because webpack adds an extra variable when converting files that use ES6
160134
declarations[declarations.length - 1]?.id?.name || // work react application;
161135
(Array.isArray(declarations[0]?.id?.elements)
162136
? declarations[0]?.id?.elements[0]?.name
163137
: undefined); //work for nextJS & Remix
164138
// Push reactHook & varName to statements array
165-
/**
166-
* Mark's notes, I'd like to alter the structure of the data
167-
* to pass on the reactHook 'useState'. That way the user will
168-
* eventually be able to view the difference between variables
169-
* stored via useState and useContext
170-
*/
171139
statements.push({ hookName, varName });
172140
}
173141
}

0 commit comments

Comments
 (0)