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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | 17x 17x 33x 35x 33x 51x 67x 36x 67x 46x 61x 52x 46x 63x 63x 63x 28x 63x 45x 45x 34x 18x 51x 17x 18x 18x 51x 52x 69x 33x 51x 18x 34x 18x 17x 52x 52x 52x 52x 52x 52x 51x 51x 51x 51x 51x 52x 52x 52x 52x 52x 51x 51x 51x 51x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 900x 52x 699x 40x 40x 114x 114x 221x 221x 221x 3715x 3x 37x 37x 37x 42x 42x 42x 700x 36x 263x 263x 263x 263x 263x 214x | import type {
DependencyGraph,
CoUsageData,
DomainAssignment,
DomainSignals,
ExportInfo,
} from './types';
/**
* Build co-usage matrix: track which files are imported together
*/
export function buildCoUsageMatrix(
graph: DependencyGraph
): Map<string, Map<string, number>> {
const coUsageMatrix = new Map<string, Map<string, number>>();
for (const [, node] of graph.nodes) {
const imports = node.imports;
for (let i = 0; i < imports.length; i++) {
const fileA = imports[i];
if (!coUsageMatrix.has(fileA)) coUsageMatrix.set(fileA, new Map());
for (let j = i + 1; j < imports.length; j++) {
const fileB = imports[j];
const fileAUsage = coUsageMatrix.get(fileA)!;
fileAUsage.set(fileB, (fileAUsage.get(fileB) || 0) + 1);
Iif (!coUsageMatrix.has(fileB)) coUsageMatrix.set(fileB, new Map());
const fileBUsage = coUsageMatrix.get(fileB)!;
fileBUsage.set(fileA, (fileBUsage.get(fileA) || 0) + 1);
}
}
}
return coUsageMatrix;
}
/**
* Extract type dependencies from AST exports
*/
export function buildTypeGraph(
graph: DependencyGraph
): Map<string, Set<string>> {
const typeGraph = new Map<string, Set<string>>();
for (const [file, node] of graph.nodes) {
for (const exp of node.exports) {
Eif (exp.typeReferences) {
for (const typeRef of exp.typeReferences) {
if (!typeGraph.has(typeRef)) typeGraph.set(typeRef, new Set());
I typeGraph.get(typeRef)!.add(file);
}
}
}
}
return typeGraph;
}
/**
* Find semantic clusters using co-usage patterns
*/
export function findSemanticClusters(
coUsageMatrix: Map<string, Map<string, number>>,
minCoUsage: number = 3
): Map<string, string[]> {
const clusters = new Map<string, string[]>();
const visited = new Set<string>();
for (const [file, coUsages] of coUsageMatrix) {
if (visited.has(file)) continue;
const cluster: string[] = [file];
visited.add(file);
for (const [relatedFile, count] of coUsages) {
if (count >= minCoUsage && !visited.has(relatedFile)) {
cluster.push(relatedFile);
visited.add(relatedFile);
}
}
if (cluster.length > 1) clusters.set(file, cluster);
}
return clusters;
}
/**
* Infer domain from semantic analysis (co-usage + types)
*/
export function inferDomainFromSemantics(
file: string,
exportName: string,
graph: DependencyGraph,
coUsageMatrix: Map<string, Map<string, number>>,
typeGraph: Map<string, Set<string>>,
exportTypeRefs?: string[]
): DomainAssignment[] {
const domainSignals = new Map<string, DomainSignals>();
const coUsages = coUsageMatrix.get(file) || new Map();
const strongCoUsages = Array.from(coUsages.entries())
.filter(([, count]) => count >= 3)
.map(([coFile]) => coFile);
for (const coFile of strongCoUsages) {
const coNode = graph.nodes.get(coFile);
if (coNode) {
for (const exp of coNode.exports) {
if (exp.inferredDomain && exp.inferredDomain !== 'unknown') {
const domain = exp.inferredDomain;
if (!domainSignals.has(domain)) {
domainSignals.set(domain, {
coUsage: false,
typeReference: false,
exportName: false,
importPath: false,
folderStructure: false,
});
}
domainSignals.get(domain)!.coUsage = true;
}
}
}
}
Eif (exportTypeRefs) {
for (const typeRef of exportTypeRefs) {
const filesWithType = typeGraph.get(typeRef);
if (filesWithType) {
for (const typeFile of filesWithType) {
if (typeFile === file) continue;
const typeNode = graph.nodes.get(typeFile);
if (typeNode) {
for (const exp of typeNode.exports) {
if (exp.inferredDomain && exp.inferredDomain !== 'unknown') {
const domain = exp.inferredDomain;
if (!domainSignals.has(domain)) {
domainSignals.set(domain, {
coUsage: false,
typeReference: false,
exportName: false,
importPath: false,
folderStructure: false,
});
}
domainSignals.get(domain)!.typeReference = true;
}
}
}
}
}
}
}
const assignments: DomainAssignment[] = [];
for (const [domain, signals] of domainSignals) {
const confidence = calculateDomainConfidence(signals);
if (confidence >= 0.3) assignments.push({ domain, confidence, signals });
}
assignments.sort((a, b) => b.confidence - a.confidence);
return assignments;
}
export function calculateDomainConfidence(signals: DomainSignals): number {
Iconst weights = {
coUsage: 0.35,
typeReference: 0.3,
exportName: 0.15,
importPath: 0.1,
folderStructure: 0.1,
};
let confidence = 0;
if (signals.coUsage) confidence += weights.coUsage;
if (signals.typeReference) confidence += weights.typeReference;
if (signals.exportName) confidence += weights.exportName;
if (signals.importPath) confidence += weights.importPath;
if (signals.folderStructure) confidence += weights.folderStructure;
return confidence;
}
/**
* Regex-based export extraction (legacy/fallback)
*/
export function extractExports(
content: string,
filePath?: string,
domainOptions?: { domainKeywords?: string[] },
fileImports?: string[]
): ExportInfo[] {
const exports: ExportInfo[] = [];
const patterns = [
/export\s+function\s+(\w+)/g,
/export\s+class\s+(\w+)/g,
/export\s+const\s+(\w+)/g,
/export\s+type\s+(\w+)/g,
/export\s+interface\s+(\w+)/g,
/export\s+default/g,
];
const types: ExportInfo['type'][] = [
'function',
'class',
'const',
'type',
'interface',
'default',
];
patterns.forEach((pattern, index) => {
let match;
while ((match = pattern.exec(content)) !== null) {
const name = match[1] || 'default';
const type = types[index];
const inferredDomain = inferDomain(
name,
filePath,
domainOptions,
fileImports
);
exports.push({ name, type, inferredDomain });
}
});
return exports;
}
/**
* Infer domain from name, path, or imports
*/
export function inferDomain(
name: string,
filePath?: string,
domainOptions?: { domainKeywords?: string[] },
fileImports?: string[]
): string {
const lower = name.toLowerCase();
const tokens = Array.from(
new Set(
lower
.replace(/([a-z0-9])([A-Z])/g, '$1 $2')
.replace(/[^a-z0-9]+/gi, ' ')
.split(' ')
.filter(Boolean)
)
);
const defaultKeywords = [
'authentication',
'authorization',
'payment',
'invoice',
'customer',
'product',
'order',
'cart',
'user',
'admin',
'repository',
'controller',
'service',
'config',
'model',
'view',
'auth',
];
const domainKeywords = domainOptions?.domainKeywords?.length
? [...domainOptions.domainKeywords, ...defaultKeywords]
: defaultKeywords;
for (const keyword of domainKeywords) {
Iif (tokens.includes(keyword)) return keyword;
}
for (const keyword of domainKeywords) {
if (lower.includes(keyword)) return keyword;
}
Eif (fileImports) {
for (const importPath of fileImports) {
const segments = importPath.split('/');
for (const segment of segments) {
const segLower = segment.toLowerCase();
const singularSegment = singularize(segLower);
for (const keyword of domainKeywords) {
if (
singularSegment === keyword ||
segLower === keyword ||
segLower.includes(keyword)
)
return keyword;
}
}
}
}
Eif (filePath) {
const segments = filePath.split('/');
for (const segment of segments) {
const segLower = segment.toLowerCase();
const singularSegment = singularize(segLower);
for (const keyword of domainKeywords) {
if (singularSegment === keyword || segLower === keyword) return keyword;
}
}
}
return 'unknown';
}
function singularize(word: string): string {
const irregulars: Record<string, string> = {
people: 'person',
children: 'child',
men: 'man',
women: 'woman',
};
Iif (irregulars[word]) return irregulars[word];
Iif (word.endsWith('ies')) return word.slice(0, -3) + 'y';
Iif (word.endsWith('ses')) return word.slice(0, -2);
if (word.endsWith('s') && word.length > 3) return word.slice(0, -1);
return word;
}
export function getCoUsageData(
file: string,
coUsageMatrix: Map<string, Map<string, number>>
): CoUsageData {
return {
file,
coImportedWith: coUsageMatrix.get(file) || new Map(),
sharedImporters: [],
};
}
export function findConsolidationCandidates(
graph: DependencyGraph,
coUsageMatrix: Map<string, Map<string, number>>,
typeGraph: Map<string, Set<string>>,
minCoUsage: number = 5,
minSharedTypes: number = 2
) {
const candidates: any[] = [];
for (const [fileA, coUsages] of coUsageMatrix) {
const nodeA = graph.nodes.get(fileA);
if (!nodeA) continue;
for (const [fileB, count] of coUsages) {
if (fileB <= fileA || count < minCoUsage) continue;
const nodeB = graph.nodes.get(fileB);
if (!nodeB) continue;
const typesA = new Set(
nodeA.exports.flatMap((e) => e.typeReferences || [])
);
const typesB = new Set(
nodeB.exports.flatMap((e) => e.typeReferences || [])
);
const sharedTypes = Array.from(typesA).filter((t) => typesB.has(t));
if (sharedTypes.length >= minSharedTypes || count >= minCoUsage * 2) {
candidates.push({
files: [fileA, fileB],
reason: `High co-usage (${count}x)`,
strength: count / 10,
});
}
}
}
return candidates.sort((a, b) => b.strength - a.strength);
}
|