1- /* eslint-disable no-inner-declarations */
1+ /* eslint-disable no-inner-declarations, no-loop-func */
22// eslint-disable-next-line import/newline-after-import
33const acorn = require ( 'acorn' ) ; // javascript parser
44const jsx = require ( 'acorn-jsx' ) ;
@@ -12,6 +12,7 @@ module.exports = elementType => {
1212 const hookState = { } ;
1313
1414 while ( Object . hasOwnProperty . call ( ast , 'body' ) ) {
15+ let tsCount = 0 ; // Counter for the number of TypeScript hooks seen (to distinguish in masterState)
1516 ast = ast . body ;
1617 const statements = [ ] ;
1718
@@ -27,7 +28,19 @@ module.exports = elementType => {
2728 body . forEach ( elem => {
2829 if ( elem . type === 'VariableDeclaration' ) {
2930 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 ) ;
3144 } ) ;
3245 }
3346 } ) ;
0 commit comments