agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
63 lines (56 loc) • 2.16 kB
JavaScript
/**
* @file Detect missing dependencies in useEffect
* @description Single responsibility: Detect missing dependencies in React useEffect hooks
*/
const walk = require('acorn-walk');
const { getLineNumber } = require('../../../utils/astParser');
const isLocalVariable = require('../check/isLocalVariable');
/**
* Detect missing dependencies in useEffect
* @param {Object} ast - AST tree
* @param {string} filePath - File path
* @returns {Array} Detected bugs
*/
function detectMissingDependencies(ast, filePath) {
const bugs = [];
walk.simple(ast, {
CallExpression(node) {
if (node.callee.name === 'useEffect' && node.arguments.length === 2) {
const deps = node.arguments[1];
// Check for empty dependency array when variables are used
if (deps.type === 'ArrayExpression' && deps.elements.length === 0) {
// This is a simplified check - real implementation would track variable usage
const effectBody = node.arguments[0].body;
if (effectBody && effectBody.type === 'BlockStatement') {
// Look for variable references in the effect
let hasExternalRefs = false;
walk.simple(effectBody, {
Identifier(identNode) {
// Check if this identifier is not a local variable
if (!isLocalVariable(identNode.name, effectBody)) {
hasExternalRefs = true;
}
}
});
if (hasExternalRefs) {
bugs.push({
type: 'missing_effect_deps',
severity: 'MEDIUM',
category: 'React',
line: getLineNumber(node),
column: node.loc ? node.loc.start.column : 0,
description: 'useEffect has missing dependencies',
recommendation: 'Include all external variables in the dependency array',
effort: 1,
impact: 'medium',
file: filePath
});
}
}
}
}
}
});
return bugs;
}
module.exports = detectMissingDependencies;