agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
49 lines (45 loc) • 1.98 kB
JavaScript
/**
* @file Nesting depth calculator for structural complexity assessment
* @description Single responsibility: Calculate maximum nesting depth to identify deeply nested code patterns
*
* This calculator measures the maximum nesting depth in JavaScript code, providing a key
* indicator of structural complexity that correlates with code readability, maintainability,
* and potential defect density. Deep nesting often indicates complex control flow that
* increases cognitive load and maintenance difficulty.
*
* Design rationale:
* - Brace-based tracking provides accurate nesting measurement across JavaScript patterns
* - Maximum depth tracking identifies the deepest nesting level for complexity assessment
* - Character-by-character analysis ensures precise brace counting regardless of formatting
* - Simple implementation maintains performance while providing accurate structural metrics
* - Conservative tracking handles edge cases and malformed code gracefully
*
* Nesting analysis methodology:
* - Opening brace detection increments current nesting level for scope tracking
* - Closing brace detection decrements nesting level with bounds checking
* - Maximum depth recording captures peak nesting complexity throughout analysis
* - Character-level parsing handles diverse formatting and minification patterns
* - Robust tracking maintains accuracy even with irregular code formatting
*/
/**
* Calculates maximum nesting depth
* @param {string} content - File content
* @returns {number} Maximum nesting depth
*/
function calculateMaxNestingDepth(content) {
let currentDepth = 0;
let maxDepth = 0;
for (let i = 0; i < content.length; i++) {
const char = content[i];
if (char === '{') {
currentDepth++;
maxDepth = Math.max(maxDepth, currentDepth);
} else if (char === '}') {
currentDepth = Math.max(0, currentDepth - 1);
}
}
return maxDepth;
}
module.exports = {
calculateMaxNestingDepth
};