|
|
|
|
|
|
|
|
| const LanguageDetector = require('../analyzers/language-detector');
|
|
|
| class CodeQualityScanner {
|
| constructor(options = {}) {
|
| this.options = options;
|
| this.issues = [];
|
| this.languageDetector = new LanguageDetector();
|
| }
|
|
|
| scan(content, filePath) {
|
|
|
| const langInfo = this.languageDetector.detectFromPath(filePath);
|
|
|
| const metrics = {
|
| filePath,
|
| language: langInfo.language,
|
| linesOfCode: this.countLines(content, langInfo),
|
| codeSmells: this.detectCodeSmells(content, langInfo),
|
| duplications: this.detectDuplication(content),
|
| longFunctions: this.detectLongFunctions(content, langInfo),
|
| deepNesting: this.detectDeepNesting(content),
|
| magicNumbers: this.detectMagicNumbers(content)
|
| };
|
|
|
| return metrics;
|
| }
|
|
|
| countLines(content, langInfo) {
|
| const lines = content.split('\n');
|
| const config = langInfo.config;
|
| const commentStarts = config.comments ? [config.comments.line, config.comments.blockStart].filter(Boolean) : ['//'];
|
|
|
| const codeLines = lines.filter(line => {
|
| const trimmed = line.trim();
|
| if (trimmed.length === 0) return false;
|
|
|
| return !commentStarts.some(c => trimmed.startsWith(c));
|
| });
|
|
|
| return {
|
| total: lines.length,
|
| code: codeLines.length,
|
| comments: lines.length - codeLines.length
|
| };
|
| }
|
|
|
| detectCodeSmells(content, langInfo) {
|
| const smells = [];
|
| const config = langInfo.config;
|
|
|
|
|
| const longParams = content.match(/\([^)]{100,}\)/g);
|
| if (longParams) {
|
| smells.push({
|
| type: 'long-parameter-list',
|
| count: longParams.length,
|
| severity: 'MEDIUM'
|
| });
|
| }
|
|
|
|
|
| const returnKeyword = config.keywords?.control?.includes('return') ? 'return' : 'return';
|
| const returns = content.match(new RegExp(`\\b${returnKeyword}\\s+`, 'g')) || [];
|
| if (returns.length > 10) {
|
| smells.push({
|
| type: 'multiple-returns',
|
| count: returns.length,
|
| severity: 'LOW'
|
| });
|
| }
|
|
|
|
|
| const logPatterns = [
|
| /console\.(log|warn|error|debug)/g,
|
| /print\(/g,
|
| /System\.out\.println/g,
|
| /Console\.WriteLine/g,
|
| /fmt\.Println/g,
|
| /echo\s/g,
|
| /puts\s/g
|
| ];
|
|
|
| let totalLogs = 0;
|
| logPatterns.forEach(pattern => {
|
| const matches = content.match(pattern);
|
| if (matches) totalLogs += matches.length;
|
| });
|
|
|
| if (totalLogs > 5) {
|
| smells.push({
|
| type: 'excessive-debug-logs',
|
| count: totalLogs,
|
| severity: 'MEDIUM'
|
| });
|
| }
|
|
|
| return smells;
|
| }
|
|
|
| detectDuplication(content) {
|
| const lines = content.split('\n');
|
| const duplicates = [];
|
| const seen = new Map();
|
|
|
| lines.forEach((line, index) => {
|
| const trimmed = line.trim();
|
| if (trimmed.length > 20) {
|
| if (seen.has(trimmed)) {
|
| seen.get(trimmed).push(index + 1);
|
| } else {
|
| seen.set(trimmed, [index + 1]);
|
| }
|
| }
|
| });
|
|
|
| seen.forEach((lineNumbers, line) => {
|
| if (lineNumbers.length > 1) {
|
| duplicates.push({
|
| line: line.substring(0, 50),
|
| occurrences: lineNumbers.length,
|
| lines: lineNumbers
|
| });
|
| }
|
| });
|
|
|
| return duplicates;
|
| }
|
|
|
| detectLongFunctions(content, langInfo) {
|
| const longFunctions = [];
|
| const config = langInfo.config;
|
|
|
|
|
| const patterns = {
|
| javascript: /(?:function\s+(\w+)|const\s+(\w+)\s*=\s*(?:async\s+)?\([^)]*\)\s*=>)\s*\{/g,
|
| python: /def\s+(\w+)\s*\([^)]*\):/g,
|
| java: /(?:public|private|protected)?\s*(?:static)?\s*\w+\s+(\w+)\s*\([^)]*\)\s*\{/g,
|
| csharp: /(?:public|private|protected)?\s*(?:static)?\s*\w+\s+(\w+)\s*\([^)]*\)\s*\{/g,
|
| go: /func\s+(?:\(\w+\s+\*?\w+\)\s+)?(\w+)\s*\([^)]*\)\s*(?:\([^)]*\))?\s*\{/g,
|
| php: /function\s+(\w+)\s*\([^)]*\)\s*\{/g,
|
| ruby: /def\s+(\w+)(?:\([^)]*\))?/g
|
| };
|
|
|
| const functionPattern = patterns[langInfo.language] || patterns.javascript;
|
|
|
| let match;
|
| while ((match = functionPattern.exec(content)) !== null) {
|
| const funcName = match[1] || match[2];
|
| const startPos = match.index;
|
|
|
|
|
| let endPos = startPos + match[0].length;
|
|
|
| if (langInfo.language === 'python' || langInfo.language === 'ruby') {
|
|
|
| const lines = content.substring(startPos).split('\n');
|
| const startIndent = lines[0].search(/\S/);
|
| let funcLines = 1;
|
| for (let i = 1; i < lines.length; i++) {
|
| const lineIndent = lines[i].search(/\S/);
|
| if (lineIndent >= 0 && lineIndent <= startIndent && lines[i].trim().length > 0) break;
|
| funcLines++;
|
| }
|
|
|
| if (funcLines > 50) {
|
| longFunctions.push({
|
| name: funcName,
|
| lines: funcLines,
|
| severity: funcLines > 100 ? 'HIGH' : 'MEDIUM'
|
| });
|
| }
|
| } else {
|
|
|
| let braceCount = 1;
|
| for (let i = endPos; i < content.length && braceCount > 0; i++) {
|
| if (content[i] === '{') braceCount++;
|
| if (content[i] === '}') braceCount--;
|
| if (braceCount === 0) {
|
| endPos = i;
|
| break;
|
| }
|
| }
|
|
|
| const funcContent = content.substring(startPos, endPos);
|
| const lines = funcContent.split('\n').length;
|
|
|
| if (lines > 50) {
|
| longFunctions.push({
|
| name: funcName,
|
| lines: lines,
|
| severity: lines > 100 ? 'HIGH' : 'MEDIUM'
|
| });
|
| }
|
| }
|
| }
|
|
|
| return longFunctions;
|
| }
|
|
|
| detectDeepNesting(content) {
|
| const lines = content.split('\n');
|
| const deepNesting = [];
|
|
|
| lines.forEach((line, index) => {
|
| const indentLevel = line.search(/\S/) / 2;
|
| if (indentLevel > 5) {
|
| deepNesting.push({
|
| line: index + 1,
|
| level: Math.floor(indentLevel),
|
| severity: indentLevel > 8 ? 'HIGH' : 'MEDIUM'
|
| });
|
| }
|
| });
|
|
|
| return deepNesting;
|
| }
|
|
|
| detectMagicNumbers(content) {
|
| const magicNumbers = [];
|
| const numberPattern = /(?<![a-zA-Z0-9_])([0-9]+)(?![a-zA-Z0-9_])/g;
|
|
|
| let match;
|
| while ((match = numberPattern.exec(content)) !== null) {
|
| const num = parseInt(match[1]);
|
|
|
| if (num > 1 && num !== 100 && num !== 1000) {
|
| const lineNumber = content.substring(0, match.index).split('\n').length;
|
| magicNumbers.push({
|
| number: num,
|
| line: lineNumber
|
| });
|
| }
|
| }
|
|
|
| return magicNumbers.slice(0, 10);
|
| }
|
| }
|
|
|
| module.exports = CodeQualityScanner;
|
|
|