Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
31 changes: 30 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
},
"devDependencies": {
"ajv": "^8.17.1",
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"react": "^19.1.0",
"@types/react": "^19.1.6"
}
}
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() === 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
61 changes: 43 additions & 18 deletions src/analyze/typescript/utils/function-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

const ts = require('typescript');
const { isReactHookCall } = require('./type-resolver');

/**
* Finds the name of the function that wraps a given node
Expand All @@ -12,17 +13,17 @@ const ts = require('typescript');
*/
function findWrappingFunction(node) {
let current = node;

while (current) {
const functionName = extractFunctionName(current);

if (functionName) {
return functionName;
}

current = current.parent;
}

return 'global';
}

Expand All @@ -36,27 +37,27 @@ function extractFunctionName(node) {
if (ts.isFunctionDeclaration(node)) {
return node.name ? node.name.escapedText : 'anonymous';
}

// Method declaration in class
if (ts.isMethodDeclaration(node)) {
return node.name ? node.name.escapedText : 'anonymous';
}

// Arrow function or function expression
if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
return findParentFunctionName(node) || 'anonymous';
}

// Constructor
if (ts.isConstructorDeclaration(node)) {
return 'constructor';
}

// Getter/Setter
if (ts.isGetAccessorDeclaration(node) || ts.isSetAccessorDeclaration(node)) {
return node.name ? `${ts.isGetAccessorDeclaration(node) ? 'get' : 'set'} ${node.name.escapedText}` : 'anonymous';
}

return null;
}

Expand All @@ -67,14 +68,38 @@ function extractFunctionName(node) {
*/
function findParentFunctionName(node) {
const parent = node.parent;

if (!parent) return null;


if (
ts.isCallExpression(parent) &&
ts.isIdentifier(parent.expression) &&
isReactHookCall(parent)
) {
if (
parent.parent &&
ts.isVariableDeclaration(parent.parent) &&
parent.parent.name
) {
return `${parent.expression.escapedText}(${parent.parent.name.escapedText})`;
}
return `${parent.expression.escapedText}()`;
}

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

// Property assignment: { myFunc: () => {} }
if (ts.isPropertyAssignment(parent) && parent.name) {
if (ts.isIdentifier(parent.name)) {
Expand All @@ -84,28 +109,28 @@ function findParentFunctionName(node) {
return parent.name.text;
}
}

// Method property in object literal: { myFunc() {} }
if (ts.isMethodDeclaration(parent) && parent.name) {
return parent.name.escapedText;
}

// Binary expression assignment: obj.myFunc = () => {}
if (ts.isBinaryExpression(parent) &&
parent.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
if (ts.isBinaryExpression(parent) &&
parent.operatorToken.kind === ts.SyntaxKind.EqualsToken) {
if (ts.isPropertyAccessExpression(parent.left)) {
return parent.left.name.escapedText;
}
}

// Call expression argument: someFunc(() => {})
if (ts.isCallExpression(parent)) {
const argIndex = parent.arguments.indexOf(node);
if (argIndex >= 0) {
return `anonymous-callback-${argIndex}`;
}
}

return null;
}

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