agentsqripts
Version:
Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems
100 lines (87 loc) • 3.25 kB
JavaScript
/**
* @file Complexity issue detection
* @description Identifies specific complexity issues in code
*/
/**
* Identify complexity issues in code content
* @param {string} content - Code content to analyze
* @param {Object} metrics - Code metrics object
* @returns {Array} Array of complexity issues
*/
function identifyComplexityIssues(content, metrics) {
const issues = [];
const lines = content.split('\n');
// Check for long functions
let currentFunction = null;
let functionStartLine = 0;
let braceCount = 0;
lines.forEach((line, index) => {
const trimmed = line.trim();
// Detect function start - more comprehensive patterns
if (trimmed.match(/^(async\s+)?function\s+\w+|^(const|let|var)\s+\w+\s*=.*=>|^(async\s+)?(const|let|var)\s+\w+\s*=\s*(async\s+)?function/)) {
if (currentFunction) {
// Previous function ended, check its length
const functionLength = index - functionStartLine;
if (functionLength > 50) {
issues.push({
type: 'long_function',
line: functionStartLine + 1,
severity: functionLength > 100 ? 'HIGH' : 'MEDIUM',
description: `Function is ${functionLength} lines long`,
recommendation: 'Consider breaking down into smaller functions'
});
}
}
currentFunction = trimmed;
functionStartLine = index;
braceCount = 0;
}
// Track braces to detect function end
braceCount += (line.match(/\{/g) || []).length;
braceCount -= (line.match(/\}/g) || []).length;
if (currentFunction && braceCount <= 0 && trimmed.includes('}')) {
currentFunction = null;
}
// Check for deep nesting - handle both spaces and tabs
const indentation = line.match(/^(\s*)/)[1];
const indentLevel = indentation.includes('\t') ?
indentation.length * 2 : // Tabs count as 2 levels
Math.floor(indentation.length / 2); // 2 spaces per level
if (indentLevel > 6) { // More than 6 levels of indentation
issues.push({
type: 'deep_nesting',
line: index + 1,
severity: 'MEDIUM',
description: `Deep nesting detected (${indentLevel} levels)`,
recommendation: 'Consider extracting nested logic into separate functions'
});
}
// Check for complex expressions
if (trimmed.includes('&&') && trimmed.includes('||') && trimmed.length > 80) {
issues.push({
type: 'complex_expression',
line: index + 1,
severity: 'LOW',
description: 'Complex boolean expression detected',
recommendation: 'Consider breaking into multiple conditions or variables'
});
}
});
// Check final function if exists
if (currentFunction) {
const functionLength = lines.length - functionStartLine;
if (functionLength > 50) {
issues.push({
type: 'long_function',
line: functionStartLine + 1,
severity: functionLength > 100 ? 'HIGH' : 'MEDIUM',
description: `Function is ${functionLength} lines long`,
recommendation: 'Consider breaking down into smaller functions'
});
}
}
return issues;
}
module.exports = {
identifyComplexityIssues
};