Skip to content

Commit 8f5c2a1

Browse files
committed
Merge branch 'master' into getMultipleHooksNames
2 parents 936f444 + 8d74c6d commit 8f5c2a1

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/backend/helpers.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ const JSXParser = acorn.Parser.extend(jsx());
1616

1717
// Returns a throttled version of an input function
1818
// 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 => {
2323
if (isOnCooldown && isCallQueued) return;
2424
if (isOnCooldown) {
2525
isCallQueued = true;
@@ -28,7 +28,7 @@ export const throttle = (f: Function, t: number): Function => {
2828
f();
2929
isOnCooldown = true;
3030
isCallQueued = false;
31-
const runAfterTimeout = (): any => {
31+
const runAfterTimeout = () : any => {
3232
if (isCallQueued) {
3333
isCallQueued = false;
3434
isOnCooldown = true; // not needed I think
@@ -44,9 +44,9 @@ export const throttle = (f: Function, t: number): Function => {
4444
};
4545

4646
// 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> => {
4848
// Initialize empty object to store the setters and getter
49-
let ast: any;
49+
let ast : any;
5050
try {
5151
ast = JSXParser.parse(elementType);
5252
} catch (e) {
@@ -59,37 +59,38 @@ export const getHooksNames = (elementType: string): Array<string> => {
5959
const hooksNames: any = {};
6060

6161
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)
6363
ast = ast.body;
64-
const statements: Array<string> = [];
64+
const statements : Array<string> = [];
6565

6666
/** All module exports always start off as a single 'FunctionDeclaration' type
6767
* Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement"
6868
* Iterate through AST of every function declaration
6969
* Check within each function declaration if there are hook declarations */
7070
ast.forEach((functionDec) => {
71-
let body: any;
71+
let body : any;
7272
if (functionDec.expression && functionDec.expression.body)
7373
body = functionDec.expression.body.body;
7474
else body = functionDec.body ? functionDec.body.body : [];
7575
// Traverse through the function's funcDecs and Expression Statements
76-
body.forEach((elem: any) => {
76+
body.forEach((elem : any) => {
7777
if (elem.type === 'VariableDeclaration') {
78-
elem.declarations.forEach((hook: any) => {
78+
elem.declarations.forEach((hook : any) => {
7979
// * TypeScript hooks appear to have no "VariableDeclarator"
8080
// * with id.name of _useState, _useState2, etc...
8181
// * hook.id.type relevant for TypeScript applications
8282
// *
8383
// * Works for useState hooks
8484
if (hook.id.type === 'ArrayPattern') {
8585
hook.id.elements.forEach((hook) => {
86-
statements.push(`_useWildcard${tsCount}`);
8786
statements.push(hook.name);
87+
// * Unshift a wildcard name to achieve similar functionality as before
88+
statements.unshift(`_useWildcard${tsCount}`);
8889
tsCount += 1;
8990
});
9091
} else {
9192
if (hook.init.object && hook.init.object.name) {
92-
const varName: any = hook.init.object.name;
93+
const varName : any = hook.init.object.name;
9394
if (!hooksNames[varName] && varName.match(/_use/)) {
9495
hooksNames[varName] = hook.id.name;
9596
}
@@ -103,10 +104,9 @@ export const getHooksNames = (elementType: string): Array<string> => {
103104
});
104105

105106
statements.forEach((el, i) => {
106-
if (el.match(/_use/)) hooksNames[el] = statements[i + 1];
107+
if (el.match(/_use/)) hookState[el] = statements[i + 2];
107108
});
108109
});
109110
}
110-
111111
return Object.values(hooksNames);
112112
};

0 commit comments

Comments
 (0)