@@ -16,10 +16,10 @@ const JSXParser = acorn.Parser.extend(jsx());
16
16
17
17
// Returns a throttled version of an input function
18
18
// The returned throttled function only executes at most once every t milliseconds
19
- export const throttle = ( f : Function , t : number ) : Function => {
20
- let isOnCooldown : boolean = false ;
21
- let isCallQueued : boolean = false ;
22
- const throttledFunc = ( ) : any => {
19
+ export const throttle = ( f : Function , t : number ) : Function => {
20
+ let isOnCooldown : boolean = false ;
21
+ let isCallQueued : boolean = false ;
22
+ const throttledFunc = ( ) : any => {
23
23
if ( isOnCooldown && isCallQueued ) return ;
24
24
if ( isOnCooldown ) {
25
25
isCallQueued = true ;
@@ -28,7 +28,7 @@ export const throttle = (f : Function, t: number) : Function => {
28
28
f ( ) ;
29
29
isOnCooldown = true ;
30
30
isCallQueued = false ;
31
- const runAfterTimeout = ( ) : any => {
31
+ const runAfterTimeout = ( ) : any => {
32
32
if ( isCallQueued ) {
33
33
isCallQueued = false ;
34
34
isOnCooldown = true ; // not needed I think
@@ -44,9 +44,9 @@ export const throttle = (f : Function, t: number) : Function => {
44
44
} ;
45
45
46
46
// Helper function to grab the getters/setters from `elementType`
47
- export const getHooksNames = ( elementType : string ) : Array < string > => {
47
+ export const getHooksNames = ( elementType : string ) : Array < string > => {
48
48
// Initialize empty object to store the setters and getter
49
- let ast : any ;
49
+ let ast : any ;
50
50
try {
51
51
ast = JSXParser . parse ( elementType ) ;
52
52
} catch ( e ) {
@@ -59,38 +59,37 @@ export const getHooksNames = (elementType : string) : Array<string> => {
59
59
const hooksNames : any = { } ;
60
60
61
61
while ( Object . hasOwnProperty . call ( ast , 'body' ) ) {
62
- let tsCount : number = 0 ; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
62
+ let tsCount : number = 0 ; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
63
63
ast = ast . body ;
64
- const statements : Array < string > = [ ] ;
64
+ const statements : Array < string > = [ ] ;
65
65
66
66
/** All module exports always start off as a single 'FunctionDeclaration' type
67
67
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
68
68
* Iterate through AST of every function declaration
69
69
* Check within each function declaration if there are hook declarations */
70
70
ast . forEach ( ( functionDec ) => {
71
- let body : any ;
71
+ let body : any ;
72
72
if ( functionDec . expression && functionDec . expression . body )
73
73
body = functionDec . expression . body . body ;
74
74
else body = functionDec . body ? functionDec . body . body : [ ] ;
75
75
// Traverse through the function's funcDecs and Expression Statements
76
- body . forEach ( ( elem : any ) => {
76
+ body . forEach ( ( elem : any ) => {
77
77
if ( elem . type === 'VariableDeclaration' ) {
78
- elem . declarations . forEach ( ( hook : any ) => {
78
+ elem . declarations . forEach ( ( hook : any ) => {
79
79
// * TypeScript hooks appear to have no "VariableDeclarator"
80
80
// * with id.name of _useState, _useState2, etc...
81
81
// * hook.id.type relevant for TypeScript applications
82
82
// *
83
83
// * Works for useState hooks
84
84
if ( hook . id . type === 'ArrayPattern' ) {
85
85
hook . id . elements . forEach ( ( hook ) => {
86
+ statements . push ( `_useWildcard${ tsCount } ` ) ;
86
87
statements . push ( hook . name ) ;
87
- // * Unshift a wildcard name to achieve similar functionality as before
88
- statements . unshift ( `_useWildcard${ tsCount } ` ) ;
89
88
tsCount += 1 ;
90
89
} ) ;
91
90
} else {
92
91
if ( hook . init . object && hook . init . object . name ) {
93
- const varName : any = hook . init . object . name ;
92
+ const varName : any = hook . init . object . name ;
94
93
if ( ! hooksNames [ varName ] && varName . match ( / _ u s e / ) ) {
95
94
hooksNames [ varName ] = hook . id . name ;
96
95
}
@@ -104,9 +103,10 @@ export const getHooksNames = (elementType : string) : Array<string> => {
104
103
} ) ;
105
104
106
105
statements . forEach ( ( el , i ) => {
107
- if ( el . match ( / _ u s e / ) ) hookState [ el ] = statements [ i + 2 ] ;
106
+ if ( el . match ( / _ u s e / ) ) hooksNames [ el ] = statements [ i + 1 ] ;
108
107
} ) ;
109
108
} ) ;
110
109
}
110
+
111
111
return Object . values ( hooksNames ) ;
112
112
} ;
0 commit comments