|
1 |
| -/* eslint-disable linebreak-style */ |
2 |
| -/* eslint-disable no-shadow */ |
3 |
| -/* eslint-disable max-len */ |
4 |
| -/* eslint-disable no-console */ |
5 |
| -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ |
6 |
| -/* eslint-disable @typescript-eslint/no-var-requires */ |
7 |
| -/* eslint-disable linebreak-style */ |
8 |
| -/* eslint-disable no-inner-declarations, no-loop-func */ |
9 |
| -// eslint-disable-next-line import/newline-after-import |
10 |
| -const acorn = require('acorn'); |
11 |
| -const jsx = require('acorn-jsx'); |
12 |
| -// import { acorn } from 'acorn'; // javascript parser |
13 |
| -// import { jsx } from 'acorn-jsx'; |
14 |
| - |
15 |
| -const JSXParser = acorn.Parser.extend(jsx()); |
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, t) => { |
20 |
| - let isOnCooldown = false; |
21 |
| - let isCallQueued = false; |
22 |
| - const throttledFunc = () => { |
23 |
| - if (isOnCooldown && isCallQueued) return; |
24 |
| - if (isOnCooldown) { |
25 |
| - isCallQueued = true; |
26 |
| - return; |
27 |
| - } |
28 |
| - f(); |
29 |
| - isOnCooldown = true; |
30 |
| - isCallQueued = false; |
31 |
| - |
32 |
| - const runAfterTimeout = () => { |
33 |
| - if (isCallQueued) { |
34 |
| - isCallQueued = false; |
35 |
| - isOnCooldown = true; // not needed I think |
36 |
| - f(); |
37 |
| - setTimeout(runAfterTimeout, t); |
38 |
| - return; |
39 |
| - } |
40 |
| - isOnCooldown = false; |
41 |
| - }; |
42 |
| - setTimeout(runAfterTimeout, t); |
43 |
| - }; |
44 |
| - return throttledFunc; |
45 |
| -}; |
46 |
| - |
47 |
| -// Helper function to grab the getters/setters from `elementType` |
48 |
| -export const getHooksNames = (elementType) => { |
49 |
| - // Initialize empty object to store the setters and getter |
50 |
| - let ast; |
51 |
| - try { |
52 |
| - ast = JSXParser.parse(elementType); |
53 |
| - } catch (e) { |
54 |
| - return ['unknown']; |
55 |
| - } |
56 |
| - const hookState = {}; |
57 |
| - const hooksNames = {}; |
58 |
| - |
59 |
| - while (Object.hasOwnProperty.call(ast, 'body')) { |
60 |
| - let tsCount = 0; // Counter for the number of TypeScript hooks seen (to distinguish in masterState) |
61 |
| - ast = ast.body; |
62 |
| - const statements = []; |
63 |
| - |
64 |
| - /** All module exports always start off as a single 'FunctionDeclaration' type |
65 |
| - * Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement" |
66 |
| - * Iterate through AST of every function declaration |
67 |
| - * Check within each function declaration if there are hook declarations */ |
68 |
| - ast.forEach((functionDec) => { |
69 |
| - let body; |
70 |
| - if (functionDec.expression && functionDec.expression.body) |
71 |
| - body = functionDec.expression.body.body; |
72 |
| - else body = functionDec.body ? functionDec.body.body : []; |
73 |
| - // Traverse through the function's funcDecs and Expression Statements |
74 |
| - body.forEach((elem) => { |
75 |
| - if (elem.type === 'VariableDeclaration') { |
76 |
| - elem.declarations.forEach((hook) => { |
77 |
| - // * TypeScript hooks appear to have no "VariableDeclarator" |
78 |
| - // * with id.name of _useState, _useState2, etc... |
79 |
| - // * hook.id.type relevant for TypeScript applications |
80 |
| - // * |
81 |
| - // * Works for useState hooks |
82 |
| - if (hook.id.type === 'ArrayPattern') { |
83 |
| - hook.id.elements.forEach((hook) => { |
84 |
| - statements.push(hook.name); |
85 |
| - // * Unshift a wildcard name to achieve similar functionality as before |
86 |
| - statements.unshift(`_useWildcard${tsCount}`); |
87 |
| - tsCount += 1; |
88 |
| - }); |
89 |
| - } else { |
90 |
| - if (hook.init.object && hook.init.object.name) { |
91 |
| - const varName = hook.init.object.name; |
92 |
| - if (!hooksNames[varName] && varName.match(/_use/)) { |
93 |
| - hooksNames[varName] = hook.id.name; |
94 |
| - } |
95 |
| - } |
96 |
| - if (hook.id.name !== undefined) { |
97 |
| - statements.push(hook.id.name); |
98 |
| - } |
99 |
| - } |
100 |
| - }); |
101 |
| - } |
102 |
| - }); |
103 |
| - |
104 |
| - statements.forEach((el, i) => { |
105 |
| - if (el.match(/_use/)) hookState[el] = statements[i + 2]; |
106 |
| - }); |
107 |
| - }); |
108 |
| - } |
109 |
| - return Object.values(hooksNames); |
110 |
| -}; |
| 1 | +/* eslint-disable linebreak-style */ |
| 2 | +/* eslint-disable no-shadow */ |
| 3 | +/* eslint-disable max-len */ |
| 4 | +/* eslint-disable no-console */ |
| 5 | +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ |
| 6 | +/* eslint-disable @typescript-eslint/no-var-requires */ |
| 7 | +/* eslint-disable linebreak-style */ |
| 8 | +/* eslint-disable no-inner-declarations, no-loop-func */ |
| 9 | +// eslint-disable-next-line import/newline-after-import |
| 10 | +const acorn = require('acorn'); |
| 11 | +const jsx = require('acorn-jsx'); |
| 12 | +// import { acorn } from 'acorn'; // javascript parser |
| 13 | +// import { jsx } from 'acorn-jsx'; |
| 14 | + |
| 15 | +const JSXParser = acorn.Parser.extend(jsx()); |
| 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 => { |
| 23 | + if (isOnCooldown && isCallQueued) return; |
| 24 | + if (isOnCooldown) { |
| 25 | + isCallQueued = true; |
| 26 | + return; |
| 27 | + } |
| 28 | + f(); |
| 29 | + isOnCooldown = true; |
| 30 | + isCallQueued = false; |
| 31 | + const runAfterTimeout = () : any => { |
| 32 | + if (isCallQueued) { |
| 33 | + isCallQueued = false; |
| 34 | + isOnCooldown = true; // not needed I think |
| 35 | + f(); |
| 36 | + setTimeout(runAfterTimeout, t); |
| 37 | + return; |
| 38 | + } |
| 39 | + isOnCooldown = false; |
| 40 | + }; |
| 41 | + setTimeout(runAfterTimeout, t); |
| 42 | + }; |
| 43 | + return throttledFunc; |
| 44 | +}; |
| 45 | + |
| 46 | +// Helper function to grab the getters/setters from `elementType` |
| 47 | +export const getHooksNames = (elementType : string) : Array<string> => { |
| 48 | + // Initialize empty object to store the setters and getter |
| 49 | + let ast : any; |
| 50 | + try { |
| 51 | + ast = JSXParser.parse(elementType); |
| 52 | + } catch (e) { |
| 53 | + return ['unknown']; |
| 54 | + } |
| 55 | + |
| 56 | + // const hookState = {}; |
| 57 | + // const hooksNames = {}; |
| 58 | + const hookState: any = {}; |
| 59 | + const hooksNames: any = {}; |
| 60 | + |
| 61 | + while (Object.hasOwnProperty.call(ast, 'body')) { |
| 62 | + let tsCount : number = 0; // Counter for the number of TypeScript hooks seen (to distinguish in masterState) |
| 63 | + ast = ast.body; |
| 64 | + const statements : Array<string> = []; |
| 65 | + |
| 66 | + /** All module exports always start off as a single 'FunctionDeclaration' type |
| 67 | + * Other types: "BlockStatement" / "ExpressionStatement" / "ReturnStatement" |
| 68 | + * Iterate through AST of every function declaration |
| 69 | + * Check within each function declaration if there are hook declarations */ |
| 70 | + ast.forEach((functionDec) => { |
| 71 | + let body : any; |
| 72 | + if (functionDec.expression && functionDec.expression.body) |
| 73 | + body = functionDec.expression.body.body; |
| 74 | + else body = functionDec.body ? functionDec.body.body : []; |
| 75 | + // Traverse through the function's funcDecs and Expression Statements |
| 76 | + body.forEach((elem : any) => { |
| 77 | + if (elem.type === 'VariableDeclaration') { |
| 78 | + elem.declarations.forEach((hook : any) => { |
| 79 | + // * TypeScript hooks appear to have no "VariableDeclarator" |
| 80 | + // * with id.name of _useState, _useState2, etc... |
| 81 | + // * hook.id.type relevant for TypeScript applications |
| 82 | + // * |
| 83 | + // * Works for useState hooks |
| 84 | + if (hook.id.type === 'ArrayPattern') { |
| 85 | + hook.id.elements.forEach((hook) => { |
| 86 | + statements.push(hook.name); |
| 87 | + // * Unshift a wildcard name to achieve similar functionality as before |
| 88 | + statements.unshift(`_useWildcard${tsCount}`); |
| 89 | + tsCount += 1; |
| 90 | + }); |
| 91 | + } else { |
| 92 | + if (hook.init.object && hook.init.object.name) { |
| 93 | + const varName : any = hook.init.object.name; |
| 94 | + if (!hooksNames[varName] && varName.match(/_use/)) { |
| 95 | + hooksNames[varName] = hook.id.name; |
| 96 | + } |
| 97 | + } |
| 98 | + if (hook.id.name !== undefined) { |
| 99 | + statements.push(hook.id.name); |
| 100 | + } |
| 101 | + } |
| 102 | + }); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + statements.forEach((el, i) => { |
| 107 | + if (el.match(/_use/)) hookState[el] = statements[i + 2]; |
| 108 | + }); |
| 109 | + }); |
| 110 | + } |
| 111 | + return Object.values(hooksNames); |
| 112 | +}; |
0 commit comments