agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
60 lines (53 loc) • 1.99 kB
JavaScript
/**
* @file Detect redundant storage patterns
* @description Single responsibility: Identify patterns where memory is used redundantly
*/
const { iterateLines } = require('../../../utils/patternDetector');
/**
* Detect redundant storage patterns
*/
function detectRedundantStorage(lines, filePath) {
const issues = [];
iterateLines(lines, (line, lineNumber, trimmed, i) => {
// Duplicate array creation
if (/\.slice\s*\(\s*\)|\[\.\.\./.test(trimmed) &&
/for\s*\(|\.map\s*\(|\.forEach\s*\(/.test(lines[Math.max(0, i - 2)] || '')) {
issues.push({
type: 'redundant_copy',
severity: 'MEDIUM',
category: 'Memory',
location: `${filePath}:${lineNumber}`,
line: lineNumber,
code: trimmed,
description: 'Creating array copies in loop - redundant memory usage',
summary: 'Redundant array copying',
recommendation: 'Copy once outside loop or use references',
effort: 2,
impact: 'Reduces memory allocation overhead',
estimatedSavings: '30-50% memory reduction'
});
}
// Caching opportunities
if (/\w+\.\w+\s*\(/g.test(trimmed) && /for\s*\(|while\s*\(/.test(trimmed)) {
const methodCalls = (trimmed.match(/\w+\.\w+\s*\(/g) || []).length;
if (methodCalls > 1) {
issues.push({
type: 'missing_cache',
severity: 'LOW',
category: 'Memory',
location: `${filePath}:${lineNumber}`,
line: lineNumber,
code: trimmed,
description: 'Multiple method calls in loop - consider caching results',
summary: 'Potential caching opportunity',
recommendation: 'Cache repeated method call results',
effort: 1,
impact: 'Reduces redundant computations and memory allocations',
estimatedSavings: '20-40% performance improvement'
});
}
}
});
return issues;
}
module.exports = detectRedundantStorage;