agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
85 lines (81 loc) • 3.92 kB
JavaScript
/**
* @file Check for early exit conditions in code blocks
* @description Single responsibility: Detect control flow statements that exit early
*
* This utility analyzes code blocks to identify early exit conditions that affect
* control flow analysis. It's essential for performance and complexity analysis,
* helping determine if loops or functions have early termination conditions that
* impact algorithmic complexity calculations.
*
* Design rationale:
* - Focuses on control flow statements that change execution path
* - Uses regex pattern matching for reliable keyword detection
* - Boundary checking prevents array index errors
* - Conservative approach includes all relevant exit mechanisms
*/
/**
* Analyze code lines for early exit control flow statements
*
* Technical function: Scans specified line range for keywords that cause early exit
*
* Implementation rationale:
* - Math.max/Math.min provide robust boundary protection against invalid indices
* - Regular expression matches common early exit patterns efficiently
* - Sequential scanning allows early return optimization for performance
* - Covers both JavaScript and Node.js specific exit mechanisms
*
* Control flow analysis strategy:
* - 'break': Exits loops early, affects complexity analysis
* - 'return': Exits functions early, critical for algorithm analysis
* - 'continue': Skips loop iterations, affects performance patterns
* - 'throw': Exception-based early exit, impacts error handling analysis
* - 'process.exit': Node.js application termination, critical for server analysis
*
* Pattern matching considerations:
* - Regex uses word boundaries implicitly through context
* - No word boundary anchors used to catch partial matches (e.g., in strings)
* - Case-sensitive matching for JavaScript keyword precision
* - Escaped dot in 'process\.exit' prevents wildcard matching
*
* Performance optimization:
* - Early return when first match found (short-circuit evaluation)
* - O(n) time complexity where n is number of lines in range
* - Regex compilation happens once and is cached by JavaScript engine
* - Boundary calculation done once before loop starts
*
* Boundary handling:
* - Negative startIndex safely clamped to 0
* - endIndex beyond array length safely clamped to array.length
* - Empty ranges (start >= end) return false without error
* - Invalid array input would cause TypeError (intended behavior)
*
* Limitations and trade-offs:
* - String matching may have false positives in comments or string literals
* - Doesn't parse JavaScript AST for perfect accuracy (trade-off for performance)
* - Conservative approach may flag legitimate uses in strings
* - Simple pattern matching chosen over complex parsing for reliability
*
* Alternative approaches considered:
* - AST parsing: Rejected for performance overhead in pattern detection
* - Individual string.includes checks: Rejected as less efficient than single regex
* - Complete tokenization: Rejected as overkill for this specific detection
*
* @param {string[]} lines - Array of code lines to analyze
* @param {number} startIndex - Starting line index (inclusive, will be clamped to valid range)
* @param {number} endIndex - Ending line index (exclusive, will be clamped to valid range)
* @returns {boolean} True if any early exit pattern found within specified range
* @example
* hasEarlyExit(['if (x) {', ' return false;', '}'], 0, 3) // returns true
* hasEarlyExit(['for (let i = 0; i < 10; i++) {', ' if (found) break;', '}'], 1, 2) // returns true
*/
function hasEarlyExit(lines, startIndex, endIndex) {
const start = Math.max(0, startIndex);
const end = Math.min(lines.length, endIndex);
for (let i = start; i < end; i++) {
if (/break|return|continue|throw|process\.exit/.test(lines[i])) {
return true;
}
}
return false;
}
module.exports = hasEarlyExit;