Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@
"isomorphic-git": "^1.27.1",
"js-yaml": "^4.1.0",
"pyodide": "^0.27.6",
"react": "^19.1.0",
"typescript": "^5.5.4",
"zod": "^3.24.4"
},
"devDependencies": {
"@types/react": "^19.1.6",
"ajv": "^8.17.1",
"lodash": "^4.17.21"
}
Expand Down
11 changes: 8 additions & 3 deletions src/analyze/typescript/detectors/analytics-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ function detectAnalyticsSource(node, customFunction) {
* @returns {boolean}
*/
function isCustomFunction(node, customFunction) {
return ts.isIdentifier(node.expression) &&
node.expression.escapedText === customFunction;
const canBeCustomFunction = ts.isIdentifier(node.expression) ||
ts.isPropertyAccessExpression(node.expression) ||
ts.isCallExpression(node.expression) || // For chained calls like getTracker().track()
ts.isElementAccessExpression(node.expression) || // For array/object access like trackers['analytics'].track()
(ts.isPropertyAccessExpression(node.expression?.expression) && ts.isThisExpression(node.expression.expression.expression)); // For class methods like this.analytics.track()

return canBeCustomFunction && node.expression.getText().includes(customFunction);
}

/**
Expand All @@ -59,7 +64,7 @@ function detectFunctionBasedProvider(node) {
}

const functionName = node.expression.escapedText;

for (const provider of Object.values(ANALYTICS_PROVIDERS)) {
if (provider.type === 'function' && provider.functionName === functionName) {
return provider.name;
Expand Down
32 changes: 32 additions & 0 deletions src/analyze/typescript/extractors/property-extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,38 @@ function extractShorthandPropertySchema(checker, prop) {
if (!symbol) {
return { type: 'any' };
}
const declarations = symbol.declarations || [];
for (const decl of declarations) {
// Detect destructuring from useState: const [state, setState] = useState<Type>(...)
if (
ts.isBindingElement(decl) &&
decl.parent &&
ts.isArrayBindingPattern(decl.parent) &&
decl.parent.parent &&
ts.isVariableDeclaration(decl.parent.parent) &&
decl.parent.parent.initializer &&
ts.isCallExpression(decl.parent.parent.initializer) &&
ts.isIdentifier(decl.parent.parent.initializer.expression) &&
decl.parent.parent.initializer.expression.escapedText === 'useState'
) {
// Try to get type from generic argument
const callExpr = decl.parent.parent.initializer;
if (callExpr.typeArguments && callExpr.typeArguments.length > 0) {
const typeNode = callExpr.typeArguments[0];
const type = checker.getTypeFromTypeNode(typeNode);
const typeString = checker.typeToString(type);
return resolveTypeToProperties(checker, typeString);
}
// Fallback: get type from initial value
if (callExpr.arguments && callExpr.arguments.length > 0) {
const initType = checker.getTypeAtLocation(callExpr.arguments[0]);
const typeString = checker.typeToString(initType);
return resolveTypeToProperties(checker, typeString);
}
// Default to any
return { type: 'any' };
}
}

const propType = checker.getTypeAtLocation(prop.name);
const typeString = checker.typeToString(propType);
Expand Down
9 changes: 9 additions & 0 deletions src/analyze/typescript/utils/function-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ function findParentFunctionName(node) {

// Variable declaration: const myFunc = () => {}
if (ts.isVariableDeclaration(parent) && parent.name) {
// Check if initializer is a useCallback call
if (
parent.initializer &&
ts.isCallExpression(parent.initializer) &&
ts.isIdentifier(parent.initializer.expression) &&
parent.initializer.expression.escapedText === 'useCallback'
) {
return `useCallback(${parent.name.escapedText})`;
}
return parent.name.escapedText;
}

Expand Down
17 changes: 16 additions & 1 deletion src/analyze/typescript/utils/type-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,25 @@ function getBasicTypeOfArrayElement(checker, element) {
return 'any';
}

/**
* Checks if a CallExpression is a React hook (useCallback, useState, etc)
* @param {Object} node - CallExpression node
* @param {string[]} hookNames - List of hook names to check
* @returns {boolean}
*/
function isReactHookCall(node, hookNames = ['useCallback', 'useState', 'useEffect', 'useMemo', 'useReducer']) {
if (!node || !node.expression) return false;
if (ts.isIdentifier(node.expression)) {
return hookNames.includes(node.expression.escapedText);
}
return false;
}

module.exports = {
resolveIdentifierToInitializer,
getTypeOfNode,
resolveTypeToProperties,
isCustomType,
getBasicTypeOfArrayElement
getBasicTypeOfArrayElement,
isReactHookCall
};
Loading