/** * CodeContextAnalyzer - Deep code analysis to understand project context * Reads actual code content line-by-line to extract meaningful context */ class CodeContextAnalyzer { constructor() { this.version = '1.0.0'; } /** * Analyze code context deeply * @param {Array} files - All files with content * @returns {Object} Deep analysis */ analyze(files) { const todoComments = this.extractTodoComments(files); const bugMarkers = this.extractBugMarkers(files); const checkboxes = this.extractCheckboxes(files); const migrationPatterns = this.detectMigrationPatterns(files); const updateNotes = this.extractUpdateNotes(files); const codeIssues = this.detectCodeIssues(files); return { todoComments, bugMarkers, checkboxes, migrationPatterns, updateNotes, codeIssues, summary: this.generateContextSummary({ todoComments, bugMarkers, checkboxes, migrationPatterns, updateNotes, codeIssues }) }; } /** * Extract TODO comments from code */ extractTodoComments(files) { const todos = []; const patterns = { todo: /\/\/\s*TODO:?\s*(.+)|\/\*\s*TODO:?\s*(.+?)\*\//gi, fixme: /\/\/\s*FIXME:?\s*(.+)|\/\*\s*FIXME:?\s*(.+?)\*\//gi, hack: /\/\/\s*HACK:?\s*(.+)|\/\*\s*HACK:?\s*(.+?)\*\//gi, note: /\/\/\s*NOTE:?\s*(.+)|\/\*\s*NOTE:?\s*(.+?)\*\//gi }; files.forEach(file => { if (!file.content) return; const lines = file.content.split('\n'); lines.forEach((line, index) => { for (const [type, pattern] of Object.entries(patterns)) { const match = pattern.exec(line); if (match) { todos.push({ type: type.toUpperCase(), message: (match[1] || match[2] || '').trim(), filePath: file.filePath, line: index + 1, priority: this.determineTodoPriority(type, match[1] || match[2]) }); } pattern.lastIndex = 0; // Reset regex } }); }); return todos; } /** * Determine TODO priority from content */ determineTodoPriority(type, message) { if (type === 'FIXME') return 'HIGH'; if (type === 'HACK') return 'HIGH'; const text = message.toLowerCase(); if (text.includes('critical') || text.includes('urgent') || text.includes('asap')) return 'CRITICAL'; if (text.includes('important') || text.includes('must')) return 'HIGH'; if (text.includes('should') || text.includes('consider')) return 'MEDIUM'; return 'MEDIUM'; } /** * Extract bug markers from code */ extractBugMarkers(files) { const bugs = []; const patterns = { bug: /\/\/\s*BUG[-:]?\s*(.+)|\/\*\s*BUG[-:]?\s*(.+?)\*\//gi, issue: /\/\/\s*ISSUE[-:]?\s*(.+)|\/\*\s*ISSUE[-:]?\s*(.+?)\*\//gi, broken: /\/\/\s*BROKEN:?\s*(.+)|\/\*\s*BROKEN:?\s*(.+?)\*\//gi, error: /\/\/\s*ERROR:?\s*(.+)|\/\*\s*ERROR:?\s*(.+?)\*\//gi }; files.forEach(file => { if (!file.content) return; const lines = file.content.split('\n'); lines.forEach((line, index) => { for (const [type, pattern] of Object.entries(patterns)) { const match = pattern.exec(line); if (match) { bugs.push({ type: type.toUpperCase(), message: (match[1] || match[2] || '').trim(), filePath: file.filePath, line: index + 1, severity: this.determineBugSeverity(match[1] || match[2]) }); } pattern.lastIndex = 0; } }); }); return bugs; } /** * Determine bug severity from message */ determineBugSeverity(message) { const text = message.toLowerCase(); if (text.includes('critical') || text.includes('crash') || text.includes('data loss')) return 'CRITICAL'; if (text.includes('security') || text.includes('vulnerability')) return 'CRITICAL'; if (text.includes('major') || text.includes('important')) return 'HIGH'; if (text.includes('minor') || text.includes('cosmetic')) return 'LOW'; return 'MEDIUM'; } /** * Extract checkboxes from markdown files */ extractCheckboxes(files) { const checkboxes = { completed: [], pending: [], total: 0, completionRate: 0 }; const completedPattern = /^\s*-\s*\[x\]\s*(.+)/gi; const pendingPattern = /^\s*-\s*\[\s\]\s*(.+)/gi; files.filter(f => f.filePath.endsWith('.md')).forEach(file => { if (!file.content) return; const lines = file.content.split('\n'); lines.forEach((line, index) => { let match; // Check for completed completedPattern.lastIndex = 0; if ((match = completedPattern.exec(line))) { checkboxes.completed.push({ text: match[1].trim(), filePath: file.filePath, line: index + 1 }); } // Check for pending pendingPattern.lastIndex = 0; if ((match = pendingPattern.exec(line))) { checkboxes.pending.push({ text: match[1].trim(), filePath: file.filePath, line: index + 1 }); } }); }); checkboxes.total = checkboxes.completed.length + checkboxes.pending.length; checkboxes.completionRate = checkboxes.total > 0 ? Math.round((checkboxes.completed.length / checkboxes.total) * 100) : 0; return checkboxes; } /** * Detect migration patterns in code */ detectMigrationPatterns(files) { const migrations = []; const patterns = { migrationComment: /\/\/\s*MIGRATION:?\s*(.+)|\/\*\s*MIGRATION:?\s*(.+?)\*\//gi, legacyCode: /\/\/\s*LEGACY:?\s*(.+)|\/\*\s*LEGACY:?\s*(.+?)\*\//gi, deprecated: /\/\/\s*DEPRECATED:?\s*(.+)|\/\*\s*DEPRECATED:?\s*(.+?)\*\//gi, newCode: /\/\/\s*NEW:?\s*(.+)|\/\*\s*NEW:?\s*(.+?)\*\//gi }; files.forEach(file => { if (!file.content) return; const lines = file.content.split('\n'); lines.forEach((line, index) => { for (const [type, pattern] of Object.entries(patterns)) { const match = pattern.exec(line); if (match) { migrations.push({ type: type.toUpperCase(), message: (match[1] || match[2] || '').trim(), filePath: file.filePath, line: index + 1, status: this.determineMigrationStatus(type) }); } pattern.lastIndex = 0; } }); }); return migrations; } /** * Determine migration status */ determineMigrationStatus(type) { if (type === 'LEGACY' || type === 'DEPRECATED') return 'NEEDS_MIGRATION'; if (type === 'NEW') return 'MIGRATED'; return 'IN_PROGRESS'; } /** * Extract update notes from markdown */ extractUpdateNotes(files) { const updates = []; const updatePattern = />\s*\*\*Update\s+(\d{1,2}\s+\w+\s+\d{4})[:\*\s]+(.+?)(?=\n\n|$)/gis; files.filter(f => f.filePath.endsWith('.md')).forEach(file => { if (!file.content) return; let match; while ((match = updatePattern.exec(file.content)) !== null) { updates.push({ date: match[1].trim(), content: match[2].trim().substring(0, 200), // First 200 chars filePath: file.filePath }); } }); return updates.sort((a, b) => { const dateA = new Date(a.date); const dateB = new Date(b.date); return dateB - dateA; // Newest first }); } /** * Detect code issues from patterns */ detectCodeIssues(files) { const issues = []; const issuePatterns = { emptyFunction: /function\s+\w+\s*\([^)]*\)\s*\{\s*\}/g, emptyClass: /class\s+\w+\s*\{\s*\}/g, console: /console\.(log|warn|error|debug)/g, debugger: /debugger;?/g, throwNotImplemented: /throw\s+new\s+Error\s*\(\s*['"`]not\s+implemented/gi }; files.filter(f => f.filePath.match(/\.(js|ts|jsx|tsx)$/)).forEach(file => { if (!file.content) return; const lines = file.content.split('\n'); for (const [issueType, pattern] of Object.entries(issuePatterns)) { let match; pattern.lastIndex = 0; while ((match = pattern.exec(file.content)) !== null) { const lineNumber = file.content.substring(0, match.index).split('\n').length; issues.push({ type: issueType, code: match[0], filePath: file.filePath, line: lineNumber, severity: this.determineIssueSeverity(issueType) }); } } }); return issues; } /** * Determine issue severity */ determineIssueSeverity(issueType) { if (issueType === 'throwNotImplemented') return 'HIGH'; if (issueType === 'emptyFunction' || issueType === 'emptyClass') return 'MEDIUM'; if (issueType === 'debugger') return 'HIGH'; if (issueType === 'console') return 'LOW'; return 'MEDIUM'; } /** * Generate context summary */ generateContextSummary(context) { return { totalTodos: context.todoComments.length, criticalTodos: context.todoComments.filter(t => t.priority === 'CRITICAL').length, totalBugs: context.bugMarkers.length, criticalBugs: context.bugMarkers.filter(b => b.severity === 'CRITICAL').length, checkboxCompletion: context.checkboxes.completionRate, totalCheckboxes: context.checkboxes.total, completedCheckboxes: context.checkboxes.completed.length, migrationItems: context.migrationPatterns.length, needsMigration: context.migrationPatterns.filter(m => m.status === 'NEEDS_MIGRATION').length, updateNotes: context.updateNotes.length, latestUpdate: context.updateNotes[0]?.date || 'Unknown', codeIssues: context.codeIssues.length, criticalIssues: context.codeIssues.filter(i => i.severity === 'HIGH' || i.severity === 'CRITICAL').length }; } } module.exports = CodeContextAnalyzer;