-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathflow.js
More file actions
296 lines (262 loc) · 8.75 KB
/
Copy pathflow.js
File metadata and controls
296 lines (262 loc) · 8.75 KB
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
/**
* Execution flow tracing — forward BFS from entry points through callees to leaves.
*
* Answers "what happens when a user hits POST /login?" by tracing from
* framework entry points (routes, commands, events) through their call chains.
*/
import { openReadonlyOrFail } from './db.js';
import { paginateResult } from './paginate.js';
import { CORE_SYMBOL_KINDS, findMatchingNodes, kindIcon } from './queries.js';
import { outputResult } from './result-formatter.js';
import { FRAMEWORK_ENTRY_PREFIXES } from './structure.js';
import { isTestFile } from './test-filter.js';
/**
* Determine the entry point type from a node name based on framework prefixes.
* @param {string} name
* @returns {'route'|'event'|'command'|'exported'|null}
*/
export function entryPointType(name) {
for (const prefix of FRAMEWORK_ENTRY_PREFIXES) {
if (name.startsWith(prefix)) {
return prefix.slice(0, -1); // 'route:', 'event:', 'command:' → 'route', 'event', 'command'
}
}
return null;
}
/**
* Query all entry points from the graph, grouped by type.
* Entry points are nodes with framework prefixes or role = 'entry'.
*
* @param {string} [dbPath]
* @param {object} [opts]
* @param {boolean} [opts.noTests]
* @returns {{ entries: object[], byType: object, count: number }}
*/
export function listEntryPointsData(dbPath, opts = {}) {
const db = openReadonlyOrFail(dbPath);
try {
const noTests = opts.noTests || false;
// Find all framework-prefixed nodes
const prefixConditions = FRAMEWORK_ENTRY_PREFIXES.map(() => 'n.name LIKE ?').join(' OR ');
const prefixParams = FRAMEWORK_ENTRY_PREFIXES.map((p) => `${p}%`);
let rows = db
.prepare(
`SELECT n.name, n.kind, n.file, n.line, n.role
FROM nodes n
WHERE (
(${prefixConditions})
OR n.role = 'entry'
)
AND n.kind NOT IN ('file', 'directory')
ORDER BY n.name`,
)
.all(...prefixParams);
if (noTests) rows = rows.filter((r) => !isTestFile(r.file));
const entries = rows.map((r) => ({
name: r.name,
kind: r.kind,
file: r.file,
line: r.line,
role: r.role,
type: entryPointType(r.name) || (r.role === 'entry' ? 'exported' : null),
}));
const byType = {};
for (const e of entries) {
const t = e.type || 'other';
if (!byType[t]) byType[t] = [];
byType[t].push(e);
}
const base = { entries, byType, count: entries.length };
return paginateResult(base, 'entries', { limit: opts.limit, offset: opts.offset });
} finally {
db.close();
}
}
/**
* Forward BFS from a matched node through callees to leaves.
*
* @param {string} name - Node name to trace from (supports partial/prefix-stripped matching)
* @param {string} [dbPath]
* @param {object} [opts]
* @param {number} [opts.depth=10]
* @param {boolean} [opts.noTests]
* @param {string} [opts.file]
* @param {string} [opts.kind]
* @returns {{ entry: object|null, depth: number, steps: object[], leaves: object[], cycles: object[], totalReached: number, truncated: boolean }}
*/
export function flowData(name, dbPath, opts = {}) {
const db = openReadonlyOrFail(dbPath);
try {
const maxDepth = opts.depth || 10;
const noTests = opts.noTests || false;
const flowOpts = { ...opts, kinds: opts.kind ? [opts.kind] : CORE_SYMBOL_KINDS };
// Phase 1: Direct LIKE match on full name (use all 10 core symbol kinds,
// not just FUNCTION_KINDS, so flow can trace from interfaces/types/structs/etc.)
let matchNode = findMatchingNodes(db, name, flowOpts)[0] ?? null;
// Phase 2: Prefix-stripped matching — try adding framework prefixes
if (!matchNode) {
for (const prefix of FRAMEWORK_ENTRY_PREFIXES) {
matchNode = findMatchingNodes(db, `${prefix}${name}`, flowOpts)[0] ?? null;
if (matchNode) break;
}
}
if (!matchNode) {
return {
entry: null,
depth: maxDepth,
steps: [],
leaves: [],
cycles: [],
totalReached: 0,
truncated: false,
};
}
const epType = entryPointType(matchNode.name);
const entry = {
name: matchNode.name,
kind: matchNode.kind,
file: matchNode.file,
line: matchNode.line,
type: epType || 'exported',
role: matchNode.role,
};
// Forward BFS through callees
const visited = new Set([matchNode.id]);
let frontier = [matchNode.id];
const steps = [];
const cycles = [];
let truncated = false;
// Track which nodes are at each depth and their depth for leaf detection
const nodeDepths = new Map();
const idToNode = new Map();
idToNode.set(matchNode.id, entry);
for (let d = 1; d <= maxDepth; d++) {
const nextFrontier = [];
const levelNodes = [];
for (const fid of frontier) {
const callees = db
.prepare(
`SELECT DISTINCT n.id, n.name, n.kind, n.file, n.line, n.role
FROM edges e JOIN nodes n ON e.target_id = n.id
WHERE e.source_id = ? AND e.kind = 'calls'`,
)
.all(fid);
for (const c of callees) {
if (noTests && isTestFile(c.file)) continue;
if (visited.has(c.id)) {
// Cycle detected
const fromNode = idToNode.get(fid);
if (fromNode) {
cycles.push({ from: fromNode.name, to: c.name, depth: d });
}
continue;
}
visited.add(c.id);
nextFrontier.push(c.id);
const nodeInfo = { name: c.name, kind: c.kind, file: c.file, line: c.line };
levelNodes.push(nodeInfo);
nodeDepths.set(c.id, d);
idToNode.set(c.id, nodeInfo);
}
}
if (levelNodes.length > 0) {
steps.push({ depth: d, nodes: levelNodes });
}
frontier = nextFrontier;
if (frontier.length === 0) break;
if (d === maxDepth && frontier.length > 0) {
truncated = true;
}
}
// Identify leaves: visited nodes that have no outgoing 'calls' edges to other visited nodes
// (or no outgoing calls at all)
const leaves = [];
for (const [id, depth] of nodeDepths) {
const outgoing = db
.prepare(
`SELECT DISTINCT n.id
FROM edges e JOIN nodes n ON e.target_id = n.id
WHERE e.source_id = ? AND e.kind = 'calls'`,
)
.all(id);
if (outgoing.length === 0) {
const node = idToNode.get(id);
if (node) {
leaves.push({ ...node, depth });
}
}
}
const base = {
entry,
depth: maxDepth,
steps,
leaves,
cycles,
totalReached: visited.size - 1, // exclude the entry node itself
truncated,
};
return paginateResult(base, 'steps', { limit: opts.limit, offset: opts.offset });
} finally {
db.close();
}
}
/**
* CLI formatter — text or JSON output.
*/
export function flow(name, dbPath, opts = {}) {
if (opts.list) {
const data = listEntryPointsData(dbPath, {
noTests: opts.noTests,
limit: opts.limit,
offset: opts.offset,
});
if (outputResult(data, 'entries', opts)) return;
if (data.count === 0) {
console.log('No entry points found. Run "codegraph build" first.');
return;
}
console.log(`\nEntry points (${data.count} total):\n`);
for (const [type, entries] of Object.entries(data.byType)) {
console.log(` ${type} (${entries.length}):`);
for (const e of entries) {
console.log(` [${kindIcon(e.kind)}] ${e.name} ${e.file}:${e.line}`);
}
console.log();
}
return;
}
const data = flowData(name, dbPath, opts);
if (outputResult(data, 'steps', opts)) return;
if (!data.entry) {
console.log(`No matching entry point or function found for "${name}".`);
return;
}
const e = data.entry;
const typeTag = e.type !== 'exported' ? ` (${e.type})` : '';
console.log(`\nFlow from: [${kindIcon(e.kind)}] ${e.name}${typeTag} ${e.file}:${e.line}`);
console.log(
`Depth: ${data.depth} Reached: ${data.totalReached} nodes Leaves: ${data.leaves.length}`,
);
if (data.truncated) {
console.log(` (truncated at depth ${data.depth})`);
}
console.log();
if (data.steps.length === 0) {
console.log(' (leaf node — no callees)');
return;
}
for (const step of data.steps) {
console.log(` depth ${step.depth}:`);
for (const n of step.nodes) {
const isLeaf = data.leaves.some((l) => l.name === n.name && l.file === n.file);
const leafTag = isLeaf ? ' [leaf]' : '';
console.log(` [${kindIcon(n.kind)}] ${n.name} ${n.file}:${n.line}${leafTag}`);
}
}
if (data.cycles.length > 0) {
console.log('\n Cycles detected:');
for (const c of data.cycles) {
console.log(` ${c.from} -> ${c.to} (at depth ${c.depth})`);
}
}
}