@@ -14,12 +14,16 @@ const jsx = require('acorn-jsx');
1414
1515const JSXParser = acorn . Parser . extend ( jsx ( ) ) ;
1616
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 => {
2327 if ( isOnCooldown && isCallQueued ) return ;
2428 if ( isOnCooldown ) {
2529 isCallQueued = true ;
@@ -28,7 +32,7 @@ export const throttle = (f : Function, t: number) : Function => {
2832 f ( ) ;
2933 isOnCooldown = true ;
3034 isCallQueued = false ;
31- const runAfterTimeout = ( ) : any => {
35+ const runAfterTimeout = ( ) : any => {
3236 if ( isCallQueued ) {
3337 isCallQueued = false ;
3438 isOnCooldown = true ; // not needed I think
@@ -44,38 +48,42 @@ export const throttle = (f : Function, t: number) : Function => {
4448} ;
4549
4650// 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 > => {
4858 // Initialize empty object to store the setters and getter
49- let ast : any ;
59+ let ast : any ;
5060 try {
5161 ast = JSXParser . parse ( elementType ) ;
5262 } catch ( e ) {
63+ console . error ( `getHooksNames ERROR: Failed to parse elementType string:\n${ elementType } ` ) ;
5364 return [ 'unknown' ] ;
5465 }
5566
56- // const hookState = {};
57- // const hooksNames = {};
58- const hookState : any = { } ;
5967 const hooksNames : any = { } ;
6068
6169 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)
6371 ast = ast . body ;
64- const statements : Array < string > = [ ] ;
72+ const statements : Array < string > = [ ] ;
6573
6674 /** All module exports always start off as a single 'FunctionDeclaration' type
6775 * Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
6876 * Iterate through AST of every function declaration
6977 * Check within each function declaration if there are hook declarations */
7078 ast . forEach ( ( functionDec ) => {
71- let body : any ;
79+ let body : any ;
7280 if ( functionDec . expression && functionDec . expression . body )
7381 body = functionDec . expression . body . body ;
7482 else body = functionDec . body ? functionDec . body . body : [ ] ;
7583 // Traverse through the function's funcDecs and Expression Statements
76- body . forEach ( ( elem : any ) => {
84+ body . forEach ( ( elem : any ) => {
7785 if ( elem . type === 'VariableDeclaration' ) {
78- elem . declarations . forEach ( ( hook : any ) => {
86+ elem . declarations . forEach ( ( hook : any ) => {
7987 // * TypeScript hooks appear to have no "VariableDeclarator"
8088 // * with id.name of _useState, _useState2, etc...
8189 // * hook.id.type relevant for TypeScript applications
@@ -90,7 +98,7 @@ export const getHooksNames = (elementType : string) : Array<string> => {
9098 } ) ;
9199 } else {
92100 if ( hook . init . object && hook . init . object . name ) {
93- const varName : any = hook . init . object . name ;
101+ const varName : any = hook . init . object . name ;
94102 if ( ! hooksNames [ varName ] && varName . match ( / _ u s e / ) ) {
95103 hooksNames [ varName ] = hook . id . name ;
96104 }
@@ -102,9 +110,8 @@ export const getHooksNames = (elementType : string) : Array<string> => {
102110 } ) ;
103111 }
104112 } ) ;
105-
106113 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 ] ;
108115 } ) ;
109116 } ) ;
110117 }
0 commit comments