UNPKG

agentsqripts

Version:

Comprehensive static code analysis toolkit for identifying technical debt, security vulnerabilities, performance issues, and code quality problems

77 lines (64 loc) 2.42 kB
/** * @file Fallback regex-based extraction for when AST parsing fails * @description Single responsibility: Extract code blocks using regex when AST parsing fails */ const normalizeForComparison = require('./normalizeForComparison'); const generateHash = require('./generateHash'); const calculateComplexity = require('./calculateComplexity'); /** * Fallback regex-based extraction for when AST parsing fails */ function extractFallbackBlocks(content, filePath) { const blocks = []; const lines = content.split('\n'); // Function patterns const functionPatterns = [ /^(?:export\s+)?(?:async\s+)?function\s+(\w+)\s*\([^)]*\)\s*{/, /^(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s+)?function\s*\([^)]*\)\s*{/, /^(?:export\s+)?(?:const|let|var)\s+(\w+)\s*=\s*(?:async\s+)?\([^)]*\)\s*=>\s*{/, /^(\w+)\s*:\s*(?:async\s+)?function\s*\([^)]*\)\s*{/ ]; for (let i = 0; i < lines.length; i++) { const line = lines[i].trim(); for (const pattern of functionPatterns) { if (pattern.test(line)) { const match = line.match(pattern); const name = match[1] || 'anonymous'; // Find the end of the function let braceCount = 1; let endLine = i; for (let j = i + 1; j < lines.length && braceCount > 0; j++) { const currentLine = lines[j]; braceCount += (currentLine.match(/{/g) || []).length; braceCount -= (currentLine.match(/}/g) || []).length; if (braceCount === 0) { endLine = j; break; } } if (endLine > i) { const blockContent = lines.slice(i, endLine + 1).join('\n'); const normalizedContent = normalizeForComparison(blockContent, 'function'); blocks.push({ type: 'function', name, file: filePath, startLine: i + 1, endLine: endLine + 1, content: blockContent, normalizedContent, hash: generateHash(normalizedContent), complexity: calculateComplexity({}, blockContent), lineCount: endLine - i + 1, astNode: 'FallbackExtraction', parameters: [], dependencies: [] }); } break; } } } return blocks; } module.exports = extractFallbackBlocks;