@@ -14,12 +14,16 @@ const jsx = require('acorn-jsx');
14
14
15
15
const JSXParser = acorn . Parser . extend ( jsx ( ) ) ;
16
16
17
- // Returns a throttled version of an input function
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 => {
17
+ /**
18
+ * @method throttle
19
+ * @param f A function to throttle
20
+ * @param t A number of milliseconds to use as throttling interval
21
+ * @returns A function that limits input function, `f`, from being called more than once every `t` milliseconds
22
+ */
23
+ export const throttle = ( f : Function , t : number ) : Function => {
24
+ let isOnCooldown : boolean = false ;
25
+ let isCallQueued : boolean = false ;
26
+ const throttledFunc = ( ) : any => {
23
27
if ( isOnCooldown && isCallQueued ) return ;
24
28
if ( isOnCooldown ) {
25
29
isCallQueued = true ;
@@ -28,7 +32,7 @@ export const throttle = (f : Function, t: number) : Function => {
28
32
f ( ) ;
29
33
isOnCooldown = true ;
30
34
isCallQueued = false ;
31
- const runAfterTimeout = ( ) : any => {
35
+ const runAfterTimeout = ( ) : any => {
32
36
if ( isCallQueued ) {
33
37
isCallQueued = false ;
34
38
isOnCooldown = true ; // not needed I think
@@ -44,38 +48,42 @@ export const throttle = (f : Function, t: number) : Function => {
44
48
} ;
45
49
46
50
// Helper function to grab the getters/setters from `elementType`
47
- export const getHooksNames = ( elementType : string ) : Array < string > => {
51
+
52
+ /**
53
+ * @method getHooksNames
54
+ * @param elementType The fiber (whose hooks we want) `type`, A stringified function of the component the Fiber whose hooks we want corresponds to
55
+ * @returns An array of strings
56
+ */
57
+ export const getHooksNames = ( elementType : string ) : Array < string > => {
48
58
// Initialize empty object to store the setters and getter
49
- let ast : any ;
59
+ let ast : any ;
50
60
try {
51
61
ast = JSXParser . parse ( elementType ) ;
52
62
} catch ( e ) {
63
+ console . error ( `getHooksNames ERROR: Failed to parse elementType string:\n${ elementType } ` ) ;
53
64
return [ 'unknown' ] ;
54
65
}
55
66
56
- // const hookState = {};
57
- // const hooksNames = {};
58
- const hookState : any = { } ;
59
67
const hooksNames : any = { } ;
60
68
61
69
while ( Object . hasOwnProperty . call ( ast , 'body' ) ) {
62
- let tsCount : number = 0 ; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
70
+ let tsCount : number = 0 ; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
63
71
ast = ast . body ;
64
- const statements : Array < string > = [ ] ;
72
+ const statements : Array < string > = [ ] ;
65
73
66
74
/** All module exports always start off as a single 'FunctionDeclaration' type
67
75
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
68
76
* Iterate through AST of every function declaration
69
77
* Check within each function declaration if there are hook declarations */
70
78
ast . forEach ( ( functionDec ) => {
71
- let body : any ;
79
+ let body : any ;
72
80
if ( functionDec . expression && functionDec . expression . body )
73
81
body = functionDec . expression . body . body ;
74
82
else body = functionDec . body ? functionDec . body . body : [ ] ;
75
83
// Traverse through the function's funcDecs and Expression Statements
76
- body . forEach ( ( elem : any ) => {
84
+ body . forEach ( ( elem : any ) => {
77
85
if ( elem . type === 'VariableDeclaration' ) {
78
- elem . declarations . forEach ( ( hook : any ) => {
86
+ elem . declarations . forEach ( ( hook : any ) => {
79
87
// * TypeScript hooks appear to have no "VariableDeclarator"
80
88
// * with id.name of _useState, _useState2, etc...
81
89
// * hook.id.type relevant for TypeScript applications
@@ -90,7 +98,7 @@ export const getHooksNames = (elementType : string) : Array<string> => {
90
98
} ) ;
91
99
} else {
92
100
if ( hook . init . object && hook . init . object . name ) {
93
- const varName : any = hook . init . object . name ;
101
+ const varName : any = hook . init . object . name ;
94
102
if ( ! hooksNames [ varName ] && varName . match ( / _ u s e / ) ) {
95
103
hooksNames [ varName ] = hook . id . name ;
96
104
}
@@ -102,9 +110,8 @@ export const getHooksNames = (elementType : string) : Array<string> => {
102
110
} ) ;
103
111
}
104
112
} ) ;
105
-
106
113
statements . forEach ( ( el , i ) => {
107
- if ( el . match ( / _ u s e / ) ) hookState [ el ] = statements [ i + 2 ] ;
114
+ if ( el . match ( / _ u s e / ) ) hooksNames [ el ] = statements [ i + 2 ] ;
108
115
} ) ;
109
116
} ) ;
110
117
}
0 commit comments