UNPKG

agentsqripts

Version:

Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems

77 lines (69 loc) 2.58 kB
/** * @file Detect memory leak patterns * @description Single responsibility: Detect common memory leak patterns in JavaScript */ const { iterateLines } = require('../../../utils/patternDetector'); const findCleanupPattern = require('../find/findCleanupPattern'); /** * Detect memory leak patterns */ function detectMemoryLeaks(lines, filePath) { const issues = []; iterateLines(lines, (line, lineNumber, trimmed, i) => { // Global variables that grow if (/global\.\w+\s*=\s*\[|window\.\w+\s*=\s*\[/.test(trimmed)) { issues.push({ type: 'global_memory_leak', severity: 'HIGH', category: 'Memory', location: `${filePath}:${lineNumber}`, line: lineNumber, code: trimmed, description: 'Global variable assignment can lead to memory leaks', summary: 'Potential global memory leak', recommendation: 'Use local scope or implement proper cleanup', effort: 2, impact: 'Prevents memory leaks in long-running applications', estimatedSavings: 'prevents memory accumulation' }); } // Event listeners without cleanup if (/addEventListener\s*\(/.test(trimmed) && !findCleanupPattern(lines, i, 'removeEventListener')) { issues.push({ type: 'event_listener_leak', severity: 'MEDIUM', category: 'Memory', location: `${filePath}:${lineNumber}`, line: lineNumber, code: trimmed, description: 'Event listener without corresponding removeEventListener', summary: 'Potential event listener memory leak', recommendation: 'Add removeEventListener in cleanup/unmount', effort: 1, impact: 'Prevents event listener accumulation', estimatedSavings: 'prevents listener memory leaks' }); } // Timers without cleanup if (/setInterval\s*\(/.test(trimmed) && !findCleanupPattern(lines, i, 'clearInterval')) { issues.push({ type: 'timer_leak', severity: 'HIGH', category: 'Memory', location: `${filePath}:${lineNumber}`, line: lineNumber, code: trimmed, description: 'setInterval without corresponding clearInterval', summary: 'Timer memory leak', recommendation: 'Store interval ID and call clearInterval in cleanup', effort: 1, impact: 'Prevents timer accumulation and memory leaks', estimatedSavings: 'prevents timer memory leaks' }); } }); return issues; } module.exports = detectMemoryLeaks;