UNPKG

agentsqripts

Version:

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

60 lines (55 loc) 2.49 kB
/** * @file Global variable detector for state management and scalability risk assessment * @description Single responsibility: Detect global variables that compromise application scalability and concurrent execution * * This detector identifies global variable usage patterns that can cause shared state issues, * race conditions, and scalability problems in multi-user applications. It analyzes global * variable declarations, modifications, and access patterns to prevent state-related bugs * and ensure proper isolation in concurrent execution environments. * * Design rationale: * - State isolation analysis prevents shared state issues in concurrent request processing * - Pattern-based detection efficiently identifies problematic global variable usage * - Context-aware analysis distinguishes between acceptable constants and problematic mutable globals * - Race condition prevention through early detection of shared mutable state * - Scalability impact assessment provides guidance for proper state management patterns * * Global variable detection scope: * - Mutable global variables that create shared state between requests * - Global state modifications that can cause race conditions and data corruption * - Improper global variable usage that prevents horizontal scaling * - Global configuration management that should use environment-based approaches * - Memory leak patterns from global variable accumulation without cleanup */ /** * Detects global variables that may cause scalability issues * @param {string} content - File content * @param {string} filePath - Path to the file * @returns {Array} Array of global variable issues */ function detectGlobalVariables(content, filePath) { const issues = []; const globalPatterns = [ /global\.\w+\s*=/, /process\.\w+\s*=/, /window\.\w+\s*=/ ]; globalPatterns.forEach(pattern => { if (pattern.test(content)) { issues.push({ type: 'global_state', severity: 'MEDIUM', category: 'API', location: filePath, pattern: 'Global Variable', summary: 'Global variable assignment detected — can cause state leakage between requests', recommendation: 'Use request-scoped or session-scoped state instead of global variables', impact: 'Global state can cause race conditions and memory leaks in concurrent environments' }); } }); return issues; } module.exports = { detectGlobalVariables };