calculate_complexity
Analyze code cyclomatic complexity to measure structural complexity and identify potential maintenance issues in software files.
Instructions
Calculate cyclomatic complexity for code files
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| files | Yes | File paths to analyze |
Implementation Reference
- src/tools/code-quality.ts:200-205 (handler)Handler for the 'calculate_complexity' tool. Parses input files, reads their contents using FileReader, computes complexity using ComplexityAnalyzer, and returns the result.case 'calculate_complexity': { const files = params.files as string[]; const codeFiles = await FileReader.readFiles(files.join(',')); const result = complexityAnalyzer.analyzeComplexity(codeFiles); return result; }
- src/tools/code-quality.ts:48-62 (registration)Registration of the 'calculate_complexity' tool in the codeQualityTools array, including name, description, and input schema.{ name: 'calculate_complexity', description: 'Calculate cyclomatic complexity for code files', inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to analyze', }, }, required: ['files'], }, },
- src/tools/code-quality.ts:51-61 (schema)Input schema definition for the 'calculate_complexity' tool, specifying required 'files' array of strings.inputSchema: { type: 'object', properties: { files: { type: 'array', items: { type: 'string' }, description: 'File paths to analyze', }, }, required: ['files'], },
- Helper function analyzeComplexity in ComplexityAnalyzer class that computes complexity metrics (average, max, min, per-file) for multiple files by delegating to calculateCyclomaticComplexity.analyzeComplexity(files: CodeFile[]): { average: number; max: number; min: number; files: Array<{ path: string; complexity: number }>; } { if (files.length === 0) { return { average: 0, max: 0, min: 0, files: [], }; } const complexities = files.map((file) => ({ path: file.path, complexity: this.calculateCyclomaticComplexity(file.content), })); const values = complexities.map((c) => c.complexity); const average = values.reduce((sum, val) => sum + val, 0) / values.length; const max = Math.max(...values); const min = Math.min(...values); return { average: Math.round(average * 100) / 100, max, min, files: complexities, }; }
- src/analyzers/code-analyzer.ts:38-63 (helper)Core helper function calculateComplexity that implements cyclomatic complexity calculation by counting matches of control flow keywords and operators in the code string.calculateComplexity(code: string): number { // Simple complexity calculation based on control flow statements const complexityKeywords = [ /\bif\s*\(/g, /\belse\s*{/g, /\bfor\s*\(/g, /\bwhile\s*\(/g, /\bswitch\s*\(/g, /\bcase\s+/g, /\bcatch\s*\(/g, /\b&&/g, /\b\|\|/g, /\?\s*.*\s*:/g, // ternary operator ]; let complexity = 1; // Base complexity for (const pattern of complexityKeywords) { const matches = code.match(pattern); if (matches) { complexity += matches.length; } } return complexity; }