File size: 14,151 Bytes
fd8cdf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
import * as path from 'node:path';
import * as posix from 'node:path/posix';
/**
 * Generates a summary string from a file's parse result and metadata.
 *
 * Priority:
 * 1. Exports list (max 5, then "...")
 * 2. Classes list
 * 3. Functions list (if no exports)
 * 4. Fallback: "{lineCount} lines of {language}"
 */
function generateSummary(parseResult, file) {
    if (!parseResult) {
        return `${file.lineCount} lines of ${file.language}`;
    }
    if (parseResult.exports.length > 0) {
        const maxExports = 5;
        const shown = parseResult.exports.slice(0, maxExports);
        const suffix = parseResult.exports.length > maxExports ? ', ...' : '';
        return `Exports: ${shown.join(', ')}${suffix}`;
    }
    if (parseResult.classes.length > 0) {
        const classNames = parseResult.classes.map(c => c.name);
        return `Classes: ${classNames.join(', ')}`;
    }
    if (parseResult.functions.length > 0) {
        const funcNames = parseResult.functions.map(f => f.name);
        return `Functions: ${funcNames.join(', ')}`;
    }
    return `${file.lineCount} lines of ${file.language}`;
}
/**
 * Assigns tags to a file node based on language, category, path patterns, and complexity.
 */
function assignTags(file) {
    const tags = [];
    // Language tag
    tags.push(file.language);
    // Category tag
    tags.push(file.category);
    // Path-based tags
    const relativeLower = file.relativePath.toLowerCase();
    if (/(?:^|\/)(test|spec|__test__|__tests__|__spec__)(?:\/|$)/.test(relativeLower) ||
        /\.(test|spec)\.[^/]+$/.test(relativeLower)) {
        tags.push('test');
    }
    if (/(?:^|\/)components?(?:\/|$)/.test(relativeLower)) {
        tags.push('component');
    }
    if (/(?:^\/|\/)(utils?|lib|helpers?)(?:\/|$)/.test(relativeLower) ||
        /^(utils?|lib|helpers?)(?:\/|$)/.test(relativeLower)) {
        tags.push('util');
    }
    if (/(?:^|\/)(?:api|routes?|controllers?)(?:\/|$)/.test(relativeLower)) {
        tags.push('api');
    }
    if (/(?:^|\/)(?:models?|entities)(?:\/|$)/.test(relativeLower)) {
        tags.push('model');
    }
    // Complexity tag based on line count
    if (file.lineCount < 50) {
        tags.push('simple');
    }
    else if (file.lineCount <= 200) {
        tags.push('moderate');
    }
    else {
        tags.push('complex');
    }
    return tags;
}
/**
 * Builds file-level graph nodes from scanned files and their parse results.
 *
 * For each file, creates a DashboardNode with:
 * - id: the file's relative path
 * - type: "file"
 * - name: the file's basename
 * - summary: generated from parse result (exports, classes, functions, or fallback)
 * - tags: language, category, path-based patterns, and complexity
 *
 * @param files - Scanned file entries with metadata
 * @param parseResults - Map of file relative path to its parse result
 * @returns Array of DashboardNode for each file
 */
export function buildFileNodes(files, parseResults) {
    return files.map(file => {
        const parseResult = parseResults.get(file.relativePath);
        return {
            id: file.relativePath,
            type: 'file',
            name: path.basename(file.relativePath),
            summary: generateSummary(parseResult, file),
            tags: assignTags(file),
        };
    });
}
/**
 * Builds function-level graph nodes for significant functions.
 *
 * A function is "significant" if it is exported OR has 10+ lines (endLine - startLine + 1 >= 10).
 *
 * For each significant function, creates a DashboardNode with:
 * - id: "{relativePath}::{functionName}"
 * - type: "function"
 * - name: the function name
 * - summary: "({params}) → {lineCount} lines"
 * - tags: language, "exported" if exported, "async" if name suggests async
 *
 * Also creates a `contains` edge from the file node to the function node.
 *
 * @param files - Scanned file entries with metadata
 * @param parseResults - Map of file relative path to its parse result
 * @returns Object with nodes and edges arrays
 */
export function buildFunctionNodes(files, parseResults) {
    const nodes = [];
    const edges = [];
    for (const file of files) {
        const parseResult = parseResults.get(file.relativePath);
        if (!parseResult)
            continue;
        for (const func of parseResult.functions) {
            const lineCount = func.endLine - func.startLine + 1;
            const isSignificant = func.isExported || lineCount >= 10;
            if (!isSignificant)
                continue;
            const nodeId = `${file.relativePath}::${func.name}`;
            const tags = [file.language];
            if (func.isExported)
                tags.push('exported');
            if (func.name.startsWith('async') || func.name.includes('Async') || func.name.includes('async')) {
                tags.push('async');
            }
            nodes.push({
                id: nodeId,
                type: 'function',
                name: func.name,
                summary: `(${func.params.join(', ')}) → ${lineCount} lines`,
                tags,
            });
            edges.push({
                source: file.relativePath,
                target: nodeId,
                type: 'contains',
            });
        }
    }
    return { nodes, edges };
}
/**
 * Builds class-level graph nodes for all classes found in parse results.
 *
 * For each class, creates a DashboardNode with:
 * - id: "{relativePath}::{className}"
 * - type: "class"
 * - name: the class name
 * - summary: "Methods: {methods}" or "Empty class" if no methods
 * - tags: language, "exported" if exported, size tag (small/medium/large-class)
 *
 * Also creates a `contains` edge from the file node to the class node.
 *
 * @param files - Scanned file entries with metadata
 * @param parseResults - Map of file relative path to its parse result
 * @returns Object with nodes and edges arrays
 */
export function buildClassNodes(files, parseResults) {
    const nodes = [];
    const edges = [];
    for (const file of files) {
        const parseResult = parseResults.get(file.relativePath);
        if (!parseResult)
            continue;
        for (const cls of parseResult.classes) {
            const nodeId = `${file.relativePath}::${cls.name}`;
            const tags = [file.language];
            if (cls.isExported)
                tags.push('exported');
            // Size tag based on method count
            const methodCount = cls.methods.length;
            if (methodCount <= 3) {
                tags.push('small-class');
            }
            else if (methodCount <= 10) {
                tags.push('medium-class');
            }
            else {
                tags.push('large-class');
            }
            const summary = cls.methods.length > 0
                ? `Methods: ${cls.methods.join(', ')}`
                : 'Empty class';
            nodes.push({
                id: nodeId,
                type: 'class',
                name: cls.name,
                summary,
                tags,
            });
            edges.push({
                source: file.relativePath,
                target: nodeId,
                type: 'contains',
            });
        }
    }
    return { nodes, edges };
}
/**
 * Common file extensions to try when resolving import paths.
 */
const RESOLVE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.mjs', '.py', '.go', '.rs', '.java'];
/**
 * Index file names to try for directory imports.
 */
const INDEX_FILES = ['index.ts', 'index.js'];
/**
 * Resolves a relative import path to a known file's relativePath.
 *
 * Tries the following resolution strategies:
 * 1. Exact path (if it already has an extension and matches)
 * 2. Path + each common extension
 * 3. Path as directory + index files
 *
 * @param importSource - The raw import path (e.g., './utils' or '../lib/helper')
 * @param importingFilePath - The relativePath of the file containing the import
 * @param knownFiles - Set of all known file relative paths
 * @returns The resolved relativePath or undefined if not found
 */
function resolveImportPath(importSource, importingFilePath, knownFiles) {
    // Get the directory of the importing file
    const importingDir = posix.dirname(importingFilePath);
    // Resolve the relative import path against the importing file's directory
    const resolved = posix.normalize(posix.join(importingDir, importSource));
    // Try exact match first (path already has extension)
    if (knownFiles.has(resolved)) {
        return resolved;
    }
    // Try appending common extensions
    for (const ext of RESOLVE_EXTENSIONS) {
        const withExt = resolved + ext;
        if (knownFiles.has(withExt)) {
            return withExt;
        }
    }
    // Try as directory import (index files)
    for (const indexFile of INDEX_FILES) {
        const asIndex = posix.join(resolved, indexFile);
        if (knownFiles.has(asIndex)) {
            return asIndex;
        }
    }
    return undefined;
}
/**
 * Builds import edges from resolved relative import paths.
 *
 * For each file's imports:
 * - Only processes imports with relative paths (starting with '.' or '..')
 * - Resolves the import path relative to the importing file's directory
 * - If the resolved path matches a known file, creates an 'imports' edge
 * - Skips external package imports and unresolvable imports
 *
 * @param files - Scanned file entries with metadata
 * @param parseResults - Map of file relative path to its parse result
 * @returns Array of DashboardEdge with type 'imports'
 */
export function buildImportEdges(files, parseResults) {
    const edges = [];
    const knownFiles = new Set(files.map(f => f.relativePath));
    for (const file of files) {
        const parseResult = parseResults.get(file.relativePath);
        if (!parseResult)
            continue;
        for (const imp of parseResult.imports) {
            // Skip external package imports (not starting with . or ..)
            if (!imp.source.startsWith('.'))
                continue;
            const resolvedPath = resolveImportPath(imp.source, file.relativePath, knownFiles);
            if (resolvedPath) {
                edges.push({
                    source: file.relativePath,
                    target: resolvedPath,
                    type: 'imports',
                });
            }
        }
    }
    return edges;
}
/**
 * Builds call edges using a simple heuristic based on named imports.
 *
 * For each file's imports that resolve to a known file:
 * - If the import has named imports (e.g., `import { formatDate } from './utils'`)
 * - And the target file has a significant function node with that name
 * - Creates a 'calls' edge from the importing file to the function node
 *
 * This is a best-effort heuristic — it won't catch all calls but provides useful edges.
 *
 * @param files - Scanned file entries with metadata
 * @param parseResults - Map of file relative path to its parse result
 * @returns Array of DashboardEdge with type 'calls'
 */
export function buildCallEdges(files, parseResults) {
    const edges = [];
    const knownFiles = new Set(files.map(f => f.relativePath));
    // Build a set of significant function node IDs for quick lookup
    const significantFunctions = new Set();
    for (const file of files) {
        const parseResult = parseResults.get(file.relativePath);
        if (!parseResult)
            continue;
        for (const func of parseResult.functions) {
            const lineCount = func.endLine - func.startLine + 1;
            const isSignificant = func.isExported || lineCount >= 10;
            if (isSignificant) {
                significantFunctions.add(`${file.relativePath}::${func.name}`);
            }
        }
    }
    for (const file of files) {
        const parseResult = parseResults.get(file.relativePath);
        if (!parseResult)
            continue;
        for (const imp of parseResult.imports) {
            // Skip external package imports
            if (!imp.source.startsWith('.'))
                continue;
            const resolvedPath = resolveImportPath(imp.source, file.relativePath, knownFiles);
            if (!resolvedPath)
                continue;
            // Check each named import against significant functions in the target file
            for (const name of imp.names) {
                const targetNodeId = `${resolvedPath}::${name}`;
                if (significantFunctions.has(targetNodeId)) {
                    edges.push({
                        source: file.relativePath,
                        target: targetNodeId,
                        type: 'calls',
                    });
                }
            }
        }
    }
    return edges;
}
/**
 * Builds graph nodes and edges from scanned files and their parse results.
 *
 * Generates:
 * - File nodes for each source file
 * - Function nodes for each extracted function
 * - Class nodes for each extracted class
 * - Import edges between files
 * - Containment edges (file → function/class)
 * - Call relationship edges where detected
 *
 * @param files - Scanned file entries with metadata
 * @param parseResults - Map of file relative path to its parse result
 * @returns GraphOutput with all generated nodes and edges
 */
export function buildGraph(files, parseResults) {
    const nodes = [];
    const edges = [];
    // Build file nodes
    const fileNodes = buildFileNodes(files, parseResults);
    nodes.push(...fileNodes);
    // Build function nodes and containment edges
    const functionResult = buildFunctionNodes(files, parseResults);
    nodes.push(...functionResult.nodes);
    edges.push(...functionResult.edges);
    // Build class nodes and containment edges
    const classResult = buildClassNodes(files, parseResults);
    nodes.push(...classResult.nodes);
    edges.push(...classResult.edges);
    // Build import edges
    const importEdges = buildImportEdges(files, parseResults);
    edges.push(...importEdges);
    // Build call edges
    const callEdges = buildCallEdges(files, parseResults);
    edges.push(...callEdges);
    return {
        nodes,
        edges,
    };
}