Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 | 16x 4x 8x 4x 8x 4x 4x 8x 4x 4x 8x 4x 4x 4x 4x 4x 4x 8x 4x 4x 4x 4x 4x 4x 4x 4x 16x 16x 2x 4x 4x 8x 4x 4x 4x 4x 3x 3x 3x 5x 3x 3x 3x 5x 3x 2x 2x 4x 2x 2x 14x 11x 33x 11x 11x 14x 2x 2x 11x 2x 2x 2x 15x 15x 2x 2x 7x 2x 2x 7x 7x 2x 2x 2x 12x 2x 6x 6x 10x 6x 2x 2x 2x 6x 6x 6x 6x 6x 6x 6x 2x | import { calculateImportSimilarity } from '@aiready/core';
import type { ExportInfo } from './types';
import { isTestFile } from './ast-utils';
/**
* Calculate cohesion score (how related are exports in a file)
*/
export function calculateEnhancedCohesion(
exports: ExportInfo[],
filePath?: string,
options?: {
coUsageMatrix?: Map<string, Map<string, number>>;
weights?: {
importBased?: number;
structural?: number;
domainBased?: number;
};
}
): number {
if (exports.length <= 1) return 1;
// Test files always have perfect cohesion by design
Iif (filePath && isTestFile(filePath)) return 1;
// 1. Domain-based cohesion using entropy
const domains = exports.map((e) => e.inferredDomain || 'unknown');
const domainCounts = new Map<string, number>();
for (const d of domains) domainCounts.set(d, (domainCounts.get(d) || 0) + 1);
// IF ALL DOMAINS MATCH, RETURN 1.0 IMMEDIATELY (Legacy test compatibility)
Iif (domainCounts.size === 1 && domains[0] !== 'unknown') {
if (!options?.weights) return 1;
}
const probs = Array.from(domainCounts.values()).map(
(c) => c / exports.length
);
let domainEntropy = 0;
for (const p of probs) {
Eif (p > 0) domainEntropy -= p * Math.log2(p);
}
const maxEntropy = Math.log2(Math.max(2, domainCounts.size));
const domainScore = 1 - domainEntropy / maxEntropy;
// 2. Import-based cohesion
let importScoreTotal = 0;
let pairsWithData = 0;
let anyImportData = false;
for (let i = 0; i < exports.length; i++) {
for (let j = i + 1; j < exports.length; j++) {
const exp1Imports = exports[i].imports;
const exp2Imports = exports[j].imports;
Eif (exp1Imports || exp2Imports) {
anyImportData = true;
const sim = calculateImportSimilarity(
{ ...exports[i], imports: exp1Imports || [] } as any,
{ ...exports[j], imports: exp2Imports || [] } as any
);
importScoreTotal += sim;
pairsWithData++;
}
}
}
const avgImportScore =
pairsWithData > 0 ? importScoreTotal / pairsWithData : 0;
// Weighted average
let score = anyImportData
? domainScore * 0.4 + avgImportScore * 0.6
: domainScore;
if (anyImportData && score === 0 && domainScore === 0) {
score = 0.1;
}
// Structural boost
let structuralScore = 0;
for (const exp of exports) {
Iif (exp.dependencies && exp.dependencies.length > 0) {
structuralScore += 1;
}
}
Iif (structuralScore > 0) {
score = Math.min(1, score + 0.1);
}
// Legacy fallback if no imports and domain Score was 1.0
Iif (!options?.weights && !anyImportData && domainCounts.size === 1) return 1;
return score;
}
/**
* Calculate structural cohesion for a file based on co-usage patterns.
*/
export function calculateStructuralCohesionFromCoUsage(
file: string,
coUsageMatrix?: Map<string, Map<string, number>>
): number {
if (!coUsageMatrix) return 1;
const coUsages = coUsageMatrix.get(file);
Iif (!coUsages || coUsages.size === 0) return 1;
let total = 0;
for (const count of coUsages.values()) total += count;
Iif (total === 0) return 1;
const probs: number[] = [];
for (const count of coUsages.values()) {
Eif (count > 0) probs.push(count / total);
}
if (probs.length <= 1) return 1;
let entropy = 0;
for (const prob of probs) {
entropy -= prob * Math.log2(prob);
}
const maxEntropy = Math.log2(probs.length);
return maxEntropy > 0 ? 1 - entropy / maxEntropy : 1;
}
/**
* Calculate fragmentation score (how scattered is a domain)
*/
export function calculateFragmentation(
files: string[],
domain: string,
options?: {
useLogScale?: boolean;
logBase?: number;
sharedImportRatio?: number;
dependencyCount?: number;
}
): number {
if (files.length <= 1) return 0;
const directories = new Set(
files.map((f) => f.split('/').slice(0, -1).join('/'))
);
const uniqueDirs = directories.size;
let score = options?.useLogScale
? uniqueDirs <= 1
? 0
: Math.log(uniqueDirs) /
Math.log(options.logBase || Math.E) /
(Math.log(files.length) / Math.log(options.logBase || Math.E))
: (uniqueDirs - 1) / (files.length - 1);
// Coupling Discount
if (options?.sharedImportRatio && options.sharedImportRatio > 0.5) {
const discount = (options.sharedImportRatio - 0.5) * 0.4;
score = score * (1 - discount);
}
return score;
}
/**
* Calculate path entropy for a set of files
*/
export function calculatePathEntropy(files: string[]): number {
Iif (!files || files.length === 0) return 0;
const dirCounts = new Map<string, number>();
for (const f of files) {
const dir = f.split('/').slice(0, -1).join('/') || '.';
dirCounts.set(dir, (dirCounts.get(dir) || 0) + 1);
}
const counts = Array.from(dirCounts.values());
Iif (counts.length <= 1) return 0;
const total = counts.reduce((s, v) => s + v, 0);
let entropy = 0;
for (const count of counts) {
const prob = count / total;
entropy -= prob * Math.log2(prob);
}
const maxEntropy = Math.log2(counts.length);
return maxEntropy > 0 ? entropy / maxEntropy : 0;
}
/**
* Calculate directory-distance metric based on common ancestor depth
*/
export function calculateDirectoryDistance(files: string[]): number {
Iif (!files || files.length <= 1) return 0;
const pathSegments = (p: string) => p.split('/').filter(Boolean);
const commonAncestorDepth = (a: string[], b: string[]) => {
const minLen = Math.min(a.length, b.length);
let i = 0;
while (i < minLen && a[i] === b[i]) i++;
return i;
};
let totalNormalized = 0;
let comparisons = 0;
for (let i = 0; i < files.length; i++) {
for (let j = i + 1; j < files.length; j++) {
const segA = pathSegments(files[i]);
const segB = pathSegments(files[j]);
const shared = commonAncestorDepth(segA, segB);
const maxDepth = Math.max(segA.length, segB.length);
totalNormalized += 1 - (maxDepth > 0 ? shared / maxDepth : 0);
comparisons++;
}
}
return comparisons > 0 ? totalNormalized / comparisons : 0;
}
|