Skip to content

Commit e64fc61

Browse files
authored
Merge pull request #8 from oslabs-beta/astParser-typescript-update
astParser updated for typescript hooks
2 parents 0ea0a4d + 82a679e commit e64fc61

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

package/astParser.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* eslint-disable no-inner-declarations */
1+
/* eslint-disable no-inner-declarations, no-loop-func */
22
// eslint-disable-next-line import/newline-after-import
33
const acorn = require('acorn'); // javascript parser
44
const 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

Comments
 (0)