| #!/usr/bin/env node |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| const fs = require('fs'); |
| const path = require('path'); |
|
|
| |
| |
| |
|
|
| class ProfessionalLogger { |
| constructor() { |
| this.projectName = path.basename(process.cwd()); |
| this.logsDir = path.join(process.cwd(), 'logs'); |
|
|
| |
| if (!fs.existsSync(this.logsDir)) { |
| fs.mkdirSync(this.logsDir, { recursive: true }); |
| } |
|
|
| |
| this.projectLogsDir = path.join(this.logsDir, this.projectName); |
| if (!fs.existsSync(this.projectLogsDir)) { |
| fs.mkdirSync(this.projectLogsDir, { recursive: true }); |
| } |
|
|
| |
| const now = new Date(); |
| const dateFolder = now.toISOString().slice(0, 10); |
| const timeFolder = now.toTimeString().slice(0, 8).replace(/:/g, '-'); |
| this.sessionFolder = `${dateFolder}_${timeFolder}`; |
| this.sessionLogsDir = path.join(this.projectLogsDir, this.sessionFolder); |
|
|
| if (!fs.existsSync(this.sessionLogsDir)) { |
| fs.mkdirSync(this.sessionLogsDir, { recursive: true }); |
| } |
|
|
| this.logFiles = { |
| error: path.join(this.sessionLogsDir, 'error.log'), |
| debug: path.join(this.sessionLogsDir, 'debug.log'), |
| audit: path.join(this.sessionLogsDir, 'audit.log'), |
| performance: path.join(this.sessionLogsDir, 'performance.log'), |
| diagnostic: path.join(this.sessionLogsDir, 'diagnostic.log') |
| }; |
|
|
| |
| this.writeSessionHeader(); |
| } |
|
|
| writeSessionHeader() { |
| const timestamp = new Date().toISOString(); |
| const header = `\n${'='.repeat(80)}\nSESSION START: ${timestamp} | Project: ${this.projectName}\n${'='.repeat(80)}\n`; |
|
|
| Object.values(this.logFiles).forEach(logFile => { |
| fs.appendFileSync(logFile, header, 'utf8'); |
| }); |
| } |
|
|
| formatLogEntry(level, category, message, data = null) { |
| const timestamp = new Date().toISOString(); |
| let entry = `[${timestamp}] [${level.toUpperCase()}] [${category}] ${message}`; |
|
|
| if (data) { |
| entry += `\n Data: ${JSON.stringify(data, null, 2)}`; |
| } |
|
|
| entry += '\n'; |
| return entry; |
| } |
|
|
| error(category, message, error = null, data = null) { |
| const logData = { ...data }; |
| if (error) { |
| logData.error = { |
| message: error.message, |
| stack: error.stack, |
| name: error.name |
| }; |
| } |
|
|
| const entry = this.formatLogEntry('ERROR', category, message, logData); |
| fs.appendFileSync(this.logFiles.error, entry, 'utf8'); |
|
|
| |
| console.error(`[ERROR] ${category}: ${message}`); |
| } |
|
|
| debug(category, message, data = null) { |
| const entry = this.formatLogEntry('DEBUG', category, message, data); |
| fs.appendFileSync(this.logFiles.debug, entry, 'utf8'); |
| } |
|
|
| audit(action, filePath, details = null) { |
| const entry = this.formatLogEntry('AUDIT', 'FILE_OPERATION', |
| `${action}: ${filePath}`, details); |
| fs.appendFileSync(this.logFiles.audit, entry, 'utf8'); |
| } |
|
|
| performance(operation, duration, details = null) { |
| const entry = this.formatLogEntry('PERFORMANCE', operation, |
| `Duration: ${duration}ms`, details); |
| fs.appendFileSync(this.logFiles.performance, entry, 'utf8'); |
| } |
|
|
| info(category, message, data = null) { |
| const entry = this.formatLogEntry('INFO', category, message, data); |
| fs.appendFileSync(this.logFiles.debug, entry, 'utf8'); |
| } |
|
|
| |
| diagnostic(category, message, data = null) { |
| const entry = this.formatLogEntry('DIAGNOSTIC', category, message, data); |
|
|
| |
| fs.appendFileSync(this.logFiles.audit, entry, 'utf8'); |
| fs.appendFileSync(this.logFiles.debug, `\n=== DIAGNOSTIC REPORT ===\n${entry}=== END DIAGNOSTIC ===\n`, 'utf8'); |
|
|
| |
| if (!fs.existsSync(this.logFiles.diagnostic)) { |
| const header = `DIAGNOSTIC REPORTS LOG - ${new Date().toISOString()}\nProject: ${this.projectName} | Session: ${this.sessionFolder}\n${'='.repeat(80)}\n\n`; |
| fs.writeFileSync(this.logFiles.diagnostic, header, 'utf8'); |
| } |
| fs.appendFileSync(this.logFiles.diagnostic, entry, 'utf8'); |
| } |
| } |
|
|
| |
| const logger = new ProfessionalLogger(); |
|
|
| |
| |
| |
|
|
| class OrganizedBackupManager { |
| constructor() { |
| this.projectName = path.basename(process.cwd()); |
| this.backupsDir = path.join(process.cwd(), '.backups'); |
| this.projectBackupDir = path.join(this.backupsDir, this.projectName); |
|
|
| |
| this.ensureBackupDirectories(); |
| } |
|
|
| |
| |
| |
| |
| ensureBackupDirectories() { |
| if (!fs.existsSync(this.backupsDir)) { |
| fs.mkdirSync(this.backupsDir, { recursive: true }); |
| logger.audit('CREATE_BACKUP_DIR', this.backupsDir); |
| } |
|
|
| if (!fs.existsSync(this.projectBackupDir)) { |
| fs.mkdirSync(this.projectBackupDir, { recursive: true }); |
| logger.audit('CREATE_PROJECT_BACKUP_DIR', this.projectBackupDir); |
| } |
| } |
|
|
| createBackup(originalFilePath) { |
| try { |
| |
| if (!fs.existsSync(originalFilePath)) { |
| throw new Error(`Original file does not exist: ${originalFilePath}`); |
| } |
|
|
| |
| const now = new Date(); |
| const dateFolder = now.toISOString().slice(0, 10); |
| const timeFolder = now.toTimeString().slice(0, 8).replace(/:/g, '-'); |
| const sessionFolder = `${dateFolder}_${timeFolder}`; |
|
|
| |
| const sessionBackupDir = path.join(this.projectBackupDir, sessionFolder); |
| if (!fs.existsSync(sessionBackupDir)) { |
| fs.mkdirSync(sessionBackupDir, { recursive: true }); |
| logger.audit('CREATE_SESSION_BACKUP_DIR', sessionBackupDir); |
| } |
|
|
| |
| const originalFileName = path.basename(originalFilePath); |
| const backupFilePath = path.join(sessionBackupDir, originalFileName); |
|
|
| |
| const originalContent = fs.readFileSync(originalFilePath, 'utf8'); |
| fs.writeFileSync(backupFilePath, originalContent, 'utf8'); |
|
|
| |
| logger.audit('BACKUP_CREATED', originalFilePath, { |
| backupPath: backupFilePath, |
| sessionFolder: sessionFolder, |
| originalSize: originalContent.length, |
| originalFileName: originalFileName |
| }); |
|
|
| logger.info('BACKUP', `Original file backed up to: ${sessionFolder}/${originalFileName}`); |
|
|
| return backupFilePath; |
|
|
| } catch (error) { |
| logger.error('BACKUP', `Failed to create backup for ${originalFilePath}`, error); |
| throw error; |
| } |
| } |
|
|
| cleanupOldBackups(maxAge = 7) { |
| try { |
| const sessionFolders = fs.readdirSync(this.projectBackupDir); |
| const cutoffTime = Date.now() - (maxAge * 24 * 60 * 60 * 1000); |
|
|
| let cleanedCount = 0; |
|
|
| sessionFolders.forEach(sessionFolder => { |
| const sessionPath = path.join(this.projectBackupDir, sessionFolder); |
| const stats = fs.statSync(sessionPath); |
|
|
| |
| if (stats.isDirectory() && stats.mtime.getTime() < cutoffTime) { |
| |
| fs.rmSync(sessionPath, { recursive: true, force: true }); |
| cleanedCount++; |
| logger.audit('BACKUP_SESSION_CLEANED', sessionPath, { |
| sessionFolder: sessionFolder, |
| age: maxAge |
| }); |
| } |
| }); |
|
|
| if (cleanedCount > 0) { |
| logger.info('BACKUP', `Cleaned up ${cleanedCount} old backup sessions`); |
| } |
|
|
| } catch (error) { |
| logger.error('BACKUP', 'Failed to cleanup old backups', error); |
| } |
| } |
| } |
|
|
| |
| |
| |
| class FileComparisonAnalyzer { |
| constructor() { |
| this.comparisonResults = []; |
| } |
|
|
| |
| |
| |
| |
| |
| compareAndAnalyze(originalContent, modifiedContent, filePath) { |
| try { |
| const comparisonId = Date.now(); |
| logger.debug('FILE_COMPARISON', `Starting comparison analysis for: ${filePath}`, { comparisonId }); |
|
|
| |
| const originalAnalysis = this.analyzeFileStructures(originalContent, 'ORIGINAL'); |
| logger.debug('FILE_ANALYSIS', `Original file analysis completed`, { |
| filePath, |
| structures: originalAnalysis.summary |
| }); |
|
|
| |
| const modifiedAnalysis = this.analyzeFileStructures(modifiedContent, 'MODIFIED'); |
| logger.debug('FILE_ANALYSIS', `Modified file analysis completed`, { |
| filePath, |
| structures: modifiedAnalysis.summary |
| }); |
|
|
| |
| const comparisonReport = this.generateComparisonReport( |
| originalAnalysis, |
| modifiedAnalysis, |
| filePath |
| ); |
|
|
| |
| this.logDetailedReport(comparisonReport, filePath); |
|
|
| |
| this.logErrorAndSkipReport(comparisonReport, filePath); |
|
|
| this.comparisonResults.push(comparisonReport); |
|
|
| return comparisonReport; |
|
|
| } catch (error) { |
| logger.error('FILE_COMPARISON', `Failed to compare files: ${filePath}`, error); |
| return null; |
| } |
| } |
|
|
| |
| analyzeFileStructures(content, type) { |
| try { |
| |
| const analyzer = new SmartFileAnalyzer(content, { |
| maxDepth: 50, |
| maxTokens: 100000, |
| maxParsingTime: 15000 |
| }); |
|
|
| const analysis = analyzer.analyzeFile(); |
|
|
| |
| const summary = { |
| totalFunctions: analysis.functions ? analysis.functions.length : 0, |
| totalClasses: analysis.classes ? analysis.classes.length : 0, |
| totalMethods: analysis.methods ? analysis.methods.length : 0, |
| totalVariables: analysis.variables ? analysis.variables.length : 0, |
| totalInterfaces: 0, |
| totalTypeAliases: 0, |
| totalAbstractClasses: 0, |
| totalStaticMethods: 0, |
| totalArrowFunctions: 0 |
| }; |
|
|
| |
| if (analysis.interfaces) summary.totalInterfaces = analysis.interfaces.length; |
| if (analysis.typeAliases) summary.totalTypeAliases = analysis.typeAliases.length; |
| if (analysis.abstractClasses) summary.totalAbstractClasses = analysis.abstractClasses.length; |
| if (analysis.staticMethods) summary.totalStaticMethods = analysis.staticMethods.length; |
| if (analysis.arrowFunctions) summary.totalArrowFunctions = analysis.arrowFunctions.length; |
|
|
| |
| const functionsWithoutComments = this.findFunctionsWithoutComments(content, analysis.functions || []); |
| const classesWithoutComments = this.findClassesWithoutComments(content, analysis.classes || []); |
|
|
| return { |
| type, |
| analysis, |
| summary, |
| functionsWithoutComments, |
| classesWithoutComments, |
| totalStructures: Object.values(summary).reduce((a, b) => a + b, 0) |
| }; |
|
|
| } catch (error) { |
| logger.error('STRUCTURE_ANALYSIS', `Failed to analyze ${type} content`, error); |
| return { type, error: error.message, summary: {}, totalStructures: 0 }; |
| } |
| } |
|
|
| |
| findFunctionsWithoutComments(content, filePath = '') { |
| const without = []; |
| const lines = normalizeLineEndings(content).split('\n'); |
| const detectedSet = new Set(); |
| |
| try { |
| |
| const tokenizer = new JavaScriptTokenizer(content); |
| const tokens = tokenizer.tokenize(); |
| |
| |
| const parser = new StateMachineStructureParser(tokens, content); |
| const structures = parser.parse(); |
| |
| |
| const allItems = [ |
| ...structures.functions, |
| ...structures.arrowFunctions, |
| ...structures.methods, |
| ...structures.classes, |
| ...structures.reactComponents, |
| ...structures.interfaces, |
| ...structures.typeAliases, |
| ...structures.enums |
| ]; |
| |
| |
| allItems.forEach(item => { |
| const key = `${item.name}@${item.line}`; |
| detectedSet.add(key); |
| |
| if (this.hasCommentAbove(lines, item.line - 1)) return; |
| without.push(item); |
| }); |
| |
| } catch (e) { |
| |
| console.warn('StateMachineStructureParser error, using fallback mode:', e.message); |
| |
| |
| const funcRegex = /^\s*(?:export\s+)?(?:async\s+)?function\s+(\w+)/gm; |
| let match; |
| while ((match = funcRegex.exec(content)) !== null) { |
| const name = match[1]; |
| const lineNum = content.substring(0, match.index).split('\n').length; |
| const key = `${name}@${lineNum}`; |
| if (detectedSet.has(key)) continue; |
| |
| if (this.hasCommentAbove(lines, lineNum - 1)) continue; |
| without.push({ type: 'function', name, line: lineNum }); |
| detectedSet.add(key); |
| } |
| |
| |
| const arrowRegex = /^\s*(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s*)?\([^)]*\)\s*=>/gm; |
| while ((match = arrowRegex.exec(content)) !== null) { |
| const name = match[1]; |
| const lineNum = content.substring(0, match.index).split('\n').length; |
| const key = `${name}@${lineNum}`; |
| if (detectedSet.has(key)) continue; |
| |
| if (this.hasCommentAbove(lines, lineNum - 1)) continue; |
| without.push({ type: 'arrow_function', name, line: lineNum }); |
| detectedSet.add(key); |
| } |
| } |
| |
| return without; |
| } |
|
|
| |
| hasCommentAbove(lines, lineIndex) { |
| for (let i = 1; i <= 7; i++) { |
| if (lineIndex - i < 0) break; |
| const prev = (lines[lineIndex - i] || '').trim(); |
| if (prev.startsWith('//') || prev.startsWith('/**') || prev.startsWith('/*') || prev.startsWith('*')) { |
| return true; |
| } |
| if (prev && !prev.startsWith('//') && !prev.startsWith('/*')) break; |
| } |
| return false; |
| } |
|
|
| |
| findClassesWithoutComments(content, classes) { |
| const withoutComments = []; |
| const lines = content.split('\n'); |
|
|
| classes.forEach(cls => { |
| const classLine = cls.line - 1; |
|
|
| |
| if (this.isLikelyVariable(cls.name, content, classLine)) { |
| return; |
| } |
|
|
| |
| if (this.isReactComponent(cls.name, content, classLine)) { |
| return; |
| } |
|
|
| let hasComment = false; |
| for (let i = 1; i <= 3; i++) { |
| const checkLine = classLine - i; |
| if (checkLine >= 0 && lines[checkLine]) { |
| const line = lines[checkLine].trim(); |
| if (line.startsWith('//') || line.startsWith('/*') || line.startsWith('*')) { |
| hasComment = true; |
| break; |
| } |
| } |
| } |
|
|
| if (!hasComment) { |
| withoutComments.push({ |
| name: cls.name, |
| line: cls.line, |
| type: cls.type || 'class' |
| }); |
| } |
| }); |
|
|
| return withoutComments; |
| } |
|
|
| |
| isLikelyVariable(name, content, lineIndex) { |
| const lines = content.split('\n'); |
| if (lineIndex < 0 || lineIndex >= lines.length) return false; |
|
|
| const currentLine = lines[lineIndex].trim(); |
| const lowerName = name.toLowerCase(); |
|
|
| |
| const escapedName = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); |
|
|
| |
| const variablePatterns = [ |
| |
| new RegExp(`\\b(const|let|var)\\s+${escapedName}\\s*=`), |
| new RegExp(`\\b${escapedName}\\s*=\\s*`), |
|
|
| |
| new RegExp(`function\\s*\\([^)]*\\b${escapedName}\\b[^)]*\\)`), |
| new RegExp(`\\([^)]*\\b${escapedName}\\b[^)]*\\)\\s*=>`), |
|
|
| |
| new RegExp(`{[^}]*\\b${escapedName}\\b[^}]*}`), |
| new RegExp(`\\[[^\\]]*\\b${escapedName}\\b[^\\]]*\\]`), |
|
|
| |
| new RegExp(`for\\s*\\([^;]*\\b${escapedName}\\b`), |
|
|
| |
| new RegExp(`\\b${escapedName}\\s*=\\s*createContext`), |
| new RegExp(`\\bconst\\s+${escapedName}\\s*=\\s*createContext`), |
|
|
| |
| new RegExp(`\\b${escapedName}\\s*=\\s*use[A-Z]`), |
| new RegExp(`\\bconst\\s+\\[.*${escapedName}.*\\]\\s*=\\s*useState`), |
|
|
| |
| new RegExp(`\\bconst\\s+${escapedName}\\s*=\\s*\\(`), |
| new RegExp(`\\bconst\\s+${escapedName}\\s*=\\s*\\w+\\s*=>`), |
| ]; |
|
|
| |
| for (let i = Math.max(0, lineIndex - 3); i <= Math.min(lines.length - 1, lineIndex + 3); i++) { |
| const line = lines[i]; |
| if (variablePatterns.some(pattern => pattern.test(line))) { |
| return true; |
| } |
| } |
|
|
| |
| const commonVariableNames = [ |
| 'result', 'data', 'item', 'value', 'temp', 'node', 'element', |
| 'current', 'next', 'prev', 'parent', 'child', 'left', 'right', |
| 'count', 'index', 'length', 'size', 'max', 'min', 'sum', |
| 'start', 'end', 'pos', 'queue', 'stack', 'list', 'array', |
| 'map', 'set', 'key', 'val', 'obj', 'target', 'source', |
| 'input', 'output', 'buffer', 'cache', 'config', 'options', |
| |
| 'dp', 'memo', 'visited', 'distances', 'graph', 'matrix', |
| 'heap', 'buckets', 'intervals', 'points', 'edges', 'vertices', |
| |
| 'user', 'users', 'state', 'props', 'context', 'ref', 'refs', |
| 'handler', 'handlers', 'callback', 'callbacks', 'event', 'events', |
| 'timeout', 'interval', 'timer', 'loading', 'error', 'success' |
| ]; |
|
|
| if (commonVariableNames.includes(lowerName)) { |
| return true; |
| } |
|
|
| |
| if (name.endsWith('Context') && /^[A-Z][a-zA-Z]*Context$/.test(name)) { |
| return true; |
| } |
|
|
| |
| if (/^[A-Z][a-zA-Z0-9]*$/.test(name)) { |
| |
| const componentPattern = new RegExp(`const\\s+${name}\\s*=`); |
| for (let i = Math.max(0, lineIndex - 2); i <= Math.min(lines.length - 1, lineIndex + 2); i++) { |
| if (componentPattern.test(lines[i])) { |
| return true; |
| } |
| } |
| } |
|
|
| |
| if (name.length <= 2 && /^[a-zA-Z][0-9]?$/.test(name)) { |
| return true; |
| } |
|
|
| |
| if (/^[a-z][a-zA-Z0-9]*$/.test(name)) { |
| return true; |
| } |
|
|
| return false; |
| } |
|
|
| |
| isReactComponent(name, content, lineIndex) { |
| const lines = content.split('\n'); |
| if (lineIndex < 0 || lineIndex >= lines.length) return false; |
|
|
| |
| if (!/^[A-Z][a-zA-Z0-9]*$/.test(name)) return false; |
|
|
| |
| const reactComponentPatterns = [ |
| |
| new RegExp(`const\\s+${name}\\s*=\\s*\\([^)]*\\)\\s*=>`), |
| |
| new RegExp(`const\\s+${name}\\s*=\\s*function`), |
| |
| new RegExp(`const\\s+${name}\\s*=\\s*React\\.memo`), |
| |
| new RegExp(`const\\s+${name}\\s*=\\s*forwardRef`), |
| |
| new RegExp(`export\\s+const\\s+${name}\\s*=`), |
| ]; |
|
|
| |
| for (let i = Math.max(0, lineIndex - 2); i <= Math.min(lines.length - 1, lineIndex + 2); i++) { |
| const line = lines[i]; |
| if (reactComponentPatterns.some(pattern => pattern.test(line))) { |
| return true; |
| } |
| } |
|
|
| |
| const jsxPatterns = [ |
| /return\s*\(/, |
| /return\s*</, |
| />\s*$/, |
| /<\/[a-zA-Z]/ |
| ]; |
|
|
| |
| for (let i = lineIndex; i < Math.min(lines.length, lineIndex + 20); i++) { |
| const line = lines[i].trim(); |
| if (jsxPatterns.some(pattern => pattern.test(line))) { |
| return true; |
| } |
| |
| if (line.includes('}') && !line.includes('{')) { |
| break; |
| } |
| } |
|
|
| return false; |
| } |
|
|
| |
| generateComparisonReport(originalAnalysis, modifiedAnalysis, filePath) { |
| const report = { |
| filePath, |
| timestamp: new Date().toISOString(), |
| original: originalAnalysis, |
| modified: modifiedAnalysis, |
| changes: { |
| functionsAdded: modifiedAnalysis.summary.totalFunctions - originalAnalysis.summary.totalFunctions, |
| classesAdded: modifiedAnalysis.summary.totalClasses - originalAnalysis.summary.totalClasses, |
| commentsAdded: 0, |
| structuresSkipped: (modifiedAnalysis.functionsWithoutComments?.length || 0) + (modifiedAnalysis.classesWithoutComments?.length || 0), |
| errors: [] |
| }, |
| skippedStructures: { |
| functions: modifiedAnalysis.functionsWithoutComments || [], |
| classes: modifiedAnalysis.classesWithoutComments || [] |
| } |
| }; |
|
|
| |
| report.changes.commentsAdded = this.calculateCommentsAdded(originalAnalysis, modifiedAnalysis); |
|
|
| return report; |
| } |
|
|
| |
| calculateCommentsAdded(original, modified) { |
| const originalCommentLines = this.countCommentLines(original.analysis?.content || ''); |
| const modifiedCommentLines = this.countCommentLines(modified.analysis?.content || ''); |
| return Math.max(0, modifiedCommentLines - originalCommentLines); |
| } |
|
|
| |
| countCommentLines(content) { |
| const lines = content.split('\n'); |
| return lines.filter(line => { |
| const trimmed = line.trim(); |
| return trimmed.startsWith('//') || trimmed.startsWith('/*') || trimmed.startsWith('*'); |
| }).length; |
| } |
|
|
| |
| logDetailedReport(report, filePath) { |
| logger.audit('FILE_COMPARISON_REPORT', filePath, { |
| summary: { |
| totalStructuresOriginal: report.original.totalStructures, |
| totalStructuresModified: report.modified.totalStructures, |
| functionsAdded: report.changes.functionsAdded, |
| classesAdded: report.changes.classesAdded, |
| commentsAdded: report.changes.commentsAdded, |
| structuresSkipped: report.changes.structuresSkipped |
| }, |
| originalStructures: report.original.summary, |
| modifiedStructures: report.modified.summary |
| }); |
| } |
|
|
| |
| logErrorAndSkipReport(report, filePath) { |
| if (report.changes.structuresSkipped > 0) { |
| |
| const analysisResult = this.analyzeSkippedPatterns(report, filePath); |
|
|
| logger.error('STRUCTURES_SKIPPED', |
| `${report.changes.structuresSkipped} structures were skipped in ${filePath}`, |
| null, |
| { |
| skippedFunctions: report.skippedStructures.functions.map(f => `${f.name} (line ${f.line})`), |
| skippedClasses: report.skippedStructures.classes.map(c => `${c.name} (line ${c.line})`), |
| totalSkipped: report.changes.structuresSkipped, |
| reasons: 'Functions/Classes without comments detected', |
| patternAnalysis: analysisResult.patterns, |
| suggestedSolutions: analysisResult.solutions, |
| codeExamples: analysisResult.examples |
| } |
| ); |
|
|
| |
| this.generateDetailedDiagnosticReport(report, filePath, analysisResult); |
| } |
|
|
| |
| logger.performance('FILE_PROCESSING_SUMMARY', 0, { |
| filePath, |
| success: true, |
| totalStructures: report.modified.totalStructures, |
| commentsAdded: report.changes.commentsAdded, |
| functionsSkipped: report.skippedStructures.functions.length, |
| classesSkipped: report.skippedStructures.classes.length, |
| errorCount: report.changes.errors.length |
| }); |
| } |
|
|
| |
| analyzeSkippedPatterns(report, filePath) { |
| const analysis = { |
| patterns: { |
| variableDeclarations: 0, |
| shortVariableNames: 0, |
| nestedStructures: 0, |
| generatedCode: 0, |
| utilityVariables: 0 |
| }, |
| solutions: [], |
| examples: [] |
| }; |
|
|
| |
| report.skippedStructures.functions.forEach(func => { |
| if (func.name.length <= 2) { |
| analysis.patterns.shortVariableNames++; |
| analysis.solutions.push(`Short variable name detected: "${func.name}" - Consider using descriptive names`); |
| } |
|
|
| if (['i', 'j', 'k', 'x', 'y', 'z', 'n', 'm'].includes(func.name)) { |
| analysis.patterns.utilityVariables++; |
| analysis.solutions.push(`Loop/utility variable detected: "${func.name}" - This is likely a loop counter or temporary variable`); |
| } |
| }); |
|
|
| |
| report.skippedStructures.classes.forEach(cls => { |
| if (cls.name.length <= 2) { |
| analysis.patterns.shortVariableNames++; |
| analysis.solutions.push(`Short variable name detected: "${cls.name}" - This appears to be a variable, not a class`); |
| } |
|
|
| if (['temp', 'result', 'data', 'item', 'node', 'value'].includes(cls.name.toLowerCase())) { |
| analysis.patterns.variableDeclarations++; |
| analysis.solutions.push(`Variable declaration detected: "${cls.name}" - This is likely a variable assignment`); |
| } |
|
|
| if (cls.name.match(/^[a-z][a-zA-Z]*$/)) { |
| analysis.patterns.variableDeclarations++; |
| analysis.solutions.push(`Camel case variable detected: "${cls.name}" - This follows variable naming convention`); |
| } |
| }); |
|
|
| |
| if (analysis.patterns.variableDeclarations > 0) { |
| analysis.examples.push({ |
| type: 'Variable Declaration Fix', |
| problem: 'const result = someFunction();', |
| solution: '// Calculate processing result\nconst result = someFunction();' |
| }); |
| } |
|
|
| if (analysis.patterns.shortVariableNames > 0) { |
| analysis.examples.push({ |
| type: 'Short Variable Name Fix', |
| problem: 'let i = 0;', |
| solution: '// Loop counter for iteration\nlet i = 0;' |
| }); |
| } |
|
|
| return analysis; |
| } |
|
|
| |
| generateDetailedDiagnosticReport(report, filePath, analysisResult) { |
| const diagnosticData = { |
| filePath: filePath, |
| timestamp: new Date().toISOString(), |
| issuesSummary: { |
| totalIssues: report.changes.structuresSkipped, |
| functionIssues: report.skippedStructures.functions.length, |
| classIssues: report.skippedStructures.classes.length, |
| mainCauses: this.identifyMainCauses(analysisResult.patterns, report) |
| }, |
| detailedAnalysis: { |
| likelyVariableDeclarations: analysisResult.patterns.variableDeclarations, |
| shortVariableNames: analysisResult.patterns.shortVariableNames, |
| utilityVariables: analysisResult.patterns.utilityVariables, |
| suggestedFixes: analysisResult.solutions.length |
| }, |
| actionableRecommendations: this.generateActionableRecommendations(report, analysisResult), |
| codeImprovementSuggestions: analysisResult.examples |
| }; |
|
|
| logger.diagnostic('STRUCTURE_ANALYSIS', |
| `Detailed diagnostic for ${path.basename(filePath)}`, |
| diagnosticData); |
| } |
|
|
| |
| identifyMainCauses(patterns, report) { |
| const causes = []; |
|
|
| if (patterns.variableDeclarations > 0) { |
| causes.push('Variable declarations misidentified as classes'); |
| } |
|
|
| if (patterns.shortVariableNames > 0) { |
| causes.push('Short variable names detected'); |
| } |
|
|
| if (patterns.utilityVariables > 0) { |
| causes.push('Loop counters and utility variables present'); |
| } |
|
|
| |
| const advancedPatterns = this.analyzeAdvancedPatterns(report.skippedStructures); |
|
|
|
|
|
|
| if (advancedPatterns.typeScriptPatterns > 0) { |
| causes.push('TypeScript advanced constructs (interfaces, types, generics)'); |
| } |
|
|
| if (advancedPatterns.reactPatterns > 0) { |
| causes.push('React patterns (hooks, components, context)'); |
| } |
|
|
| if (advancedPatterns.modernJsPatterns > 0) { |
| causes.push('Modern JavaScript patterns (destructuring, async/await)'); |
| } |
|
|
| if (advancedPatterns.classMethodPatterns > 0) { |
| causes.push('Advanced class methods and decorators'); |
| } |
|
|
| return causes.length > 0 ? causes : ['Unknown pattern - requires manual review']; |
| } |
|
|
| |
| analyzeAdvancedPatterns(skippedStructures) { |
| const patterns = { |
| typeScriptPatterns: 0, |
| reactPatterns: 0, |
| modernJsPatterns: 0, |
| classMethodPatterns: 0, |
| algorithmPatterns: 0 |
| }; |
|
|
| const allStructures = [ |
| ...(skippedStructures.functions || []), |
| ...(skippedStructures.classes || []) |
| ]; |
|
|
| allStructures.forEach(structure => { |
| const name = structure.name; |
| const lowerName = name.toLowerCase(); |
|
|
| |
| if (this.isTypeScriptPattern(name)) { |
| patterns.typeScriptPatterns++; |
| } |
|
|
| |
| if (this.isReactPattern(name)) { |
| patterns.reactPatterns++; |
| } |
|
|
| |
| if (this.isModernJsPattern(name)) { |
| patterns.modernJsPatterns++; |
| } |
|
|
| |
| if (this.isClassMethodPattern(name)) { |
| patterns.classMethodPatterns++; |
| } |
|
|
| |
| if (this.isAlgorithmPattern(name)) { |
| patterns.algorithmPatterns++; |
| } |
| }); |
|
|
| return patterns; |
| } |
|
|
| |
| isTypeScriptPattern(name) { |
| const tsPatterns = [ |
| /^[A-Z][a-zA-Z]*Options$/, |
| /^[A-Z][a-zA-Z]*Config$/, |
| /^[A-Z][a-zA-Z]*Handler$/, |
| /^[A-Z][a-zA-Z]*Listener$/, |
| /^[A-Z][a-zA-Z]*Result$/, |
| /^[A-Z][a-zA-Z]*Error$/, |
| /^[A-Z][a-zA-Z]*Stats$/, |
| /^[A-Z][a-zA-Z]*Schema$/, |
| /^I[A-Z][a-zA-Z]*$/, |
| /^T[A-Z][a-zA-Z]*$/, |
| ]; |
|
|
| return tsPatterns.some(pattern => pattern.test(name)); |
| } |
|
|
| |
| isReactPattern(name) { |
| const reactPatterns = [ |
| /^use[A-Z][a-zA-Z]*$/, |
| /^[A-Z][a-zA-Z]*Context$/, |
| /^[A-Z][a-zA-Z]*Provider$/, |
| /^handle[A-Z][a-zA-Z]*$/, |
| /^on[A-Z][a-zA-Z]*$/, |
| /^render[A-Z][a-zA-Z]*$/, |
| /^[a-z]+Ref$/, |
| /^[A-Z][a-zA-Z]*Table$/, |
| /^[A-Z][a-zA-Z]*Form$/, |
| /^[A-Z][a-zA-Z]*Modal$/, |
| /^[A-Z][a-zA-Z]*Button$/, |
| /^[A-Z][a-zA-Z]*Input$/, |
| /^[A-Z][a-zA-Z]*List$/, |
| /^[A-Z][a-zA-Z]*Card$/, |
| /^[A-Z][a-zA-Z]*Builder$/, |
| /^[A-Z][a-zA-Z]*Dashboard$/, |
| /^[A-Z][a-zA-Z]*Panel$/, |
| /^[A-Z][a-zA-Z]*Widget$/, |
| ]; |
|
|
| return reactPatterns.some(pattern => pattern.test(name)); |
| } |
|
|
| |
| isModernJsPattern(name) { |
| const modernPatterns = [ |
| /^[a-z]+Async$/, |
| /^[a-z]+Promise$/, |
| /^[a-z]+Generator$/, |
| /^[a-z]+Iterator$/, |
| /^create[A-Z][a-zA-Z]*$/, |
| /^build[A-Z][a-zA-Z]*$/, |
| /^process[A-Z][a-zA-Z]*$/, |
| ]; |
|
|
| return modernPatterns.some(pattern => pattern.test(name)); |
| } |
|
|
| |
| isClassMethodPattern(name) { |
| const classPatterns = [ |
| /^[a-z]+Connection$/, |
| /^[a-z]+Manager$/, |
| /^[a-z]+Builder$/, |
| /^[a-z]+Factory$/, |
| /^[a-z]+Repository$/, |
| /^[a-z]+Service$/, |
| /^[a-z]+Controller$/, |
| ]; |
|
|
| return classPatterns.some(pattern => pattern.test(name)); |
| } |
|
|
| |
| isAlgorithmPattern(name) { |
| const algoPatterns = [ |
| /^[a-z]{1,3}$/, |
| /^[a-z]+Tree$/, |
| /^[a-z]+Node$/, |
| /^[a-z]+Graph$/, |
| /^[a-z]+Queue$/, |
| /^[a-z]+Stack$/, |
| ]; |
|
|
| return algoPatterns.some(pattern => pattern.test(name)); |
| } |
|
|
| |
| generateActionableRecommendations(report, analysisResult) { |
| const recommendations = []; |
|
|
| if (analysisResult.patterns.variableDeclarations > 5) { |
| recommendations.push({ |
| priority: 'HIGH', |
| action: 'Review variable declarations', |
| description: 'Many variables were misidentified as classes. Consider adding comments above variable declarations.', |
| example: '// Store calculation result\nconst result = calculate();' |
| }); |
| } |
|
|
| if (analysisResult.patterns.shortVariableNames > 3) { |
| recommendations.push({ |
| priority: 'MEDIUM', |
| action: 'Use descriptive variable names', |
| description: 'Short variable names (i, j, x, y) are common and may not need comments.', |
| example: 'for (let index = 0; index < items.length; index++)' |
| }); |
| } |
|
|
| if (report.changes.structuresSkipped > 20) { |
| recommendations.push({ |
| priority: 'LOW', |
| action: 'Consider code refactoring', |
| description: 'High number of skipped structures suggests complex code that may benefit from refactoring.', |
| example: 'Break down large functions into smaller, well-documented functions' |
| }); |
| } |
|
|
| |
| const advancedPatterns = this.analyzeAdvancedPatterns(report.skippedStructures); |
|
|
| if (advancedPatterns.typeScriptPatterns > 0) { |
| recommendations.push({ |
| priority: 'HIGH', |
| action: 'Add TypeScript interface/type documentation', |
| description: 'TypeScript constructs detected. These need proper documentation for better code understanding.', |
| example: '// Interface defining database connection options\ninterface ConnectionOptions { ... }' |
| }); |
| } |
|
|
| if (advancedPatterns.reactPatterns > 0) { |
| recommendations.push({ |
| priority: 'HIGH', |
| action: 'Document React components and hooks', |
| description: 'React patterns detected. Components and custom hooks should have clear documentation.', |
| example: '// Custom hook for managing API calls with loading state\nconst useApi = () => { ... }' |
| }); |
| } |
|
|
| if (advancedPatterns.modernJsPatterns > 0) { |
| recommendations.push({ |
| priority: 'MEDIUM', |
| action: 'Document async/await and modern patterns', |
| description: 'Modern JavaScript patterns detected. Async operations need clear documentation.', |
| example: '// Asynchronously processes user data with error handling\nasync function processUserData() { ... }' |
| }); |
| } |
|
|
| if (advancedPatterns.classMethodPatterns > 0) { |
| recommendations.push({ |
| priority: 'MEDIUM', |
| action: 'Add class method documentation', |
| description: 'Advanced class methods detected. Repository/Service patterns need clear documentation.', |
| example: '// Repository class for managing user database operations\nclass UserRepository { ... }' |
| }); |
| } |
|
|
| return recommendations; |
| } |
| } |
|
|
| |
| const fileComparisonAnalyzer = new FileComparisonAnalyzer(); |
|
|
| |
| const backupManager = new OrganizedBackupManager(); |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function calculateLineNumber(content, index) { |
| const normalizedContent = content.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); |
| return normalizedContent.substring(0, index).split('\n').length; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| function normalizeLineEndings(content) { |
| return content.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| function analyzeCommentStatus(content, structures) { |
| const commentStatusMap = new Map(); |
| const normalizedContent = normalizeLineEndings(content); |
| const lines = normalizedContent.split('\n'); |
|
|
| structures.forEach(structure => { |
| const structureLine = structure.line - 1; |
| let hasComment = false; |
| let commentLines = []; |
|
|
| |
| for (let i = 1; i <= 5; i++) { |
| const checkIndex = structureLine - i; |
| if (checkIndex >= 0 && checkIndex < lines.length) { |
| const line = lines[checkIndex]; |
| const trimmed = line.trim(); |
|
|
| |
| if (trimmed.startsWith('//') || trimmed.startsWith('/*') || trimmed.includes('*/')) { |
| const commentContent = trimmed.toLowerCase(); |
| const structureName = structure.name.toLowerCase(); |
|
|
| |
| |
| if (commentContent.includes(structureName) || |
| (commentContent.includes('en:') && |
| (commentContent.includes(structureName) || |
| (commentContent.includes('interface') && structure.type === 'interface_declaration') || |
| (commentContent.includes('type') && structure.type === 'type_alias' && commentContent.includes('alias')) || |
| (commentContent.includes('static') && structure.type === 'static_method') || |
| (commentContent.includes('abstract') && structure.type === 'abstract_class'))) || |
| (commentContent.includes('th:') && |
| (commentContent.includes(structureName) || |
| (commentContent.includes('interface') && structure.type === 'interface_declaration') || |
| (commentContent.includes('type') && structure.type === 'type_alias' && commentContent.includes('alias')) || |
| (commentContent.includes('static') && structure.type === 'static_method') || |
| (commentContent.includes('abstract') && structure.type === 'abstract_class'))) || |
| commentContent.includes('======')) { |
| hasComment = true; |
| commentLines.push(checkIndex + 1); |
| break; |
| } |
| } |
|
|
| |
| else if (trimmed && |
| !trimmed.startsWith('export') && |
| !trimmed.startsWith('@') && |
| !trimmed.startsWith('{') && |
| !trimmed.startsWith('}')) { |
| break; |
| } |
| } |
| } |
|
|
| commentStatusMap.set(structure.name, { |
| hasComment, |
| commentLines, |
| structure: structure |
| }); |
| }); |
|
|
| return commentStatusMap; |
| } |
|
|
| |
| |
| |
|
|
| |
| const TOKEN_TYPES = { |
| |
| KEYWORD: 'KEYWORD', |
|
|
| |
| IDENTIFIER: 'IDENTIFIER', |
|
|
| |
| EQUALS: 'EQUALS', |
| ARROW: 'ARROW', |
| PAREN_OPEN: 'PAREN_OPEN', |
| PAREN_CLOSE: 'PAREN_CLOSE', |
| BRACE_OPEN: 'BRACE_OPEN', |
| BRACE_CLOSE: 'BRACE_CLOSE', |
| BRACKET_OPEN: 'BRACKET_OPEN', |
| BRACKET_CLOSE: 'BRACKET_CLOSE', |
| SEMICOLON: 'SEMICOLON', |
| COMMA: 'COMMA', |
|
|
| |
| LINE_COMMENT: 'LINE_COMMENT', |
| BLOCK_COMMENT: 'BLOCK_COMMENT', |
|
|
| |
| STRING: 'STRING', |
|
|
| |
| NUMBER: 'NUMBER', |
|
|
| |
| WHITESPACE: 'WHITESPACE', |
| NEWLINE: 'NEWLINE', |
|
|
| |
| EOF: 'EOF' |
| }; |
|
|
| |
| const KEYWORDS = new Set([ |
| |
| |
| |
| 'function', 'const', 'let', 'var', 'async', 'await', |
| 'class', 'constructor', 'static', 'get', 'set', 'abstract', |
| 'if', 'else', 'for', 'while', 'do', 'switch', 'case', |
| 'return', 'break', 'continue', 'throw', 'try', 'catch', |
| 'import', 'export', 'default', 'from', 'as', 'finally', |
| 'with', 'delete', 'new', 'this', 'super', 'instanceof', |
| 'of', 'in', 'null', 'undefined', 'true', 'false', |
| 'yield', 'debugger', 'arguments', 'eval', |
|
|
| |
| |
| |
| 'interface', 'type', 'enum', 'namespace', 'module', |
| 'declare', 'readonly', 'public', 'private', 'protected', |
| 'implements', 'extends', 'keyof', 'typeof', 'infer', |
| 'never', 'unknown', 'any', 'void', 'string', 'number', 'boolean', |
| 'object', 'symbol', 'bigint', 'unique', 'is', 'asserts', |
| 'override', 'satisfies', 'out', 'in', 'const', |
|
|
| |
| |
| |
| 'Partial', 'Required', 'Pick', 'Omit', 'Exclude', 'Extract', |
| 'NonNullable', 'Parameters', 'ReturnType', 'InstanceType', |
| 'ThisType', 'Record', 'Readonly', 'Array', 'Promise', |
| 'Awaited', 'ConstructorParameters', 'ThisParameterType', |
| 'OmitThisParameter', 'Uppercase', 'Lowercase', 'Capitalize', 'Uncapitalize', |
|
|
| |
| |
| |
| 'React', 'Component', 'PureComponent', 'memo', 'forwardRef', |
| 'createContext', 'useContext', 'createRef', 'useRef', |
| 'useState', 'useEffect', 'useReducer', 'useCallback', 'useMemo', |
| 'useLayoutEffect', 'useImperativeHandle', 'useDebugValue', |
| 'useDeferredValue', 'useTransition', 'useId', 'useSyncExternalStore', |
| 'useInsertionEffect', 'startTransition', 'flushSync', |
|
|
| |
| |
| |
| 'Fragment', 'StrictMode', 'Suspense', 'SuspenseList', 'Profiler', |
| 'createElement', 'createFactory', 'cloneElement', 'isValidElement', |
| 'Children', 'lazy', 'ErrorBoundary', 'Portal', 'createPortal', |
|
|
| |
| |
| |
| 'Router', 'Route', 'Routes', 'Link', 'NavLink', 'Navigate', |
| 'useNavigate', 'useLocation', 'useParams', 'useSearchParams', |
| 'Outlet', 'BrowserRouter', 'HashRouter', 'MemoryRouter', |
| 'Provider', 'Consumer', 'connect', 'useSelector', 'useDispatch', |
|
|
| |
| |
| |
| 'JSX', 'IntrinsicElements', 'ElementType', 'ComponentProps', |
| 'PropsWithChildren', 'PropsWithRef', 'RefAttributes', |
| 'ClassAttributes', 'HTMLAttributes', 'DOMAttributes', |
| 'CSSProperties', 'MouseEvent', 'KeyboardEvent', 'FormEvent', |
| 'ChangeEvent', 'FocusEvent', 'TouchEvent', 'WheelEvent', |
|
|
| |
| |
| |
| 'GetServerSideProps', 'GetStaticProps', 'GetStaticPaths', |
| 'NextPage', 'NextApiRequest', 'NextApiResponse', 'NextApiHandler', |
| 'AppProps', 'Document', 'Head', 'Image', 'Link', |
| 'useRouter', 'withRouter', 'getServerSideProps', 'getStaticProps', |
|
|
| |
| |
| |
| 'require', 'module', 'exports', '__dirname', '__filename', |
| 'process', 'global', 'Buffer', 'console', 'setTimeout', 'setInterval', |
| 'clearTimeout', 'clearInterval', 'setImmediate', 'clearImmediate', |
|
|
| |
| |
| |
| 'describe', 'it', 'test', 'expect', 'beforeEach', 'afterEach', |
| 'beforeAll', 'afterAll', 'jest', 'mock', 'spy', 'stub', |
| 'render', 'screen', 'fireEvent', 'waitFor', 'act', |
|
|
| |
| |
| |
| 'webpack', 'vite', 'rollup', 'parcel', 'babel', 'tsc', |
| 'eslint', 'prettier', 'tsconfig', 'package', 'dependencies', |
|
|
| |
| |
| |
| 'styled', 'css', 'keyframes', 'createGlobalStyle', 'ThemeProvider', |
| 'makeStyles', 'useStyles', 'withStyles', 'createStyles', |
| 'Box', 'Stack', 'Grid', 'Container', 'Paper', 'Card', |
|
|
| |
| |
| |
| 'fetch', 'axios', 'query', 'mutation', 'subscription', |
| 'GraphQL', 'REST', 'API', 'endpoint', 'middleware', |
| 'mongoose', 'prisma', 'sequelize', 'typeorm', 'knex', |
|
|
| |
| |
| |
| 'lodash', 'moment', 'dayjs', 'date-fns', 'ramda', |
| 'rxjs', 'observable', 'subject', 'subscription', |
| 'express', 'koa', 'fastify', 'nest', 'apollo', |
|
|
| |
| |
| |
| 'development', 'production', 'staging', 'test', |
| 'debug', 'trace', 'warn', 'error', 'info', 'log', |
| 'performance', 'profiler', 'benchmark', 'optimization' |
| ]); |
| |
| |
| |
| class TokenizerSecurityManager { |
| constructor(options = {}) { |
| |
| this.MAX_DEPTH = options.maxDepth || 100; |
| this.MAX_TOKENS = options.maxTokens || 500000; |
| this.MAX_PARSING_TIME = options.maxParsingTime || 30000; |
| this.MAX_LOOP_ITERATIONS = options.maxLoopIterations || 1000000; |
|
|
| this.startTime = null; |
| this.iterationCount = 0; |
| this.currentDepth = 0; |
| this.warnings = []; |
| } |
|
|
| |
| |
| |
| |
| |
| startParsing() { |
| this.startTime = Date.now(); |
| this.iterationCount = 0; |
| this.currentDepth = 0; |
| this.warnings = []; |
| } |
|
|
| |
| checkIteration() { |
| this.iterationCount++; |
|
|
| |
| if (this.startTime && (Date.now() - this.startTime) > this.MAX_PARSING_TIME) { |
| throw new Error(`SECURITY: Parsing timeout after ${this.MAX_PARSING_TIME}ms. File may contain malicious patterns.`); |
| } |
|
|
| |
| if (this.iterationCount > this.MAX_LOOP_ITERATIONS) { |
| throw new Error(`SECURITY: Too many parsing iterations (${this.MAX_LOOP_ITERATIONS}). File may contain complexity attack patterns.`); |
| } |
| } |
|
|
| |
| enterDepth() { |
| this.currentDepth++; |
| if (this.currentDepth > this.MAX_DEPTH) { |
| throw new Error(`SECURITY: Parsing depth exceeded ${this.MAX_DEPTH} levels. File may contain deeply nested malicious structures.`); |
| } |
| } |
|
|
| |
| exitDepth() { |
| if (this.currentDepth > 0) { |
| this.currentDepth--; |
| } |
| } |
|
|
| |
| checkTokenCount(tokenCount) { |
| if (tokenCount > this.MAX_TOKENS) { |
| throw new Error(`SECURITY: Token count exceeded ${this.MAX_TOKENS}. File too complex for safe processing.`); |
| } |
| } |
|
|
| |
| addWarning(message) { |
| this.warnings.push(`SECURITY WARNING: ${message}`); |
| } |
|
|
| |
| getStats() { |
| return { |
| processingTime: this.startTime ? Date.now() - this.startTime : 0, |
| iterations: this.iterationCount, |
| maxDepth: this.currentDepth, |
| warnings: this.warnings |
| }; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| class JavaScriptTokenizer { |
| constructor(code, securityOptions = {}) { |
| this.code = code; |
| this.cursor = 0; |
| this.line = 1; |
| this.column = 1; |
| this.tokens = []; |
| this.braceDepth = 0; |
| this.parenDepth = 0; |
| this.bracketDepth = 0; |
| |
| this.security = new TokenizerSecurityManager(securityOptions); |
| } |
|
|
| |
| tokenize() { |
| |
| this.security.startParsing(); |
|
|
| try { |
| while (this.cursor < this.code.length) { |
| |
| this.security.checkIteration(); |
| this.readNextToken(); |
| } |
|
|
| |
| this.security.checkTokenCount(this.tokens.length); |
|
|
| |
| this.addToken(TOKEN_TYPES.EOF, '', this.line, this.column); |
|
|
| |
| const stats = this.security.getStats(); |
| if (stats.warnings.length > 0) { |
| console.warn('Tokenizer Security Warnings:'); |
| stats.warnings.forEach(warning => console.warn(` - ${warning}`)); |
| } |
|
|
| return this.tokens; |
| } catch (error) { |
| |
| if (error.message.includes('SECURITY:')) { |
| console.error(`SECURITY ALERT: ${error.message}`); |
| console.error('File processing stopped for security reasons.'); |
| } |
| throw error; |
| } |
| } |
|
|
| |
| readNextToken() { |
| const char = this.currentChar(); |
|
|
| |
| if (this.isWhitespace(char)) { |
| this.readWhitespace(); |
| return; |
| } |
|
|
| |
| if (char === '\n') { |
| this.addToken(TOKEN_TYPES.NEWLINE, char, this.line, this.column); |
| this.advance(); |
| this.line++; |
| this.column = 1; |
| return; |
| } |
|
|
| |
| if (this.isLetter(char) || char === '_' || char === '$') { |
| this.readIdentifierOrKeyword(); |
| return; |
| } |
|
|
| |
| if (this.isDigit(char)) { |
| this.readNumber(); |
| return; |
| } |
|
|
| |
| if (char === '"' || char === "'" || char === '`') { |
| this.readString(char); |
| return; |
| } |
|
|
| |
| if (char === '/' && this.peek() === '/') { |
| this.readLineComment(); |
| return; |
| } |
|
|
| if (char === '/' && this.peek() === '*') { |
| this.readBlockComment(); |
| return; |
| } |
|
|
| |
| if (char === '=' && this.peek() === '>') { |
| this.addToken(TOKEN_TYPES.ARROW, '=>', this.line, this.column); |
| this.advance(2); |
| return; |
| } |
|
|
| |
| this.readOperatorOrPunctuation(char); |
| } |
|
|
| |
| readIdentifierOrKeyword() { |
| const startLine = this.line; |
| const startColumn = this.column; |
| let value = ''; |
|
|
| while (this.cursor < this.code.length && |
| (this.isAlphaNumeric(this.currentChar()) || |
| this.currentChar() === '_' || |
| this.currentChar() === '$')) { |
| value += this.currentChar(); |
| this.advance(); |
| } |
|
|
| const type = KEYWORDS.has(value) ? TOKEN_TYPES.KEYWORD : TOKEN_TYPES.IDENTIFIER; |
| this.addToken(type, value, startLine, startColumn); |
| } |
|
|
| |
| readNumber() { |
| const startLine = this.line; |
| const startColumn = this.column; |
| let value = ''; |
|
|
| while (this.cursor < this.code.length && this.isDigit(this.currentChar())) { |
| value += this.currentChar(); |
| this.advance(); |
| } |
|
|
| |
| if (this.currentChar() === '.' && this.isDigit(this.peek())) { |
| value += this.currentChar(); |
| this.advance(); |
|
|
| while (this.cursor < this.code.length && this.isDigit(this.currentChar())) { |
| value += this.currentChar(); |
| this.advance(); |
| } |
| } |
|
|
| this.addToken(TOKEN_TYPES.NUMBER, value, startLine, startColumn); |
| } |
|
|
| |
| readString(quote) { |
| const startLine = this.line; |
| const startColumn = this.column; |
| let value = quote; |
| this.advance(); |
|
|
| while (this.cursor < this.code.length && this.currentChar() !== quote) { |
| if (this.currentChar() === '\\') { |
| value += this.currentChar(); |
| this.advance(); |
| if (this.cursor < this.code.length) { |
| value += this.currentChar(); |
| |
| if (this.currentChar() === '\n') { |
| this.line++; |
| this.column = 1; |
| } |
| this.advance(); |
| } |
| } else { |
| |
| if (this.currentChar() === '\n') { |
| this.line++; |
| this.column = 1; |
| } |
| value += this.currentChar(); |
| this.advance(); |
| } |
| } |
|
|
| if (this.cursor < this.code.length) { |
| value += this.currentChar(); |
| this.advance(); |
| } |
|
|
| this.addToken(TOKEN_TYPES.STRING, value, startLine, startColumn); |
| } |
|
|
| |
| readLineComment() { |
| const startLine = this.line; |
| const startColumn = this.column; |
| let value = ''; |
|
|
| while (this.cursor < this.code.length && this.currentChar() !== '\n') { |
| value += this.currentChar(); |
| this.advance(); |
| } |
|
|
| this.addToken(TOKEN_TYPES.LINE_COMMENT, value, startLine, startColumn); |
| |
| |
| |
| if (this.cursor < this.code.length && this.currentChar() === '\n') { |
| this.advance(); |
| this.line++; |
| this.column = 1; |
| } |
| } |
|
|
| |
| readBlockComment() { |
| const startLine = this.line; |
| const startColumn = this.column; |
| let value = ''; |
|
|
| while (this.cursor < this.code.length - 1 && |
| !(this.currentChar() === '*' && this.peek() === '/')) { |
| if (this.currentChar() === '\n') { |
| this.line++; |
| this.column = 1; |
| } |
| value += this.currentChar(); |
| this.advance(); |
| } |
|
|
| if (this.cursor < this.code.length - 1) { |
| value += this.currentChar(); |
| this.advance(); |
| value += this.currentChar(); |
| this.advance(); |
| } |
|
|
| this.addToken(TOKEN_TYPES.BLOCK_COMMENT, value, startLine, startColumn); |
| } |
|
|
| |
| readWhitespace() { |
| while (this.cursor < this.code.length && this.isWhitespace(this.currentChar())) { |
| this.advance(); |
| } |
| } |
|
|
| |
| readOperatorOrPunctuation(char) { |
| const tokenMap = { |
| '=': TOKEN_TYPES.EQUALS, |
| '(': TOKEN_TYPES.PAREN_OPEN, |
| ')': TOKEN_TYPES.PAREN_CLOSE, |
| '{': TOKEN_TYPES.BRACE_OPEN, |
| '}': TOKEN_TYPES.BRACE_CLOSE, |
| '[': TOKEN_TYPES.BRACKET_OPEN, |
| ']': TOKEN_TYPES.BRACKET_CLOSE, |
| ';': TOKEN_TYPES.SEMICOLON, |
| ',': TOKEN_TYPES.COMMA |
| }; |
|
|
| |
| if (char === '{') this.braceDepth++; |
| else if (char === '}') this.braceDepth--; |
| else if (char === '(') this.parenDepth++; |
| else if (char === ')') this.parenDepth--; |
| else if (char === '[') this.bracketDepth++; |
| else if (char === ']') this.bracketDepth--; |
|
|
| const type = tokenMap[char] || TOKEN_TYPES.IDENTIFIER; |
| this.addToken(type, char, this.line, this.column); |
| this.advance(); |
| } |
|
|
| |
| currentChar() { |
| return this.cursor >= this.code.length ? '\0' : this.code[this.cursor]; |
| } |
|
|
| peek(offset = 1) { |
| const peekPos = this.cursor + offset; |
| return peekPos >= this.code.length ? '\0' : this.code[peekPos]; |
| } |
|
|
| advance(count = 1) { |
| for (let i = 0; i < count && this.cursor < this.code.length; i++) { |
| this.cursor++; |
| this.column++; |
| } |
| } |
|
|
| addToken(type, value, line, column) { |
| this.tokens.push({ |
| type, |
| value, |
| line, |
| column, |
| position: this.cursor - value.length, |
| braceDepth: this.braceDepth, |
| parenDepth: this.parenDepth, |
| bracketDepth: this.bracketDepth |
| }); |
| } |
|
|
| isWhitespace(char) { |
| return char === ' ' || char === '\t' || char === '\r'; |
| } |
|
|
| isLetter(char) { |
| return /[a-zA-Z]/.test(char); |
| } |
|
|
| isDigit(char) { |
| return /[0-9]/.test(char); |
| } |
|
|
| isAlphaNumeric(char) { |
| return this.isLetter(char) || this.isDigit(char); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| class FunctionPatternMatcher { |
| constructor(tokens, securityOptions = {}) { |
| this.tokens = tokens.filter(t => t.type !== TOKEN_TYPES.WHITESPACE); |
| this.functions = []; |
| this.cursor = 0; |
| this.scopeStack = []; |
| this.braceDepth = 0; |
| this.inClass = false; |
| this.classNames = []; |
| |
| this.security = new TokenizerSecurityManager(securityOptions); |
| } |
|
|
| |
| findFunctions() { |
| |
| this.security.startParsing(); |
|
|
| try { |
| this.buildScopeMap(); |
| this.cursor = 0; |
|
|
| while (this.cursor < this.tokens.length) { |
| |
| this.security.checkIteration(); |
|
|
| const func = this.matchFunctionPattern(); |
| if (func) { |
| this.functions.push(func); |
| } else { |
| this.cursor++; |
| } |
| } |
|
|
| |
| const stats = this.security.getStats(); |
| if (stats.warnings.length > 0) { |
| console.warn('Parser Security Warnings:'); |
| stats.warnings.forEach(warning => console.warn(` - ${warning}`)); |
| } |
|
|
| return this.functions; |
| } catch (error) { |
| |
| if (error.message.includes('SECURITY:')) { |
| console.error(`SECURITY ALERT: ${error.message}`); |
| console.error('Function parsing stopped for security reasons.'); |
| } |
| throw error; |
| } |
| } |
|
|
| |
| buildScopeMap() { |
| let braceDepth = 0; |
| let inClass = false; |
| let classDepth = -1; |
|
|
| for (let i = 0; i < this.tokens.length; i++) { |
| const token = this.tokens[i]; |
|
|
| if (token.type === TOKEN_TYPES.KEYWORD && token.value === 'class') { |
| inClass = true; |
| classDepth = braceDepth + 1; |
| } |
|
|
| if (token.type === TOKEN_TYPES.BRACE_OPEN) { |
| braceDepth++; |
| |
| if (braceDepth > this.security.MAX_DEPTH) { |
| this.security.addWarning(`Deep nesting detected: ${braceDepth} levels (max recommended: ${this.security.MAX_DEPTH})`); |
| |
| continue; |
| } |
| this.security.enterDepth(); |
| } |
|
|
| if (token.type === TOKEN_TYPES.BRACE_CLOSE) { |
| braceDepth--; |
| this.security.exitDepth(); |
| |
| if (inClass && braceDepth < classDepth) { |
| inClass = false; |
| classDepth = -1; |
| } |
| } |
|
|
| |
| token.inClass = inClass; |
| token.braceDepth = braceDepth; |
| } |
| } |
|
|
| |
| matchFunctionPattern() { |
| |
| if (this.isInCommentOrString()) { |
| return null; |
| } |
|
|
| |
| |
| |
| |
|
|
| |
| return this.matchInterfaceDeclaration() || |
| this.matchTypeAlias() || |
| this.matchEnumDeclaration() || |
| this.matchNamespaceDeclaration() || |
| this.matchModuleDeclaration() || |
| this.matchDeclareStatement() || |
|
|
| |
| this.matchClassDeclaration() || |
| this.matchAbstractClass() || |
| this.matchGenericClass() || |
|
|
| |
| this.matchReactComponent() || |
| this.matchExportedComponent() || |
| this.matchReactHooks() || |
| this.matchReactForwardRef() || |
| this.matchReactMemo() || |
| this.matchHigherOrderComponent() || |
|
|
| |
| this.matchFunctionDeclaration() || |
| this.matchArrowFunction() || |
| this.matchAsyncFunction() || |
| this.matchGeneratorFunction() || |
| this.matchIIFE() || |
| this.matchCurriedFunction() || |
| this.matchFactoryFunction() || |
|
|
| |
| this.matchAsyncClassMethod() || |
| this.matchGetter() || |
| this.matchSetter() || |
| this.matchStaticMethod() || |
| this.matchPrivateMethod() || |
| this.matchProtectedMethod() || |
| this.matchClassMethod() || |
|
|
| |
| this.matchDestructuringAssignment() || |
| this.matchSpreadOperator() || |
| this.matchAsyncIterator() || |
| this.matchProxyHandler() || |
|
|
| |
| this.matchAlgorithmFunction() || |
| this.matchDataStructureMethod() || |
| this.matchMathFunction() || |
|
|
| |
| this.matchExpressRoute() || |
| this.matchExpressMiddleware() || |
| this.matchNodeJSModule(); |
| } |
|
|
| |
| matchFunctionDeclaration() { |
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'function' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const functionToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
| const params = this.extractParameters(this.cursor + 2); |
|
|
| this.cursor += 3; |
|
|
| return { |
| type: 'function_declaration', |
| name: nameToken.value, |
| line: functionToken.line, |
| column: functionToken.column, |
| parameters: params, |
| isAsync: false |
| }; |
| } |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'function' && |
| this.peekToken(1)?.value === '*' && |
| this.peekToken(2)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(3)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const functionToken = this.currentToken(); |
| const nameToken = this.peekToken(2); |
|
|
| |
| if (!nameToken.value || |
| nameToken.value.trim() === '' || |
| nameToken.value === '*' || |
| nameToken.value.includes('*') || |
| /^[^\w]/.test(nameToken.value) || |
| nameToken.value.length < 2) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 3); |
| this.cursor += 4; |
|
|
| return { |
| type: 'generator_function', |
| name: nameToken.value, |
| line: functionToken.line, |
| column: functionToken.column, |
| parameters: params, |
| isAsync: false, |
| isGenerator: true |
| }; |
| } |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| (this.currentToken()?.value === 'const' || this.currentToken()?.value === 'let' || this.currentToken()?.value === 'var') && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.EQUALS && |
| this.peekToken(3)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(3)?.value === 'function' && |
| this.peekToken(4)?.value === '*') { |
|
|
| const keywordToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| if (!nameToken.value || |
| nameToken.value.trim() === '' || |
| nameToken.value === '*' || |
| nameToken.value.includes('*') || |
| /^[^\w]/.test(nameToken.value) || |
| nameToken.value.length < 2) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 5); |
| this.cursor += 6; |
|
|
| return { |
| type: 'generator_function', |
| name: nameToken.value, |
| line: keywordToken.line, |
| column: keywordToken.column, |
| parameters: params, |
| isAsync: false, |
| isGenerator: true |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| matchArrowFunction() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| (this.currentToken()?.value === 'const' || this.currentToken()?.value === 'let' || this.currentToken()?.value === 'var') && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.EQUALS) { |
|
|
| |
| let arrowPos = this.findArrowInRange(this.cursor + 3, this.cursor + 10); |
| if (arrowPos !== -1) { |
| const keywordToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
| const params = this.extractParametersBeforeArrow(this.cursor + 3, arrowPos); |
|
|
| this.cursor = arrowPos + 1; |
|
|
| return { |
| type: 'arrow_function', |
| name: nameToken.value, |
| line: keywordToken.line, |
| column: keywordToken.column, |
| parameters: params, |
| isAsync: this.checkAsyncBeforeArrow(this.cursor - 5, arrowPos) |
| }; |
| } |
| } |
| return null; |
| } |
|
|
| |
| matchAsyncFunction() { |
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'async' && |
| this.peekToken(1)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(1)?.value === 'function' && |
| this.peekToken(2)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(3)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const asyncToken = this.currentToken(); |
| const nameToken = this.peekToken(2); |
| const params = this.extractParameters(this.cursor + 3); |
|
|
| this.cursor += 4; |
|
|
| return { |
| type: 'async_function', |
| name: nameToken.value, |
| line: asyncToken.line, |
| column: asyncToken.column, |
| parameters: params, |
| isAsync: true |
| }; |
| } |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'async' && |
| this.peekToken(1)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(1)?.value === 'function' && |
| this.peekToken(2)?.value === '*' && |
| this.peekToken(3)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(4)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const asyncToken = this.currentToken(); |
| const nameToken = this.peekToken(3); |
|
|
| |
| if (!nameToken.value || |
| nameToken.value.trim() === '' || |
| nameToken.value === '*' || |
| nameToken.value.includes('*') || |
| /^[^\w]/.test(nameToken.value) || |
| nameToken.value.length < 2) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 4); |
| this.cursor += 5; |
|
|
| return { |
| type: 'async_generator_function', |
| name: nameToken.value, |
| line: asyncToken.line, |
| column: asyncToken.column, |
| parameters: params, |
| isAsync: true, |
| isGenerator: true |
| }; |
| } |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| (this.currentToken()?.value === 'const' || this.currentToken()?.value === 'let' || this.currentToken()?.value === 'var') && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.EQUALS && |
| this.peekToken(3)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(3)?.value === 'async' && |
| this.peekToken(4)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(4)?.value === 'function' && |
| this.peekToken(5)?.value === '*') { |
|
|
| const keywordToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| if (!nameToken.value || |
| nameToken.value.trim() === '' || |
| nameToken.value === '*' || |
| nameToken.value.includes('*') || |
| /^[^\w]/.test(nameToken.value) || |
| nameToken.value.length < 2) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 6); |
| this.cursor += 7; |
|
|
| return { |
| type: 'async_generator_function', |
| name: nameToken.value, |
| line: keywordToken.line, |
| column: keywordToken.column, |
| parameters: params, |
| isAsync: true, |
| isGenerator: true |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| matchClassMethod() { |
| const currentToken = this.currentToken(); |
|
|
| if (currentToken?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(1)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const nameToken = currentToken; |
|
|
| |
| const excludedNames = [ |
| 'constructor', 'super', 'this', 'new', 'Map', 'Set', 'Array', 'Object', 'Promise', |
| 'console', 'Math', 'JSON', 'Date', 'String', 'Number', 'Boolean', 'Error', |
| 'require', 'module', 'exports', 'process', 'Buffer', 'setTimeout', 'setInterval', |
| 'function', 'return', 'if', 'else', 'for', 'while', 'switch', 'case', 'break', 'continue' |
| ]; |
|
|
| if (excludedNames.includes(nameToken.value)) { |
| return null; |
| } |
|
|
| |
| if (nameToken.value.length === 1 && /[+\-*/=<>!&|^%]/.test(nameToken.value)) { |
| return null; |
| } |
|
|
| |
| const prevToken = this.peekToken(-1); |
| if (prevToken && (prevToken.value === '.' || prevToken.value === 'new' || prevToken.type === TOKEN_TYPES.PAREN_CLOSE)) { |
| return null; |
| } |
|
|
| |
| const inClass = this.isInClassContext(); |
| const nextTokenAfterParen = this.findTokenAfterParentheses(); |
| const hasMethodBody = nextTokenAfterParen && nextTokenAfterParen.type === TOKEN_TYPES.BRACE_OPEN; |
|
|
| |
| if (!inClass && !hasMethodBody) { |
| return null; |
| } |
|
|
| |
| if (inClass && !hasMethodBody) { |
| |
| if (!this.isLikelyMethodName(nameToken.value)) { |
| return null; |
| } |
| } |
|
|
| |
| if (!nameToken.value || nameToken.value.trim() === '' || /^[^\w]/.test(nameToken.value)) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 1); |
| this.cursor += 2; |
|
|
| return { |
| type: 'class_method', |
| name: nameToken.value, |
| line: nameToken.line, |
| column: nameToken.column, |
| parameters: params, |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchAsyncClassMethod() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'async' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const asyncToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| if (!this.isInClassContext()) { |
| return null; |
| } |
|
|
| |
| const excludedNames = [ |
| 'constructor', 'super', 'this', 'new', 'Map', 'Set', 'Array', 'Object', 'Promise', |
| 'console', 'Math', 'JSON', 'Date', 'String', 'Number', 'Boolean', 'Error', |
| 'require', 'module', 'exports', 'process', 'Buffer', 'setTimeout', 'setInterval', |
| 'function', 'return', 'if', 'else', 'for', 'while', 'switch', 'case', 'break', 'continue' |
| ]; |
|
|
| if (excludedNames.includes(nameToken.value)) { |
| return null; |
| } |
|
|
| |
| const nextTokenAfterParen = this.findTokenAfterParentheses(); |
| if (!nextTokenAfterParen || nextTokenAfterParen.type !== TOKEN_TYPES.BRACE_OPEN) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 2); |
| this.cursor += 3; |
|
|
| return { |
| type: 'async_class_method', |
| name: nameToken.value, |
| line: asyncToken.line, |
| column: asyncToken.column, |
| parameters: params, |
| isAsync: true |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchGetter() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'get' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const getToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| if (!this.isInClassContext()) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 2); |
| this.cursor += 3; |
|
|
| return { |
| type: 'getter', |
| name: nameToken.value, |
| line: getToken.line, |
| column: getToken.column, |
| parameters: params, |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchSetter() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'set' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const setToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| if (!this.isInClassContext()) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 2); |
| this.cursor += 3; |
|
|
| return { |
| type: 'setter', |
| name: nameToken.value, |
| line: setToken.line, |
| column: setToken.column, |
| parameters: params, |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchStaticMethod() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'static' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const staticToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| if (!this.isInClassContext()) { |
| return null; |
| } |
|
|
| const params = this.extractParameters(this.cursor + 2); |
| this.cursor += 3; |
|
|
| return { |
| type: 'static_method', |
| name: nameToken.value, |
| line: staticToken.line, |
| column: staticToken.column, |
| parameters: params, |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchClassDeclaration() { |
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'abstract' && |
| this.peekToken(1)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(1)?.value === 'class' && |
| this.peekToken(2)?.type === TOKEN_TYPES.IDENTIFIER) { |
|
|
| const abstractToken = this.currentToken(); |
| const classToken = this.peekToken(1); |
| const nameToken = this.peekToken(2); |
|
|
| |
| const currentDepth = abstractToken.braceDepth || 0; |
| |
| if (currentDepth > 2) { |
| return null; |
| } |
|
|
| this.cursor += 3; |
|
|
| return { |
| type: 'class_declaration', |
| name: nameToken.value, |
| line: abstractToken.line, |
| column: abstractToken.column, |
| parameters: [], |
| isAsync: false, |
| isAbstract: true |
| }; |
| } |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'class' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER) { |
|
|
| const classToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| const currentDepth = classToken.braceDepth || 0; |
| |
| if (currentDepth > 2) { |
| return null; |
| } |
|
|
| this.cursor += 2; |
|
|
| return { |
| type: 'class_declaration', |
| name: nameToken.value, |
| line: classToken.line, |
| column: classToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchInterfaceDeclaration() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'interface' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER) { |
|
|
| const interfaceToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| this.cursor += 2; |
|
|
| return { |
| type: 'interface_declaration', |
| name: nameToken.value, |
| line: interfaceToken.line, |
| column: interfaceToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchTypeAlias() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'type' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.EQUALS) { |
|
|
| const typeToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| this.cursor += 3; |
|
|
| return { |
| type: 'type_alias', |
| name: nameToken.value, |
| line: typeToken.line, |
| column: typeToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchEnumDeclaration() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'enum' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER) { |
|
|
| const enumToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| this.cursor += 2; |
|
|
| return { |
| type: 'enum_declaration', |
| name: nameToken.value, |
| line: enumToken.line, |
| column: enumToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchReactComponent() { |
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'const' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(1)?.value.match(/^[A-Z][a-zA-Z0-9]*$/) && |
| this.peekToken(2)?.type === TOKEN_TYPES.EQUALS && |
| this.peekToken(3)?.type === TOKEN_TYPES.PAREN_OPEN) { |
|
|
| const constToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
| const params = this.extractParameters(this.cursor + 3); |
|
|
| this.cursor += 4; |
|
|
| return { |
| type: 'react_component', |
| name: nameToken.value, |
| line: constToken.line, |
| column: constToken.column, |
| parameters: params, |
| isAsync: false, |
| isReactComponent: true |
| }; |
| } |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'function' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(1)?.value.match(/^[A-Z][a-zA-Z0-9]*$/)) { |
|
|
| const functionToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
| const params = this.extractParameters(this.cursor + 2); |
|
|
| this.cursor += 3; |
|
|
| return { |
| type: 'react_component', |
| name: nameToken.value, |
| line: functionToken.line, |
| column: functionToken.column, |
| parameters: params, |
| isAsync: false, |
| isReactComponent: true |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| matchExportedComponent() { |
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'export' && |
| this.peekToken(1)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(1)?.value === 'const' && |
| this.peekToken(2)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.value.match(/^[A-Z][a-zA-Z0-9]*$/)) { |
|
|
| const exportToken = this.currentToken(); |
| const nameToken = this.peekToken(2); |
| const params = this.extractParameters(this.cursor + 4); |
|
|
| this.cursor += 5; |
|
|
| return { |
| type: 'exported_component', |
| name: nameToken.value, |
| line: exportToken.line, |
| column: exportToken.column, |
| parameters: params, |
| isAsync: false, |
| isExported: true, |
| isReactComponent: true |
| }; |
| } |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'export' && |
| this.peekToken(1)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(1)?.value === 'function' && |
| this.peekToken(2)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.value.match(/^[A-Z][a-zA-Z0-9]*$/)) { |
|
|
| const exportToken = this.currentToken(); |
| const nameToken = this.peekToken(2); |
| const params = this.extractParameters(this.cursor + 3); |
|
|
| this.cursor += 4; |
|
|
| return { |
| type: 'exported_component', |
| name: nameToken.value, |
| line: exportToken.line, |
| column: exportToken.column, |
| parameters: params, |
| isAsync: false, |
| isExported: true, |
| isReactComponent: true |
| }; |
| } |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'export' && |
| this.peekToken(1)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(1)?.value === 'default' && |
| this.peekToken(2)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(2)?.value === 'class' && |
| this.peekToken(3)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(3)?.value.match(/^[A-Z][a-zA-Z0-9]*$/)) { |
|
|
| const exportToken = this.currentToken(); |
| const nameToken = this.peekToken(3); |
|
|
| this.cursor += 4; |
|
|
| return { |
| type: 'exported_class_component', |
| name: nameToken.value, |
| line: exportToken.line, |
| column: exportToken.column, |
| parameters: [], |
| isAsync: false, |
| isExported: true, |
| isReactComponent: true |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| isLikelyMethodName(name) { |
| |
| const methodPatterns = [ |
| /^(get|set|is|has|can|should|will|create|make|build|init|setup|config|configure)/, |
| /^(process|handle|manage|calculate|compute|validate|check|verify|parse|format)/, |
| /^(render|update|delete|remove|add|insert|save|load|fetch|send|receive|execute)/, |
| /^(start|stop|pause|resume|restart|reset|clear|clean|refresh|reload|sync)/, |
| /^(connect|disconnect|login|logout|authenticate|authorize|encrypt|decrypt)/, |
| /^(on[A-Z]|handle[A-Z]|process[A-Z])/, |
| /^[a-z][a-zA-Z]*[A-Z]/, |
| /^[a-z]+[0-9]+/, |
| /.*[A-Z].*[A-Z].*/ |
| ]; |
|
|
| |
| const excludedPatterns = [ |
| /^(if|else|for|while|switch|case|break|continue|return|try|catch|finally|throw)$/, |
| /^(var|let|const|function|class|import|export|default|from|as|typeof|instanceof)$/, |
| /^(null|undefined|true|false|this|super|new|delete|void)$/ |
| ]; |
|
|
| |
| if (excludedPatterns.some(pattern => pattern.test(name))) { |
| return false; |
| } |
|
|
| |
| return methodPatterns.some(pattern => pattern.test(name)) || |
| (name.length > 2 && /^[a-z][a-zA-Z0-9_]*$/.test(name)); |
| } |
|
|
| |
| currentToken() { |
| return this.cursor < this.tokens.length ? this.tokens[this.cursor] : null; |
| } |
|
|
| peekToken(offset) { |
| const pos = this.cursor + offset; |
| return pos >= 0 && pos < this.tokens.length ? this.tokens[pos] : null; |
| } |
|
|
| isInCommentOrString() { |
| const token = this.currentToken(); |
| return token?.type === TOKEN_TYPES.LINE_COMMENT || |
| token?.type === TOKEN_TYPES.BLOCK_COMMENT || |
| token?.type === TOKEN_TYPES.STRING; |
| } |
|
|
| extractParameters(startPos) { |
| const params = []; |
| let pos = startPos; |
| let depth = 1; |
|
|
| while (pos < this.tokens.length && depth > 0) { |
| const token = this.tokens[pos]; |
|
|
| if (token.type === TOKEN_TYPES.PAREN_OPEN) { |
| depth++; |
| } else if (token.type === TOKEN_TYPES.PAREN_CLOSE) { |
| depth--; |
| } else if (token.type === TOKEN_TYPES.IDENTIFIER && depth === 1) { |
| params.push(token.value); |
| } |
|
|
| pos++; |
| } |
|
|
| return params; |
| } |
|
|
| extractParametersBeforeArrow(startPos, arrowPos) { |
| const params = []; |
| let pos = startPos; |
|
|
| while (pos < arrowPos) { |
| const token = this.tokens[pos]; |
| if (token.type === TOKEN_TYPES.IDENTIFIER) { |
| params.push(token.value); |
| } |
| pos++; |
| } |
|
|
| return params; |
| } |
|
|
| |
| findTokenAfterParentheses() { |
| let pos = this.cursor + 1; |
| let depth = 1; |
|
|
| while (pos < this.tokens.length && depth > 0) { |
| const token = this.tokens[pos]; |
| if (token.type === TOKEN_TYPES.PAREN_OPEN) { |
| depth++; |
| } else if (token.type === TOKEN_TYPES.PAREN_CLOSE) { |
| depth--; |
| } |
| pos++; |
| } |
|
|
| |
| while (pos < this.tokens.length && this.tokens[pos].type === TOKEN_TYPES.WHITESPACE) { |
| pos++; |
| } |
|
|
| return pos < this.tokens.length ? this.tokens[pos] : null; |
| } |
|
|
| findArrowInRange(start, end) { |
| |
| const searchEnd = Math.min(end + 15, this.tokens.length); |
|
|
| for (let i = start; i < searchEnd; i++) { |
| if (this.tokens[i]?.type === TOKEN_TYPES.ARROW) { |
| |
| let braceCount = 0; |
| let parenCount = 0; |
|
|
| for (let j = start; j < i; j++) { |
| const checkToken = this.tokens[j]; |
| if (checkToken) { |
| if (checkToken.type === TOKEN_TYPES.BRACE_OPEN) braceCount++; |
| else if (checkToken.type === TOKEN_TYPES.BRACE_CLOSE) braceCount--; |
| else if (checkToken.type === TOKEN_TYPES.PAREN_OPEN) parenCount++; |
| else if (checkToken.type === TOKEN_TYPES.PAREN_CLOSE) parenCount--; |
| } |
| } |
|
|
| |
| if (braceCount === 0 && parenCount <= 0) { |
| return i; |
| } |
| } |
| } |
| return -1; |
| } |
|
|
| checkAsyncBeforeArrow(start, end) { |
| for (let i = start; i < end; i++) { |
| if (this.tokens[i]?.type === TOKEN_TYPES.KEYWORD && |
| this.tokens[i]?.value === 'async') { |
| return true; |
| } |
| } |
| return false; |
| } |
|
|
| isInClassContext() { |
| |
| let braceDepth = 0; |
| let foundClass = false; |
| let classStart = -1; |
|
|
| |
| for (let i = Math.max(0, this.cursor - 100); i < this.cursor; i++) { |
| const token = this.tokens[i]; |
| if (!token) continue; |
|
|
| if (token.type === TOKEN_TYPES.KEYWORD && token.value === 'class') { |
| foundClass = true; |
| classStart = i; |
| braceDepth = 0; |
| } |
|
|
| if (foundClass && i > classStart) { |
| if (token.type === TOKEN_TYPES.BRACE_OPEN) { |
| braceDepth++; |
| } else if (token.type === TOKEN_TYPES.BRACE_CLOSE) { |
| braceDepth--; |
| if (braceDepth <= 0) { |
| foundClass = false; |
| } |
| } |
| } |
| } |
|
|
| |
| return foundClass && braceDepth > 0; |
| } |
| } |
|
|
| |
| |
| |
|
|
| |
| class StructureParser { |
| constructor(tokens, securityOptions = {}) { |
| this.tokens = tokens.filter(t => t.type !== TOKEN_TYPES.WHITESPACE); |
| this.cursor = 0; |
| this.scopeStack = []; |
| this.structures = []; |
| |
| this.security = new TokenizerSecurityManager(securityOptions); |
| } |
|
|
| |
| |
| |
| |
| |
| parse() { |
| |
| this.security.startParsing(); |
|
|
| try { |
| while (this.cursor < this.tokens.length) { |
| |
| this.security.checkIteration(); |
|
|
| const structure = this.parseDeclaration(); |
| if (structure) { |
| this.structures.push(structure); |
| } |
| } |
|
|
| |
| const stats = this.security.getStats(); |
| if (stats.warnings.length > 0) { |
| console.warn('Parser Security Warnings:'); |
| stats.warnings.forEach(warning => console.warn(` - ${warning}`)); |
| } |
|
|
| return this.structures; |
| } catch (error) { |
| |
| if (error.message.includes('SECURITY:')) { |
| console.error(`SECURITY ALERT: ${error.message}`); |
| console.error('Structure parsing stopped for security reasons.'); |
| } |
| throw error; |
| } |
| } |
|
|
| |
| parseDeclaration() { |
| const currentToken = this.currentToken(); |
| if (!currentToken) return null; |
|
|
| |
| if (this.isInCommentOrString()) { |
| this.advance(); |
| return null; |
| } |
|
|
| |
| if (currentToken.type === TOKEN_TYPES.KEYWORD && currentToken.value === 'class') { |
| return this.parseClassDeclaration(); |
| } |
|
|
| |
| if (currentToken.type === TOKEN_TYPES.KEYWORD && currentToken.value === 'function') { |
| return this.parseFunctionDeclaration(); |
| } |
|
|
| |
| if (currentToken.type === TOKEN_TYPES.KEYWORD && ['const', 'let', 'var'].includes(currentToken.value)) { |
| return this.parseVariableDeclaration(); |
| } |
|
|
| |
| this.advance(); |
| return null; |
| } |
|
|
| |
| parseClassDeclaration() { |
| const classToken = this.advance(); |
| if (this.currentToken()?.type !== TOKEN_TYPES.IDENTIFIER) return null; |
|
|
| const nameToken = this.advance(); |
| const classStructure = { |
| type: 'class_declaration', |
| name: nameToken.value, |
| line: classToken.line, |
| column: classToken.column, |
| methods: [] |
| }; |
|
|
| |
| if (this.currentToken()?.type === TOKEN_TYPES.BRACE_OPEN) { |
| const bodyStartIndex = this.cursor; |
| const bodyEndIndex = this.findMatchingBrace(bodyStartIndex); |
|
|
| if (bodyEndIndex !== -1) { |
| |
| this.security.enterDepth(); |
|
|
| |
| this.parseClassBody(classStructure, bodyEndIndex); |
| this.cursor = bodyEndIndex + 1; |
|
|
| this.security.exitDepth(); |
| } |
| } |
|
|
| return classStructure; |
| } |
|
|
| |
| parseClassBody(classStructure, bodyEndIndex) { |
| this.advance(); |
|
|
| while (this.cursor < bodyEndIndex) { |
| const token = this.currentToken(); |
| if (!token) break; |
|
|
| let isAsync = false; |
| let isStatic = false; |
| let kind = 'method'; |
|
|
| |
| if (token.type === TOKEN_TYPES.KEYWORD) { |
| if (token.value === 'async') { |
| isAsync = true; |
| this.advance(); |
| } else if (token.value === 'static') { |
| isStatic = true; |
| this.advance(); |
| } else if (token.value === 'get' || token.value === 'set') { |
| kind = token.value; |
| this.advance(); |
| } |
| } |
|
|
| |
| const nameToken = this.currentToken(); |
| const parenToken = this.peekToken(1); |
|
|
| if (nameToken?.type === TOKEN_TYPES.IDENTIFIER && parenToken?.type === TOKEN_TYPES.PAREN_OPEN) { |
| |
| if (nameToken.value !== 'constructor') { |
| classStructure.methods.push({ |
| type: 'class_method', |
| name: nameToken.value, |
| line: nameToken.line, |
| column: nameToken.column, |
| isAsync, |
| isStatic, |
| kind, |
| parameters: this.extractParameters(this.cursor + 1) |
| }); |
| } |
|
|
| |
| const methodBodyStart = this.findToken(TOKEN_TYPES.BRACE_OPEN, this.cursor); |
| if (methodBodyStart !== -1) { |
| const methodBodyEnd = this.findMatchingBrace(methodBodyStart); |
| if (methodBodyEnd !== -1) { |
| this.cursor = methodBodyEnd + 1; |
| continue; |
| } |
| } |
| } |
| this.advance(); |
| } |
| } |
|
|
| |
| parseFunctionDeclaration() { |
| const funcToken = this.advance(); |
|
|
| |
| let isGenerator = false; |
| if (this.currentToken()?.value === '*') { |
| isGenerator = true; |
| this.advance(); |
| } |
|
|
| if (this.currentToken()?.type !== TOKEN_TYPES.IDENTIFIER) { |
| |
| return null; |
| } |
|
|
| const nameToken = this.advance(); |
| const funcStructure = { |
| type: isGenerator ? 'generator_function' : 'function_declaration', |
| name: nameToken.value, |
| line: funcToken.line, |
| column: funcToken.column, |
| parameters: this.extractParameters(this.cursor), |
| isAsync: false |
| }; |
|
|
| |
| const bodyStart = this.findToken(TOKEN_TYPES.BRACE_OPEN, this.cursor); |
| if (bodyStart !== -1) { |
| const bodyEnd = this.findMatchingBrace(bodyStart); |
| if (bodyEnd !== -1) { |
| this.cursor = bodyEnd + 1; |
| } |
| } |
|
|
| return funcStructure; |
| } |
|
|
| |
| parseVariableDeclaration() { |
| this.advance(); |
| const nameToken = this.currentToken(); |
| if (nameToken?.type !== TOKEN_TYPES.IDENTIFIER) return null; |
| this.advance(); |
|
|
| if (this.currentToken()?.type !== TOKEN_TYPES.EQUALS) return null; |
| this.advance(); |
|
|
| |
| let isAsync = false; |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && this.currentToken().value === 'async') { |
| isAsync = true; |
| this.advance(); |
| } |
|
|
| |
| const nextToken = this.currentToken(); |
|
|
| let isArrowFunc = false; |
| if (nextToken?.type === TOKEN_TYPES.PAREN_OPEN) { |
| const closingParen = this.findMatchingParen(this.cursor); |
| if (closingParen !== -1 && this.tokens[closingParen + 1]?.type === TOKEN_TYPES.ARROW) { |
| isArrowFunc = true; |
| } |
| } else if (nextToken?.type === TOKEN_TYPES.IDENTIFIER) { |
| |
| if (this.peekToken(1)?.type === TOKEN_TYPES.ARROW) { |
| isArrowFunc = true; |
| } |
| } |
|
|
| if (isArrowFunc) { |
| const funcStructure = { |
| type: 'arrow_function', |
| name: nameToken.value, |
| line: nameToken.line, |
| column: nameToken.column, |
| isAsync, |
| parameters: this.extractArrowParameters(this.cursor) |
| }; |
|
|
| const bodyStart = this.findToken(TOKEN_TYPES.BRACE_OPEN, this.cursor); |
| if (bodyStart !== -1) { |
| const bodyEnd = this.findMatchingBrace(bodyStart); |
| if (bodyEnd !== -1) { |
| this.cursor = bodyEnd + 1; |
| } |
| } else { |
| |
| const semicolon = this.findToken(TOKEN_TYPES.SEMICOLON, this.cursor); |
| if (semicolon !== -1) this.cursor = semicolon + 1; |
| } |
| return funcStructure; |
| } |
|
|
| return null; |
| } |
|
|
| |
|
|
| |
| findMatchingBrace(startIndex) { |
| let depth = 1; |
| for (let i = startIndex + 1; i < this.tokens.length; i++) { |
| |
| if (i - startIndex > this.security.MAX_LOOP_ITERATIONS) { |
| this.security.addWarning(`Potential infinite loop detected in findMatchingBrace`); |
| break; |
| } |
|
|
| if (this.tokens[i].type === TOKEN_TYPES.BRACE_OPEN) { |
| depth++; |
| } else if (this.tokens[i].type === TOKEN_TYPES.BRACE_CLOSE) { |
| depth--; |
| if (depth === 0) { |
| return i; |
| } |
| } |
| } |
| return -1; |
| } |
|
|
| |
| findMatchingParen(startIndex) { |
| let depth = 1; |
| for (let i = startIndex + 1; i < this.tokens.length; i++) { |
| if (this.tokens[i].type === TOKEN_TYPES.PAREN_OPEN) { |
| depth++; |
| } else if (this.tokens[i].type === TOKEN_TYPES.PAREN_CLOSE) { |
| depth--; |
| if (depth === 0) { |
| return i; |
| } |
| } |
| } |
| return -1; |
| } |
|
|
| |
| findToken(type, startIndex) { |
| for (let i = startIndex; i < this.tokens.length; i++) { |
| if (this.tokens[i].type === type) return i; |
| } |
| return -1; |
| } |
|
|
| |
| extractParameters(parenIndex) { |
| if (this.tokens[parenIndex]?.type !== TOKEN_TYPES.PAREN_OPEN) return []; |
|
|
| const params = []; |
| const closingParen = this.findMatchingParen(parenIndex); |
| if (closingParen === -1) return []; |
|
|
| |
| for (let i = parenIndex + 1; i < closingParen; i++) { |
| const token = this.tokens[i]; |
| if (token.type === TOKEN_TYPES.IDENTIFIER) { |
| params.push(token.value); |
| } |
| } |
|
|
| return params; |
| } |
|
|
| |
| extractArrowParameters(startIndex) { |
| const firstToken = this.tokens[startIndex]; |
| if (!firstToken) return []; |
|
|
| if (firstToken.type === TOKEN_TYPES.PAREN_OPEN) { |
| return this.extractParameters(startIndex); |
| } else if (firstToken.type === TOKEN_TYPES.IDENTIFIER) { |
| return [firstToken.value]; |
| } |
|
|
| return []; |
| } |
|
|
| |
| isInCommentOrString() { |
| const currentToken = this.currentToken(); |
| return currentToken && ( |
| currentToken.type === TOKEN_TYPES.LINE_COMMENT || |
| currentToken.type === TOKEN_TYPES.BLOCK_COMMENT || |
| currentToken.type === TOKEN_TYPES.STRING |
| ); |
| } |
|
|
| currentToken() { return this.tokens[this.cursor]; } |
| peekToken(offset = 1) { return this.tokens[this.cursor + offset]; } |
| advance() { |
| if (!this.isAtEnd()) this.cursor++; |
| return this.tokens[this.cursor - 1]; |
| } |
| isAtEnd() { return this.cursor >= this.tokens.length; } |
|
|
| |
| findFunctions() { |
| const structures = this.parse(); |
| const allItems = []; |
|
|
| structures.forEach(s => { |
| if (s.type === 'class_declaration') { |
| |
| allItems.push({ |
| type: s.type, |
| name: s.name, |
| line: s.line, |
| column: s.column, |
| parameters: [] |
| }); |
|
|
| |
| if (s.methods) { |
| s.methods.forEach(m => allItems.push(m)); |
| } |
| } else { |
| allItems.push(s); |
| } |
| }); |
|
|
| return allItems; |
| } |
| } |
|
|
| |
| |
| |
|
|
| |
| class SmartFileAnalyzer { |
| constructor(content, securityOptions = {}) { |
| this.content = content; |
| this.lines = content.split('\n'); |
| this.security = new TokenizerSecurityManager(securityOptions); |
|
|
| |
| this.fileBlueprint = { |
| classes: new Map(), |
| functions: new Map(), |
| patterns: new Set(), |
| keywords: new Set(), |
| relationships: new Map(), |
| context: { |
| type: 'unknown', |
| domain: 'unknown', |
| complexity: 'simple' |
| } |
| }; |
| } |
|
|
| |
| analyzeFile() { |
| try { |
| this.security.startParsing(); |
|
|
| |
| this.analyzeTypeScriptStructures(); |
|
|
| |
| const tokenizer = new JavaScriptTokenizer(this.content, this.security); |
| const tokens = tokenizer.tokenize(); |
| const structureAnalyzer = new StructureAnalyzer(tokens, this.content); |
| const structures = structureAnalyzer.analyzeAll(); |
|
|
| |
| this.analyzeStructures(structures); |
|
|
| |
| this.findPatternsAndRelationships(); |
|
|
| |
| this.generateDynamicKeywords(); |
|
|
| |
| this.determineFileContext(); |
|
|
| |
| this.aiIntentAnalysis = this.analyzeCodeIntent(); |
|
|
| console.log(` Smart Analysis Complete: Found ${this.fileBlueprint.classes.size} classes, ${this.fileBlueprint.functions.size} functions, ${this.fileBlueprint.keywords.size} keywords`); |
|
|
| |
| if (this.aiIntentAnalysis && typeof this.aiIntentAnalysis === 'object') { |
| |
| let totalIntents = 0; |
| let totalRisks = 0; |
|
|
| if (this.aiIntentAnalysis.businessLogic) totalIntents += this.aiIntentAnalysis.businessLogic.size || 0; |
| if (this.aiIntentAnalysis.securityMeasures) totalRisks += this.aiIntentAnalysis.securityMeasures.size || 0; |
| if (this.aiIntentAnalysis.algorithmicPurpose) totalIntents += this.aiIntentAnalysis.algorithmicPurpose.size || 0; |
|
|
| console.log(` AI Intent Analysis: Detected ${totalIntents} logical patterns, ${totalRisks} security concerns`); |
| } else { |
| console.log(` AI Intent Analysis: Completed basic analysis`); |
| } |
|
|
| return this.fileBlueprint; |
|
|
| } catch (error) { |
| console.error(' Smart Analysis Error:', error.message); |
| return null; |
| } |
| } |
|
|
| |
| analyzeTypeScriptStructures() { |
| |
| this.analyzeInterfaces(); |
|
|
| |
| this.analyzeTypeAliases(); |
|
|
| |
| this.analyzeEnums(); |
|
|
| |
| this.analyzeAbstractClasses(); |
|
|
| |
| this.analyzeGenerics(); |
|
|
| |
| this.analyzeConstDeclarations(); |
| } |
|
|
| |
| analyzeConstDeclarations() { |
| const constRegex = /const\s+(\w+)\s*=\s*([^;]+);?/g; |
| let match; |
|
|
| while ((match = constRegex.exec(this.content)) !== null) { |
| const constName = match[1]; |
| const constValue = match[2].trim(); |
| const lineNumber = calculateLineNumber(this.content, match.index); |
|
|
| const constType = this.inferConstType(constName, constValue); |
|
|
| |
| if (constType === 'require' || constType === 'import') { |
| continue; |
| } |
|
|
| |
| |
| |
| |
| const constInfo = { |
| name: constName, |
| line: lineNumber, |
| type: constType, |
| value: constValue, |
| purpose: this.inferConstPurpose(constName, constValue, constType), |
| complexity: constValue.length > 200 ? 'high' : constValue.length > 50 ? 'moderate' : 'simple' |
| }; |
|
|
| |
| if (constType === 'function' || constType === 'arrow_function' || constType === 'middleware') { |
| this.fileBlueprint.functions.set(constName, constInfo); |
| this.addPattern(constName, 'const_function'); |
| } else { |
| this.fileBlueprint.classes.set(constName, constInfo); |
| this.addPattern(constName, constType); |
| } |
|
|
| this.addDynamicKeyword(constName.toLowerCase(), `${constName} ${constType}`, this.getThaiConstType(constType, constName)); |
| } |
| } |
|
|
| |
| inferConstType(constName, constValue) { |
| const name = constName.toLowerCase(); |
| const value = constValue.toLowerCase(); |
|
|
| |
| if (value.includes('require(') || value.includes('import(')) { |
| return 'require'; |
| } |
|
|
| |
| if (value.includes('=>') || value.includes('function(')) { |
| if (name.includes('middleware') || name.includes('auth') || name.includes('limit') || value.includes('(req, res')) { |
| return 'middleware'; |
| } |
| return 'arrow_function'; |
| } |
|
|
| |
| if (value.includes('express()')) { |
| return 'express_app'; |
| } |
|
|
| |
| if (name.includes('config') || name.includes('setting') || name.includes('option')) { |
| return 'configuration'; |
| } |
|
|
| |
| if (name.includes('database') || name.includes('db') || name.includes('data') || value.includes('users:') || value.includes('posts:')) { |
| return 'data_store'; |
| } |
|
|
| |
| if (name.includes('storage') || name.includes('upload') || value.includes('multer.') || value.includes('diskStorage')) { |
| return 'storage_config'; |
| } |
|
|
| |
| if (name.includes('limit') || value.includes('rateLimit(')) { |
| return 'rate_limiter'; |
| } |
|
|
| |
| if (value.startsWith('{') || value.includes('{')) { |
| return 'object'; |
| } |
|
|
| |
| if (value.startsWith('[')) { |
| return 'array'; |
| } |
|
|
| return 'constant'; |
| } |
|
|
| |
| inferConstPurpose(constName, constValue, constType) { |
| const name = constName.toLowerCase(); |
|
|
| switch (constType) { |
| case 'configuration': |
| return 'app_config'; |
| case 'data_store': |
| return 'in_memory_database'; |
| case 'storage_config': |
| return 'file_upload_config'; |
| case 'rate_limiter': |
| return 'api_rate_limiter'; |
| case 'middleware': |
| if (name.includes('auth')) return 'auth_middleware'; |
| if (name.includes('validate')) return 'validation_middleware'; |
| if (name.includes('async')) return 'async_wrapper'; |
| return 'express_middleware'; |
| case 'arrow_function': |
| if (name.includes('find')) return 'finder_function'; |
| if (name.includes('generate')) return 'generator_function'; |
| return 'utility_function'; |
| case 'express_app': |
| return 'express_application'; |
| default: |
| return constType; |
| } |
| } |
|
|
| |
| getThaiConstType(constType, constName) { |
| const name = constName.toLowerCase(); |
|
|
| switch (constType) { |
| case 'configuration': |
| return `การกำหนดค่า ${constName}`; |
| case 'data_store': |
| return `ที่เก็บข้อมูล ${constName}`; |
| case 'storage_config': |
| return `การกำหนดค่าการจัดเก็บไฟล์ ${constName}`; |
| case 'rate_limiter': |
| return `ตัวจำกัดอัตรา ${constName}`; |
| case 'middleware': |
| return `มิดเดิลแวร์ ${constName}`; |
| case 'arrow_function': |
| return `ฟังก์ชัน ${constName}`; |
| case 'express_app': |
| return `แอปพลิเคชัน Express ${constName}`; |
| case 'object': |
| return `ออบเจ็กต์ ${constName}`; |
| case 'array': |
| return `อาร์เรย์ ${constName}`; |
| default: |
| return `ค่าคงที่ ${constName}`; |
| } |
| } |
| analyzeInterfaces() { |
| |
| |
| |
| |
| const interfaceRegex = /interface\s+(\w+)(?:<[^>]*>)?\s*(?:extends\s+[^{]+)?\s*{/g; |
| let match; |
|
|
| while ((match = interfaceRegex.exec(this.content)) !== null) { |
| const interfaceName = match[1]; |
| const lineNumber = calculateLineNumber(this.content, match.index); |
|
|
| const interfaceInfo = { |
| name: interfaceName, |
| line: lineNumber, |
| type: 'interface', |
| properties: this.extractInterfaceProperties(match.index), |
| purpose: this.inferInterfacePurpose(interfaceName), |
| complexity: 'simple' |
| }; |
|
|
| this.fileBlueprint.classes.set(interfaceName, interfaceInfo); |
| this.addPattern(interfaceName, 'interface'); |
| this.addDynamicKeyword(interfaceName.toLowerCase(), `${interfaceName} interface`, `อินเทอร์เฟซ ${interfaceName}`); |
| } |
| } |
|
|
| |
| analyzeTypeAliases() { |
| const typeRegex = /type\s+(\w+)(?:<[^>]*>)?\s*=\s*([^;]+);?/g; |
| let match; |
|
|
| while ((match = typeRegex.exec(this.content)) !== null) { |
| const typeName = match[1]; |
| const typeDefinition = match[2].trim(); |
| const lineNumber = this.content.substring(0, match.index).split('\n').length; |
|
|
| const typeInfo = { |
| name: typeName, |
| line: lineNumber, |
| type: 'type_alias', |
| definition: typeDefinition, |
| purpose: this.inferTypePurpose(typeName, typeDefinition), |
| complexity: typeDefinition.length > 100 ? 'high' : 'simple' |
| }; |
|
|
| this.fileBlueprint.functions.set(typeName, typeInfo); |
| this.addPattern(typeName, 'type'); |
| this.addDynamicKeyword(typeName.toLowerCase(), `${typeName} type definition`, `นิยามประเภท ${typeName}`); |
| } |
| } |
|
|
| |
| analyzeEnums() { |
| const enumRegex = /enum\s+(\w+)\s*{([^}]+)}/g; |
| let match; |
|
|
| while ((match = enumRegex.exec(this.content)) !== null) { |
| const enumName = match[1]; |
| const enumBody = match[2]; |
| const lineNumber = this.content.substring(0, match.index).split('\n').length; |
|
|
| const enumInfo = { |
| name: enumName, |
| line: lineNumber, |
| type: 'enum', |
| values: this.extractEnumValues(enumBody), |
| purpose: this.inferEnumPurpose(enumName), |
| complexity: 'simple' |
| }; |
|
|
| this.fileBlueprint.classes.set(enumName, enumInfo); |
| this.addPattern(enumName, 'enum'); |
| this.addDynamicKeyword(enumName.toLowerCase(), `${enumName} enumeration`, `การนับ ${enumName}`); |
| } |
| } |
|
|
| |
| analyzeAbstractClasses() { |
| |
| |
| |
| |
| const abstractRegex = /abstract\s+class\s+(\w+)(?:<[^>]*>)?(?:\s+extends\s+\w+)?(?:\s+implements\s+[^{]+)?\s*{/g; |
| let match; |
|
|
| while ((match = abstractRegex.exec(this.content)) !== null) { |
| const className = match[1]; |
| const lineNumber = this.content.substring(0, match.index).split('\n').length; |
|
|
| const classInfo = { |
| name: className, |
| line: lineNumber, |
| type: 'abstract_class', |
| isAbstract: true, |
| methods: new Map(), |
| properties: new Set(), |
| purpose: this.inferAbstractClassPurpose(className), |
| complexity: 'moderate' |
| }; |
|
|
| this.fileBlueprint.classes.set(className, classInfo); |
| this.addPattern(className, 'abstract_class'); |
| this.addDynamicKeyword(className.toLowerCase(), `${className} abstract class`, `คลาสนามธรรม ${className}`); |
| } |
| } |
|
|
| |
| analyzeGenerics() { |
| const genericRegex = /<([^>]+)>/g; |
| let match; |
|
|
| while ((match = genericRegex.exec(this.content)) !== null) { |
| const genericParams = match[1].split(',').map(param => param.trim()); |
|
|
| genericParams.forEach(param => { |
| if (param.length === 1 && /[A-Z]/.test(param)) { |
| this.addDynamicKeyword(`generic_${param.toLowerCase()}`, `Generic type ${param}`, `ประเภททั่วไป ${param}`); |
| } |
| }); |
| } |
| } |
| analyzeStructures(structures) { |
| structures.forEach(structure => { |
| if (structure.type === 'class_declaration') { |
| this.analyzeClass(structure); |
| } else { |
| this.analyzeFunction(structure); |
| } |
| }); |
| } |
|
|
| |
| analyzeClass(classStructure) { |
| const className = classStructure.name; |
| const classInfo = { |
| name: className, |
| line: classStructure.line, |
| methods: new Map(), |
| properties: new Set(), |
| extends: null, |
| implements: [], |
| purpose: this.inferClassPurpose(className), |
| complexity: this.calculateClassComplexity(classStructure) |
| }; |
|
|
| |
| if (classStructure.methods) { |
| classStructure.methods.forEach(method => { |
| const methodInfo = { |
| name: method.name, |
| type: method.type, |
| line: method.line, |
| isAsync: method.isAsync || false, |
| isStatic: method.isStatic || false, |
| kind: method.kind || 'method', |
| parameters: method.parameters || [], |
| purpose: this.inferMethodPurpose(method.name, className), |
| accessibility: this.inferAccessibility(method.name) |
| }; |
| classInfo.methods.set(method.name, methodInfo); |
| }); |
| } |
|
|
| this.fileBlueprint.classes.set(className, classInfo); |
| this.addPattern(className, 'class'); |
| } |
|
|
| |
| analyzeFunction(funcStructure) { |
| const functionName = funcStructure.name; |
| const functionInfo = { |
| name: functionName, |
| type: funcStructure.type, |
| line: funcStructure.line, |
| parameters: funcStructure.parameters || [], |
| isAsync: funcStructure.isAsync || false, |
| isGenerator: funcStructure.isGenerator || false, |
| purpose: this.inferFunctionPurpose(functionName), |
| scope: 'global', |
| complexity: this.calculateFunctionComplexity(funcStructure) |
| }; |
|
|
| this.fileBlueprint.functions.set(functionName, functionInfo); |
| this.addPattern(functionName, 'function'); |
| } |
|
|
| |
| findPatternsAndRelationships() { |
| |
| for (const [className, classInfo] of this.fileBlueprint.classes) { |
| |
| this.findClassRelationships(className, classInfo); |
|
|
| |
| this.detectDesignPatterns(className, classInfo); |
| } |
|
|
| |
| for (const [funcName, funcInfo] of this.fileBlueprint.functions) { |
| this.findFunctionRelationships(funcName, funcInfo); |
| } |
| } |
|
|
| |
| generateDynamicKeywords() { |
| |
| if (!this.fileBlueprint || !this.fileBlueprint.classes || !this.fileBlueprint.functions) { |
| console.log(' Warning: fileBlueprint not properly initialized, skipping keyword generation'); |
| return; |
| } |
|
|
| try { |
| |
| for (const className of this.fileBlueprint.classes.keys()) { |
| this.addDynamicKeyword(className.toLowerCase(), `${className} class`, `คลาส ${className}`); |
|
|
| |
| if (className.endsWith('Manager')) { |
| this.addDynamicKeyword(className.toLowerCase(), `${className} manager`, `ตัวจัดการ ${className.replace('Manager', '')}`); |
| } |
| if (className.endsWith('Controller')) { |
| this.addDynamicKeyword(className.toLowerCase(), `${className} controller`, `ตัวควบคุม ${className.replace('Controller', '')}`); |
| } |
| if (className.endsWith('Service')) { |
| this.addDynamicKeyword(className.toLowerCase(), `${className} service`, `บริการ ${className.replace('Service', '')}`); |
| } |
| } |
|
|
| |
| const allMethods = new Set(); |
| for (const classInfo of this.fileBlueprint.classes.values()) { |
| if (classInfo.methods) { |
| for (const methodName of classInfo.methods.keys()) { |
| allMethods.add(methodName); |
| } |
| } |
| } |
| for (const funcName of this.fileBlueprint.functions.keys()) { |
| allMethods.add(funcName); |
| } |
|
|
| |
| for (const methodName of allMethods) { |
| this.addMethodSpecificKeywords(methodName); |
| } |
|
|
| } catch (error) { |
| console.log(' Warning: Error generating dynamic keywords:', error.message); |
| } |
| } |
|
|
| |
| addMethodSpecificKeywords(methodName) { |
| const name = methodName.toLowerCase(); |
|
|
| |
| if (name.includes('create') || name.includes('add') || name.includes('insert')) { |
| this.addDynamicKeyword(name, `Create ${methodName}`, `สร้าง ${methodName}`); |
| } |
| if (name.includes('get') || name.includes('fetch') || name.includes('find') || name.includes('retrieve')) { |
| this.addDynamicKeyword(name, `Get ${methodName}`, `ดึง ${methodName}`); |
| } |
| if (name.includes('update') || name.includes('modify') || name.includes('edit') || name.includes('change')) { |
| this.addDynamicKeyword(name, `Update ${methodName}`, `อัปเดต ${methodName}`); |
| } |
| if (name.includes('delete') || name.includes('remove') || name.includes('destroy')) { |
| this.addDynamicKeyword(name, `Delete ${methodName}`, `ลบ ${methodName}`); |
| } |
|
|
| |
| if (name.includes('validate') || name.includes('check') || name.includes('verify')) { |
| this.addDynamicKeyword(name, `Validate ${methodName}`, `ตรวจสอบ ${methodName}`); |
| } |
| if (name.includes('process') || name.includes('handle') || name.includes('execute')) { |
| this.addDynamicKeyword(name, `Process ${methodName}`, `ประมวลผล ${methodName}`); |
| } |
|
|
| |
| if (name.includes('generate') && name.includes('key')) { |
| this.addDynamicKeyword(name, 'Generate encryption key', 'สร้างคีย์การเข้ารหัส'); |
| } |
| if (name.includes('encrypt')) { |
| this.addDynamicKeyword(name, 'Encrypt data', 'เข้ารหัสข้อมูล'); |
| } |
| if (name.includes('decrypt')) { |
| this.addDynamicKeyword(name, 'Decrypt data', 'ถอดรหัสข้อมูล'); |
| } |
| if (name.includes('cache') && name.includes('size')) { |
| this.addDynamicKeyword(name, 'Get cache size', 'ดึงขนาดแคช'); |
| } |
| if (name.includes('max') && name.includes('cache')) { |
| this.addDynamicKeyword(name, 'Set maximum cache size', 'กำหนดขนาดแคชสูงสุด'); |
| } |
| } |
|
|
| |
| determineFileContext() { |
| |
| if (!this.fileBlueprint || !this.fileBlueprint.classes || !this.fileBlueprint.functions) { |
| this.fileBlueprint.context = { |
| type: 'unknown', |
| domain: 'unknown', |
| complexity: 'simple' |
| }; |
| return; |
| } |
|
|
| const classNames = Array.from(this.fileBlueprint.classes.keys()).map(name => name.toLowerCase()); |
| const functionNames = Array.from(this.fileBlueprint.functions.keys()).map(name => name.toLowerCase()); |
| const allNames = [...classNames, ...functionNames]; |
|
|
| |
| const content = this.content.toLowerCase(); |
|
|
| |
| if (content.includes('express') || content.includes('app.get') || content.includes('app.post') || |
| content.includes('req, res') || allNames.some(name => name.includes('app') || name.includes('server') || name.includes('router'))) { |
| this.fileBlueprint.context.type = 'api'; |
| this.fileBlueprint.context.domain = 'web'; |
| } else if (allNames.some(name => name.includes('crypto') || name.includes('encrypt') || name.includes('hash')) || |
| content.includes('bcrypt') || content.includes('jwt') || content.includes('crypto')) { |
| this.fileBlueprint.context.type = 'security'; |
| this.fileBlueprint.context.domain = 'crypto'; |
| } else if (allNames.some(name => name.includes('database') || name.includes('db') || name.includes('sql')) || |
| content.includes('database') || content.includes('mongodb') || content.includes('mysql')) { |
| this.fileBlueprint.context.type = 'database'; |
| this.fileBlueprint.context.domain = 'data'; |
| } else if (allNames.some(name => name.includes('api') || name.includes('client') || name.includes('request'))) { |
| this.fileBlueprint.context.type = 'api'; |
| this.fileBlueprint.context.domain = 'web'; |
| } else if (allNames.some(name => name.includes('algorithm') || name.includes('sort') || name.includes('search'))) { |
| this.fileBlueprint.context.type = 'algorithm'; |
| this.fileBlueprint.context.domain = 'computation'; |
| } else if (allNames.some(name => name.includes('component') || name.includes('render') || name.includes('ui'))) { |
| this.fileBlueprint.context.type = 'ui'; |
| this.fileBlueprint.context.domain = 'frontend'; |
| } |
| const totalStructures = this.fileBlueprint.classes.size + this.fileBlueprint.functions.size; |
| if (totalStructures > 20) { |
| this.fileBlueprint.context.complexity = 'complex'; |
| } else if (totalStructures > 10) { |
| this.fileBlueprint.context.complexity = 'moderate'; |
| } |
| } |
|
|
| |
| addPattern(name, type) { |
| this.fileBlueprint.patterns.add(`${type}:${name.toLowerCase()}`); |
| } |
|
|
| addDynamicKeyword(key, english, thai) { |
| this.fileBlueprint.keywords.add(JSON.stringify({ key, english, thai })); |
| } |
|
|
| inferClassPurpose(className) { |
| const name = className.toLowerCase(); |
| if (name.includes('manager')) return 'manager'; |
| if (name.includes('controller')) return 'controller'; |
| if (name.includes('service')) return 'service'; |
| if (name.includes('model')) return 'model'; |
| if (name.includes('view')) return 'view'; |
| if (name.includes('component')) return 'component'; |
| if (name.includes('error')) return 'error'; |
| if (name.includes('validator')) return 'validator'; |
| if (name.includes('parser')) return 'parser'; |
| if (name.includes('tokenizer')) return 'tokenizer'; |
| return 'class'; |
| } |
|
|
| inferMethodPurpose(methodName, className) { |
| const method = methodName.toLowerCase(); |
| const cls = className.toLowerCase(); |
|
|
| |
| if (cls.includes('crypto') || cls.includes('security')) { |
| if (method.includes('generate')) return 'generate_crypto'; |
| if (method.includes('encrypt')) return 'encrypt_data'; |
| if (method.includes('decrypt')) return 'decrypt_data'; |
| } |
|
|
| if (cls.includes('cache') || cls.includes('memory')) { |
| if (method.includes('size')) return 'get_size'; |
| if (method.includes('clear')) return 'clear_cache'; |
| if (method.includes('max')) return 'set_limit'; |
| } |
|
|
| |
| if (method.includes('get') || method.includes('fetch')) return 'getter'; |
| if (method.includes('set') || method.includes('assign')) return 'setter'; |
| if (method.includes('create') || method.includes('add')) return 'creator'; |
| if (method.includes('delete') || method.includes('remove')) return 'destroyer'; |
| if (method.includes('update') || method.includes('modify')) return 'updater'; |
| if (method.includes('validate') || method.includes('check')) return 'validator'; |
| if (method.includes('process') || method.includes('handle')) return 'processor'; |
|
|
| return 'method'; |
| } |
|
|
| inferFunctionPurpose(functionName) { |
| const name = functionName.toLowerCase(); |
| if (name.includes('demo') || name.includes('test') || name.includes('example')) return 'demo'; |
| if (name.includes('init') || name.includes('setup')) return 'initializer'; |
| if (name.includes('main') || name.includes('run')) return 'main'; |
| if (name.includes('helper') || name.includes('util')) return 'utility'; |
| return 'function'; |
| } |
|
|
| inferAccessibility(methodName) { |
| if (methodName.startsWith('_')) return 'private'; |
| if (methodName.startsWith('#')) return 'private'; |
| return 'public'; |
| } |
|
|
| calculateClassComplexity(classStructure) { |
| const methodCount = classStructure.methods ? classStructure.methods.length : 0; |
| if (methodCount > 15) return 'high'; |
| if (methodCount > 8) return 'medium'; |
| return 'low'; |
| } |
|
|
| calculateFunctionComplexity(funcStructure) { |
| const paramCount = funcStructure.parameters ? funcStructure.parameters.length : 0; |
| if (paramCount > 5) return 'high'; |
| if (paramCount > 2) return 'medium'; |
| return 'low'; |
| } |
|
|
| findClassRelationships(className, classInfo) { |
| |
| |
| } |
|
|
| detectDesignPatterns(className, classInfo) { |
| |
| |
| } |
|
|
| findFunctionRelationships(funcName, funcInfo) { |
| |
| |
| } |
|
|
| |
| |
| |
|
|
| |
| extractInterfaceProperties(startIndex) { |
| const properties = []; |
| const startPos = this.content.indexOf('{', startIndex); |
| const endPos = this.findMatchingBrace(startPos); |
|
|
| if (startPos === -1 || endPos === -1) return properties; |
|
|
| const interfaceBody = this.content.substring(startPos + 1, endPos); |
| const lines = interfaceBody.split('\n'); |
|
|
| lines.forEach(line => { |
| const trimmed = line.trim(); |
| if (trimmed && !trimmed.startsWith('//') && !trimmed.startsWith('/*')) { |
| const propertyMatch = trimmed.match(/(\w+)(\?)?:\s*([^;]+);?/); |
| if (propertyMatch) { |
| properties.push({ |
| name: propertyMatch[1], |
| optional: !!propertyMatch[2], |
| type: propertyMatch[3].trim() |
| }); |
| } |
| } |
| }); |
|
|
| return properties; |
| } |
|
|
| |
| extractEnumValues(enumBody) { |
| const values = []; |
| const lines = enumBody.split('\n'); |
|
|
| lines.forEach(line => { |
| const trimmed = line.trim(); |
| if (trimmed && !trimmed.startsWith('//')) { |
| const valueMatch = trimmed.match(/(\w+)(?:\s*=\s*([^,]+))?/); |
| if (valueMatch) { |
| values.push({ |
| name: valueMatch[1], |
| value: valueMatch[2] ? valueMatch[2].trim() : null |
| }); |
| } |
| } |
| }); |
|
|
| return values; |
| } |
|
|
| |
| findMatchingBrace(startPos) { |
| let depth = 1; |
| let pos = startPos + 1; |
|
|
| while (pos < this.content.length && depth > 0) { |
| const char = this.content[pos]; |
| if (char === '{') depth++; |
| else if (char === '}') depth--; |
| pos++; |
| } |
|
|
| return depth === 0 ? pos - 1 : -1; |
| } |
|
|
| |
| inferInterfacePurpose(interfaceName) { |
| const name = interfaceName.toLowerCase(); |
|
|
| if (name.includes('config') || name.includes('options')) return 'configuration'; |
| if (name.includes('result') || name.includes('response')) return 'result_type'; |
| if (name.includes('request') || name.includes('params')) return 'input_type'; |
| if (name.includes('error') || name.includes('exception')) return 'error_type'; |
| if (name.includes('event') || name.includes('listener')) return 'event_type'; |
| if (name.includes('data') || name.includes('model')) return 'data_type'; |
| if (name.includes('service') || name.includes('client')) return 'service_interface'; |
| if (name.includes('repository') || name.includes('dao')) return 'repository_interface'; |
|
|
| return 'interface'; |
| } |
|
|
| |
| inferTypePurpose(typeName, typeDefinition) { |
| const name = typeName.toLowerCase(); |
| const definition = typeDefinition.toLowerCase(); |
|
|
| if (name.includes('listener') || name.includes('callback') || name.includes('handler')) return 'callback_type'; |
| if (name.includes('rule') || name.includes('schema') || name.includes('validation')) return 'validation_type'; |
| if (name.includes('strategy') || name.includes('policy')) return 'strategy_type'; |
| if (definition.includes('string') && definition.includes('|')) return 'string_union'; |
| if (definition.includes('number') && definition.includes('|')) return 'number_union'; |
| if (definition.includes('=>')) return 'function_type'; |
| if (definition.includes('{') && definition.includes('}')) return 'object_type'; |
|
|
| return 'type_alias'; |
| } |
|
|
| |
| inferEnumPurpose(enumName) { |
| const name = enumName.toLowerCase(); |
|
|
| if (name.includes('status') || name.includes('state')) return 'status_enum'; |
| if (name.includes('type') || name.includes('kind')) return 'type_enum'; |
| if (name.includes('level') || name.includes('priority')) return 'level_enum'; |
| if (name.includes('direction') || name.includes('order')) return 'direction_enum'; |
| if (name.includes('mode') || name.includes('strategy')) return 'mode_enum'; |
|
|
| return 'enumeration'; |
| } |
|
|
| |
| inferAbstractClassPurpose(className) { |
| const name = className.toLowerCase(); |
|
|
| if (name.includes('base') || name.includes('abstract')) return 'base_class'; |
| if (name.includes('repository') || name.includes('dao')) return 'repository_base'; |
| if (name.includes('service') || name.includes('provider')) return 'service_base'; |
| if (name.includes('controller') || name.includes('handler')) return 'controller_base'; |
| if (name.includes('component') || name.includes('widget')) return 'component_base'; |
|
|
| return 'abstract_class'; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| analyzeCodeIntent() { |
| try { |
| console.log('\n AI Intent Understanding Engine: เริ่มการวิเคราะห์ความตั้งใจของโค้ด...'); |
|
|
| |
| const intentMap = { |
| businessLogic: new Map(), |
| dataFlow: new Map(), |
| errorHandling: new Map(), |
| performanceOptimization: new Map(), |
| securityMeasures: new Map(), |
| userInteraction: new Map(), |
| systemIntegration: new Map(), |
| algorithmicPurpose: new Map() |
| }; |
|
|
| |
| this.analyzeClassesIntent(intentMap); |
| this.analyzeFunctionsIntent(intentMap); |
| this.analyzeDataFlowIntent(intentMap); |
| this.analyzeLogicalPatterns(intentMap); |
|
|
| |
| this.generateIntentBasedComments(intentMap); |
|
|
| console.log(' AI Intent Analysis เสร็จสมบูรณ์'); |
| return intentMap; |
|
|
| } catch (error) { |
| console.error(' AI Intent Analysis ล้มเหลว:', error.message); |
| return null; |
| } |
| } |
|
|
| |
| analyzeClassesIntent(intentMap) { |
| this.fileBlueprint.classes.forEach((classInfo, className) => { |
| |
| const businessIntent = this.inferBusinessIntent(className, classInfo); |
| if (businessIntent) { |
| intentMap.businessLogic.set(className, businessIntent); |
| } |
|
|
| |
| const architectureRole = this.inferArchitectureRole(className, classInfo); |
| if (architectureRole) { |
| intentMap.systemIntegration.set(className, architectureRole); |
| } |
|
|
| |
| const securityRole = this.inferSecurityRole(className, classInfo); |
| if (securityRole) { |
| intentMap.securityMeasures.set(className, securityRole); |
| } |
| }); |
| } |
|
|
| |
| analyzeFunctionsIntent(intentMap) { |
| this.fileBlueprint.functions.forEach((funcInfo, funcName) => { |
| |
| const algorithmIntent = this.inferAlgorithmicIntent(funcName, funcInfo); |
| if (algorithmIntent) { |
| intentMap.algorithmicPurpose.set(funcName, algorithmIntent); |
| } |
|
|
| |
| const performanceIntent = this.inferPerformanceIntent(funcName, funcInfo); |
| if (performanceIntent) { |
| intentMap.performanceOptimization.set(funcName, performanceIntent); |
| } |
|
|
| |
| const errorHandlingIntent = this.inferErrorHandlingIntent(funcName, funcInfo); |
| if (errorHandlingIntent) { |
| intentMap.errorHandling.set(funcName, errorHandlingIntent); |
| } |
| }); |
| } |
|
|
| |
| analyzeDataFlowIntent(intentMap) { |
| |
| const dataFlowPatterns = this.extractDataFlowPatterns(); |
|
|
| dataFlowPatterns.forEach((pattern, operation) => { |
| const intent = this.inferDataFlowIntent(operation, pattern); |
| if (intent) { |
| intentMap.dataFlow.set(operation, intent); |
| } |
| }); |
| } |
|
|
| |
| analyzeLogicalPatterns(intentMap) { |
| |
| const conditionalPatterns = this.extractConditionalPatterns(); |
|
|
| conditionalPatterns.forEach((pattern, condition) => { |
| const logicalIntent = this.inferLogicalIntent(condition, pattern); |
| if (logicalIntent) { |
| intentMap.businessLogic.set(`logic_${condition}`, logicalIntent); |
| } |
| }); |
| } |
|
|
| |
| inferBusinessIntent(className, classInfo) { |
| const name = className.toLowerCase(); |
| const methods = Array.isArray(classInfo.methods) ? classInfo.methods : []; |
|
|
| |
| if (name.includes('order') || name.includes('cart') || name.includes('payment')) { |
| return { |
| domain: 'e-commerce', |
| purpose: 'การจัดการการซื้อขายและการชำระเงิน', |
| businessValue: 'สร้างรายได้และจัดการธุรกรรม', |
| userImpact: 'ช่วยให้ผู้ใช้สามารถซื้อสินค้าได้อย่างปลอดภัย' |
| }; |
| } |
|
|
| |
| if (name.includes('user') || name.includes('auth') || name.includes('account')) { |
| return { |
| domain: 'user-management', |
| purpose: 'การจัดการผู้ใช้และการรับรองตัวตน', |
| businessValue: 'สร้างและรักษาฐานผู้ใช้', |
| userImpact: 'ให้ผู้ใช้เข้าถึงระบบได้อย่างปลอดภัย' |
| }; |
| } |
|
|
| |
| if (name.includes('processor') || name.includes('analyzer') || name.includes('parser')) { |
| return { |
| domain: 'data-processing', |
| purpose: 'การประมวลผลและวิเคราะห์ข้อมูล', |
| businessValue: 'เปลี่ยนข้อมูลดิบให้เป็นข้อมูลที่มีค่า', |
| userImpact: 'ได้รับข้อมูลที่ถูกต้องและมีประโยชน์' |
| }; |
| } |
|
|
| return null; |
| } |
| inferArchitectureRole(className, classInfo) { |
| const name = className.toLowerCase(); |
|
|
| |
| if (name.includes('controller')) { |
| return { |
| pattern: 'MVC', |
| role: 'Controller', |
| responsibility: 'จัดการ Request และ Response', |
| coupling: 'กลาง - เชื่อมต่อ Model และ View' |
| }; |
| } |
|
|
| if (name.includes('model') || name.includes('entity')) { |
| return { |
| pattern: 'MVC', |
| role: 'Model', |
| responsibility: 'จัดการข้อมูลและ Business Logic', |
| coupling: 'ต่ำ - ไม่ขึ้นกับ UI' |
| }; |
| } |
|
|
| |
| if (name.includes('repository') || name.includes('dao')) { |
| return { |
| pattern: 'Repository', |
| role: 'Data Access', |
| responsibility: 'แยก Business Logic จาก Data Access', |
| coupling: 'ต่ำ - เฉพาะ Data Layer' |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| inferSecurityRole(className, classInfo) { |
| const name = className.toLowerCase(); |
| const methods = Array.isArray(classInfo.methods) ? classInfo.methods : []; |
|
|
| if (name.includes('security') || name.includes('crypto') || name.includes('auth')) { |
| const hasEncryption = methods.some(m => (typeof m === 'string' ? m : '').includes('encrypt') || (typeof m === 'string' ? m : '').includes('decrypt')); |
| const hasValidation = methods.some(m => (typeof m === 'string' ? m : '').includes('validate') || (typeof m === 'string' ? m : '').includes('verify')); |
|
|
| return { |
| level: hasEncryption ? 'high' : 'medium', |
| measures: { |
| encryption: hasEncryption, |
| validation: hasValidation, |
| authentication: name.includes('auth') |
| }, |
| threat_protection: 'ป้องกันการเข้าถึงที่ไม่ได้รับอนุญาต' |
| }; |
| } |
|
|
| return null; |
| } |
| inferAlgorithmicIntent(funcName, funcInfo) { |
| const name = funcName.toLowerCase(); |
| const params = funcInfo.parameters || []; |
|
|
| |
| if (name.includes('sort')) { |
| return { |
| type: 'sorting', |
| purpose: 'จัดเรียงข้อมูลตามเกณฑ์ที่กำหนด', |
| complexity: params.length > 2 ? 'complex' : 'simple', |
| optimization: 'เพื่อการค้นหาและประมวลผลที่รวดเร็ว' |
| }; |
| } |
|
|
| |
| if (name.includes('search') || name.includes('find')) { |
| return { |
| type: 'searching', |
| purpose: 'ค้นหาข้อมูลตามเงื่อนไขที่กำหนด', |
| strategy: name.includes('binary') ? 'binary_search' : 'linear_search', |
| optimization: 'ลดเวลาในการค้นหาข้อมูล' |
| }; |
| } |
|
|
| |
| if (name.includes('validate') || name.includes('check')) { |
| return { |
| type: 'validation', |
| purpose: 'ตรวจสอบความถูกต้องของข้อมูล', |
| scope: 'data_integrity', |
| importance: 'critical' |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| inferPerformanceIntent(funcName, funcInfo) { |
| const name = funcName.toLowerCase(); |
|
|
| if (name.includes('cache') || name.includes('memoize')) { |
| return { |
| strategy: 'caching', |
| goal: 'ลดการคำนวณซ้ำ', |
| impact: 'เพิ่มความเร็วในการตอบสนอง' |
| }; |
| } |
|
|
| if (name.includes('async') || name.includes('promise')) { |
| return { |
| strategy: 'asynchronous', |
| goal: 'ป้องกันการบล็อกการทำงาน', |
| impact: 'ทำงานหลายอย่างพร้อมกันได้' |
| }; |
| } |
|
|
| if (name.includes('optimize') || name.includes('efficient')) { |
| return { |
| strategy: 'optimization', |
| goal: 'ลดการใช้ทรัพยากร', |
| impact: 'ประหยัดหน่วยความจำและพลังงาน' |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| inferErrorHandlingIntent(funcName, funcInfo) { |
| const name = funcName.toLowerCase(); |
|
|
| if (name.includes('try') || name.includes('catch') || name.includes('handle')) { |
| return { |
| approach: 'defensive_programming', |
| goal: 'ป้องกันการ crash ของระบบ', |
| user_experience: 'ให้ข้อความ error ที่เข้าใจง่าย' |
| }; |
| } |
|
|
| if (name.includes('retry') || name.includes('fallback')) { |
| return { |
| approach: 'resilience', |
| goal: 'ทำให้ระบบทำงานต่อได้แม้เกิดปัญหา', |
| reliability: 'สูง' |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| extractDataFlowPatterns() { |
| const patterns = new Map(); |
| const lines = this.content.split('\n'); |
|
|
| lines.forEach((line, index) => { |
| const trimmed = line.trim(); |
|
|
| |
| if (trimmed.includes('=') && !trimmed.includes('==') && !trimmed.includes('!=')) { |
| const assignment = trimmed.match(/(\w+)\s*=\s*(.+)/); |
| if (assignment) { |
| patterns.set(`assignment_${index}`, { |
| variable: assignment[1], |
| value: assignment[2], |
| line: index + 1, |
| intent: 'data_transformation' |
| }); |
| } |
| } |
|
|
| |
| if (trimmed.includes('.map(') || trimmed.includes('.filter(') || |
| trimmed.includes('.reduce(') || trimmed.includes('.transform(')) { |
| patterns.set(`transformation_${index}`, { |
| type: 'data_transformation', |
| line: index + 1, |
| intent: 'data_processing' |
| }); |
| } |
| }); |
|
|
| return patterns; |
| } |
|
|
| |
| extractConditionalPatterns() { |
| const patterns = new Map(); |
| const lines = this.content.split('\n'); |
|
|
| lines.forEach((line, index) => { |
| const trimmed = line.trim(); |
|
|
| |
| if (trimmed.startsWith('if ') || trimmed.includes('if(')) { |
| patterns.set(`condition_${index}`, { |
| type: 'conditional_logic', |
| line: index + 1, |
| condition: trimmed, |
| intent: 'decision_making' |
| }); |
| } |
|
|
| |
| if (trimmed.startsWith('switch ') || trimmed.includes('switch(')) { |
| patterns.set(`switch_${index}`, { |
| type: 'multiple_choice', |
| line: index + 1, |
| intent: 'route_selection' |
| }); |
| } |
| }); |
|
|
| return patterns; |
| } |
|
|
| |
| inferDataFlowIntent(operation, pattern) { |
| if (pattern.intent === 'data_transformation') { |
| return { |
| purpose: 'เปลี่ยนรูปแบบข้อมูลให้เหมาะกับการใช้งาน', |
| impact: 'ทำให้ข้อมูลใช้งานได้ง่ายขึ้น', |
| type: 'transformation' |
| }; |
| } |
|
|
| if (pattern.intent === 'data_processing') { |
| return { |
| purpose: 'ประมวลผลข้อมูลเพื่อหาผลลัพธ์ที่ต้องการ', |
| impact: 'ได้ข้อมูลใหม่ที่มีค่ามากขึ้น', |
| type: 'processing' |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| inferLogicalIntent(condition, pattern) { |
| if (pattern.intent === 'decision_making') { |
| return { |
| purpose: 'ตัดสินใจเลือกเส้นทางการทำงาน', |
| impact: 'ควบคุมการไหลของโปรแกรม', |
| complexity: 'medium' |
| }; |
| } |
|
|
| if (pattern.intent === 'route_selection') { |
| return { |
| purpose: 'เลือกการดำเนินการตามสถานการณ์', |
| impact: 'จัดการกรณีต่างๆ ได้อย่างเป็นระบบ', |
| complexity: 'high' |
| }; |
| } |
|
|
| return null; |
| } |
|
|
| |
| generateIntentBasedComments(intentMap) { |
| console.log('\n กำลังสร้าง Smart Comments จาก AI Intent Analysis...'); |
|
|
| |
| intentMap.businessLogic.forEach((intent, item) => { |
| if (intent.purpose && intent.businessValue) { |
| const smartComment = this.createSmartComment(item, intent, 'business'); |
| this.addSmartCommentToBlueprint(item, smartComment); |
| } |
| }); |
|
|
| |
| intentMap.algorithmicPurpose.forEach((intent, item) => { |
| if (intent.purpose && intent.optimization) { |
| const smartComment = this.createSmartComment(item, intent, 'algorithm'); |
| this.addSmartCommentToBlueprint(item, smartComment); |
| } |
| }); |
|
|
| |
| intentMap.securityMeasures.forEach((intent, item) => { |
| if (intent.threat_protection) { |
| const smartComment = this.createSmartComment(item, intent, 'security'); |
| this.addSmartCommentToBlueprint(item, smartComment); |
| } |
| }); |
|
|
| console.log(' Smart Comments สร้างเสร็จแล้ว'); |
| } |
|
|
| |
| createSmartComment(item, intent, category) { |
| switch (category) { |
| case 'business': |
| return { |
| thai: `// ${intent.purpose} - ${intent.businessValue}`, |
| english: `// ${intent.purpose} - Creates business value through ${intent.businessValue}`, |
| impact: intent.userImpact |
| }; |
|
|
| case 'algorithm': |
| return { |
| thai: `// ${intent.purpose} - เพื่อ${intent.optimization}`, |
| english: `// ${intent.purpose} - Optimized for ${intent.optimization}`, |
| complexity: intent.complexity |
| }; |
|
|
| case 'security': |
| return { |
| thai: `// ${intent.threat_protection} - ระดับความปลอดภัย: ${intent.level}`, |
| english: `// ${intent.threat_protection} - Security level: ${intent.level}`, |
| measures: intent.measures |
| }; |
|
|
| default: |
| return { |
| thai: `// วัตถุประสงค์: ${intent.purpose || 'ไม่ระบุ'}`, |
| english: `// Purpose: ${intent.purpose || 'Not specified'}` |
| }; |
| } |
| } |
|
|
| |
| addSmartCommentToBlueprint(item, smartComment) { |
| if (!this.fileBlueprint.smartComments) { |
| this.fileBlueprint.smartComments = new Map(); |
| } |
|
|
| this.fileBlueprint.smartComments.set(item, smartComment); |
| } |
|
|
| |
| getIntentAnalysisReport() { |
| const intentAnalysis = this.analyzeCodeIntent(); |
|
|
| if (!intentAnalysis) { |
| return null; |
| } |
|
|
| return { |
| summary: { |
| totalBusinessLogic: intentAnalysis.businessLogic ? intentAnalysis.businessLogic.size : 0, |
| totalAlgorithms: intentAnalysis.algorithmicPurpose ? intentAnalysis.algorithmicPurpose.size : 0, |
| totalSecurityMeasures: intentAnalysis.securityMeasures ? intentAnalysis.securityMeasures.size : 0, |
| totalDataFlows: intentAnalysis.dataFlow ? intentAnalysis.dataFlow.size : 0 |
| }, |
| details: { |
| businessLogic: intentAnalysis.businessLogic ? Array.from(intentAnalysis.businessLogic.entries()) : [], |
| algorithms: intentAnalysis.algorithmicPurpose ? Array.from(intentAnalysis.algorithmicPurpose.entries()) : [], |
| security: intentAnalysis.securityMeasures ? Array.from(intentAnalysis.securityMeasures.entries()) : [], |
| dataFlow: intentAnalysis.dataFlow ? Array.from(intentAnalysis.dataFlow.entries()) : [] |
| }, |
| smartComments: this.fileBlueprint && this.fileBlueprint.smartComments ? |
| Array.from(this.fileBlueprint.smartComments.entries()) : [] |
| }; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| class StructureAnalyzer { |
| constructor(tokens, content) { |
| this.tokens = tokens.filter(t => t.type !== TOKEN_TYPES.WHITESPACE); |
| this.content = content; |
| this.lines = content.split('\n'); |
| this.structures = []; |
| } |
|
|
| |
| |
| |
| |
| |
| analyzeAll() { |
| let cursor = 0; |
|
|
| while (cursor < this.tokens.length) { |
| const structure = this.analyzeStructureAt(cursor); |
| if (structure) { |
| this.structures.push(structure); |
| cursor = structure.endTokenIndex || cursor + 1; |
| } else { |
| cursor++; |
| } |
| } |
|
|
| return this.structures; |
| } |
|
|
| |
| analyzeStructureAt(startCursor) { |
| const token = this.tokens[startCursor]; |
| if (!token) return null; |
|
|
| |
| if (token.type === TOKEN_TYPES.KEYWORD && token.value === 'interface') { |
| return this.analyzeInterface(startCursor); |
| } |
|
|
| |
| if (token.type === TOKEN_TYPES.KEYWORD && token.value === 'type') { |
| return this.analyzeTypeAlias(startCursor); |
| } |
|
|
| |
| if (token.type === TOKEN_TYPES.KEYWORD && token.value === 'class') { |
| return this.analyzeClass(startCursor); |
| } |
|
|
| |
| if (token.type === TOKEN_TYPES.KEYWORD && token.value === 'abstract') { |
| const nextToken = this.tokens[startCursor + 1]; |
| if (nextToken && nextToken.value === 'class') { |
| return this.analyzeAbstractClass(startCursor); |
| } |
| } |
|
|
| |
| if (token.type === TOKEN_TYPES.KEYWORD && |
| ['const', 'let', 'var'].includes(token.value)) { |
| const result = this.analyzeVariableDeclaration(startCursor); |
| if (result) return result; |
| } |
|
|
| |
| if (token.type === TOKEN_TYPES.KEYWORD && token.value === 'function') { |
| return this.analyzeFunction(startCursor); |
| } |
|
|
| return null; |
| } |
|
|
| |
| analyzeClass(startCursor) { |
| const classToken = this.tokens[startCursor]; |
| const nameToken = this.tokens[startCursor + 1]; |
|
|
| if (!nameToken || nameToken.type !== TOKEN_TYPES.IDENTIFIER) { |
| return null; |
| } |
|
|
| |
| const classBounds = this.findBlockBounds(startCursor + 2, TOKEN_TYPES.BRACE_OPEN); |
| if (!classBounds) return null; |
|
|
| |
| const members = this.analyzeClassMembers(classBounds.startIndex, classBounds.endIndex); |
|
|
| |
| const classContent = this.extractContentBetweenLines(classToken.line, classBounds.endLine); |
| const purpose = this.inferClassPurpose(nameToken.value, classContent, members); |
|
|
| return { |
| type: 'class', |
| name: nameToken.value, |
| line: classToken.line, |
| column: classToken.column, |
| startTokenIndex: startCursor, |
| endTokenIndex: classBounds.endTokenIndex, |
| endLine: classBounds.endLine, |
| members: members, |
| purpose: purpose, |
| content: classContent |
| }; |
| } |
|
|
| |
| analyzeVariableDeclaration(startCursor) { |
| const keywordToken = this.tokens[startCursor]; |
| const nameToken = this.tokens[startCursor + 1]; |
| const equalsToken = this.tokens[startCursor + 2]; |
|
|
| if (!nameToken || nameToken.type !== TOKEN_TYPES.IDENTIFIER || |
| !equalsToken || equalsToken.type !== TOKEN_TYPES.EQUALS) { |
| return null; |
| } |
|
|
| |
| const arrowFuncResult = this.analyzeArrowFunction(startCursor); |
| if (arrowFuncResult) return arrowFuncResult; |
|
|
| |
| const braceToken = this.tokens[startCursor + 3]; |
| if (braceToken && braceToken.type === TOKEN_TYPES.BRACE_OPEN) { |
| return this.analyzeObjectDeclaration(startCursor); |
| } |
|
|
| return null; |
| } |
|
|
| |
| analyzeArrowFunction(startCursor) { |
| const keywordToken = this.tokens[startCursor]; |
| const nameToken = this.tokens[startCursor + 1]; |
| const equalsToken = this.tokens[startCursor + 2]; |
|
|
| if (!nameToken || nameToken.type !== TOKEN_TYPES.IDENTIFIER || |
| !equalsToken || equalsToken.type !== TOKEN_TYPES.EQUALS) { |
| return null; |
| } |
|
|
| |
| let arrowIndex = -1; |
| for (let i = startCursor + 3; i < startCursor + 15 && i < this.tokens.length; i++) { |
| if (this.tokens[i].type === TOKEN_TYPES.ARROW) { |
| arrowIndex = i; |
| break; |
| } |
| } |
|
|
| if (arrowIndex === -1) return null; |
|
|
| |
| const params = this.extractArrowFunctionParameters(startCursor + 3, arrowIndex); |
|
|
| |
| const functionBounds = this.findArrowFunctionBounds(arrowIndex); |
| if (!functionBounds) return null; |
|
|
| const functionContent = this.extractContentBetweenLines(keywordToken.line, functionBounds.endLine); |
| const purpose = { |
| english: `Arrow function: ${nameToken.value}`, |
| thai: `ฟังก์ชันแบบ arrow: ${nameToken.value}` |
| }; |
|
|
| return { |
| type: 'arrow_function', |
| name: nameToken.value, |
| line: keywordToken.line, |
| column: keywordToken.column, |
| startTokenIndex: startCursor, |
| endTokenIndex: functionBounds.endTokenIndex, |
| endLine: functionBounds.endLine, |
| parameters: params, |
| purpose: purpose, |
| content: functionContent |
| }; |
| } |
|
|
| |
| analyzeObjectDeclaration(startCursor) { |
| const keywordToken = this.tokens[startCursor]; |
| const nameToken = this.tokens[startCursor + 1]; |
| const equalsToken = this.tokens[startCursor + 2]; |
| const braceToken = this.tokens[startCursor + 3]; |
|
|
| if (!nameToken || nameToken.type !== TOKEN_TYPES.IDENTIFIER || |
| !equalsToken || equalsToken.type !== TOKEN_TYPES.EQUALS || |
| !braceToken || braceToken.type !== TOKEN_TYPES.BRACE_OPEN) { |
| return null; |
| } |
|
|
| |
| const objectBounds = this.findBlockBounds(startCursor + 3, TOKEN_TYPES.BRACE_OPEN); |
| if (!objectBounds) return null; |
|
|
| |
| const properties = this.analyzeObjectProperties(objectBounds.startIndex, objectBounds.endIndex); |
|
|
| |
| const objectContent = this.extractContentBetweenLines(keywordToken.line, objectBounds.endLine); |
| const purpose = this.inferObjectPurpose(nameToken.value, objectContent, properties); |
|
|
| return { |
| type: 'object', |
| name: nameToken.value, |
| line: keywordToken.line, |
| column: keywordToken.column, |
| startTokenIndex: startCursor, |
| endTokenIndex: objectBounds.endTokenIndex, |
| endLine: objectBounds.endLine, |
| properties: properties, |
| purpose: purpose, |
| content: objectContent |
| }; |
| } |
|
|
| |
| analyzeInterface(startCursor) { |
| const interfaceToken = this.tokens[startCursor]; |
| const nameToken = this.tokens[startCursor + 1]; |
|
|
| if (!nameToken || nameToken.type !== TOKEN_TYPES.IDENTIFIER) { |
| return null; |
| } |
|
|
| |
| const interfaceBounds = this.findBlockBounds(startCursor, TOKEN_TYPES.BRACE_OPEN); |
| if (!interfaceBounds) return null; |
|
|
| const interfaceContent = this.extractContentBetweenLines(interfaceToken.line, interfaceBounds.endLine); |
| const purpose = { |
| english: `Interface definition for ${nameToken.value}`, |
| thai: `การกำหนด interface สำหรับ ${nameToken.value}` |
| }; |
|
|
| return { |
| type: 'interface_declaration', |
| name: nameToken.value, |
| line: interfaceToken.line, |
| column: interfaceToken.column, |
| startTokenIndex: startCursor, |
| endTokenIndex: interfaceBounds.endTokenIndex, |
| endLine: interfaceBounds.endLine, |
| purpose: purpose, |
| content: interfaceContent |
| }; |
| } |
|
|
| |
| analyzeTypeAlias(startCursor) { |
| const typeToken = this.tokens[startCursor]; |
| const nameToken = this.tokens[startCursor + 1]; |
| const equalsToken = this.tokens[startCursor + 2]; |
|
|
| if (!nameToken || nameToken.type !== TOKEN_TYPES.IDENTIFIER || |
| !equalsToken || equalsToken.type !== TOKEN_TYPES.EQUALS) { |
| return null; |
| } |
|
|
| |
| let endTokenIndex = startCursor + 3; |
| while (endTokenIndex < this.tokens.length) { |
| const token = this.tokens[endTokenIndex]; |
| if (token.type === TOKEN_TYPES.SEMICOLON || |
| token.type === TOKEN_TYPES.NEWLINE || |
| token.type === TOKEN_TYPES.EOF) { |
| break; |
| } |
| endTokenIndex++; |
| } |
|
|
| const endLine = this.tokens[endTokenIndex]?.line || typeToken.line; |
| const typeContent = this.extractContentBetweenLines(typeToken.line, endLine); |
| const purpose = { |
| english: `Type alias definition for ${nameToken.value}`, |
| thai: `การกำหนด type alias สำหรับ ${nameToken.value}` |
| }; |
|
|
| return { |
| type: 'type_alias', |
| name: nameToken.value, |
| line: typeToken.line, |
| column: typeToken.column, |
| startTokenIndex: startCursor, |
| endTokenIndex: endTokenIndex, |
| endLine: endLine, |
| purpose: purpose, |
| content: typeContent |
| }; |
| } |
|
|
| |
| analyzeAbstractClass(startCursor) { |
| const abstractToken = this.tokens[startCursor]; |
| const classToken = this.tokens[startCursor + 1]; |
| const nameToken = this.tokens[startCursor + 2]; |
|
|
| if (!classToken || classToken.value !== 'class' || |
| !nameToken || nameToken.type !== TOKEN_TYPES.IDENTIFIER) { |
| return null; |
| } |
|
|
| |
| const classBounds = this.findBlockBounds(startCursor, TOKEN_TYPES.BRACE_OPEN); |
| if (!classBounds) return null; |
|
|
| const classContent = this.extractContentBetweenLines(abstractToken.line, classBounds.endLine); |
| const purpose = { |
| english: `Abstract class: ${nameToken.value}`, |
| thai: `คลาสนามธรรม: ${nameToken.value}` |
| }; |
|
|
| return { |
| type: 'abstract_class', |
| name: nameToken.value, |
| line: abstractToken.line, |
| column: abstractToken.column, |
| startTokenIndex: startCursor, |
| endTokenIndex: classBounds.endTokenIndex, |
| endLine: classBounds.endLine, |
| purpose: purpose, |
| content: classContent |
| }; |
| } |
|
|
| |
| analyzeFunction(startCursor) { |
| const functionToken = this.tokens[startCursor]; |
| const nameToken = this.tokens[startCursor + 1]; |
|
|
| if (!nameToken || nameToken.type !== TOKEN_TYPES.IDENTIFIER) { |
| return null; |
| } |
|
|
| |
| const params = this.extractFunctionParameters(startCursor + 2); |
|
|
| |
| const functionBounds = this.findFunctionBounds(startCursor); |
| if (!functionBounds) return null; |
|
|
| |
| const functionContent = this.extractContentBetweenLines(functionToken.line, functionBounds.endLine); |
| const purpose = this.inferFunctionPurpose(nameToken.value, functionContent, params); |
|
|
| return { |
| type: 'function', |
| name: nameToken.value, |
| line: functionToken.line, |
| column: functionToken.column, |
| startTokenIndex: startCursor, |
| endTokenIndex: functionBounds.endTokenIndex, |
| endLine: functionBounds.endLine, |
| parameters: params, |
| purpose: purpose, |
| content: functionContent |
| }; |
| } |
|
|
| |
| findBlockBounds(startCursor, openType) { |
| let depth = 0; |
| let startIndex = -1; |
| let endIndex = -1; |
| let endTokenIndex = -1; |
|
|
| for (let i = startCursor; i < this.tokens.length; i++) { |
| const token = this.tokens[i]; |
|
|
| if (token.type === openType) { |
| if (depth === 0) { |
| startIndex = i; |
| } |
| depth++; |
| } else if (token.type === TOKEN_TYPES.BRACE_CLOSE) { |
| depth--; |
| if (depth === 0) { |
| endIndex = i; |
| endTokenIndex = i; |
| break; |
| } |
| } |
| } |
|
|
| if (startIndex === -1 || endIndex === -1) { |
| return null; |
| } |
|
|
| return { |
| startIndex, |
| endIndex, |
| endTokenIndex, |
| endLine: this.tokens[endIndex]?.line || 0 |
| }; |
| } |
|
|
| |
| findFunctionBounds(startCursor) { |
| |
| let braceStart = -1; |
| let parenDepth = 0; |
|
|
| for (let i = startCursor; i < this.tokens.length; i++) { |
| const token = this.tokens[i]; |
|
|
| if (token.type === TOKEN_TYPES.PAREN_OPEN) { |
| parenDepth++; |
| } else if (token.type === TOKEN_TYPES.PAREN_CLOSE) { |
| parenDepth--; |
| } else if (token.type === TOKEN_TYPES.BRACE_OPEN && parenDepth === 0) { |
| braceStart = i; |
| break; |
| } |
| } |
|
|
| if (braceStart === -1) return null; |
|
|
| return this.findBlockBounds(braceStart, TOKEN_TYPES.BRACE_OPEN); |
| } |
|
|
| |
| findArrowFunctionBounds(arrowIndex) { |
| |
| |
| let startIndex = arrowIndex + 1; |
|
|
| if (startIndex < this.tokens.length) { |
| const nextToken = this.tokens[startIndex]; |
|
|
| if (nextToken.type === TOKEN_TYPES.BRACE_OPEN) { |
| |
| return this.findBlockBounds(startIndex, TOKEN_TYPES.BRACE_OPEN); |
| } else { |
| |
| |
| let endIndex = startIndex; |
| let parenDepth = 0; |
| let braceDepth = 0; |
|
|
| while (endIndex < this.tokens.length) { |
| const token = this.tokens[endIndex]; |
|
|
| if (token.type === TOKEN_TYPES.PAREN_OPEN) { |
| parenDepth++; |
| } else if (token.type === TOKEN_TYPES.PAREN_CLOSE) { |
| parenDepth--; |
| if (parenDepth < 0) break; |
| } else if (token.type === TOKEN_TYPES.BRACE_OPEN) { |
| braceDepth++; |
| } else if (token.type === TOKEN_TYPES.BRACE_CLOSE) { |
| braceDepth--; |
| if (braceDepth < 0) break; |
| } else if ((token.type === TOKEN_TYPES.SEMICOLON || |
| token.type === TOKEN_TYPES.COMMA || |
| token.type === TOKEN_TYPES.NEWLINE) && |
| parenDepth === 0 && braceDepth === 0) { |
| break; |
| } |
| endIndex++; |
| } |
|
|
| return { |
| startIndex: arrowIndex + 1, |
| endIndex: endIndex - 1, |
| endTokenIndex: endIndex - 1, |
| endLine: this.tokens[endIndex - 1]?.line || this.tokens[arrowIndex]?.line |
| }; |
| } |
| } |
|
|
| return null; |
| } |
|
|
| |
| extractArrowFunctionParameters(startIndex, arrowIndex) { |
| const params = []; |
|
|
| |
| let currentParam = ''; |
| let parenDepth = 0; |
|
|
| for (let i = startIndex; i < arrowIndex; i++) { |
| const token = this.tokens[i]; |
|
|
| if (token.type === TOKEN_TYPES.PAREN_OPEN) { |
| if (parenDepth === 0) { |
| |
| parenDepth++; |
| continue; |
| } |
| parenDepth++; |
| } else if (token.type === TOKEN_TYPES.PAREN_CLOSE) { |
| parenDepth--; |
| if (parenDepth === 0) { |
| |
| if (currentParam.trim()) { |
| params.push(currentParam.trim()); |
| currentParam = ''; |
| } |
| break; |
| } |
| } else if (token.type === TOKEN_TYPES.COMMA && parenDepth === 1) { |
| if (currentParam.trim()) { |
| params.push(currentParam.trim()); |
| currentParam = ''; |
| } |
| continue; |
| } |
|
|
| if (parenDepth > 0 || (parenDepth === 0 && token.type === TOKEN_TYPES.IDENTIFIER)) { |
| currentParam += token.value; |
| } |
| } |
|
|
| |
| if (params.length === 0 && currentParam.trim()) { |
| params.push(currentParam.trim()); |
| } |
|
|
| return params; |
| } |
|
|
| |
| analyzeClassMembers(startIndex, endIndex) { |
| const members = []; |
| |
| |
| return members; |
| } |
|
|
| |
| |
| |
| |
| |
| analyzeObjectProperties(startIndex, endIndex) { |
| const properties = []; |
|
|
| for (let i = startIndex + 1; i < endIndex; i++) { |
| const token = this.tokens[i]; |
|
|
| if (token.type === TOKEN_TYPES.IDENTIFIER) { |
| const nextToken = this.tokens[i + 1]; |
| if (nextToken && nextToken.value === ':') { |
| |
| const valueToken = this.tokens[i + 2]; |
| properties.push({ |
| name: token.value, |
| line: token.line, |
| value: valueToken?.value || '', |
| type: this.inferPropertyType(valueToken) |
| }); |
| } |
| } |
| } |
|
|
| return properties; |
| } |
|
|
| |
| extractFunctionParameters(startCursor) { |
| const params = []; |
| let depth = 0; |
|
|
| for (let i = startCursor; i < this.tokens.length; i++) { |
| const token = this.tokens[i]; |
|
|
| if (token.type === TOKEN_TYPES.PAREN_OPEN) { |
| depth++; |
| } else if (token.type === TOKEN_TYPES.PAREN_CLOSE) { |
| depth--; |
| if (depth === 0) break; |
| } else if (token.type === TOKEN_TYPES.IDENTIFIER && depth === 1) { |
| params.push(token.value); |
| } |
| } |
|
|
| return params; |
| } |
|
|
| |
| extractContentBetweenLines(startLine, endLine) { |
| const lines = []; |
| for (let i = startLine - 1; i < endLine && i < this.lines.length; i++) { |
| lines.push(this.lines[i]); |
| } |
| return lines.join('\n'); |
| } |
|
|
| |
| inferClassPurpose(className, content, members) { |
| const name = className.toLowerCase(); |
|
|
| |
| if (name.includes('tokenizer')) { |
| return { |
| english: `${className} tokenizer`, |
| thai: `โทเค็นไนเซอร์ ${className}`, |
| category: 'parser' |
| }; |
| } |
|
|
| if (name.includes('matcher') || name.includes('pattern')) { |
| return { |
| english: `${className} pattern matcher`, |
| thai: `ตัวจับรูปแบบ ${className}`, |
| category: 'matcher' |
| }; |
| } |
|
|
| if (name.includes('generator') || name.includes('creator')) { |
| return { |
| english: `${className} generator`, |
| thai: `ตัวสร้าง ${className}`, |
| category: 'generator' |
| }; |
| } |
|
|
| if (name.includes('analyzer') || name.includes('scanner')) { |
| return { |
| english: `${className} analyzer`, |
| thai: `ตัววิเคราะห์ ${className}`, |
| category: 'analyzer' |
| }; |
| } |
|
|
| |
| if (content.includes('constructor') && content.includes('this.')) { |
| return { |
| english: `${className} class`, |
| thai: `คลาส ${className}`, |
| category: 'class' |
| }; |
| } |
|
|
| return { |
| english: `${className} component`, |
| thai: `ส่วนประกอบ ${className}`, |
| category: 'component' |
| }; |
| } |
|
|
| |
| inferObjectPurpose(objectName, content, properties) { |
| const name = objectName.toLowerCase(); |
|
|
| |
| if (name.includes('types') || name.includes('type')) { |
| return { |
| english: `${objectName} type definitions`, |
| thai: `คำจำกัดความประเภท ${objectName}`, |
| category: 'types' |
| }; |
| } |
|
|
| if (name.includes('config') || name.includes('settings')) { |
| return { |
| english: `${objectName} configuration`, |
| thai: `การกำหนดค่า ${objectName}`, |
| category: 'config' |
| }; |
| } |
|
|
| if (name.includes('constants') || name.includes('const')) { |
| return { |
| english: `${objectName} constants`, |
| thai: `ค่าคงที่ ${objectName}`, |
| category: 'constants' |
| }; |
| } |
|
|
| |
| if (properties.length > 5 && properties.every(p => p.type === 'string')) { |
| return { |
| english: `${objectName} enumeration`, |
| thai: `การแจงนับ ${objectName}`, |
| category: 'enum' |
| }; |
| } |
|
|
| return { |
| english: `${objectName} object`, |
| thai: `วัตถุ ${objectName}`, |
| category: 'object' |
| }; |
| } |
|
|
| |
| inferFunctionPurpose(functionName, content, parameters) { |
| const name = functionName.toLowerCase(); |
|
|
| |
| if (content.includes('return') && content.includes('new ')) { |
| return { |
| english: `Create ${functionName}`, |
| thai: `สร้าง ${functionName}`, |
| category: 'factory' |
| }; |
| } |
|
|
| if (content.includes('this.') && content.includes('=')) { |
| return { |
| english: `Initialize ${functionName}`, |
| thai: `เริ่มต้น ${functionName}`, |
| category: 'constructor' |
| }; |
| } |
|
|
| |
| return { |
| english: `Process ${functionName}`, |
| thai: `ประมวลผล ${functionName}`, |
| category: 'process' |
| }; |
| } |
|
|
| |
| inferPropertyType(token) { |
| if (!token) return 'unknown'; |
|
|
| if (token.type === TOKEN_TYPES.STRING) return 'string'; |
| if (token.type === TOKEN_TYPES.NUMBER) return 'number'; |
| if (token.value === 'true' || token.value === 'false') return 'boolean'; |
| if (token.type === TOKEN_TYPES.BRACE_OPEN) return 'object'; |
|
|
| return 'identifier'; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| class EnhancedPatternDetector extends StructureAnalyzer { |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| matchNamespaceDeclaration() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'namespace' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.BRACE_OPEN) { |
|
|
| const namespaceToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| this.cursor += 3; |
|
|
| return { |
| type: 'namespace_declaration', |
| name: nameToken.value, |
| line: namespaceToken.line, |
| column: namespaceToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchModuleDeclaration() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'module' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.BRACE_OPEN) { |
|
|
| const moduleToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| this.cursor += 3; |
|
|
| return { |
| type: 'module_declaration', |
| name: nameToken.value, |
| line: moduleToken.line, |
| column: moduleToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchDeclareStatement() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'declare' && |
| this.peekToken(1)?.type === TOKEN_TYPES.KEYWORD) { |
|
|
| const declareToken = this.currentToken(); |
| const typeToken = this.peekToken(1); |
| const nameToken = this.peekToken(2); |
|
|
| if (nameToken?.type === TOKEN_TYPES.IDENTIFIER) { |
| this.cursor += 3; |
|
|
| return { |
| type: 'declare_statement', |
| name: nameToken.value, |
| line: declareToken.line, |
| column: declareToken.column, |
| parameters: [], |
| isAsync: false, |
| declareType: typeToken.value |
| }; |
| } |
| } |
| return null; |
| } |
|
|
| |
| matchAbstractClass() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'abstract' && |
| this.peekToken(1)?.type === TOKEN_TYPES.KEYWORD && |
| this.peekToken(1)?.value === 'class' && |
| this.peekToken(2)?.type === TOKEN_TYPES.IDENTIFIER) { |
|
|
| const abstractToken = this.currentToken(); |
| const classToken = this.peekToken(1); |
| const nameToken = this.peekToken(2); |
|
|
| this.cursor += 3; |
|
|
| return { |
| type: 'abstract_class', |
| name: nameToken.value, |
| line: abstractToken.line, |
| column: abstractToken.column, |
| parameters: [], |
| isAsync: false, |
| isAbstract: true |
| }; |
| } |
| return null; |
| } |
|
|
| |
| matchGenericClass() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'class' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(2)?.type === TOKEN_TYPES.LESS_THAN) { |
|
|
| const classToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| let genericEnd = -1; |
| let depth = 0; |
| for (let i = this.cursor + 2; i < this.tokens.length; i++) { |
| if (this.tokens[i].type === TOKEN_TYPES.LESS_THAN) { |
| depth++; |
| } else if (this.tokens[i].type === TOKEN_TYPES.GREATER_THAN) { |
| depth--; |
| if (depth === 0) { |
| genericEnd = i; |
| break; |
| } |
| } |
| } |
|
|
| if (genericEnd !== -1) { |
| this.cursor = genericEnd + 1; |
|
|
| return { |
| type: 'generic_class', |
| name: nameToken.value, |
| line: classToken.line, |
| column: classToken.column, |
| parameters: [], |
| isAsync: false, |
| isGeneric: true |
| }; |
| } |
| } |
| return null; |
| } |
|
|
| |
| |
| |
|
|
| |
| matchReactHooks() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'const' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(1)?.value?.startsWith('use') && |
| this.peekToken(2)?.type === TOKEN_TYPES.EQUALS) { |
|
|
| const constToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| let arrowPos = -1; |
| for (let i = this.cursor + 3; i < this.cursor + 10 && i < this.tokens.length; i++) { |
| if (this.tokens[i].type === TOKEN_TYPES.ARROW) { |
| arrowPos = i; |
| break; |
| } |
| } |
|
|
| if (arrowPos !== -1) { |
| this.cursor = arrowPos + 1; |
|
|
| return { |
| type: 'react_hook', |
| name: nameToken.value, |
| line: constToken.line, |
| column: constToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| } |
| return null; |
| } |
|
|
| |
| matchReactForwardRef() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'const' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER) { |
|
|
| |
| let hasReactForwardRef = false; |
| for (let i = this.cursor + 2; i < this.cursor + 15 && i < this.tokens.length; i++) { |
| if (this.tokens[i].type === TOKEN_TYPES.IDENTIFIER && |
| this.tokens[i].value === 'React' && |
| this.tokens[i + 1]?.value === '.' && |
| this.tokens[i + 2]?.value === 'forwardRef') { |
| hasReactForwardRef = true; |
| this.cursor = i + 3; |
| break; |
| } |
| } |
|
|
| if (hasReactForwardRef) { |
| const constToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| return { |
| type: 'react_forwardref', |
| name: nameToken.value, |
| line: constToken.line, |
| column: constToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| } |
| return null; |
| } |
|
|
| |
| matchReactMemo() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'const' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER) { |
|
|
| |
| let hasReactMemo = false; |
| for (let i = this.cursor + 2; i < this.cursor + 15 && i < this.tokens.length; i++) { |
| if (this.tokens[i].type === TOKEN_TYPES.IDENTIFIER && |
| this.tokens[i].value === 'React' && |
| this.tokens[i + 1]?.value === '.' && |
| this.tokens[i + 2]?.value === 'memo') { |
| hasReactMemo = true; |
| this.cursor = i + 3; |
| break; |
| } |
| } |
|
|
| if (hasReactMemo) { |
| const constToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| return { |
| type: 'react_memo', |
| name: nameToken.value, |
| line: constToken.line, |
| column: constToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| } |
| return null; |
| } |
|
|
| |
| matchHigherOrderComponent() { |
| if (this.currentToken()?.type === TOKEN_TYPES.KEYWORD && |
| this.currentToken()?.value === 'const' && |
| this.peekToken(1)?.type === TOKEN_TYPES.IDENTIFIER && |
| this.peekToken(1)?.value?.startsWith('with')) { |
|
|
| const constToken = this.currentToken(); |
| const nameToken = this.peekToken(1); |
|
|
| |
| let arrowPos = -1; |
| for (let i = this.cursor + 3; i < this.cursor + 15 && i < this.tokens.length; i++) { |
| if (this.tokens[i].type === TOKEN_TYPES.ARROW) { |
| arrowPos = i; |
| break; |
| } |
| } |
|
|
| if (arrowPos !== -1) { |
| this.cursor = arrowPos + 1; |
|
|
| return { |
| type: 'higher_order_component', |
| name: nameToken.value, |
| line: constToken.line, |
| column: constToken.column, |
| parameters: [], |
| isAsync: false |
| }; |
| } |
| } |
| return null; |
| } |
| } |
|
|
| |
| |
| |
|
|
| |
| class CommentGenerator { |
| constructor() { |
| this.functionDescriptions = { |
| |
| |
| |
| 'get': ['Get data', 'ดึงข้อมูล'], |
| 'fetch': ['Fetch data', 'ดึงข้อมูล'], |
| 'retrieve': ['Retrieve data', 'ดึงข้อมูล'], |
| 'load': ['Load data', 'โหลดข้อมูล'], |
| 'read': ['Read data', 'อ่านข้อมูล'], |
| 'find': ['Find data', 'ค้นหาข้อมูล'], |
| 'search': ['Search data', 'ค้นหาข้อมูล'], |
| 'query': ['Query data', 'สอบถามข้อมูล'], |
| 'select': ['Select data', 'เลือกข้อมูล'], |
| 'filter': ['Filter data', 'กรองข้อมูล'], |
| 'sort': ['Sort data', 'เรียงข้อมูล'], |
| 'order': ['Order data', 'จัดเรียงข้อมูล'], |
| 'create': ['Create data', 'สร้างข้อมูล'], |
| 'insert': ['Insert data', 'เพิ่มข้อมูล'], |
| 'addinterceptor': ['Add interceptor', 'เพิ่มตัวสกัดกั้น'], |
| 'addrequestinterceptor': ['Add request interceptor', 'เพิ่มตัวสกัดกั้นคำขอ'], |
| 'addresponseinterceptor': ['Add response interceptor', 'เพิ่มตัวสกัดกั้นการตอบกลับ'], |
| 'tojson': ['Convert to JSON', 'แปลงเป็น JSON'], |
| 'fromjson': ['Create from JSON', 'สร้างจาก JSON'], |
| 'isretryable': ['Check if retryable', 'ตรวจสอบว่าลองใหม่ได้หรือไม่'], |
| 'performrequest': ['Perform request', 'ดำเนินการขอ'], |
| 'retryrequest': ['Retry request', 'ลองคำขอใหม่'], |
| 'withretry': ['With retry logic', 'พร้อมตรรกะลองใหม่'], |
| 'post': ['Post data', 'โพสต์ข้อมูล'], |
| 'put': ['Put data', 'ใส่ข้อมูล'], |
| 'update': ['Update data', 'อัพเดทข้อมูล'], |
| 'modify': ['Modify data', 'แก้ไขข้อมูล'], |
| 'edit': ['Edit data', 'แก้ไขข้อมูล'], |
| 'patch': ['Patch data', 'แพทช์ข้อมูล'], |
| 'change': ['Change data', 'เปลี่ยนข้อมูล'], |
| 'delete': ['Delete data', 'ลบข้อมูล'], |
| 'remove': ['Remove item', 'ลบรายการ'], |
| 'destroy': ['Destroy data', 'ทำลายข้อมูล'], |
| 'drop': ['Drop data', 'ทิ้งข้อมูล'], |
| 'save': ['Save data', 'บันทึกข้อมูล'], |
| 'store': ['Store data', 'เก็บข้อมูล'], |
| 'persist': ['Persist data', 'บันทึกถาวร'], |
| 'commit': ['Commit changes', 'ยืนยันการเปลี่ยนแปลง'], |
| 'rollback': ['Rollback changes', 'ย้อนกลับการเปลี่ยนแปลง'], |
|
|
| |
| |
| |
| 'validate': ['Validate input', 'ตรวจสอบความถูกต้อง'], |
| 'check': ['Check data', 'ตรวจสอบข้อมูล'], |
| 'verify': ['Verify data', 'ยืนยันข้อมูล'], |
| 'test': ['Test function', 'ทดสอบฟังก์ชัน'], |
| 'ensure': ['Ensure condition', 'ประกันเงื่อนไข'], |
| 'assert': ['Assert condition', 'ยืนยันเงื่อนไข'], |
| 'confirm': ['Confirm action', 'ยืนยันการกระทำ'], |
| 'authorize': ['Authorize user', 'อนุญาตผู้ใช้'], |
| 'authenticate': ['Authenticate user', 'รับรองผู้ใช้'], |
| 'permit': ['Permit access', 'อนุญาตการเข้าถึง'], |
| 'allow': ['Allow action', 'อนุญาตการกระทำ'], |
| 'deny': ['Deny access', 'ปฏิเสธการเข้าถึง'], |
| 'reject': ['Reject request', 'ปฏิเสธคำขอ'], |
| 'approve': ['Approve request', 'อนุมัติคำขอ'], |
|
|
| |
| |
| |
| 'process': ['Process data', 'ประมวลผลข้อมูล'], |
| 'handle': ['Handle event', 'จัดการเหตุการณ์'], |
| 'execute': ['Execute command', 'ดำเนินการคำสั่ง'], |
| 'run': ['Run process', 'รันกระบวนการ'], |
| 'perform': ['Perform action', 'ปฏิบัติการ'], |
| 'operate': ['Operate system', 'ดำเนินการระบบ'], |
| 'start': ['Start process', 'เริ่มกระบวนการ'], |
| 'begin': ['Begin operation', 'เริ่มการดำเนินการ'], |
| 'launch': ['Launch application', 'เปิดแอปพลิเคชัน'], |
| 'init': ['Initialize system', 'เริ่มต้นระบบ'], |
| 'initialize': ['Initialize data', 'เริ่มต้นข้อมูล'], |
| 'setup': ['Setup configuration', 'ตั้งค่าระบบ'], |
| 'configure': ['Configure settings', 'กำหนดค่าการตั้งค่า'], |
| 'stop': ['Stop process', 'หยุดกระบวนการ'], |
| 'end': ['End operation', 'สิ้นสุดการดำเนินการ'], |
| 'finish': ['Finish task', 'เสร็จสิ้นงาน'], |
| 'complete': ['Complete operation', 'ดำเนินการเสร็จสิ้น'], |
| 'terminate': ['Terminate process', 'ยุติกระบวนการ'], |
| 'kill': ['Kill process', 'ฆ่ากระบวนการ'], |
| 'abort': ['Abort operation', 'ยกเลิกการดำเนินการ'], |
| 'cancel': ['Cancel request', 'ยกเลิกคำขอ'], |
| 'pause': ['Pause execution', 'หยุดชั่วคราว'], |
| 'resume': ['Resume execution', 'ดำเนินการต่อ'], |
| 'restart': ['Restart system', 'เริ่มต้นใหม่'], |
| 'reload': ['Reload data', 'โหลดข้อมูลใหม่'], |
| 'refresh': ['Refresh content', 'รีเฟรชเนื้อหา'], |
| 'reset': ['Reset state', 'รีเซ็ตสถานะ'], |
| 'clear': ['Clear data', 'เคลียร์ข้อมูล'], |
| 'clean': ['Clean up', 'ทำความสะอาด'], |
| 'flush': ['Flush cache', 'ล้างแคช'], |
| 'purge': ['Purge data', 'ลบข้อมูลออก'], |
|
|
| |
| |
| |
| 'parse': ['Parse data', 'แปลงข้อมูล'], |
| 'format': ['Format data', 'จัดรูปแบบข้อมูล'], |
| 'convert': ['Convert data', 'แปลงข้อมูล'], |
| 'transform': ['Transform data', 'เปลี่ยนแปลงข้อมูล'], |
| 'map': ['Map data', 'แมปข้อมูล'], |
| 'reduce': ['Reduce data', 'ลดข้อมูล'], |
| 'filter': ['Filter data', 'กรองข้อมูล'], |
| 'compress': ['Compress data', 'บีบอัดข้อมูล'], |
| 'decompress': ['Decompress data', 'คลายการบีบอัด'], |
| 'encode': ['Encode data', 'เข้ารหัสข้อมูล'], |
| 'decode': ['Decode data', 'ถอดรหัสข้อมูล'], |
| 'encrypt': ['Encrypt data', 'เข้ารหัสข้อมูล'], |
| 'decrypt': ['Decrypt data', 'ถอดรหัสข้อมูล'], |
| 'hash': ['Hash data', 'แฮชข้อมูล'], |
| 'serialize': ['Serialize data', 'ซีเรียลไลซ์ข้อมูล'], |
| 'deserialize': ['Deserialize data', 'ดีซีเรียลไลซ์ข้อมูล'], |
| 'stringify': ['Stringify data', 'แปลงเป็นสตริง'], |
| 'normalize': ['Normalize data', 'ทำให้ข้อมูลเป็นมาตรฐาน'], |
| 'sanitize': ['Sanitize input', 'ทำความสะอาดข้อมูลนำเข้า'], |
| 'escape': ['Escape characters', 'หลีกเลี่ยงอักขระ'], |
|
|
| |
| |
| |
| 'calculate': ['Calculate value', 'คำนวณค่า'], |
| 'compute': ['Compute result', 'คำนวณผลลัพธ์'], |
| 'evaluate': ['Evaluate expression', 'ประเมินนิพจน์'], |
| 'measure': ['Measure value', 'วัดค่า'], |
| 'count': ['Count items', 'นับจำนวน'], |
| 'sum': ['Sum values', 'รวมค่า'], |
| 'total': ['Total amount', 'จำนวนรวม'], |
| 'average': ['Average value', 'ค่าเฉลี่ย'], |
| 'mean': ['Mean value', 'ค่าเฉลี่ย'], |
| 'median': ['Median value', 'ค่ามัธยฐาน'], |
| 'mode': ['Mode value', 'ค่าฐานนิยม'], |
| 'min': ['Minimum value', 'ค่าต่ำสุด'], |
| 'max': ['Maximum value', 'ค่าสูงสุด'], |
| 'range': ['Range of values', 'ช่วงค่า'], |
| 'variance': ['Variance calculation', 'การคำนวณความแปรปรวน'], |
| 'deviation': ['Deviation calculation', 'การคำนวณส่วนเบียงเบน'], |
| 'round': ['Round number', 'ปัดเศษจำนวน'], |
| 'ceil': ['Ceiling number', 'ปัดขึ้น'], |
| 'floor': ['Floor number', 'ปัดลง'], |
| 'abs': ['Absolute value', 'ค่าสัมบูรณ์'], |
| 'sqrt': ['Square root', 'รากที่สอง'], |
| 'pow': ['Power calculation', 'การยกกำลัง'], |
| 'log': ['Logarithm calculation', 'การคำนวณลอการิทึม'], |
| 'sin': ['Sine function', 'ฟังก์ชันไซน์'], |
| 'cos': ['Cosine function', 'ฟังก์ชันโคไซน์'], |
| 'tan': ['Tangent function', 'ฟังก์ชันแทนเจนต์'], |
|
|
| |
| |
| |
| 'render': ['Render content', 'แสดงผลเนื้อหา'], |
| 'display': ['Display info', 'แสดงข้อมูล'], |
| 'show': ['Show element', 'แสดงองค์ประกอบ'], |
| 'hide': ['Hide element', 'ซ่อนองค์ประกอบ'], |
| 'toggle': ['Toggle state', 'สลับสถานะ'], |
| 'open': ['Open dialog', 'เปิดไดอะล็อก'], |
| 'close': ['Close window', 'ปิดหน้าต่าง'], |
| 'modal': ['Modal dialog', 'ไดอะล็อกโมดัล'], |
| 'popup': ['Popup window', 'หน้าต่างป๊อปอัป'], |
| 'tooltip': ['Tooltip display', 'แสดงคำแนะนำ'], |
| 'menu': ['Menu system', 'ระบบเมนู'], |
| 'dropdown': ['Dropdown menu', 'เมนูดรอปดาวน์'], |
| 'sidebar': ['Sidebar component', 'ส่วนประกอบแถบข้าง'], |
| 'navbar': ['Navigation bar', 'แถบนำทาง'], |
| 'header': ['Header component', 'ส่วนหัว'], |
| 'footer': ['Footer component', 'ส่วนท้าย'], |
| 'panel': ['Panel component', 'ส่วนประกอบแผง'], |
| 'tab': ['Tab component', 'ส่วนประกอบแท็บ'], |
| 'card': ['Card component', 'ส่วนประกอบการ์ด'], |
| 'button': ['Button component', 'ส่วนประกอบปุ่ม'], |
| 'input': ['Input field', 'ช่องข้อมูลนำเข้า'], |
| 'form': ['Form component', 'ส่วนประกอบฟอร์ม'], |
| 'table': ['Table component', 'ส่วนประกอบตาราง'], |
| 'list': ['List component', 'ส่วนประกอบรายการ'], |
| 'grid': ['Grid layout', 'เลย์เอาต์กริด'], |
| 'chart': ['Chart visualization', 'การแสดงผลแผนภูมิ'], |
| 'graph': ['Graph display', 'การแสดงผลกราฟ'], |
|
|
| |
| |
| |
| 'send': ['Send data', 'ส่งข้อมูล'], |
| 'receive': ['Receive data', 'รับข้อมูล'], |
| 'request': ['Request data', 'ขอข้อมูล'], |
| 'response': ['Response data', 'ตอบกลับข้อมูล'], |
| 'connect': ['Connect server', 'เชื่อมต่อเซิร์ฟเวอร์'], |
| 'disconnect': ['Disconnect server', 'ตัดการเชื่อมต่อ'], |
| 'socket': ['Socket connection', 'การเชื่อมต่อซ็อกเก็ต'], |
| 'websocket': ['WebSocket connection', 'การเชื่อมต่อเว็บซ็อกเก็ต'], |
| 'http': ['HTTP request', 'คำขอ HTTP'], |
| 'https': ['HTTPS request', 'คำขอ HTTPS'], |
| 'api': ['API endpoint', 'จุดสิ้นสุด API'], |
| 'rest': ['REST API', 'REST API'], |
| 'graphql': ['GraphQL query', 'คิวรี GraphQL'], |
| 'ajax': ['AJAX request', 'คำขอ AJAX'], |
| 'xhr': ['XMLHttpRequest', 'XMLHttpRequest'], |
| 'download': ['Download file', 'ดาวน์โหลดไฟล์'], |
| 'upload': ['Upload file', 'อัปโหลดไฟล์'], |
| 'stream': ['Stream data', 'สตรีมข้อมูล'], |
| 'broadcast': ['Broadcast message', 'ออกอากาศข้อความ'], |
| 'publish': ['Publish event', 'เผยแพร่เหตุการณ์'], |
| 'subscribe': ['Subscribe event', 'สมัครสมาชิกเหตุการณ์'], |
| 'emit': ['Emit signal', 'ส่งสัญญาณ'], |
| 'listen': ['Listen event', 'ฟังเหตุการณ์'], |
| 'poll': ['Poll data', 'โพลข้อมูล'], |
|
|
| |
| |
| |
| 'file': ['File operation', 'การดำเนินการไฟล์'], |
| 'folder': ['Folder operation', 'การดำเนินการโฟลเดอร์'], |
| 'directory': ['Directory operation', 'การดำเนินการไดเรกทอรี'], |
| 'path': ['Path handling', 'การจัดการพาธ'], |
| 'write': ['Write file', 'เขียนไฟล์'], |
| 'read': ['Read file', 'อ่านไฟล์'], |
| 'copy': ['Copy file', 'คัดลอกไฟล์'], |
| 'move': ['Move file', 'ย้ายไฟล์'], |
| 'rename': ['Rename file', 'เปลี่ยนชื่อไฟล์'], |
| 'create': ['Create file', 'สร้างไฟล์'], |
| 'mkdir': ['Make directory', 'สร้างไดเรกทอรี'], |
| 'exists': ['Check existence', 'ตรวจสอบการมีอยู่'], |
| 'stat': ['File statistics', 'สถิติไฟล์'], |
| 'watch': ['Watch changes', 'เฝ้าดูการเปลี่ยนแปลง'], |
| 'backup': ['Backup data', 'สำรองข้อมูล'], |
| 'restore': ['Restore data', 'กู้คืนข้อมูล'], |
| 'archive': ['Archive files', 'เก็บถาวรไฟล์'], |
| 'extract': ['Extract archive', 'แยกไฟล์เก็บถาวร'], |
| 'zip': ['Zip files', 'บีบอัดไฟล์'], |
| 'unzip': ['Unzip files', 'แตกไฟล์บีบอัด'], |
|
|
| |
| |
| |
| 'cache': ['Cache data', 'แคชข้อมูล'], |
| 'memoize': ['Memoize function', 'จดจำฟังก์ชัน'], |
| 'buffer': ['Buffer data', 'บัฟเฟอร์ข้อมูล'], |
| 'pool': ['Connection pool', 'พูลการเชื่อมต่อ'], |
| 'queue': ['Queue system', 'ระบบคิว'], |
| 'stack': ['Stack operations', 'การดำเนินการสแต็ก'], |
| 'heap': ['Heap management', 'การจัดการฮีป'], |
| 'memory': ['Memory management', 'การจัดการหน่วยความจำ'], |
| 'garbage': ['Garbage collection', 'การเก็บขยะ'], |
| 'leak': ['Memory leak', 'การรั่วไหลของหน่วยความจำ'], |
| 'allocate': ['Allocate memory', 'จัดสรรหน่วยความจำ'], |
| 'deallocate': ['Deallocate memory', 'ยกเลิกการจัดสรร'], |
| 'optimize': ['Optimize performance', 'เพิ่มประสิทธิภาพ'], |
| 'throttle': ['Throttle execution', 'จำกัดการดำเนินการ'], |
| 'debounce': ['Debounce function', 'ดีเบาส์ฟังก์ชัน'], |
|
|
| |
| |
| |
| 'error': ['Error handling', 'การจัดการข้อผิดพลาด'], |
| 'exception': ['Exception handling', 'การจัดการข้อยกเว้น'], |
| 'throw': ['Throw error', 'โยนข้อผิดพลาด'], |
| 'catch': ['Catch error', 'จับข้อผิดดพลาด'], |
| 'try': ['Try operation', 'ลองการดำเนินการ'], |
| 'finally': ['Finally block', 'บล็อกสุดท้าย'], |
| 'log': ['Log message', 'บันทึกข้อความ'], |
| 'warn': ['Warning message', 'ข้อความเตือน'], |
| 'info': ['Information message', 'ข้อความข้อมูล'], |
| 'debug': ['Debug message', 'ข้อความดีบัก'], |
| 'trace': ['Trace execution', 'ติดตามการดำเนินการ'], |
| 'monitor': ['Monitor system', 'ตรวจสอบระบบ'], |
| 'alert': ['Alert notification', 'การแจ้งเตือน'], |
| 'notify': ['Notify user', 'แจ้งผู้ใช้'], |
|
|
| |
| |
| |
| 'login': ['Login user', 'เข้าสู่ระบบผู้ใช้'], |
| 'logout': ['Logout user', 'ออกจากระบบผู้ใช้'], |
| 'signin': ['Sign in user', 'ลงชื่อเข้าใช้'], |
| 'signout': ['Sign out user', 'ลงชื่อออก'], |
| 'signup': ['Sign up user', 'สมัครสมาชิก'], |
| 'register': ['Register user', 'ลงทะเบียนผู้ใช้'], |
| 'auth': ['Authentication', 'การรับรองตัวตน'], |
| 'token': ['Token management', 'การจัดการโทเคน'], |
| 'jwt': ['JWT token', 'โทเคน JWT'], |
| 'session': ['Session management', 'การจัดการเซสชัน'], |
| 'cookie': ['Cookie handling', 'การจัดการคุกกี้'], |
| 'csrf': ['CSRF protection', 'การป้องกัน CSRF'], |
| 'xss': ['XSS protection', 'การป้องกัน XSS'], |
| 'cors': ['CORS handling', 'การจัดการ CORS'], |
| 'secure': ['Security function', 'ฟังก์ชันความปลอดภัย'], |
| 'permission': ['Permission check', 'ตรวจสอบสิทธิ์'], |
| 'role': ['Role management', 'การจัดการบทบาท'], |
| 'acl': ['Access control', 'การควบคุมการเข้าถึง'], |
|
|
| |
| |
| |
| 'test': ['Test function', 'ทดสอบฟังก์ชัน'], |
| 'spec': ['Specification test', 'ทดสอบข้อกำหนด'], |
| 'suite': ['Test suite', 'ชุดทดสอบ'], |
| 'case': ['Test case', 'กรณีทดสอบ'], |
| 'mock': ['Mock function', 'ฟังก์ชันจำลอง'], |
| 'stub': ['Stub function', 'ฟังก์ชันดัมมี่'], |
| 'spy': ['Spy function', 'ฟังก์ชันสายลับ'], |
| 'expect': ['Expect assertion', 'การยืนยันคาดหวัง'], |
| 'should': ['Should assertion', 'การยืนยันควร'], |
| 'describe': ['Describe test', 'อธิบายการทดสอบ'], |
| 'it': ['Test case description', 'คำอธิบายกรณีทดสอบ'], |
| 'before': ['Before hook', 'ฮุคก่อนหน้า'], |
| 'after': ['After hook', 'ฮุคหลังจาก'], |
| 'setup': ['Setup test', 'ตั้งค่าการทดสอบ'], |
| 'teardown': ['Teardown test', 'ทำลายการทดสอบ'], |
| 'benchmark': ['Benchmark test', 'ทดสอบประสิทธิภาพ'], |
| 'performance': ['Performance test', 'ทดสอบประสิทธิภาพ'], |
|
|
| |
| |
| |
| 'util': ['Utility function', 'ฟังก์ชันเครื่องมือ'], |
| 'helper': ['Helper function', 'ฟังก์ชันช่วยเหลือ'], |
| 'tool': ['Tool function', 'ฟังก์ชันเครื่องมือ'], |
| 'generator': ['Generator function', 'ฟังก์ชันตัวสร้าง'], |
| 'factory': ['Factory function', 'ฟังก์ชันโรงงาน'], |
| 'builder': ['Builder function', 'ฟังก์ชันผู้สร้าง'], |
| 'creator': ['Creator function', 'ฟังก์ชันผู้สร้าง'], |
| 'constructor': ['Constructor function', 'ฟังก์ชันคอนสตรัคเตอร์'], |
| 'destructor': ['Destructor function', 'ฟังก์ชันดีสตรัคเตอร์'], |
| 'adapter': ['Adapter pattern', 'รูปแบบอะแดปเตอร์'], |
| 'wrapper': ['Wrapper function', 'ฟังก์ชันห่อหุ้ม'], |
| 'decorator': ['Decorator pattern', 'รูปแบบเดคคอเรเตอร์'], |
| 'observer': ['Observer pattern', 'รูปแบบผู้สังเกตการณ์'], |
| 'strategy': ['Strategy pattern', 'รูปแบบกลยุทธ์'], |
| 'singleton': ['Singleton pattern', 'รูปแบบซิงเกิลตัน'], |
|
|
| |
| |
| |
| 'class': ['Class definition', 'คำจำกัดความคลาส'], |
| 'interface': ['Interface definition', 'คำจำกัดความอินเทอร์เฟซ'], |
| 'type': ['Type definition', 'คำจำกัดความประเภท'], |
| 'enum': ['Enumeration', 'การนับเลข'], |
| 'struct': ['Structure definition', 'คำจำกัดความโครงสร้าง'], |
| 'module': ['Module definition', 'คำจำกัดความโมดูล'], |
| 'component': ['Component class', 'คลาสส่วนประกอบ'], |
| 'service': ['Service class', 'คลาสบริการ'], |
| 'controller': ['Controller class', 'คลาสควบคุม'], |
| 'model': ['Model class', 'คลาสแบบจำลอง'], |
| 'view': ['View class', 'คลาสมุมมอง'], |
| 'repository': ['Repository class', 'คลาสที่เก็บ'], |
| 'dao': ['Data Access Object', 'วัตถุการเข้าถึงข้อมูล'], |
| 'dto': ['Data Transfer Object', 'วัตถุการถ่ายโอนข้อมูล'], |
| 'entity': ['Entity class', 'คลาสเอนทิตี'], |
| 'manager': ['Manager class', 'คลาสผู้จัดการ'], |
| 'handler': ['Handler class', 'คลาสผู้จัดการ'], |
| 'processor': ['Processor class', 'คลาสผู้ประมวลผล'], |
| 'validator': ['Validator class', 'คลาสตัวตรวจสอบ'], |
| 'parser': ['Parser class', 'คลาสตัวแยกวิเคราะห์'], |
| 'formatter': ['Formatter class', 'คลาสตัวจัดรูปแบบ'], |
| 'converter': ['Converter class', 'คลาสตัวแปลง'], |
| 'transformer': ['Transformer class', 'คลาสตัวเปลี่ยนแปลง'], |
| 'builder': ['Builder class', 'คลาสตัวสร้าง'], |
| 'factory': ['Factory class', 'คลาสโรงงาน'], |
| 'strategy': ['Strategy class', 'คลาสกลยุทธ์'], |
| 'command': ['Command class', 'คลาสคำสั่ง'], |
| 'event': ['Event class', 'คลาสเหตุการณ์'], |
| 'listener': ['Listener class', 'คลาสตัวฟัง'], |
| 'observer': ['Observer class', 'คลาสผู้สังเกตการณ์'], |
| 'subject': ['Subject class', 'คลาสหัวข้อ'], |
| 'proxy': ['Proxy class', 'คลาสพร็อกซี่'], |
| 'adapter': ['Adapter class', 'คลาสอะแดปเตอร์'], |
| 'facade': ['Facade class', 'คลาสหน้าต่าง'], |
| 'bridge': ['Bridge class', 'คลาสสะพาน'], |
| 'composite': ['Composite class', 'คลาสคอมโพสิต'], |
| 'decorator': ['Decorator class', 'คลาสเดคคอเรเตอร์'], |
| 'flyweight': ['Flyweight class', 'คลาสน้ำหนักเบา'], |
| 'iterator': ['Iterator class', 'คลาสตัววนซ้ำ'], |
| 'mediator': ['Mediator class', 'คลาสผู้ไกล่เกลี่ย'], |
| 'memento': ['Memento class', 'คลาสที่ระลึก'], |
| 'prototype': ['Prototype class', 'คลาสต้นแบบ'], |
| 'state': ['State class', 'คลาสสถานะ'], |
| 'template': ['Template class', 'คลาสแม่แบบ'], |
| 'visitor': ['Visitor class', 'คลาสผู้เยือน'], |
| 'calculator': ['Calculator class', 'คลาสเครื่องคิดเลข'], |
| 'advanced': ['Advanced system', 'ระบบขั้นสูง'], |
| 'complex': ['Complex system', 'ระบบที่ซับซ้อน'], |
| 'simple': ['Simple function', 'ฟังก์ชันง่าย'], |
| 'basic': ['Basic function', 'ฟังก์ชันพื้นฐาน'], |
| 'core': ['Core function', 'ฟังก์ชันหลัก'], |
| 'main': ['Main function', 'ฟังก์ชันหลัก'], |
| 'primary': ['Primary function', 'ฟังก์ชันหลัก'], |
| 'secondary': ['Secondary function', 'ฟังก์ชันรอง'], |
| 'custom': ['Custom function', 'ฟังก์ชันกำหนดเอง'], |
| 'dynamic': ['Dynamic function', 'ฟังก์ชันไดนามิก'], |
| 'static': ['Static function', 'ฟังก์ชันสแตติก'], |
| 'async': ['Async function', 'ฟังก์ชันแบบอะซิงค์'], |
| 'sync': ['Sync function', 'ฟังก์ชันแบบซิงค์'], |
| 'generic': ['Generic function', 'ฟังก์ชันทั่วไป'], |
| 'specific': ['Specific function', 'ฟังก์ชันเฉพาะ'], |
| 'abstract': ['Abstract function', 'ฟังก์ชันนามธรรม'], |
| 'concrete': ['Concrete function', 'ฟังก์ชันที่เป็นรูปธรรม'], |
| 'virtual': ['Virtual function', 'ฟังก์ชันเสมือน'], |
| 'override': ['Override function', 'ฟังก์ชันเขียนทับ'], |
| 'implement': ['Implementation', 'การนำไปใช้'], |
| 'extend': ['Extension function', 'ฟังก์ชันส่วนขยาย'], |
| 'inherit': ['Inheritance function', 'ฟังก์ชันการสืบทอด'], |
| 'compose': ['Composition function', 'ฟังก์ชันการผสมผสาน'], |
| 'aggregate': ['Aggregation function', 'ฟังก์ชันการรวบรวม'], |
| 'associate': ['Association function', 'ฟังก์ชันการเชื่อมโยง'], |
| 'delegate': ['Delegation function', 'ฟังก์ชันการมอบหมาย'], |
| 'encapsulate': ['Encapsulation function', 'ฟังก์ชันการห่อหุ้ม'], |
| 'polymorphic': ['Polymorphic function', 'ฟังก์ชันแบบพหุรูป'], |
|
|
| |
| |
| |
| 'react': ['React component', 'คอมโพเนนต์ React'], |
| 'vue': ['Vue component', 'คอมโพเนนต์ Vue'], |
| 'angular': ['Angular component', 'คอมโพเนนต์ Angular'], |
| 'nodejs': ['Node.js function', 'ฟังก์ชัน Node.js'], |
| 'express': ['Express middleware', 'มิดเดิลแวร์ Express'], |
| 'redux': ['Redux action', 'การกระทำ Redux'], |
| 'mongoose': ['Mongoose model', 'แบบจำลอง Mongoose'], |
| 'sequelize': ['Sequelize model', 'แบบจำลอง Sequelize'], |
| 'typeorm': ['TypeORM entity', 'เอนทิตี TypeORM'], |
| 'prisma': ['Prisma model', 'แบบจำลอง Prisma'], |
| 'graphql': ['GraphQL resolver', 'ตัวแยกวิเคราะห์ GraphQL'], |
| 'apollo': ['Apollo client', 'ลูกค้า Apollo'], |
| 'next': ['Next.js page', 'หน้า Next.js'], |
| 'nuxt': ['Nuxt.js page', 'หน้า Nuxt.js'], |
| 'gatsby': ['Gatsby component', 'คอมโพเนนต์ Gatsby'], |
| 'svelte': ['Svelte component', 'คอมโพเนนต์ Svelte'], |
| 'webpack': ['Webpack configuration', 'การกำหนดค่า Webpack'], |
| 'vite': ['Vite configuration', 'การกำหนดค่า Vite'], |
| 'rollup': ['Rollup configuration', 'การกำหนดค่า Rollup'], |
| 'babel': ['Babel transformation', 'การเปลี่ยนแปลง Babel'], |
| 'eslint': ['ESLint rule', 'กฎ ESLint'], |
| 'prettier': ['Prettier formatter', 'ตัวจัดรูปแบบ Prettier'], |
| 'jest': ['Jest test', 'การทดสอบ Jest'], |
| 'mocha': ['Mocha test', 'การทดสอบ Mocha'], |
| 'cypress': ['Cypress test', 'การทดสอบ Cypress'], |
| 'playwright': ['Playwright test', 'การทดสอบ Playwright'], |
| 'socket': ['Socket.io handler', 'ตัวจัดการ Socket.io'], |
| 'passport': ['Passport strategy', 'กลยุทธ์ Passport'], |
| 'multer': ['Multer middleware', 'มิดเดิลแวร์ Multer'], |
| 'cors': ['CORS middleware', 'มิดเดิลแวร์ CORS'], |
| 'helmet': ['Helmet security', 'ความปลอดภัย Helmet'], |
| 'bcrypt': ['Bcrypt hashing', 'การแฮช Bcrypt'], |
| 'jwt': ['JWT authentication', 'การรับรองตัวตน JWT'], |
| 'axios': ['Axios request', 'คำขอ Axios'], |
| 'lodash': ['Lodash utility', 'เครื่องมือ Lodash'], |
| 'moment': ['Moment.js date', 'วันที่ Moment.js'], |
| 'dayjs': ['Day.js date', 'วันที่ Day.js'], |
| 'luxon': ['Luxon date', 'วันที่ Luxon'], |
|
|
| |
| |
| |
| 'node': ['Node structure', 'โครงสร้างโหนด'], |
| 'tree': ['Tree structure', 'โครงสร้างต้นไม้'], |
| 'graph': ['Graph structure', 'โครงสร้างกราฟ'], |
| 'linkedlist': ['LinkedList structure', 'โครงสร้างลิงค์ลิสต์'], |
| 'algorithm': ['Algorithm implementation', 'การใช้งานอัลกอริทึม'], |
| 'sort': ['Sort algorithm', 'อัลกอริทึมการเรียง'], |
| 'search': ['Search algorithm', 'อัลกอริทึมการค้นหา'], |
|
|
| |
| |
| |
| 'default': ['Function', 'ฟังก์ชัน'] |
| }; |
| } |
|
|
| |
| generateFunctionComment(func, options = {}) { |
| const { aiMode = false, addMissing = false, structure = null } = options; |
|
|
| let description; |
|
|
| |
| if (structure && structure.purpose) { |
| description = { |
| english: structure.purpose.english, |
| thai: structure.purpose.thai |
| }; |
| } else { |
| |
| if (func.type === 'interface_declaration') { |
| description = { |
| english: `Interface definition for ${func.name}`, |
| thai: `การกำหนด interface สำหรับ ${func.name}` |
| }; |
| } else if (func.type === 'type_alias') { |
| description = { |
| english: `Type alias definition for ${func.name}`, |
| thai: `การกำหนด type alias สำหรับ ${func.name}` |
| }; |
| } else if (func.type === 'static_method') { |
| description = { |
| english: `Static method: ${func.name}`, |
| thai: `เมธอดแบบ static: ${func.name}` |
| }; |
| } else if (func.type === 'arrow_function') { |
| description = { |
| english: `Arrow function: ${func.name}`, |
| thai: `ฟังก์ชันแบบ arrow: ${func.name}` |
| }; |
| } else { |
| |
| description = this.getFunctionDescription(func.name, func.type); |
| } |
| } |
|
|
| |
| let comment = ''; |
| comment += '// ======================================================================\n'; |
| comment += `// EN: ${description.english}\n`; |
| comment += `// TH: ${description.thai}\n`; |
| comment += '// ======================================================================\n'; |
|
|
| |
| if (structure && structure.type === 'object' && structure.properties && structure.properties.length > 0) { |
| const categorizedProps = this.categorizeObjectProperties(structure.properties, structure.name); |
| return this.generateObjectComments(comment, categorizedProps, structure); |
| } |
|
|
| return comment; |
| } |
|
|
| |
| generateObjectComments(headerComment, categorizedProps, structure) { |
| |
| |
| return headerComment.trim(); |
| } |
|
|
| |
| categorizeObjectProperties(properties, objectName) { |
| const categories = { |
| keywords: [], |
| identifiers: [], |
| operators: [], |
| comments: [], |
| strings: [], |
| numbers: [], |
| whitespace: [], |
| other: [] |
| }; |
|
|
| properties.forEach(prop => { |
| const name = prop.name.toLowerCase(); |
|
|
| if (name.includes('keyword')) { |
| categories.keywords.push(prop); |
| } else if (name.includes('identifier')) { |
| categories.identifiers.push(prop); |
| } else if (name.includes('equals') || name.includes('arrow') || |
| name.includes('paren') || name.includes('brace') || |
| name.includes('semicolon') || name.includes('comma')) { |
| categories.operators.push(prop); |
| } else if (name.includes('comment') || name.includes('line') || name.includes('block')) { |
| categories.comments.push(prop); |
| } else if (name.includes('string')) { |
| categories.strings.push(prop); |
| } else if (name.includes('number')) { |
| categories.numbers.push(prop); |
| } else if (name.includes('whitespace') || name.includes('newline') || name.includes('eof')) { |
| categories.whitespace.push(prop); |
| } else { |
| categories.other.push(prop); |
| } |
| }); |
|
|
| return categories; |
| } |
|
|
| |
| generatePropertyComment(prop, objectName) { |
| const name = prop.name.toLowerCase(); |
|
|
| |
| if (objectName.includes('TOKEN')) { |
| if (name.includes('keyword')) return ' // คำหลัก'; |
| if (name.includes('identifier')) return ' // ชื่อตัวแปร/ฟังก์ชัน'; |
| if (name.includes('equals')) return ' // ='; |
| if (name.includes('arrow')) return ' // =>'; |
| if (name.includes('paren_open')) return ' // ('; |
| if (name.includes('paren_close')) return ' // )'; |
| if (name.includes('brace_open')) return ' // {'; |
| if (name.includes('brace_close')) return ' // }'; |
| if (name.includes('semicolon')) return ' // ;'; |
| if (name.includes('comma')) return ' // ,'; |
| if (name.includes('line_comment')) return ' // //'; |
| if (name.includes('block_comment')) return ' // /* */'; |
| if (name.includes('string')) return ' // สตริง'; |
| if (name.includes('number')) return ' // ตัวเลข'; |
| if (name.includes('whitespace')) return ' // ช่องว่าง'; |
| if (name.includes('newline')) return ' // บรรทัดใหม่'; |
| if (name.includes('eof')) return ' // จุดสิ้นสุดไฟล์'; |
| } |
|
|
| return ''; |
| } |
|
|
| |
| getCategoryComment(category) { |
| const categoryComments = { |
| keywords: 'คำหลัก - Keywords', |
| identifiers: 'ชื่อตัวแปร/ฟังก์ชัน - Identifiers', |
| operators: 'เครื่องหมาย - Operators and punctuation', |
| comments: 'คอมเมนต์ - Comments', |
| strings: 'สตริง - Strings', |
| numbers: 'ตัวเลข - Numbers', |
| whitespace: 'ช่องว่าง - Whitespace', |
| other: 'อื่นๆ - Others' |
| }; |
|
|
| return categoryComments[category] || 'อื่นๆ - Others'; |
| } |
|
|
| |
| getFunctionDescription(functionName, funcType = '') { |
| const name = functionName.toLowerCase(); |
| const type = funcType.toLowerCase(); |
|
|
| |
| for (const [keyword, descriptions] of Object.entries(this.functionDescriptions)) { |
| if (keyword !== 'default' && name.includes(keyword)) { |
| |
| if (keyword === 'node' && (type.includes('class') || type === 'class_declaration')) { |
| return { |
| english: 'Node class', |
| thai: 'คลาสโหนด' |
| }; |
| } |
|
|
| |
| if (name.includes('tree') && name.includes('node') && (type.includes('class') || type === 'class_declaration')) { |
| return { |
| english: 'TreeNode class', |
| thai: 'คลาสโหนดต้นไม้' |
| }; |
| } |
|
|
| |
| if (name.includes('graph') && name.includes('node') && (type.includes('class') || type === 'class_declaration')) { |
| return { |
| english: 'GraphNode class', |
| thai: 'คลาสโหนดกราฟ' |
| }; |
| } |
|
|
| |
| if (type.includes('class') || type === 'class_declaration') { |
| const baseDesc = descriptions[0].toLowerCase(); |
| if (!baseDesc.includes('class')) { |
| return { |
| english: `${descriptions[0]} class`, |
| thai: `คลาส${descriptions[1]}` |
| }; |
| } |
| } |
|
|
| return { |
| english: descriptions[0], |
| thai: descriptions[1] |
| }; |
| } |
| } |
|
|
| |
| if (type.includes('class') || type === 'class_declaration') { |
| return { |
| english: `${functionName} class`, |
| thai: `คลาส ${functionName}` |
| }; |
| } |
|
|
| const defaultDesc = this.functionDescriptions.default; |
| return { |
| english: `${defaultDesc[0]} ${functionName}`, |
| thai: `${defaultDesc[1]} ${functionName}` |
| }; |
| } |
|
|
| |
| findRealStartLine(lines, functionLine) { |
| let startLine = functionLine; |
| const originalLine = lines[functionLine]; |
|
|
| if (!originalLine) { |
| |
| return functionLine; |
| } |
|
|
| |
| const currentTrimmed = originalLine.trim(); |
|
|
| |
| if (currentTrimmed === '}' || |
| currentTrimmed === '});' || |
| currentTrimmed === ');' || |
| currentTrimmed.match(/^}\s*[,;]?\s*$/)) { |
|
|
| |
| if (this.isValidStartLine(currentTrimmed)) { |
| return startLine; |
| } |
|
|
| |
| return this.findAlternativeStartLine(lines, functionLine); |
| } |
| for (let i = functionLine - 1; i >= Math.max(0, functionLine - 10); i--) { |
| const line = lines[i]; |
| if (!line) continue; |
|
|
| const trimmed = line.trim(); |
|
|
| |
| if (trimmed === '') continue; |
|
|
| |
| if (trimmed.startsWith('//') || trimmed.startsWith('/*')) { |
| if (trimmed.includes('======') || trimmed.includes('EN:') || trimmed.includes(':ชื่อ') || trimmed.includes(':คลาส') || trimmed.includes('class:')) { |
| break; |
| } |
| continue; |
| } |
|
|
| |
| if (trimmed.startsWith('@') || |
| trimmed.startsWith('export') || |
| trimmed.startsWith('import') || |
| trimmed.includes('interface ') || |
| trimmed.includes('type ') || |
| (trimmed.endsWith(',') && !trimmed.includes('{')) || |
| (trimmed.endsWith(';') && !trimmed.includes('{'))) { |
| startLine = i; |
| continue; |
| } |
|
|
| |
| break; |
| } |
|
|
| return startLine; |
| } |
|
|
| |
| isValidStartLine(trimmedLine) { |
| |
| const validPatterns = [ |
| |
| |
| |
| |
| |
| /^class\s+\w+/, |
| /^abstract\s+class\s+\w+/, |
| /^interface\s+\w+/, |
| /^type\s+\w+\s*=/, |
| /^enum\s+\w+/, |
| /^function\s+\w+/, |
| /^async\s+function/, |
| /^function\*/, |
| /^async\s+function\*/, |
| /^const\s+\w+\s*=/, |
| /^let\s+\w+\s*=/, |
| /^var\s+\w+\s*=/, |
| /^\w+\s*:/, |
| /^\w+\s*\(/, |
|
|
| |
| /^export/, |
| /^module\.exports/, |
|
|
| |
| /^static\s+\w+\s*\(/, |
| /^abstract\s+\w+\s*\(/, |
| /^public\s+\w+\s*\(/, |
| /^private\s+\w+\s*\(/, |
| /^protected\s+\w+\s*\(/, |
| /^readonly\s+\w+\s*:/, |
| /^get\s+\w+\s*\(/, |
| /^set\s+\w+\s*\(/, |
| /^async\s+\w+\s*\(/, |
|
|
| |
| /^class\s+\w+<[^>]+>/, |
| /^interface\s+\w+<[^>]+>/, |
| /^function\s+\w+<[^>]+>/, |
|
|
| |
| /^@\w+/ |
| ]; |
|
|
| |
| return validPatterns.some(pattern => pattern.test(trimmedLine)); |
| } |
|
|
| |
| isAppropriateCommentLocation(lines, lineIndex) { |
| const currentLine = lines[lineIndex]; |
| if (!currentLine) return false; |
|
|
| const trimmed = currentLine.trim(); |
|
|
| |
| if (lines[lineIndex].includes('=====') || lines[lineIndex].startsWith('// EN:') || lines[lineIndex].startsWith('// TH:')) { |
| return false; |
| } |
|
|
| |
| |
| |
| |
|
|
| |
| const inappropriatePatterns = [ |
| /^}\s*$/, |
| /^}\);?\s*$/, |
| /^};?\s*$/, |
| /^\);\s*$/, |
| /^,\s*$/, |
| /^;\s*$/, |
| /^break;?\s*$/, |
| /^continue;?\s*$/, |
| /^case\s/, |
| /^default:\s*$/, |
| /^else\s*{?\s*$/, |
| /^catch\s*\(/, |
| /^finally\s*{?\s*$/, |
| /^\s*$|^$/, |
| /^<\/\w+>/, |
| /^\/\*/, |
| /^\/\//, |
| /^\s*\*+/, |
| /return\s/, |
| /app\.\w+/, |
| /\.map\(/, |
| /\.find\(/, |
| /\.filter\(/, |
| /\.includes\(/, |
| ]; |
|
|
| |
| |
| |
| |
|
|
| |
| const validDeclarationPatterns = [ |
| |
| /^(class|interface|enum|type)\s+\w+/, |
| /^abstract\s+class\s+\w+/, |
| /^(export\s+)?(default\s+)?(class|interface|type|enum|function|const|let|var)/, |
| /^(async\s+)?function\s+\w+/, |
| /^(const|let|var)\s+\w+\s*=\s*(\w+\s*=>\s*|async\s*\w*\s*=>\s*|\([^)]*\)\s*=>\s*)/, |
| /^(const|let|var)\s+\w+\s*=/, |
| /^(public|private|protected|static|async)\s+\w+/, |
| /^\w+\s*\([^)]*\)\s*\{/, |
| /^(get|set)\s+\w+/, |
| /^@\w+/, |
| |
| /^(const|let|var)\s+\w+\s*=\s*React\.forwardRef/, |
| /^(const|let|var)\s+\w+\s*=\s*React\.memo/, |
| /^(const|let|var)\s+\w+\s*=\s*\([^)]*\)\s*=>\s*\{/, |
| |
| /^namespace\s+\w+/, |
| /^module\s+\w+/, |
| /^declare\s+(global|module|namespace|class|interface|type|enum|function|var|let|const)/, |
| ]; |
|
|
| |
| if (validDeclarationPatterns.some(pattern => pattern.test(trimmed))) { |
| return true; |
| } |
|
|
| |
| const strictInappropriatePatterns = [ |
| /^}\s*$/, |
| /^}\);?\s*$/, |
| /^\);\s*$/, |
| /^,\s*$/, |
| /^;\s*$/, |
| /^break;?\s*$/, |
| /^continue;?\s*$/, |
| /^case\s/, |
| /^default:\s*$/, |
| /^else\s*{?\s*$/, |
| /^catch\s*\(/, |
| /^finally\s*{?\s*$/, |
| /^\s*$|^$/, |
| /^<\/\w+>/, |
| /^\/\*/, |
| /^\/\//, |
| /^\s*\*+/, |
| /return\s/, |
| /app\.\w+/, |
| /\.map\(/, |
| /\.find\(/, |
| /\.filter\(/, |
| /\.includes\(/, |
| ]; |
|
|
| |
| if (strictInappropriatePatterns.some(pattern => pattern.test(trimmed))) { |
| return false; |
| } |
|
|
| |
| |
| |
| |
|
|
| |
| const methodWithParametersPattern = /^(async\s+)?(\w+)\s*\([^)]*\)\s*\{/; |
| if (methodWithParametersPattern.test(trimmed)) { |
| |
| const contextAnalysis = this.analyzeMethodContext(lines, lineIndex); |
| return contextAnalysis.isAppropriateForComment; |
| } |
|
|
| |
| if (/^};?\s*$/.test(trimmed)) { |
| |
| const declarationEndAnalysis = this.analyzeDeclarationEnd(lines, lineIndex); |
| return declarationEndAnalysis.isDeclarationEnd; |
| } |
|
|
| |
| let braceBalance = 0; |
| let contextLevel = 0; |
| let lastDeclarationType = null; |
|
|
| |
| for (let i = Math.max(0, lineIndex - 30); i < lineIndex; i++) { |
| const line = lines[i]; |
| if (!line) continue; |
|
|
| const lineTrimmed = line.trim(); |
| if (!lineTrimmed) continue; |
|
|
| |
| const openCount = (lineTrimmed.match(/\{/g) || []).length; |
| |
| |
| |
| |
| const closeCount = (lineTrimmed.match(/\}/g) || []).length; |
| braceBalance += openCount - closeCount; |
|
|
| |
| if (lineTrimmed.match(/^(class|interface|function|const|let|var|export)/)) { |
| if (braceBalance === 0) { |
| contextLevel = 0; |
| } else if (braceBalance === 1) { |
| contextLevel = 1; |
| } |
| lastDeclarationType = lineTrimmed.match(/^(\w+)/)?.[1]; |
| } |
| } |
|
|
| |
| |
| if (contextLevel === 0) return true; |
|
|
| |
| if (contextLevel === 1 && lastDeclarationType === 'class') { |
| return this.isValidStartLine(trimmed); |
| } |
|
|
| |
| if (braceBalance > 1) return false; |
|
|
| return false; |
| } |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| analyzeMethodContext(lines, lineIndex) { |
| const currentLine = lines[lineIndex].trim(); |
|
|
| |
| let inClass = false; |
| let classLevel = 0; |
| let braceLevel = 0; |
|
|
| |
| for (let i = lineIndex - 1; i >= Math.max(0, lineIndex - 50); i--) { |
| const line = lines[i]; |
| if (!line) continue; |
|
|
| const trimmed = line.trim(); |
|
|
| |
| const openCount = (trimmed.match(/\{/g) || []).length; |
| const closeCount = (trimmed.match(/\}/g) || []).length; |
| braceLevel += closeCount - openCount; |
|
|
| |
| if (/^(class|interface|abstract\s+class)\s+\w+/.test(trimmed) && braceLevel === 0) { |
| inClass = true; |
| classLevel = 0; |
| break; |
| } |
|
|
| |
| if (/^(function|const\s+\w+\s*=|let\s+\w+\s*=|var\s+\w+\s*=)/.test(trimmed) && braceLevel === 0) { |
| break; |
| } |
| } |
|
|
| |
| if (inClass && braceLevel <= 1) { |
| return { |
| isAppropriateForComment: true, |
| context: 'class_method', |
| reason: 'Method definition within class context' |
| }; |
| } |
|
|
| |
| if (braceLevel === 0) { |
| return { |
| isAppropriateForComment: true, |
| context: 'top_level_function', |
| reason: 'Top-level function definition' |
| }; |
| } |
|
|
| |
| return { |
| isAppropriateForComment: false, |
| context: 'nested_implementation', |
| reason: 'Nested function in implementation details' |
| }; |
| } |
|
|
| |
| |
| |
| |
| analyzeDeclarationEnd(lines, lineIndex) { |
| |
| let braceLevel = 1; |
|
|
| for (let i = lineIndex - 1; i >= Math.max(0, lineIndex - 100); i--) { |
| const line = lines[i]; |
| if (!line) continue; |
|
|
| const trimmed = line.trim(); |
|
|
| |
| const openCount = (trimmed.match(/\{/g) || []).length; |
| const closeCount = (trimmed.match(/\}/g) || []).length; |
| braceLevel += closeCount - openCount; |
|
|
| |
| if (braceLevel === 0) { |
| |
| const declarationPatterns = [ |
| /^(interface|type|enum|namespace|module)\s+\w+/, |
| /^(const|let|var)\s+\w+\s*=\s*\{/, |
| /^(export\s+)?(interface|type|enum)\s+\w+/, |
| ]; |
|
|
| const isDeclaration = declarationPatterns.some(pattern => pattern.test(trimmed)); |
|
|
| return { |
| isDeclarationEnd: isDeclaration, |
| declarationType: isDeclaration ? trimmed.match(/^(\w+)/)?.[1] : null, |
| reason: isDeclaration ? 'End of type/interface/enum declaration' : 'End of implementation block' |
| }; |
| } |
| } |
|
|
| return { |
| isDeclarationEnd: false, |
| declarationType: null, |
| reason: 'Cannot determine declaration context' |
| }; |
| } |
|
|
| |
| findAlternativeStartLine(lines, originalLine) { |
| |
| |
| console.log(` Warning: Line number might be incorrect for line ${originalLine}, using fallback`); |
| return Math.max(0, originalLine); |
| } |
|
|
| |
| generateZoneHeader(title, description) { |
| return '// ======================================================================\n' + |
| `// ${title}/${description}\n` + |
| '// ======================================================================\n'; |
| } |
|
|
| |
| generateFormattedComment(smartComment, type) { |
| if (!smartComment || !smartComment.english || !smartComment.thai) { |
| return this.generateFunctionComment({ name: 'Unknown', type: type }); |
| } |
|
|
| |
| return `// ======================================================================\n` + |
| `// EN: ${smartComment.english}\n` + |
| `// TH: ${smartComment.thai}\n` + |
| `// ======================================================================`; |
| } |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function analyzeFileWithSmartLearning(content, options = {}) { |
| try { |
| console.log(' Starting Smart Learning Analysis...'); |
|
|
| |
| const analyzer = new SmartFileAnalyzer(content, options.security || {}); |
|
|
| |
| const blueprint = analyzer.analyzeFile(); |
|
|
| if (!blueprint) { |
| console.log(' Smart Learning Analysis failed, falling back to traditional method'); |
| return null; |
| } |
|
|
| |
| console.log('\n เริ่มต้น AI Intent Understanding Engine...'); |
| const intentReport = analyzer.getIntentAnalysisReport(); |
|
|
| if (intentReport) { |
| console.log(` Business Logic: ${intentReport.summary.totalBusinessLogic} items`); |
| console.log(` Algorithms: ${intentReport.summary.totalAlgorithms} items`); |
| console.log(` Security Measures: ${intentReport.summary.totalSecurityMeasures} items`); |
| console.log(` Data Flows: ${intentReport.summary.totalDataFlows} items`); |
| console.log(` Smart Comments: ${intentReport.smartComments.length} generated`); |
|
|
| |
| blueprint.intentAnalysis = intentReport; |
| } |
|
|
| |
| console.log('\n Smart Learning Results:'); |
| console.log(` - Classes: ${blueprint.classes.size}`); |
| console.log(` - Functions: ${blueprint.functions.size}`); |
| console.log(` - Patterns: ${blueprint.patterns.size}`); |
| console.log(` - Keywords: ${blueprint.keywords.size}`); |
| console.log(` - File Type: ${blueprint.context.type}`); |
| console.log(` - Domain: ${blueprint.context.domain}`); |
| console.log(` - Complexity: ${blueprint.context.complexity}`); |
|
|
| return { |
| success: true, |
| blueprint: blueprint, |
| analyzer: analyzer |
| }; |
|
|
| } catch (error) { |
| console.error(' Smart Learning Analysis Error:', error.message); |
| return null; |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| function generateSmartComment(functionName, type, blueprint, structureInfo = {}) { |
| try { |
| |
| if (!blueprint) { |
| const generator = new CommentGenerator(); |
| return generator.getFunctionDescription(functionName, type); |
| } |
|
|
| let comment = null; |
|
|
| |
| |
| |
|
|
| |
| if (type === 'interface') { |
| comment = generateInterfaceComment(functionName, blueprint, structureInfo); |
| } else if (type === 'type_alias') { |
| comment = generateTypeAliasComment(functionName, blueprint, structureInfo); |
| } else if (type === 'enum') { |
| comment = generateEnumComment(functionName, blueprint, structureInfo); |
| } else if (type === 'abstract_class') { |
| comment = generateAbstractClassComment(functionName, blueprint, structureInfo); |
| } |
| |
| |
| |
| else if (['configuration', 'data_store', 'storage_config', 'rate_limiter', 'middleware', 'arrow_function', 'express_app', 'object', 'array', 'constant'].includes(type)) { |
| comment = generateConstComment(functionName, blueprint, structureInfo, type); |
| } |
| else if (type.includes('class') || type === 'class_declaration') { |
| comment = generateSmartClassComment(functionName, blueprint, structureInfo); |
| } else { |
| comment = generateSmartFunctionComment(functionName, blueprint, structureInfo); |
| } |
| if (!comment) { |
| console.log(` Falling back to traditional method for: ${functionName}`); |
| const generator = new CommentGenerator(); |
| return generator.getFunctionDescription(functionName, type); |
| } |
|
|
| return comment; |
|
|
| } catch (error) { |
| console.error(` Smart Comment Generation Error for ${functionName}:`, error.message); |
|
|
| |
| const generator = new CommentGenerator(); |
| return generator.getFunctionDescription(functionName, type); |
| } |
| } |
| |
| |
| |
| |
| |
| function generateSmartClassComment(className, blueprint, structureInfo) { |
| const classInfo = blueprint.classes.get(className); |
|
|
| if (!classInfo) { |
| return null; |
| } |
|
|
| const context = blueprint.context; |
| let englishComment = ''; |
| let thaiComment = ''; |
|
|
| |
| switch (classInfo.purpose) { |
| case 'manager': |
| if (context.domain === 'crypto') { |
| englishComment = `${className} - Security and cryptographic operations manager`; |
| thaiComment = `${className} - ตัวจัดการงานด้านความปลอดภัยและการเข้ารหัส`; |
| } else if (context.domain === 'data') { |
| englishComment = `${className} - Data management and processing manager`; |
| thaiComment = `${className} - ตัวจัดการข้อมูลและการประมวลผล`; |
| } else { |
| englishComment = `${className} - System operations manager`; |
| thaiComment = `${className} - ตัวจัดการการดำเนินงานระบบ`; |
| } |
| break; |
|
|
| case 'controller': |
| englishComment = `${className} - Application flow controller`; |
| thaiComment = `${className} - ตัวควบคุมการทำงานของแอปพลิเคชัน`; |
| break; |
|
|
| case 'service': |
| englishComment = `${className} - Business logic service provider`; |
| thaiComment = `${className} - ผู้ให้บริการตรรกะทางธุรกิจ`; |
| break; |
|
|
| case 'error': |
| englishComment = `${className} - Custom error handling class`; |
| thaiComment = `${className} - คลาสจัดการข้อผิดพลาดแบบกำหนดเอง`; |
| break; |
|
|
| case 'validator': |
| englishComment = `${className} - Data validation and verification`; |
| thaiComment = `${className} - การตรวจสอบและยืนยันข้อมูล`; |
| break; |
|
|
| default: |
| |
| if (context.type === 'security') { |
| englishComment = `${className} - Security-related class`; |
| thaiComment = `${className} - คลาสที่เกี่ยวข้องกับความปลอดภัย`; |
| } else if (context.type === 'api') { |
| englishComment = `${className} - API-related class`; |
| thaiComment = `${className} - คลาสที่เกี่ยวข้องกับ API`; |
| } else if (context.type === 'database') { |
| englishComment = `${className} - Database-related class`; |
| thaiComment = `${className} - คลาสที่เกี่ยวข้องกับฐานข้อมูล`; |
| } else { |
| englishComment = `${className} class`; |
| thaiComment = `คลาส ${className}`; |
| } |
| } |
|
|
| return { |
| english: englishComment, |
| thai: thaiComment |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| function generateSmartFunctionComment(functionName, blueprint, structureInfo) { |
| |
| let functionInfo = blueprint.functions.get(functionName); |
|
|
| |
| if (!functionInfo) { |
| for (const classInfo of blueprint.classes.values()) { |
| const methodInfo = classInfo.methods.get(functionName); |
| if (methodInfo) { |
| functionInfo = { |
| ...methodInfo, |
| parentClass: classInfo.name, |
| isMethod: true |
| }; |
| break; |
| } |
| } |
| } |
|
|
| if (!functionInfo) { |
| return null; |
| } |
|
|
| const context = blueprint.context; |
| let englishComment = ''; |
| let thaiComment = ''; |
|
|
| |
| switch (functionInfo.purpose) { |
| case 'generate_crypto': |
| englishComment = `Generate cryptographic ${functionName.toLowerCase().includes('key') ? 'key' : 'data'}`; |
| thaiComment = `สร้าง${functionName.toLowerCase().includes('key') ? 'คีย์' : 'ข้อมูล'}เข้ารหัส`; |
| break; |
|
|
| case 'encrypt_data': |
| englishComment = 'Encrypt sensitive data'; |
| thaiComment = 'เข้ารหัสข้อมูลที่สำคัญ'; |
| break; |
|
|
| case 'decrypt_data': |
| englishComment = 'Decrypt encrypted data'; |
| thaiComment = 'ถอดรหัสข้อมูลที่เข้ารหัส'; |
| break; |
|
|
| case 'get_size': |
| if (functionName.toLowerCase().includes('cache')) { |
| englishComment = 'Get current cache size'; |
| thaiComment = 'ดึงขนาดแคชปัจจุบัน'; |
| } else { |
| englishComment = 'Get size information'; |
| thaiComment = 'ดึงข้อมูลขนาด'; |
| } |
| break; |
|
|
| case 'set_limit': |
| if (functionName.toLowerCase().includes('cache')) { |
| englishComment = 'Set maximum cache limit'; |
| thaiComment = 'กำหนดขีดจำกัดแคชสูงสุด'; |
| } else { |
| englishComment = 'Set operational limit'; |
| thaiComment = 'กำหนดขีดจำกัดการทำงาน'; |
| } |
| break; |
|
|
| case 'clear_cache': |
| englishComment = 'Clear cache data'; |
| thaiComment = 'ล้างข้อมูลแคช'; |
| break; |
|
|
| case 'getter': |
| if (context.domain === 'crypto') { |
| englishComment = `Get ${functionName.replace(/^get/, '').toLowerCase()} value`; |
| thaiComment = `ดึงค่า ${functionName.replace(/^get/, '').toLowerCase()}`; |
| } else { |
| englishComment = `Get ${functionName.replace(/^get/, '').toLowerCase()}`; |
| thaiComment = `ดึง ${functionName.replace(/^get/, '').toLowerCase()}`; |
| } |
| break; |
|
|
| case 'setter': |
| let setterName = functionName |
| .replace(/^set([A-Z])/, '$1') |
| .replace(/([A-Z])/g, ' $1') |
| .toLowerCase() |
| .trim(); |
| englishComment = `Set ${setterName}`; |
| thaiComment = `กำหนด ${setterName}`; |
| break; |
|
|
| case 'creator': |
| let creatorName = functionName |
| .replace(/^create([A-Z])/, '$1') |
| .replace(/([A-Z])/g, ' $1') |
| .toLowerCase() |
| .trim(); |
| englishComment = `Create new ${creatorName}`; |
| thaiComment = `สร้าง ${creatorName} ใหม่`; |
| break; |
|
|
| case 'validator': |
| let validatorName = functionName |
| .replace(/^(validate|check|verify)([A-Z])/, '$2') |
| .replace(/([A-Z])/g, ' $1') |
| .toLowerCase() |
| .trim(); |
| englishComment = `Validate ${validatorName}`; |
| thaiComment = `ตรวจสอบ ${validatorName}`; |
| break; |
|
|
| case 'processor': |
| let processorName = functionName |
| .replace(/^(process|handle|execute)([A-Z])/, '$2') |
| .replace(/([A-Z])/g, ' $1') |
| .toLowerCase() |
| .trim(); |
| englishComment = `Process ${processorName}`; |
| thaiComment = `ประมวลผล ${processorName}`; |
| break; |
|
|
| case 'demo': |
| |
| let cleanedName = functionName; |
| if (functionName === 'demonstrateAlgorithms') { |
| cleanedName = 'advanced algorithms and data structures'; |
| } else if (functionName === 'demonstrateFeatures') { |
| cleanedName = 'system features'; |
| } else { |
| |
| cleanedName = functionName |
| .replace(/^(demo|test|example)([A-Z])/, '$2') |
| .replace(/([A-Z])/g, ' $1') |
| .toLowerCase() |
| .trim(); |
| } |
| englishComment = `Demonstration of ${cleanedName}`; |
| thaiComment = `การสาธิต ${cleanedName}`; |
| break; |
|
|
| default: |
| |
| if (context.type === 'security' && functionName.toLowerCase().includes('key')) { |
| englishComment = 'Security key operation'; |
| thaiComment = 'การดำเนินการด้านคีย์ความปลอดภัย'; |
| } else if (context.type === 'api' && functionName.toLowerCase().includes('request')) { |
| englishComment = 'API request operation'; |
| thaiComment = 'การดำเนินการคำขอ API'; |
| } else { |
| |
| return null; |
| } |
| } |
|
|
| return { |
| english: englishComment, |
| thai: thaiComment |
| }; |
| } |
|
|
| |
| |
| |
|
|
| |
| function generateInterfaceComment(interfaceName, blueprint, structureInfo) { |
| const interfaceInfo = blueprint.classes.get(interfaceName); |
|
|
| if (!interfaceInfo) { |
| return null; |
| } |
|
|
| let englishComment = ''; |
| let thaiComment = ''; |
|
|
| switch (interfaceInfo.purpose) { |
| case 'configuration': |
| englishComment = `${interfaceName} - Configuration interface`; |
| thaiComment = `${interfaceName} - อินเทอร์เฟซการกำหนดค่า`; |
| break; |
| case 'result_type': |
| englishComment = `${interfaceName} - Result data structure`; |
| thaiComment = `${interfaceName} - โครงสร้างข้อมูลผลลัพธ์`; |
| break; |
| case 'input_type': |
| englishComment = `${interfaceName} - Input parameters interface`; |
| thaiComment = `${interfaceName} - อินเทอร์เฟซพารามิเตอร์นำเข้า`; |
| break; |
| case 'error_type': |
| englishComment = `${interfaceName} - Error information interface`; |
| thaiComment = `${interfaceName} - อินเทอร์เฟซข้อมูลข้อผิดพลาด`; |
| break; |
| case 'event_type': |
| englishComment = `${interfaceName} - Event data interface`; |
| thaiComment = `${interfaceName} - อินเทอร์เฟซข้อมูลเหตุการณ์`; |
| break; |
| case 'data_type': |
| englishComment = `${interfaceName} - Data model interface`; |
| thaiComment = `${interfaceName} - อินเทอร์เฟซโมเดลข้อมูล`; |
| break; |
| case 'service_interface': |
| englishComment = `${interfaceName} - Service contract interface`; |
| thaiComment = `${interfaceName} - อินเทอร์เฟซสัญญาการให้บริการ`; |
| break; |
| case 'repository_interface': |
| englishComment = `${interfaceName} - Repository pattern interface`; |
| thaiComment = `${interfaceName} - อินเทอร์เฟซรูปแบบพื้นที่เก็บข้อมูล`; |
| break; |
| default: |
| englishComment = `${interfaceName} - Interface definition`; |
| thaiComment = `${interfaceName} - นิยามอินเทอร์เฟซ`; |
| } |
|
|
| return { |
| english: englishComment, |
| thai: thaiComment |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| function generateTypeAliasComment(typeName, blueprint, structureInfo) { |
| const typeInfo = blueprint.functions.get(typeName); |
|
|
| if (!typeInfo) { |
| return null; |
| } |
|
|
| let englishComment = ''; |
| let thaiComment = ''; |
|
|
| switch (typeInfo.purpose) { |
| case 'callback_type': |
| englishComment = `${typeName} - Callback function type`; |
| thaiComment = `${typeName} - ประเภทฟังก์ชันเรียกกลับ`; |
| break; |
| case 'validation_type': |
| englishComment = `${typeName} - Validation rule type`; |
| thaiComment = `${typeName} - ประเภทกฎการตรวจสอบ`; |
| break; |
| case 'strategy_type': |
| englishComment = `${typeName} - Strategy pattern type`; |
| thaiComment = `${typeName} - ประเภทรูปแบบกลยุทธ์`; |
| break; |
| case 'string_union': |
| englishComment = `${typeName} - String union type`; |
| thaiComment = `${typeName} - ประเภทการรวมสตริง`; |
| break; |
| case 'number_union': |
| englishComment = `${typeName} - Number union type`; |
| thaiComment = `${typeName} - ประเภทการรวมตัวเลข`; |
| break; |
| case 'function_type': |
| englishComment = `${typeName} - Function signature type`; |
| thaiComment = `${typeName} - ประเภทลายเซ็นฟังก์ชัน`; |
| break; |
| case 'object_type': |
| englishComment = `${typeName} - Object structure type`; |
| thaiComment = `${typeName} - ประเภทโครงสร้างออบเจ็กต์`; |
| break; |
| default: |
| englishComment = `${typeName} - Type alias definition`; |
| thaiComment = `${typeName} - นิยามนามแฝงประเภท`; |
| } |
|
|
| return { |
| english: englishComment, |
| thai: thaiComment |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| function generateEnumComment(enumName, blueprint, structureInfo) { |
| const enumInfo = blueprint.classes.get(enumName); |
|
|
| if (!enumInfo) { |
| return null; |
| } |
|
|
| let englishComment = ''; |
| let thaiComment = ''; |
|
|
| switch (enumInfo.purpose) { |
| case 'status_enum': |
| englishComment = `${enumName} - Status enumeration`; |
| thaiComment = `${enumName} - การแจงนับสถานะ`; |
| break; |
| case 'type_enum': |
| englishComment = `${enumName} - Type enumeration`; |
| thaiComment = `${enumName} - การแจงนับประเภท`; |
| break; |
| case 'level_enum': |
| englishComment = `${enumName} - Level enumeration`; |
| thaiComment = `${enumName} - การแจงนับระดับ`; |
| break; |
| case 'direction_enum': |
| englishComment = `${enumName} - Direction enumeration`; |
| thaiComment = `${enumName} - การแจงนับทิศทาง`; |
| break; |
| case 'mode_enum': |
| englishComment = `${enumName} - Mode enumeration`; |
| thaiComment = `${enumName} - การแจงนับโหมด`; |
| break; |
| default: |
| englishComment = `${enumName} - Enumeration values`; |
| thaiComment = `${enumName} - ค่าการแจงนับ`; |
| } |
|
|
| return { |
| english: englishComment, |
| thai: thaiComment |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| function generateAbstractClassComment(className, blueprint, structureInfo) { |
| const classInfo = blueprint.classes.get(className); |
|
|
| if (!classInfo) { |
| return null; |
| } |
|
|
| let englishComment = ''; |
| let thaiComment = ''; |
|
|
| switch (classInfo.purpose) { |
| case 'base_class': |
| englishComment = `${className} - Abstract base class`; |
| thaiComment = `${className} - คลาสฐานนามธรรม`; |
| break; |
| case 'repository_base': |
| englishComment = `${className} - Abstract repository base class`; |
| thaiComment = `${className} - คลาสฐานพื้นที่เก็บข้อมูลนามธรรม`; |
| break; |
| case 'service_base': |
| englishComment = `${className} - Abstract service base class`; |
| thaiComment = `${className} - คลาสฐานบริการนามธรรม`; |
| break; |
| case 'controller_base': |
| englishComment = `${className} - Abstract controller base class`; |
| thaiComment = `${className} - คลาสฐานตัวควบคุมนามธรรม`; |
| break; |
| case 'component_base': |
| englishComment = `${className} - Abstract component base class`; |
| thaiComment = `${className} - คลาสฐานส่วนประกอบนามธรรม`; |
| break; |
| default: |
| englishComment = `${className} - Abstract class definition`; |
| thaiComment = `${className} - นิยามคลาสนามธรรม`; |
| } |
|
|
| return { |
| english: englishComment, |
| thai: thaiComment |
| }; |
| } |
|
|
| |
| |
| |
| |
| |
| function generateConstComment(constName, blueprint, structureInfo, constType) { |
| |
| let constInfo = blueprint.classes.get(constName) || blueprint.functions.get(constName); |
|
|
| if (!constInfo) { |
| return null; |
| } |
|
|
| let englishComment = ''; |
| let thaiComment = ''; |
|
|
| switch (constType) { |
| case 'configuration': |
| englishComment = `${constName} - Application configuration settings`; |
| thaiComment = `${constName} - การกำหนดค่าแอปพลิเคชัน`; |
| break; |
|
|
| case 'data_store': |
| englishComment = `${constName} - In-memory data storage`; |
| thaiComment = `${constName} - ที่เก็บข้อมูลในหน่วยความจำ`; |
| break; |
|
|
| case 'storage_config': |
| englishComment = `${constName} - File upload storage configuration`; |
| thaiComment = `${constName} - การกำหนดค่าการจัดเก็บไฟล์อัปโหลด`; |
| break; |
|
|
| case 'rate_limiter': |
| if (constName.toLowerCase().includes('auth')) { |
| englishComment = `${constName} - Authentication rate limiting middleware`; |
| thaiComment = `${constName} - มิดเดิลแวร์จำกัดอัตราการรับรองตัวตน`; |
| } else { |
| englishComment = `${constName} - API rate limiting middleware`; |
| thaiComment = `${constName} - มิดเดิลแวร์จำกัดอัตรา API`; |
| } |
| break; |
|
|
| case 'middleware': |
| if (constInfo.purpose === 'auth_middleware') { |
| englishComment = `${constName} - Authentication middleware function`; |
| thaiComment = `${constName} - ฟังก์ชันมิดเดิลแวร์การรับรองตัวตน`; |
| } else if (constInfo.purpose === 'validation_middleware') { |
| englishComment = `${constName} - Request validation middleware function`; |
| thaiComment = `${constName} - ฟังก์ชันมิดเดิลแวร์ตรวจสอบคำขอ`; |
| } else if (constInfo.purpose === 'async_wrapper') { |
| englishComment = `${constName} - Async error handling wrapper`; |
| thaiComment = `${constName} - ตัวห่อหุ้มจัดการข้อผิดพลาดแบบ async`; |
| } else { |
| englishComment = `${constName} - Express middleware function`; |
| thaiComment = `${constName} - ฟังก์ชันมิดเดิลแวร์ Express`; |
| } |
| break; |
|
|
| case 'arrow_function': |
| if (constInfo.purpose === 'finder_function') { |
| englishComment = `${constName} - Data finder utility function`; |
| thaiComment = `${constName} - ฟังก์ชันยูทิลิตี้ค้นหาข้อมูล`; |
| } else if (constInfo.purpose === 'generator_function') { |
| englishComment = `${constName} - ID generator utility function`; |
| thaiComment = `${constName} - ฟังก์ชันยูทิลิตี้สร้าง ID`; |
| } else { |
| englishComment = `${constName} - Utility function`; |
| thaiComment = `${constName} - ฟังก์ชันยูทิลิตี้`; |
| } |
| break; |
|
|
| case 'express_app': |
| englishComment = `${constName} - Express application instance`; |
| thaiComment = `${constName} - อินสแตนซ์แอปพลิเคชัน Express`; |
| break; |
|
|
| case 'object': |
| englishComment = `${constName} - Configuration object`; |
| thaiComment = `${constName} - ออบเจ็กต์การกำหนดค่า`; |
| break; |
|
|
| case 'array': |
| englishComment = `${constName} - Data array`; |
| thaiComment = `${constName} - อาร์เรย์ข้อมูล`; |
| break; |
|
|
| default: |
| englishComment = `${constName} - Constant definition`; |
| thaiComment = `${constName} - นิยามค่าคงที่`; |
| } |
|
|
| return { |
| english: englishComment, |
| thai: thaiComment |
| }; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| function processFile(filePath, options = {}) { |
| const startTime = Date.now(); |
|
|
| try { |
| |
| logger.debug('FILE_PROCESSING', `Starting to process file: ${filePath}`, { |
| options: options, |
| startTime: new Date().toISOString() |
| }); |
|
|
| |
| const stat = fs.statSync(filePath); |
| const fileSizeInMB = stat.size / (1024 * 1024); |
| const maxSizeInMB = 10; |
|
|
| if (fileSizeInMB > maxSizeInMB) { |
| if (options.verbose) { |
| console.warn(` Skipping large file: ${filePath} (${fileSizeInMB.toFixed(2)} MB > ${maxSizeInMB} MB limit)`); |
| } |
| return { |
| success: true, |
| changes: false, |
| skipped: true, |
| reason: `File too large (${fileSizeInMB.toFixed(2)} MB)`, |
| statistics: { |
| totalStructures: 0, |
| detectedStructures: 0, |
| missedStructures: 0, |
| skippedStructures: 0, |
| inappropriateContexts: 0, |
| lineNumberCorrections: 0, |
| commentsAdded: 0, |
| errorDetails: [] |
| } |
| }; |
| } |
|
|
| |
| if (stat.isSymbolicLink()) { |
| if (options.verbose) { |
| console.warn(` Skipping symbolic link: ${filePath} (security protection)`); |
| } |
| return { |
| success: true, |
| changes: false, |
| skipped: true, |
| reason: 'Symbolic link protection', |
| statistics: { |
| totalStructures: 0, |
| detectedStructures: 0, |
| missedStructures: 0, |
| skippedStructures: 0, |
| inappropriateContexts: 0, |
| lineNumberCorrections: 0, |
| commentsAdded: 0, |
| errorDetails: [] |
| } |
| }; |
| } |
|
|
| |
| const content = fs.readFileSync(filePath, 'utf8'); |
|
|
| |
| if (options.backup) { |
| logger.debug('BACKUP', `Creating backup for: ${filePath}`); |
| createBackup(filePath); |
| } |
|
|
| |
| let processedContent = content; |
| let fileStatistics = null; |
|
|
| |
| if (options.removeComments) { |
| |
| processedContent = removeComments(processedContent); |
| } else { |
| |
| |
| processedContent = fixComments(processedContent); |
|
|
| |
| if (options.addMissing || options.aiMode) { |
| const result = addMissingComments(processedContent, options); |
| processedContent = result.processedContent; |
| fileStatistics = result.statistics; |
| } |
| } |
|
|
| |
| if (!fileStatistics) { |
| fileStatistics = { |
| totalStructures: 0, |
| detectedStructures: 0, |
| missedStructures: 0, |
| skippedStructures: 0, |
| inappropriateContexts: 0, |
| lineNumberCorrections: 0, |
| commentsAdded: 0, |
| errorDetails: [] |
| }; |
| } |
|
|
| |
| if (options.organizeZones) { |
| processedContent = organizeCodeByZones(processedContent); |
| } |
|
|
| |
| let comparisonReport = null; |
| if (content !== processedContent) { |
| comparisonReport = fileComparisonAnalyzer.compareAndAnalyze( |
| content, |
| processedContent, |
| filePath |
| ); |
|
|
| |
| if (comparisonReport && fileStatistics) { |
| fileStatistics.comparisonAnalysis = { |
| structuresSkipped: comparisonReport.changes.structuresSkipped, |
| skippedFunctions: comparisonReport.skippedStructures.functions, |
| skippedClasses: comparisonReport.skippedStructures.classes, |
| totalOriginalStructures: comparisonReport.original.totalStructures, |
| totalModifiedStructures: comparisonReport.modified.totalStructures |
| }; |
| } |
| } |
|
|
| |
| if (!options.dryRun) { |
| fs.writeFileSync(filePath, processedContent, 'utf8'); |
| logger.audit('FILE_MODIFIED', filePath, { |
| originalSize: content.length, |
| newSize: processedContent.length, |
| commentsAdded: fileStatistics?.commentsAdded || 0, |
| comparisonReport: comparisonReport ? { |
| structuresSkipped: comparisonReport.changes.structuresSkipped, |
| functionsSkipped: comparisonReport.skippedStructures.functions.length, |
| classesSkipped: comparisonReport.skippedStructures.classes.length |
| } : null |
| }); |
| } |
|
|
| |
| const duration = Date.now() - startTime; |
| logger.performance('FILE_PROCESSING', duration, { |
| filePath: filePath, |
| originalSize: content.length, |
| newSize: processedContent.length, |
| changes: content !== processedContent, |
| statistics: fileStatistics |
| }); |
|
|
| |
| logger.debug('FILE_PROCESSING', `Successfully processed: ${filePath}`, { |
| changes: content !== processedContent, |
| duration: duration, |
| statistics: fileStatistics |
| }); |
|
|
| return { |
| success: true, |
| originalSize: content.length, |
| newSize: processedContent.length, |
| changes: content !== processedContent, |
| preview: options.dryRun ? processedContent : null, |
| statistics: fileStatistics |
| }; |
|
|
| } catch (error) { |
| const duration = Date.now() - startTime; |
|
|
| |
| logger.error('FILE_PROCESSING', `Failed to process file: ${filePath}`, error, { |
| duration: duration, |
| options: options |
| }); |
|
|
| console.error(`Error processing file ${filePath}: ${error.message}`); |
|
|
| return { |
| success: false, |
| error: error.message, |
| statistics: { |
| totalStructures: 0, |
| detectedStructures: 0, |
| missedStructures: 0, |
| skippedStructures: 0, |
| inappropriateContexts: 0, |
| lineNumberCorrections: 0, |
| commentsAdded: 0, |
| errorDetails: [{ |
| file: filePath, |
| type: 'file_processing_error', |
| message: error.message, |
| stack: error.stack, |
| timestamp: new Date().toISOString() |
| }] |
| } |
| }; |
| } |
| } |
|
|
| |
| |
| |
| function removeComments(content) { |
| try { |
| let result = content; |
|
|
| |
| const protectedStrings = []; |
| let stringIndex = 0; |
|
|
| |
| result = result.replace(/(["'`])(?:(?!\1)[^\\]|\\.)*\1|\/(?:[^\/\\]|\\.)+\/[gimuy]*/g, (match) => { |
| const placeholder = `__PROTECTED_STRING_${stringIndex}__`; |
| protectedStrings[stringIndex] = match; |
| stringIndex++; |
| return placeholder; |
| }); |
|
|
| |
| result = result.replace(/(?<!:)\/\/(?!\/).*$/gm, ''); |
|
|
| |
| result = result.replace(/\/\*[\s\S]*?\*\//g, ''); |
|
|
| |
| for (let i = 0; i < protectedStrings.length; i++) { |
| result = result.replace(`__PROTECTED_STRING_${i}__`, protectedStrings[i]); |
| } |
|
|
| |
| result = result.replace(/^\s*[\r\n]/gm, ''); |
|
|
| |
| result = result.replace(/\n\s*\n\s*\n/g, '\n\n'); |
|
|
| |
| result = result.replace(/[ \t]+$/gm, ''); |
|
|
| return result; |
|
|
| } catch (error) { |
| console.warn('Warning: Error removing comments, returning original content'); |
| return content; |
| } |
| } |
|
|
| |
| |
| |
| function fixComments(content) { |
| |
| content = content.replace(/\/\*\*[\s\S]*?\*\//g, (match) => { |
| |
| const lines = match.split('\n'); |
| const convertedLines = []; |
|
|
| for (let line of lines) { |
| |
| line = line.replace(/^\s*\/\*\*\s*/, '').replace(/\s*\*\/\s*$/, '').replace(/^\s*\*\s*/, '').trim(); |
|
|
| if (line) { |
| convertedLines.push(`// ${line}`); |
| } |
| } |
|
|
| return convertedLines.join('\n'); |
| }); |
|
|
| return content; |
| } |
|
|
| |
| |
| |
| function addMissingComments(content, options = {}) { |
| try { |
| const statistics = { totalStructures: 0, detectedStructures: 0, missedStructures: 0, |
| skippedStructures: 0, inappropriateContexts: 0, lineNumberCorrections: 0, |
| commentsAdded: 0, errorDetails: [] }; |
|
|
| const tokenizer = new JavaScriptTokenizer(content, { maxDepth: 50, maxTokens: 100000, maxParsingTime: 15000 }); |
| const tokens = tokenizer.tokenize(); |
|
|
| |
| const parser = new StateMachineStructureParser(tokens, content); |
| const parsed = parser.parse(); |
| let structures = [ |
| ...parsed.functions, ...parsed.classes, ...parsed.methods, |
| ...parsed.arrowFunctions, ...parsed.reactComponents, |
| ...parsed.interfaces, ...parsed.typeAliases, ...parsed.enums |
| ]; |
|
|
| statistics.detectedStructures = structures.length; |
| if (options.verbose) console.log(` [StateMachine v5.2 FINAL] Detected ${structures.length} structures`); |
|
|
| if (structures.length === 0) return { processedContent: content, statistics }; |
|
|
| const allItems = [...structures]; |
| const missingElements = detectMissingElements(content, allItems); |
|
|
| |
| missingElements.forEach(el => { |
| if (el.type.includes('correction')) { |
| const item = allItems.find(i => i.name === el.name); |
| if (item) item.line = el.line; |
| statistics.lineNumberCorrections++; |
| } |
| }); |
|
|
| const generator = new CommentGenerator(); |
| const commentStatusMap = analyzeCommentStatus(content, structures); |
| let lines = normalizeLineEndings(content).split('\n'); |
| let processedLines = [...lines]; |
|
|
| |
| allItems.sort((a, b) => b.line - a.line); |
|
|
| allItems.forEach(item => { |
| let lineIndex = item.line - 1; |
| if (lineIndex < 0 || lineIndex >= processedLines.length) return; |
|
|
| |
| while (lineIndex > 0 && processedLines[lineIndex].includes('=====')) { |
| lineIndex--; |
| } |
|
|
| |
| if (commentStatusMap.get(item.name)?.hasComment) return; |
|
|
| const realStart = generator.findRealStartLine(processedLines, lineIndex); |
| if (realStart === -1) { |
| statistics.skippedStructures++; |
| return; |
| } |
|
|
| |
| const comment = generator.generateFunctionComment(item); |
| const indent = processedLines[realStart].match(/^\s*/)[0]; |
| const indentedComment = comment.split('\n').map(l => indent + l); |
|
|
| processedLines.splice(realStart, 0, ...indentedComment); |
| statistics.commentsAdded++; |
| }); |
|
|
| return { processedContent: processedLines.join('\n'), statistics }; |
|
|
| } catch (e) { |
| console.error('addMissingComments error:', e.message); |
| return { processedContent: content, statistics: { commentsAdded: 0, errorDetails: [e.message] } }; |
| } |
| } |
|
|
| |
| function findMethodsWithRegex(content) { |
| const methods = []; |
| const lines = content.split('\n'); |
|
|
| lines.forEach((line, index) => { |
| const trimmed = line.trim(); |
|
|
| |
| const patterns = [ |
| /^\s*(\w+)\s*\(\s*[^)]*\)\s*\{/, |
| /^\s*async\s+(\w+)\s*\(\s*[^)]*\)\s*\{/, |
| /^\s*static\s+(\w+)\s*\(\s*[^)]*\)\s*\{/, |
| /^\s*get\s+(\w+)\s*\(\s*[^)]*\)\s*\{/, |
| /^\s*set\s+(\w+)\s*\(\s*[^)]*\)\s*\{/, |
| ]; |
|
|
| for (const pattern of patterns) { |
| const match = trimmed.match(pattern); |
| if (match && match[1]) { |
| const methodName = match[1]; |
|
|
| |
| const excludedNames = [ |
| 'constructor', 'super', 'this', 'new', 'Map', 'Set', 'Array', 'Object', 'Promise', |
| 'console', 'Math', 'JSON', 'Date', 'String', 'Number', 'Boolean', 'Error', |
| 'require', 'module', 'exports', 'process', 'Buffer', 'setTimeout', 'setInterval', |
| 'function', 'return', 'if', 'else', 'for', 'while', 'switch', 'case', 'break', 'continue' |
| ]; |
|
|
| if (!excludedNames.includes(methodName) && methodName !== '*') { |
| |
| let inClass = false; |
| for (let i = Math.max(0, index - 20); i < index; i++) { |
| if (lines[i] && lines[i].includes('class ')) { |
| let braceCount = 0; |
| for (let j = i; j <= index; j++) { |
| const testLine = lines[j] || ''; |
| braceCount += (testLine.match(/\{/g) || []).length; |
| braceCount -= (testLine.match(/\}/g) || []).length; |
| } |
| if (braceCount > 0) { |
| inClass = true; |
| break; |
| } |
| } |
| } |
|
|
| if (inClass) { |
| |
| let type = 'class_method'; |
| if (trimmed.includes('async ')) type = 'async_class_method'; |
| else if (trimmed.includes('static ')) type = 'static_method'; |
| else if (trimmed.includes('get ')) type = 'getter'; |
| else if (trimmed.includes('set ')) type = 'setter'; |
|
|
| methods.push({ |
| type: type, |
| name: methodName, |
| line: index + 1, |
| column: trimmed.indexOf(methodName), |
| parameters: [], |
| isAsync: trimmed.includes('async '), |
| fromRegex: true |
| }); |
| } |
| } |
| } |
| } |
| }); |
|
|
| return methods; |
| } |
|
|
| |
| |
| |
| function createBackup(filePath) { |
| try { |
| |
| return backupManager.createBackup(filePath); |
| } catch (error) { |
| logger.error('BACKUP', `Failed to create backup for ${filePath}`, error); |
| console.error(`Error creating backup: ${error.message}`); |
| return null; |
| } |
| } |
|
|
| |
| |
| |
| function organizeCodeByZones(content) { |
| |
| const generator = new CommentGenerator(); |
|
|
| |
| let organized = content; |
|
|
| |
| if (!content.includes('╔═') && !content.includes('Zone')) { |
| const header = generator.generateZoneHeader( |
| 'Main Application Zone', |
| 'Core functionality and business logic' |
| ); |
| organized = header + '\n' + organized; |
| } |
|
|
| return organized; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
| function showHelp() { |
| console.log(` |
| ╔══════════════════════════════════════════════════════════════════════════════════╗ |
| ║ Universal Comment Fixer Tool v3.0.1-beta ║ |
| ║ Professional Comment Standardization for JS/TS Projects ║ |
| ║ FINAL BETA - No more beta updates after this ║ |
| ╚══════════════════════════════════════════════════════════════════════════════════╝ |
| |
| USAGE: |
| fix-comments [target] [options] |
| |
| TARGETS: |
| [file] Process single file |
| [directory] Process all files in directory |
| . Process current directory |
| |
| OPTIONS: |
| -d, --dry-run Preview changes without modifying files |
| -v, --verbose Show detailed processing information |
| -b, --backup Create backup before making changes |
| -r, --recursive Process all files in subdirectories |
| --add-missing Add comments to functions without comments |
| --ai-mode AI-friendly mode (add @function, @description) |
| --organize-zones Organize code into logical zones |
| --format Format code beautifully (Code Magician mode) |
| -s, --smart-learning Enable AI Intent Understanding Engine |
| --ext <list> Specify file extensions (comma-separated) |
| -h, --help Show this help message |
| --version Show version information |
| |
| EXAMPLES: |
| fix-comments app.js # Fix single file |
| fix-comments ./src --backup # Fix directory with backup |
| fix-comments . -r --add-missing # Recursive with missing comments |
| fix-comments ./src --ai-mode --verbose # AI-friendly with details |
| fix-comments . --dry-run # Preview changes only |
| fix-comments messy-code.js --format # Format messy code beautifully |
| fix-comments ./src --format -r # Format all files recursively |
| |
| SUPPORTED FILES: |
| .js, .ts, .jsx, .tsx - JavaScript/TypeScript files |
| |
| SECURITY FEATURES: |
| - Path Traversal Protection: Prevents ../../../etc/passwd attacks |
| - File Size Limits: 10MB maximum to prevent DoS attacks |
| - Parsing Timeout: 30-second maximum to prevent infinite loops |
| - Depth Limits: 100-level maximum nesting to prevent complexity attacks |
| - Extension Validation: Only valid file extensions accepted |
| - System Directory Blacklist: Blocks access to sensitive system paths |
| - Working Directory Enforcement: All operations within project scope |
| |
| SECURITY WARNINGS: |
| Run this tool only within your project directories |
| Avoid running in Home directory or system directories |
| Tool has built-in protection but exercise caution |
| Review changes before committing to version control |
| |
| FINAL BETA RELEASE NOTES: |
| This is the LAST BETA VERSION - Next release: v1.0.0 Production Ready (Q1 2026) |
| Current users can continue using this version normally |
| No more beta version updates will be released |
| |
| Known Issues (Final Beta): |
| • Complex TypeScript interface chains may need manual review |
| • Arrow functions with complex destructuring might generate warnings |
| • JSX components with conditional rendering require additional validation |
| |
| Final Beta Guidelines: |
| • Always use --backup flag for safety |
| • Try --dry-run first to preview changes |
| • Report issues with minimal reproducible code samples |
| • Check diagnostic.log for detailed analysis |
| |
| Issues & Documentation: https://github.com/chahuadev/chahuadev-fix-comments |
| `); |
| } |
|
|
| |
| |
| |
| function showVersion() { |
| console.log('Universal Comment Fixer Tool v3.0.1-beta'); |
| console.log('Professional Comment Standardization Tool for JavaScript/TypeScript Projects'); |
| console.log(' FINAL BETA RELEASE - No more beta updates after this version'); |
| console.log('Copyright (c) 2025 Chahua Development Co., Ltd.'); |
| console.log(''); |
| console.log(' Next Release: v1.0.0 Production Ready (Q1 2026)'); |
| console.log(' Current users can continue using this version normally'); |
| console.log(''); |
| console.log(' Known Issues in Final Beta:'); |
| console.log(' • Complex TypeScript interface chains may need manual review'); |
| console.log(' • Arrow functions with complex destructuring might generate warnings'); |
| console.log(' • JSX components with conditional rendering require additional validation'); |
| console.log(''); |
| console.log(' Report issues with code samples at:'); |
| console.log(' https://github.com/chahuadev/chahuadev-fix-comments/issues'); |
| } |
|
|
| |
| |
| |
| function processDirectory(dirPath, options = {}) { |
| const extensions = options.extensions || ['.js', '.ts', '.jsx', '.tsx']; |
| const results = { |
| total: 0, |
| processed: 0, |
| errors: 0, |
| files: [], |
| |
| statistics: { |
| totalStructures: 0, |
| detectedStructures: 0, |
| missedStructures: 0, |
| skippedStructures: 0, |
| inappropriateContexts: 0, |
| lineNumberCorrections: 0, |
| commentsAdded: 0, |
| errorDetails: [] |
| } |
| }; |
|
|
| try { |
| const files = fs.readdirSync(dirPath); |
|
|
| for (const file of files) { |
| const filePath = path.join(dirPath, file); |
|
|
| |
| const lstat = fs.lstatSync(filePath); |
| if (lstat.isSymbolicLink()) { |
| if (options.verbose) { |
| console.warn(` Skipping symbolic link: ${filePath} (circular reference protection)`); |
| } |
| continue; |
| } |
|
|
| const stat = fs.statSync(filePath); |
|
|
| if (stat.isDirectory() && options.recursive) { |
| |
| const secureSkipDirs = [ |
| 'node_modules', '.git', 'dist', 'build', '.chahuadev-fix-comments-backups', |
| '.npm', '.cache', '.vscode', '.idea', 'coverage', 'logs', 'tmp', 'temp', |
| 'System32', 'etc', 'proc', 'sys', 'Windows', 'Program Files', 'Users', |
| '.ssh', '.aws', '.config', '.docker' |
| ]; |
|
|
| if (secureSkipDirs.some(skipDir => file.toLowerCase().includes(skipDir.toLowerCase()))) { |
| if (options.verbose) { |
| console.warn(` Skipping protected directory: ${filePath}`); |
| } |
| continue; |
| } |
|
|
| |
| const subResults = processDirectory(filePath, options); |
| results.total += subResults.total; |
| results.processed += subResults.processed; |
| results.errors += subResults.errors; |
| results.files.push(...subResults.files); |
|
|
| |
| if (subResults.statistics) { |
| results.statistics.totalStructures += subResults.statistics.totalStructures; |
| results.statistics.detectedStructures += subResults.statistics.detectedStructures; |
| results.statistics.missedStructures += subResults.statistics.missedStructures; |
| results.statistics.skippedStructures += subResults.statistics.skippedStructures; |
| results.statistics.inappropriateContexts += subResults.statistics.inappropriateContexts; |
| results.statistics.lineNumberCorrections += subResults.statistics.lineNumberCorrections; |
| results.statistics.commentsAdded += subResults.statistics.commentsAdded; |
| results.statistics.errorDetails.push(...subResults.statistics.errorDetails); |
| } |
|
|
| } else if (stat.isFile()) { |
| const ext = path.extname(file); |
| if (extensions.includes(ext)) { |
| results.total++; |
|
|
| if (options.verbose) { |
| console.log(`Processing: ${filePath}`); |
| } |
|
|
| const result = processFile(filePath, options); |
| if (result.success) { |
| results.processed++; |
| if (result.changes || options.dryRun) { |
| results.files.push({ |
| path: filePath, |
| changes: result.changes, |
| originalSize: result.originalSize, |
| newSize: result.newSize |
| }); |
| } |
|
|
| |
| if (result.statistics) { |
| results.statistics.totalStructures += result.statistics.totalStructures; |
| results.statistics.detectedStructures += result.statistics.detectedStructures; |
| results.statistics.missedStructures += result.statistics.missedStructures; |
| results.statistics.skippedStructures += result.statistics.skippedStructures; |
| results.statistics.inappropriateContexts += result.statistics.inappropriateContexts; |
| results.statistics.lineNumberCorrections += result.statistics.lineNumberCorrections; |
| results.statistics.commentsAdded += result.statistics.commentsAdded; |
| if (result.statistics.errorDetails) { |
| results.statistics.errorDetails.push(...result.statistics.errorDetails); |
| } |
| } |
| } else { |
| results.errors++; |
| console.error(`Error processing ${filePath}: ${result.error}`); |
|
|
| |
| results.statistics.errorDetails.push({ |
| file: filePath, |
| type: 'processing_error', |
| message: result.error, |
| timestamp: new Date().toISOString() |
| }); |
| } |
| } |
| } |
| } |
|
|
| } catch (error) { |
| console.error(`Error reading directory ${dirPath}: ${error.message}`); |
| results.errors++; |
| } |
|
|
| return results; |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| |
|
|
| |
| |
| |
| |
|
|
| |
| |
| function formatCode(content, options = {}) { |
| try { |
| |
| |
| |
| console.log('Starting Code Health Diagnosis...'); |
|
|
| const healthCheck = performCodeHealthCheck(content); |
|
|
| if (!healthCheck.isHealthy) { |
| console.error('CODE HEALTH CHECK FAILED:'); |
| console.error(` Syntax Error detected at line ${healthCheck.errorLine}, column ${healthCheck.errorColumn}`); |
| console.error(` Error: ${healthCheck.errorMessage}`); |
| console.error(' OPERATION ABORTED: Cannot format unhealthy code.'); |
| console.error(' Please fix syntax errors first, then try again.'); |
|
|
| |
| return content + `\n\n// WARNING: FORMATTING ABORTED: Syntax Error at line ${healthCheck.errorLine}\n// ${healthCheck.errorMessage}`; |
| } |
|
|
| console.log('Code Health Check PASSED - Patient is ready for surgery'); |
| console.log(` Analysis: ${healthCheck.stats.functions} functions, ${healthCheck.stats.classes} classes, ${healthCheck.stats.complexity} complexity`); |
|
|
| |
| |
| |
| if (options.enableSmartLearning) { |
| console.log('\n Phase 1.5: AI Intent Understanding Engine...'); |
| const smartAnalysis = analyzeFileWithSmartLearning(content, { security: { maxDepth: 50, maxTokens: 100000, maxParsingTime: 15000 } }); |
|
|
| if (smartAnalysis && smartAnalysis.intentAnalysis) { |
| console.log(' AI Intent Analysis completed successfully'); |
| console.log('\n Intent Analysis Results:'); |
| console.log(JSON.stringify(smartAnalysis.intentAnalysis.summary, null, 2)); |
|
|
| |
| if (smartAnalysis.intentAnalysis.smartComments.length > 0) { |
| console.log('\n Generated Smart Comments:'); |
| smartAnalysis.intentAnalysis.smartComments.forEach(([item, comment]) => { |
| console.log(` ${item}: ${comment.thai}`); |
| }); |
| } |
|
|
| |
| options.intentAnalysis = smartAnalysis.intentAnalysis; |
| } else { |
| console.log(' AI Intent Analysis failed or returned no results'); |
| } |
| } |
|
|
| |
| |
| |
| console.log('Starting Safe Surgical Formatting...'); |
|
|
| const indentSize = options.indentSize || 4; |
|
|
| |
| let formatted = content; |
|
|
| |
| formatted = formatted.replace(/}([a-zA-Z_][a-zA-Z0-9_]*\s*\()/g, '}\n\n$1'); |
| formatted = formatted.replace(/}(async\s+[a-zA-Z_][a-zA-Z0-9_]*\s*\()/g, '}\n\n$1'); |
| formatted = formatted.replace(/}(class\s+[a-zA-Z_][a-zA-Z0-9_]*)/g, '}\n\n$1'); |
| formatted = formatted.replace(/}(function\s+[a-zA-Z_][a-zA-Z0-9_]*\s*\()/g, '}\n\n$1'); |
|
|
| |
| formatted = formatted.replace(/const\s*{\s*([^}]+)\s*}\s*=/g, (match, vars) => { |
| const cleanVars = vars.replace(/\s*,\s*/g, ', ').trim(); |
| return `const { ${cleanVars} } =`; |
| }); |
|
|
| |
| formatted = addComprehensiveSpacing(formatted); |
|
|
| |
| formatted = formatted.replace(/;(?![^{]*})(?![^()]*\))/g, ';\n'); |
|
|
| |
| formatted = formatted.replace(/{(?![^}]*}[^{]*$)/g, '{\n'); |
| formatted = formatted.replace(/([^{;\n\s])}/g, '$1\n}'); |
|
|
| |
| const lines = formatted.split('\n'); |
| const result = []; |
| let indentLevel = 0; |
| let insideFunction = false; |
|
|
| for (let i = 0; i < lines.length; i++) { |
| let line = lines[i].trim(); |
|
|
| if (!line) continue; |
|
|
| |
| if (line.includes('function ') || line.includes('class ') || line.startsWith('async ')) { |
| insideFunction = true; |
| } |
| if (line === '}' || line.startsWith('}')) { |
| indentLevel = Math.max(0, indentLevel - 1); |
| insideFunction = false; |
| } |
|
|
| |
| const indentation = ' '.repeat(indentLevel * indentSize); |
| result.push(indentation + line); |
|
|
| |
| if (line.endsWith('{')) { |
| indentLevel++; |
| } |
| } |
|
|
| return smartCleanup(result.join('\n')); |
|
|
| } catch (error) { |
| console.error('Enhanced Format error:', error.message); |
| return content; |
| } |
| } |
|
|
| |
| |
| |
|
|
| |
| |
| function performCodeHealthCheck(content) { |
| const healthReport = { |
| isHealthy: false, |
| errorLine: null, |
| errorColumn: null, |
| errorMessage: '', |
| stats: { |
| functions: 0, |
| classes: 0, |
| complexity: 'simple', |
| tokens: 0 |
| } |
| }; |
|
|
| try { |
| |
| console.log(' Phase 1: Tokenization Analysis...'); |
| const tokenizer = new JavaScriptTokenizer(content); |
| const tokens = tokenizer.tokenize(); |
|
|
| if (!tokens || tokens.length === 0) { |
| healthReport.errorMessage = 'Failed to tokenize content - possible syntax error'; |
| healthReport.errorLine = 1; |
| healthReport.errorColumn = 1; |
| return healthReport; |
| } |
|
|
| healthReport.stats.tokens = tokens.length; |
|
|
| |
| console.log(' Phase 2: Structure Analysis...'); |
| const analyzer = new StructureAnalyzer(tokens, content); |
| const structures = analyzer.analyzeAll(); |
|
|
| |
| console.log(' Phase 3: Syntax Validation...'); |
| const syntaxCheck = validateSyntax(content, tokens); |
|
|
| if (!syntaxCheck.isValid) { |
| healthReport.errorLine = syntaxCheck.errorLine; |
| healthReport.errorColumn = syntaxCheck.errorColumn; |
| healthReport.errorMessage = syntaxCheck.errorMessage; |
| return healthReport; |
| } |
|
|
| |
| healthReport.stats.functions = countFunctions(structures); |
| healthReport.stats.classes = countClasses(structures); |
| healthReport.stats.complexity = assessComplexity(structures); |
|
|
| |
| healthReport.isHealthy = true; |
| console.log(' All health checks PASSED'); |
|
|
| return healthReport; |
|
|
| } catch (error) { |
| console.error(' Health check failed:', error.message); |
|
|
| |
| const lineMatch = error.message.match(/line (\d+)/i); |
| const colMatch = error.message.match(/column (\d+)/i); |
|
|
| healthReport.errorLine = lineMatch ? parseInt(lineMatch[1]) : 1; |
| healthReport.errorColumn = colMatch ? parseInt(colMatch[1]) : 1; |
| healthReport.errorMessage = error.message; |
|
|
| return healthReport; |
| } |
| } |
|
|
| |
| function validateSyntax(content, tokens) { |
| const result = { |
| isValid: true, |
| errorLine: null, |
| errorColumn: null, |
| errorMessage: '' |
| }; |
|
|
| try { |
| |
| const stack = []; |
| const pairs = { |
| '{': '}', |
| '[': ']', |
| '(': ')' |
| }; |
|
|
| for (const token of tokens) { |
| if (token.type === 'BRACE_OPEN' || token.type === 'BRACKET_OPEN' || token.type === 'PAREN_OPEN') { |
| stack.push({ token: token.value, line: token.line, column: token.column }); |
| } else if (token.type === 'BRACE_CLOSE' || token.type === 'BRACKET_CLOSE' || token.type === 'PAREN_CLOSE') { |
| if (stack.length === 0) { |
| result.isValid = false; |
| result.errorLine = token.line; |
| result.errorColumn = token.column; |
| result.errorMessage = `Unexpected closing '${token.value}'`; |
| return result; |
| } |
|
|
| const lastOpen = stack.pop(); |
| const expectedClose = pairs[lastOpen.token]; |
|
|
| if (expectedClose !== token.value) { |
| result.isValid = false; |
| result.errorLine = token.line; |
| result.errorColumn = token.column; |
| result.errorMessage = `Mismatched: expected '${expectedClose}' but found '${token.value}'`; |
| return result; |
| } |
| } |
| } |
|
|
| |
| if (stack.length > 0) { |
| const unclosed = stack[stack.length - 1]; |
| result.isValid = false; |
| result.errorLine = unclosed.line; |
| result.errorColumn = unclosed.column; |
| result.errorMessage = `Unclosed '${unclosed.token}'`; |
| return result; |
| } |
|
|
| return result; |
|
|
| } catch (error) { |
| result.isValid = false; |
| result.errorMessage = `Syntax validation failed: ${error.message}`; |
| return result; |
| } |
| } |
|
|
| |
| function countFunctions(structures) { |
| let count = 0; |
| if (structures && structures.functions) { |
| count = structures.functions.length || 0; |
| } |
| return count; |
| } |
|
|
| |
| function countClasses(structures) { |
| let count = 0; |
| if (structures && structures.classes) { |
| count = structures.classes.length || 0; |
| } |
| return count; |
| } |
|
|
| |
| function assessComplexity(structures) { |
| const functionCount = countFunctions(structures); |
| const classCount = countClasses(structures); |
| const totalElements = functionCount + classCount; |
|
|
| if (totalElements <= 5) return 'simple'; |
| if (totalElements <= 15) return 'moderate'; |
| return 'complex'; |
| } |
| function addComprehensiveSpacing(content) { |
| let formatted = content; |
|
|
| |
| formatted = formatted.replace(/=\s*>/g, ' => '); |
| formatted = formatted.replace(/\s+=>\s+/g, ' => '); |
|
|
| |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])=([^=])/g, '$1 = $2'); |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])==([^=])/g, '$1 == $2'); |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])===([^=])/g, '$1 === $2'); |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])!=([^=])/g, '$1 != $2'); |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])!==([^=])/g, '$1 !== $2'); |
|
|
| |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])<([a-zA-Z0-9_(\[])/g, '$1 < $2'); |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])>([a-zA-Z0-9_(\[])/g, '$1 > $2'); |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])<=([a-zA-Z0-9_(\[])/g, '$1 <= $2'); |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])>=([a-zA-Z0-9_(\[])/g, '$1 >= $2'); |
|
|
| |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])&&([a-zA-Z0-9_(\[])/g, '$1 && $2'); |
| formatted = formatted.replace(/([a-zA-Z0-9_)\]])\|\|([a-zA-Z0-9_(\[])/g, '$1 || $2'); |
|
|
| |
| formatted = formatted.replace(/(\w)\+(\w)/g, '$1 + $2'); |
| formatted = formatted.replace(/(\w)-(\w)/g, '$1 - $2'); |
| formatted = formatted.replace(/(\w)\*(\w)/g, '$1 * $2'); |
| formatted = formatted.replace(/(\w)\/(\w)/g, '$1 / $2'); |
|
|
| |
| formatted = formatted.replace(/\b(if|for|while|switch|catch)\(/g, '$1 ('); |
| formatted = formatted.replace(/\b(function|class|async function)\s+/g, '$1 '); |
|
|
| |
| formatted = formatted.replace(/([^{\s]){/g, '$1 {'); |
|
|
| |
| formatted = formatted.replace(/,(?!\s)/g, ', '); |
|
|
| |
| formatted = formatted.replace(/(\w):(?!\s)/g, '$1: '); |
|
|
| return formatted; |
| } |
|
|
| |
| function smartCleanup(content) { |
| let cleaned = content; |
|
|
| |
| cleaned = cleaned.replace(/\n{4,}/g, '\n\n\n'); |
|
|
| |
| cleaned = cleaned.replace(/(\n.+\n)(\s*)(class |function |async function )/g, '$1\n$2$3'); |
|
|
| |
| cleaned = cleaned.replace(/(\n\s*})\n(\s*)(class |function |const |let |var )/g, '$1\n\n$2$3'); |
|
|
| |
| cleaned = cleaned.replace(/[ \t]+$/gm, ''); |
|
|
| |
| const lines = cleaned.split('\n'); |
| const indentSize = 4; |
| let indentLevel = 0; |
| const fixedLines = []; |
|
|
| for (const line of lines) { |
| const trimmed = line.trim(); |
|
|
| if (!trimmed) { |
| fixedLines.push(''); |
| continue; |
| } |
|
|
| |
| if (trimmed === '}' || trimmed.startsWith('}')) { |
| indentLevel = Math.max(0, indentLevel - 1); |
| } |
|
|
| |
| fixedLines.push(' '.repeat(indentLevel * indentSize) + trimmed); |
|
|
| |
| if (trimmed.endsWith('{')) { |
| indentLevel++; |
| } |
| } |
|
|
| return fixedLines.join('\n').trim(); |
| } |
|
|
|
|
|
|
| |
| function formatFile(filePath, options = {}) { |
| try { |
| |
| const ext = path.extname(filePath).toLowerCase(); |
| if (!options.extensions.includes(ext)) { |
| return { success: false, error: `Unsupported file extension: ${ext}` }; |
| } |
|
|
| |
| const content = fs.readFileSync(filePath, 'utf8'); |
|
|
| |
| const formattedContent = formatCode(content, options); |
|
|
| if (options.dryRun) { |
| return { |
| success: true, |
| changes: formattedContent !== content, |
| preview: formattedContent |
| }; |
| } else { |
| |
| if (options.backup && formattedContent !== content) { |
| createBackup(filePath); |
| } |
|
|
| |
| if (formattedContent !== content) { |
| fs.writeFileSync(filePath, formattedContent, 'utf8'); |
| return { success: true, changes: true }; |
| } else { |
| return { success: true, changes: false }; |
| } |
| } |
| } catch (error) { |
| return { success: false, error: error.message }; |
| } |
| } |
| function main() { |
| const sessionStartTime = Date.now(); |
| const args = process.argv.slice(2); |
|
|
| |
| logger.info('SESSION', 'Code Magician session started', { |
| args: args, |
| cwd: process.cwd(), |
| nodeVersion: process.version, |
| platform: process.platform |
| }); |
|
|
| |
| if (args.length === 0 || args.includes('-h') || args.includes('--help')) { |
| showHelp(); |
| return; |
| } |
|
|
| if (args.includes('--version')) { |
| showVersion(); |
| return; |
| } |
|
|
| |
| const options = { |
| dryRun: args.includes('-d') || args.includes('--dry-run'), |
| verbose: args.includes('-v') || args.includes('--verbose'), |
| backup: args.includes('-b') || args.includes('--backup'), |
| recursive: args.includes('-r') || args.includes('--recursive'), |
| addMissing: args.includes('--add-missing'), |
| aiMode: args.includes('--ai-mode'), |
| organizeZones: args.includes('--organize-zones'), |
| formatOnly: args.includes('--format'), |
| removeComments: args.includes('--remove-comments') || args.includes('--remove'), |
| enableSmartLearning: args.includes('--smart-learning') || args.includes('-s'), |
| extensions: ['.js', '.ts', '.jsx', '.tsx'] |
| }; |
|
|
| |
| const extIndex = args.findIndex(arg => arg === '--ext'); |
| if (extIndex !== -1 && args[extIndex + 1]) { |
| |
| const inputExtensions = args[extIndex + 1] |
| .split(',') |
| .map(ext => ext.trim().toLowerCase()) |
| |
| .filter(ext => { |
| |
| if (!/^\.[a-z0-9]+$/.test(ext)) { |
| console.warn(`Warning: Invalid extension format '${ext}' ignored. Extensions must start with '.' followed by alphanumeric characters.`); |
| return false; |
| } |
| |
| if (ext.length > 10) { |
| console.warn(`Warning: Extension '${ext}' too long (max 10 chars), ignored.`); |
| return false; |
| } |
| return true; |
| }); |
|
|
| if (inputExtensions.length > 0) { |
| options.extensions = inputExtensions; |
| } else { |
| console.warn(`Warning: No valid extensions provided, using defaults: ${options.extensions.join(', ')}`); |
| } |
| } |
|
|
| |
| let target = '.'; |
| for (let i = 0; i < args.length; i++) { |
| if (!args[i].startsWith('-') && |
| (extIndex === -1 || i !== extIndex + 1)) { |
| target = args[i]; |
| break; |
| } |
| } |
|
|
| |
| const workingDir = process.cwd(); |
| const resolvedTarget = path.resolve(target); |
|
|
| |
| if (!resolvedTarget.startsWith(workingDir)) { |
| console.error(` Security Error: Target path '${target}' is outside of the current project directory.`); |
| console.error(` Working directory: ${workingDir}`); |
| console.error(` Resolved target: ${resolvedTarget}`); |
| console.error(` Access denied for security reasons.`); |
| process.exit(1); |
| } |
|
|
| |
| const forbiddenPaths = [ |
| 'node_modules', '.git', '.npm', '.cache', 'System32', 'etc', 'proc', 'sys', |
| 'Windows', 'Program Files', 'Users', '.ssh', '.aws', '.config' |
| ]; |
|
|
| const targetLower = target.toLowerCase(); |
| for (const forbidden of forbiddenPaths) { |
| if (targetLower.includes(forbidden.toLowerCase())) { |
| console.error(` Security Error: Access to '${forbidden}' directories is prohibited for safety.`); |
| process.exit(1); |
| } |
| } |
|
|
| |
| if (!fs.existsSync(target)) { |
| console.error(`Error: Target '${target}' does not exist.`); |
| process.exit(1); |
| } |
|
|
| |
| console.log(` Starting Code Magician v3.0.1-beta...`); |
| console.log(` Beta Release - Help us improve with your feedback!`); |
| console.log(`Target: ${target}`); |
| console.log(`Mode: ${options.dryRun ? 'DRY RUN' : 'LIVE'}`); |
| if (options.formatOnly) { |
| console.log(`Operation: CODE FORMATTING ONLY`); |
| } |
|
|
| const startTime = Date.now(); |
| const stat = fs.statSync(target); |
| let results; |
|
|
| if (stat.isFile()) { |
| |
| console.log(`Processing single file...`); |
|
|
| if (options.formatOnly) { |
| |
| const result = formatFile(target, options); |
| if (result.success) { |
| console.log(` File formatted successfully`); |
| if (options.dryRun && result.preview) { |
| console.log('\n--- FORMATTED PREVIEW ---'); |
| console.log(result.preview); |
| console.log('--- END PREVIEW ---\n'); |
| } |
| } else { |
| console.error(` Error formatting file: ${result.error}`); |
| } |
| results = { processed: 1, modified: result.success ? 1 : 0, errors: result.success ? 0 : 1 }; |
| } else { |
| |
| const result = processFile(target, options); |
| if (result.success) { |
| if (result.changes) { |
| console.log(` File processed successfully`); |
| if (options.dryRun && result.preview) { |
| console.log('\n--- PREVIEW ---'); |
| console.log(result.preview); |
| console.log('--- END PREVIEW ---\n'); |
| } |
| } else { |
| console.log(`- No changes needed`); |
| } |
| } else { |
| console.error(` Error processing file: ${result.error}`); |
| } |
| results = { processed: 1, modified: result.changes ? 1 : 0, errors: result.success ? 0 : 1 }; |
| } |
|
|
| } else if (stat.isDirectory()) { |
| |
| console.log(`Processing directory${options.recursive ? ' (recursive)' : ''}...`); |
| results = processDirectory(target, options); |
|
|
| |
| const endTime = Date.now(); |
| const duration = ((endTime - startTime) / 1000).toFixed(2); |
|
|
| console.log(`\n═══════════════════════════════════════════════════════════════════════════════════`); |
| console.log(`SUMMARY:`); |
| console.log(` Files found: ${results.total}`); |
| console.log(` Files processed: ${results.processed}`); |
| console.log(` Files with changes: ${results.files.length}`); |
| console.log(` Errors: ${results.errors}`); |
| console.log(` Duration: ${duration}s`); |
|
|
| |
| if (results.statistics) { |
| console.log(`\nSTRUCTURE ANALYSIS STATISTICS:`); |
| console.log(` Total structures found: ${results.statistics.totalStructures}`); |
| console.log(` Successfully detected: ${results.statistics.detectedStructures}`); |
| console.log(` Missed by parser: ${results.statistics.missedStructures}`); |
| console.log(` Comments added: ${results.statistics.commentsAdded}`); |
| console.log(` Skipped (inappropriate context): ${results.statistics.inappropriateContexts}`); |
| console.log(` Line number corrections: ${results.statistics.lineNumberCorrections}`); |
|
|
| |
| const detectionRate = results.statistics.totalStructures > 0 |
| ? ((results.statistics.detectedStructures / results.statistics.totalStructures) * 100).toFixed(1) |
| : '0.0'; |
| console.log(` Detection success rate: ${detectionRate}%`); |
|
|
| |
| if (results.statistics.errorDetails && results.statistics.errorDetails.length > 0) { |
| console.log(`\nERROR DETAILS:`); |
| const errorSummary = {}; |
| results.statistics.errorDetails.forEach(error => { |
| const key = error.type || 'unknown_error'; |
| errorSummary[key] = (errorSummary[key] || 0) + 1; |
| }); |
|
|
| Object.entries(errorSummary).forEach(([type, count]) => { |
| console.log(` ${type}: ${count} occurrences`); |
| }); |
|
|
| |
| if (options.verbose && results.statistics.errorDetails.length <= 10) { |
| console.log(`\nDETAILED ERRORS:`); |
| results.statistics.errorDetails.forEach(error => { |
| console.log(` • ${error.file || 'unknown'}: ${error.message || error.type}`); |
| }); |
| } else if (results.statistics.errorDetails.length > 10) { |
| console.log(` (Use --verbose to see detailed error list)`); |
| } |
| } |
|
|
| |
| const totalIssues = results.statistics.missedStructures + |
| results.statistics.inappropriateContexts + |
| results.errors; |
|
|
| if (totalIssues > 0) { |
| console.log(`\n BETA TESTING INSIGHTS:`); |
| console.log(` Total learning opportunities: ${totalIssues}`); |
| if (results.statistics.missedStructures > 0) { |
| console.log(` TypeScript patterns to enhance: ${results.statistics.missedStructures}`); |
| console.log(` Help us: Share your .ts files with interfaces/abstract classes!`); |
| } |
| if (results.statistics.inappropriateContexts > 0) { |
| console.log(` Context analysis improvements needed: ${results.statistics.inappropriateContexts}`); |
| console.log(` Help us: Submit JSX/arrow function examples that caused issues!`); |
| } |
| if (results.errors > 0) { |
| console.log(` Processing challenges: ${results.errors}`); |
| console.log(` Help us: Report these with --verbose logs to our GitHub!`); |
| } |
| } else { |
| console.log(`\n EXCELLENT! All structures processed successfully!`); |
| console.log(` Your code is a perfect example for our pattern recognition!`); |
| console.log(` Consider sharing your project structure to help other developers!`); |
| } |
| } |
|
|
| if (options.dryRun && results.files.length > 0) { |
| console.log(`\nFiles that would be modified:`); |
| results.files.forEach(file => { |
| console.log(` - ${file.path}`); |
| }); |
| } |
|
|
| console.log(`═══════════════════════════════════════════════════════════════════════════════════`); |
| } |
|
|
| console.log(`\n Universal Code Magician Beta completed ${options.dryRun ? '(DRY RUN)' : 'successfully'}!`); |
| console.log(` Your feedback helps us build better tools for the developer community!`); |
|
|
|
|
| |
| const sessionDuration = Date.now() - sessionStartTime; |
| logger.performance('SESSION_COMPLETE', sessionDuration, { |
| mode: options.dryRun ? 'DRY_RUN' : 'LIVE', |
| totalFiles: results?.total || 0, |
| processedFiles: results?.processed || 0, |
| modifiedFiles: results?.files?.length || 0, |
| errors: results?.errors || 0, |
| options: options |
| }); |
|
|
| |
| backupManager.cleanupOldBackups(7); |
|
|
| logger.info('SESSION', 'Code Magician session completed successfully', { |
| duration: sessionDuration, |
| summary: results |
| }); |
| } |
|
|
| |
| |
| |
| function detectMissingElements(content, detectedItems) { |
| const missing = []; |
| |
| const normalizedContent = content.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); |
| const lines = normalizedContent.split('\n'); |
| const detectedNames = new Set(detectedItems.map(item => item.name)); |
|
|
| |
| const classRegex = /^\s*class\s+(\w+)/gm; |
| let match; |
| while ((match = classRegex.exec(normalizedContent)) !== null) { |
| const className = match[1]; |
| const lineNumber = normalizedContent.substring(0, match.index).split('\n').length; |
|
|
| |
| if (!detectedNames.has(className)) { |
| missing.push({ |
| type: 'class', |
| name: className, |
| line: lineNumber, |
| reason: 'Class not detected by parsing system' |
| }); |
| } else { |
| |
| const existingItem = detectedItems.find(item => item.name === className); |
| if (existingItem && existingItem.line === 1 && lineNumber > 1) { |
| missing.push({ |
| type: 'class_correction', |
| name: className, |
| line: lineNumber, |
| reason: 'Line number correction needed' |
| }); |
| } |
| } |
| } |
|
|
| |
| const functionRegex = /^\s*(?:export\s+)?(?:async\s+)?function\s+(\w+)/gm; |
| let funcMatch; |
| while ((funcMatch = functionRegex.exec(normalizedContent)) !== null) { |
| const funcName = funcMatch[1]; |
| const lineNumber = normalizedContent.substring(0, funcMatch.index).split('\n').length; |
|
|
| if (!detectedNames.has(funcName)) { |
| missing.push({ |
| type: 'function', |
| name: funcName, |
| line: lineNumber, |
| reason: 'Function not detected by parsing system' |
| }); |
| } |
| } |
|
|
| |
| const arrowFuncRegex = /^\s*(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s*)?\([^)]*\)\s*=>/gm; |
| let arrowMatch; |
| while ((arrowMatch = arrowFuncRegex.exec(normalizedContent)) !== null) { |
| const funcName = arrowMatch[1]; |
| const lineNumber = normalizedContent.substring(0, arrowMatch.index).split('\n').length; |
|
|
| if (!detectedNames.has(funcName)) { |
| missing.push({ |
| type: 'arrow_function', |
| name: funcName, |
| line: lineNumber, |
| reason: 'Arrow function not detected by parsing system' |
| }); |
| } |
| } |
|
|
| |
| const methodRegex = /^\s+(?:async\s+)?(\w+)\s*\([^)]*\)\s*{/gm; |
| let methodMatch; |
| while ((methodMatch = methodRegex.exec(normalizedContent)) !== null) { |
| const methodName = methodMatch[1]; |
| const lineNumber = normalizedContent.substring(0, methodMatch.index).split('\n').length; |
|
|
| |
| if (methodName !== 'constructor' && |
| !['if', 'for', 'while', 'switch', 'catch', 'function'].includes(methodName) && |
| !detectedNames.has(methodName)) { |
| |
| } |
| } |
|
|
| return missing; |
| } |
|
|
| |
| |
| |
| |
| class StateMachineStructureParser { |
| constructor(tokens, originalContent) { |
| this.tokens = tokens.filter(t => t.type !== TOKEN_TYPES.WHITESPACE && t.type !== TOKEN_TYPES.NEWLINE); |
| this.content = originalContent; |
| this.cursor = 0; |
| this.structures = { |
| classes: [], functions: [], methods: [], arrowFunctions: [], |
| reactComponents: [], interfaces: [], typeAliases: [], enums: [] |
| }; |
| } |
|
|
| parse() { |
| while (this.cursor < this.tokens.length) { |
| this.tryParseDeclaration(); |
| this.cursor++; |
| } |
| return this.structures; |
| } |
|
|
| tryParseDeclaration() { |
| const token = this.tokens[this.cursor]; |
| if (!token || token.type !== TOKEN_TYPES.IDENTIFIER) return; |
|
|
| const name = token.value; |
| const prev = this.tokens[this.cursor - 1]; |
| const next = this.tokens[this.cursor + 1]; |
| const prev2 = this.tokens[this.cursor - 2]; |
|
|
| |
| if (prev && ['class', 'interface', 'enum', 'type', 'abstract'].includes(prev.value)) { |
| const target = prev.value === 'class' ? 'classes' : |
| prev.value === 'interface' ? 'interfaces' : |
| prev.value === 'enum' ? 'enums' : 'typeAliases'; |
| this.structures[target].push({ |
| type: prev.value + '_declaration', |
| name, |
| line: token.line |
| }); |
| return; |
| } |
|
|
| |
| if (prev && prev.value === 'function' && next && next.value === '(') { |
| this.structures.functions.push({ type: 'function_declaration', name, line: token.line }); |
| return; |
| } |
|
|
| |
| |
| |
| const hasDotBefore = prev && prev.value === '.'; |
| const hasNewBefore = prev && prev.value === 'new'; |
| if (next && next.value === '(' && this.isInsideClassBody() && !hasDotBefore && !hasNewBefore) { |
| if (name !== 'constructor') { |
| this.structures.methods.push({ type: 'class_method', name, line: token.line }); |
| } |
| return; |
| } |
|
|
| |
| if (prev && prev.value === '=' && ['const','let','var'].includes(prev2?.value || '')) { |
| const isReact = /^[A-Z]/.test(name) && this.looksLikeReactFC(token.line); |
| const target = isReact ? 'reactComponents' : 'arrowFunctions'; |
| this.structures[target].push({ |
| type: isReact ? 'react_component' : 'arrow_function', |
| name, |
| line: token.line |
| }); |
| } |
| } |
|
|
| isInsideClassBody() { |
| let braceCount = 0; |
| for (let i = this.cursor - 1; i >= 0; i--) { |
| if (this.tokens[i].value === '{') braceCount++; |
| if (this.tokens[i].value === '}') braceCount--; |
| if (this.tokens[i].value === 'class' && braceCount > 0) return true; |
| if (braceCount < 0) return false; |
| } |
| return false; |
| } |
|
|
| looksLikeReactFC(startLine) { |
| for (let i = this.cursor; i < Math.min(this.cursor + 80, this.tokens.length); i++) { |
| if (this.tokens[i].line > startLine + 50) break; |
| if (this.tokens[i].value === '=>' || |
| (this.tokens[i].value === 'return' && this.tokens[i+1]?.value === '<')) { |
| return true; |
| } |
| } |
| return false; |
| } |
| } |
|
|
| |
| if (require.main === module) { |
| main(); |
| } |
|
|
| |
| module.exports = { |
| |
| processFile, |
| processDirectory, |
| fixComments, |
| addMissingComments, |
| removeComments, |
|
|
| |
| JavaScriptTokenizer, |
| FunctionPatternMatcher, |
| StructureParser, |
| SmartFileAnalyzer, |
| StructureAnalyzer, |
| EnhancedPatternDetector, |
| CommentGenerator, |
| StateMachineStructureParser, |
|
|
| |
| TokenizerSecurityManager, |
| ProfessionalLogger, |
| OrganizedBackupManager, |
| FileComparisonAnalyzer, |
|
|
| |
| formatCode, |
| formatFile, |
| performCodeHealthCheck, |
| validateSyntax, |
| addComprehensiveSpacing, |
| smartCleanup, |
|
|
| |
| analyzeFileWithSmartLearning, |
| generateSmartComment, |
| generateSmartClassComment, |
| generateSmartFunctionComment, |
|
|
| |
| generateInterfaceComment, |
| generateTypeAliasComment, |
| generateEnumComment, |
| generateAbstractClassComment, |
| generateConstComment, |
|
|
| |
| createBackup, |
| organizeCodeByZones, |
|
|
| |
| showHelp, |
| showVersion, |
| main, |
|
|
| |
| detectMissingElements, |
| findMethodsWithRegex, |
| countFunctions, |
| countClasses, |
| assessComplexity, |
|
|
| |
| calculateLineNumber, |
| normalizeLineEndings, |
| analyzeCommentStatus, |
| }; |