UNPKG

agentsqripts

Version:

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

38 lines (31 loc) 1.07 kB
/** * @file Code complexity calculation utilities * @description Responsible for calculating complexity scores for code blocks */ /** * Calculates complexity score for a code block * @param {string} content - Block content * @returns {number} Complexity score */ function calculateBlockComplexity(content) { let complexity = 0; // Control flow complexity const controlPatterns = [ /\bif\s*\(/g, /\belse\s*\{/g, /\bwhile\s*\(/g, /\bfor\s*\(/g, /\bswitch\s*\(/g, /\bcase\s+/g, /\btry\s*\{/g, /\bcatch\s*\(/g ]; controlPatterns.forEach(pattern => { const matches = content.match(pattern); if (matches) complexity += matches.length * 2; }); // Function calls and method chaining const callMatches = content.match(/\w+\s*\(/g); if (callMatches) complexity += callMatches.length; // Operators and expressions const operatorMatches = content.match(/[+\-*\/%=<>!&|]/g); if (operatorMatches) complexity += Math.floor(operatorMatches.length / 3); return complexity; } module.exports = { calculateBlockComplexity };