1
- /* eslint-disable no-inner-declarations */
1
+ /* eslint-disable no-inner-declarations, no-loop-func */
2
2
// eslint-disable-next-line import/newline-after-import
3
3
const acorn = require ( 'acorn' ) ; // javascript parser
4
4
const jsx = require ( 'acorn-jsx' ) ;
@@ -12,6 +12,7 @@ module.exports = elementType => {
12
12
const hookState = { } ;
13
13
14
14
while ( Object . hasOwnProperty . call ( ast , 'body' ) ) {
15
+ let tsCount = 0 ; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
15
16
ast = ast . body ;
16
17
const statements = [ ] ;
17
18
@@ -27,7 +28,19 @@ module.exports = elementType => {
27
28
body . forEach ( elem => {
28
29
if ( elem . type === 'VariableDeclaration' ) {
29
30
elem . declarations . forEach ( hook => {
30
- statements . push ( hook . id . name ) ;
31
+ // * TypeScript hooks appear to have no "VariableDeclarator"
32
+ // * with id.name of _useState, _useState2, etc...
33
+ // * hook.id.type relevant for TypeScript applications
34
+ // *
35
+ // * Works for useState hooks
36
+ if ( hook . id . type === 'ArrayPattern' ) {
37
+ hook . id . elements . forEach ( hook => {
38
+ statements . push ( hook . name ) ;
39
+ // * Unshift a wildcard name to achieve similar functionality as before
40
+ statements . unshift ( `_useWildcard${ tsCount } ` ) ;
41
+ tsCount += 1 ;
42
+ } ) ;
43
+ } else statements . push ( hook . id . name ) ;
31
44
} ) ;
32
45
}
33
46
} ) ;
0 commit comments