UNPKG

agentsqripts

Version:

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

42 lines (37 loc) 2.04 kB
/** * @file Dependency-based similarity calculator using set intersection analysis * @description Single responsibility: Assess code block similarity through shared dependency patterns * * This calculator evaluates similarity between code blocks by analyzing their dependency * patterns, including imports, requires, and external function calls. Blocks with similar * dependencies often indicate related functionality and shared architectural patterns, * making dependency analysis a crucial dimension for comprehensive similarity assessment. * * Design rationale: * - Dependency analysis reveals functional relationships beyond surface-level code patterns * - Jaccard similarity coefficient provides standard measurement for set-based similarity * - Set-based operations enable efficient comparison of dependency collections * - Conservative scoring for asymmetric cases (one block has dependencies, other doesn't) * - Perfect similarity for blocks without dependencies (both focusing on pure logic) * * Dependency similarity methodology: * - Set intersection identifies shared dependencies indicating functional overlap * - Set union provides total dependency scope for normalization * - Jaccard coefficient (intersection/union) yields standard similarity measurement * - Perfect similarity (1.0) when both blocks have no dependencies (pure implementations) * - Conservative similarity (0.5) when one block has dependencies and other doesn't */ /** * Calculate dependency similarity */ function calculateDependencySimilarity(block1, block2) { const deps1 = new Set(block1.dependencies || []); const deps2 = new Set(block2.dependencies || []); if (deps1.size === 0 && deps2.size === 0) return 1.0; if (deps1.size === 0 || deps2.size === 0) return 0.5; // Calculate Jaccard similarity const intersection = new Set([...deps1].filter(x => deps2.has(x))); const union = new Set([...deps1, ...deps2]); return intersection.size / union.size; } module.exports = calculateDependencySimilarity;