@@ -45,7 +45,7 @@ export const throttle = (f, t) => {
45
45
} ;
46
46
47
47
// Helper function to grab the getters/setters from `elementType`
48
- export const getHooksNames = elementType => {
48
+ export const getHooksNames = ( elementType ) => {
49
49
// Initialize empty object to store the setters and getter
50
50
let ast ;
51
51
try {
@@ -65,21 +65,22 @@ export const getHooksNames = elementType => {
65
65
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
66
66
* Iterate through AST of every function declaration
67
67
* Check within each function declaration if there are hook declarations */
68
- ast . forEach ( functionDec => {
68
+ ast . forEach ( ( functionDec ) => {
69
69
let body ;
70
- if ( functionDec . expression && functionDec . expression . body ) body = functionDec . expression . body . body ;
70
+ if ( functionDec . expression && functionDec . expression . body )
71
+ body = functionDec . expression . body . body ;
71
72
else body = functionDec . body ? functionDec . body . body : [ ] ;
72
73
// Traverse through the function's funcDecs and Expression Statements
73
- body . forEach ( elem => {
74
+ body . forEach ( ( elem ) => {
74
75
if ( elem . type === 'VariableDeclaration' ) {
75
- elem . declarations . forEach ( hook => {
76
+ elem . declarations . forEach ( ( hook ) => {
76
77
// * TypeScript hooks appear to have no "VariableDeclarator"
77
78
// * with id.name of _useState, _useState2, etc...
78
79
// * hook.id.type relevant for TypeScript applications
79
80
// *
80
81
// * Works for useState hooks
81
82
if ( hook . id . type === 'ArrayPattern' ) {
82
- hook . id . elements . forEach ( hook => {
83
+ hook . id . elements . forEach ( ( hook ) => {
83
84
statements . push ( hook . name ) ;
84
85
// * Unshift a wildcard name to achieve similar functionality as before
85
86
statements . unshift ( `_useWildcard${ tsCount } ` ) ;
@@ -92,7 +93,9 @@ export const getHooksNames = elementType => {
92
93
hooksNames [ varName ] = hook . id . name ;
93
94
}
94
95
}
95
- statements . push ( hook . id . name ) ;
96
+ if ( hook . id . name !== undefined ) {
97
+ statements . push ( hook . id . name ) ;
98
+ }
96
99
}
97
100
} ) ;
98
101
}
0 commit comments