@@ -11,28 +11,34 @@ module.exports = elementType => {
11
11
// All module exports will always start off as a single 'FunctionDeclaration' type
12
12
while ( Object . hasOwnProperty . call ( ast , 'body' ) ) {
13
13
// Traverse down .body once before invoking parsing logic and will loop through any .body after
14
- ast = ast . body ;
14
+ // depending on the type of state mgmt, ast will have varying levels where
15
+ // body array appears in the hierarchy. console.log(ast) on various apps to see this.
16
+ if ( ast . body [ 0 ] . expression ) {
17
+ ast = ast . body [ 0 ] . expression . body . body ;
18
+ } else {
19
+ ast = ast . body [ 0 ] . body . body ;
20
+ }
15
21
// Iterate through AST of every function declaration
16
22
// Check within each function declaration if there are hook declarations
17
- ast . forEach ( functionDec => {
18
- const { body } = functionDec . body ;
19
- const statements = [ ] ;
20
- // Traverse through the function's funcDecs and Expression Statements
21
- body . forEach ( program => {
22
- // Hook Declarations will only be under 'VariableDeclaration' type
23
- if ( program . type === 'VariableDeclaration' ) {
24
- program . declarations . forEach ( dec => {
25
- statements . push ( dec . id . name ) ;
26
- } ) ;
27
- }
28
- } ) ;
29
- // Iterate through the array and determine getter/setters based on pattern
30
- for ( let i = 0 ; i < statements . length ; i += 1 ) {
31
- if ( statements [ i ] . match ( / _ u s e / ) ) {
32
- hookState [ statements [ i ] ] = statements [ i + 2 ] ;
33
- }
23
+ // ast.forEach(functionDec => { // removed 11/6 to account for change on line 16
24
+ // const { body } = functionDec.body; // removed 11/6 to account for change on line 16
25
+ const statements = [ ] ;
26
+ // Traverse through the function's funcDecs and Expression Statements
27
+ ast . forEach ( program => { // changed from body to ast 11/6 to account for change on line 16
28
+ // Hook Declarations will only be under 'VariableDeclaration' type
29
+ if ( program . type === 'VariableDeclaration' ) {
30
+ program . declarations . forEach ( dec => {
31
+ statements . push ( dec . id . name ) ;
32
+ } ) ;
34
33
}
35
34
} ) ;
35
+ // Iterate through the array and determine getter/setters based on pattern
36
+ for ( let i = 0 ; i < statements . length ; i += 1 ) {
37
+ if ( statements [ i ] . match ( / _ u s e / ) ) {
38
+ hookState [ statements [ i ] ] = statements [ i + 2 ] ;
39
+ }
40
+ }
41
+ // });
36
42
}
37
43
return hookState ;
38
44
} ;
0 commit comments