UNPKG

agentsqripts

Version:

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

86 lines (75 loc) 2.39 kB
/** * @file Logical block extraction * @description Extracts logical blocks from code for WET analysis */ /** * Extract logical blocks from code content for similarity comparison * @param {string} content - Code content to analyze * @param {Object} options - Extraction options * @returns {Array} Array of logical blocks */ function extractLogicalBlocks(content, options = {}) { const { minLines = 3, ignoreComments = true } = options; const blocks = []; const lines = content.split('\n'); let currentBlock = []; let inFunction = false; let braceCount = 0; let functionStartLine = 0; lines.forEach((line, index) => { const trimmed = line.trim(); // Skip empty lines and comments if requested if (!trimmed || (ignoreComments && (trimmed.startsWith('//') || trimmed.startsWith('/*')))) { return; } // Track function boundaries if (trimmed.includes('function') || trimmed.match(/^\s*\w+\s*=\s*\(/)) { if (currentBlock.length >= minLines) { blocks.push({ type: 'code_block', lines: [...currentBlock], startLine: functionStartLine + 1, endLine: index, content: currentBlock.join('\n') }); } currentBlock = []; inFunction = true; functionStartLine = index; braceCount = 0; } // Track braces to determine block boundaries const openBraces = (line.match(/\{/g) || []).length; const closeBraces = (line.match(/\}/g) || []).length; braceCount += openBraces - closeBraces; currentBlock.push(line); // End of function/block if (inFunction && braceCount <= 0 && trimmed.includes('}')) { if (currentBlock.length >= minLines) { blocks.push({ type: 'function_block', lines: [...currentBlock], startLine: functionStartLine + 1, endLine: index + 1, content: currentBlock.join('\n') }); } currentBlock = []; inFunction = false; } }); // Handle remaining block if (currentBlock.length >= minLines) { blocks.push({ type: 'trailing_block', lines: [...currentBlock], startLine: lines.length - currentBlock.length + 1, endLine: lines.length, content: currentBlock.join('\n') }); } return blocks; } module.exports = { extractLogicalBlocks };