@fabersep/atlas 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +30 -0
- package/LICENSE +202 -0
- package/README.md +139 -2
- package/dist/packs/csharp/calls.d.ts +4 -0
- package/dist/packs/csharp/calls.js +167 -0
- package/dist/packs/csharp/declarations.d.ts +4 -0
- package/dist/packs/csharp/declarations.js +142 -0
- package/dist/packs/csharp/pack.d.ts +2 -0
- package/dist/packs/csharp/pack.js +39 -0
- package/dist/packs/csharp/resolve.d.ts +2 -0
- package/dist/packs/csharp/resolve.js +35 -0
- package/dist/packs/csharp/signals.d.ts +7 -0
- package/dist/packs/csharp/signals.js +143 -0
- package/dist/packs/dart/calls.d.ts +4 -0
- package/dist/packs/dart/calls.js +130 -0
- package/dist/packs/dart/declarations.d.ts +11 -0
- package/dist/packs/dart/declarations.js +79 -0
- package/dist/packs/dart/members.d.ts +4 -0
- package/dist/packs/dart/members.js +62 -0
- package/dist/packs/dart/pack.d.ts +2 -0
- package/dist/packs/dart/pack.js +37 -0
- package/dist/packs/dart/resolve.d.ts +2 -0
- package/dist/packs/dart/resolve.js +32 -0
- package/dist/packs/index.d.ts +3 -0
- package/dist/packs/index.js +8 -0
- package/dist/packs/json/pack.d.ts +2 -0
- package/dist/packs/json/pack.js +27 -0
- package/dist/packs/msbuild/pack.d.ts +2 -0
- package/dist/packs/msbuild/pack.js +75 -0
- package/dist/packs/msbuild/xml.d.ts +16 -0
- package/dist/packs/msbuild/xml.js +123 -0
- package/dist/packs/typescript/calls.d.ts +4 -0
- package/dist/packs/typescript/calls.js +201 -0
- package/dist/packs/typescript/declarations.d.ts +9 -0
- package/dist/packs/typescript/declarations.js +74 -0
- package/dist/packs/typescript/members.d.ts +4 -0
- package/dist/packs/typescript/members.js +92 -0
- package/dist/packs/typescript/pack.d.ts +2 -0
- package/dist/packs/typescript/pack.js +53 -0
- package/dist/packs/typescript/resolve.d.ts +2 -0
- package/dist/packs/typescript/resolve.js +68 -0
- package/dist/packs/typescript/signals.d.ts +7 -0
- package/dist/packs/typescript/signals.js +186 -0
- package/dist/packs/yaml/pack.d.ts +2 -0
- package/dist/packs/yaml/pack.js +35 -0
- package/dist/src/cli.d.ts +2 -0
- package/dist/src/cli.js +371 -0
- package/dist/src/graph/build.d.ts +17 -0
- package/dist/src/graph/build.js +41 -0
- package/dist/src/graph/metrics.d.ts +12 -0
- package/dist/src/graph/metrics.js +75 -0
- package/dist/src/graph/topology.d.ts +5 -0
- package/dist/src/graph/topology.js +54 -0
- package/dist/src/health/compare.d.ts +2 -0
- package/dist/src/health/compare.js +38 -0
- package/dist/src/health/duplicates.d.ts +11 -0
- package/dist/src/health/duplicates.js +66 -0
- package/dist/src/health/health.d.ts +12 -0
- package/dist/src/health/health.js +94 -0
- package/dist/src/health/types.d.ts +101 -0
- package/dist/src/health/types.js +5 -0
- package/dist/src/index.d.ts +23 -0
- package/dist/src/index.js +20 -0
- package/dist/src/init/init.d.ts +73 -0
- package/dist/src/init/init.js +305 -0
- package/dist/src/init/scout.d.ts +45 -0
- package/dist/src/init/scout.js +208 -0
- package/dist/src/jsonc.d.ts +5 -0
- package/dist/src/jsonc.js +34 -0
- package/dist/src/mcp/server.d.ts +266 -0
- package/dist/src/mcp/server.js +414 -0
- package/dist/src/metrics/doctor.d.ts +15 -0
- package/dist/src/metrics/doctor.js +125 -0
- package/dist/src/metrics/log.d.ts +43 -0
- package/dist/src/metrics/log.js +101 -0
- package/dist/src/metrics/stats.d.ts +43 -0
- package/dist/src/metrics/stats.js +172 -0
- package/dist/src/model.d.ts +178 -0
- package/dist/src/model.js +2 -0
- package/dist/src/outputs.d.ts +16 -0
- package/dist/src/outputs.js +48 -0
- package/dist/src/packs/calls.d.ts +9 -0
- package/dist/src/packs/calls.js +119 -0
- package/dist/src/packs/dataShape.d.ts +6 -0
- package/dist/src/packs/dataShape.js +54 -0
- package/dist/src/packs/pack.d.ts +51 -0
- package/dist/src/packs/pack.js +3 -0
- package/dist/src/packs/packFiles.d.ts +5 -0
- package/dist/src/packs/packFiles.js +12 -0
- package/dist/src/packs/symbols.d.ts +39 -0
- package/dist/src/packs/symbols.js +98 -0
- package/dist/src/packs/treeSitter.d.ts +8 -0
- package/dist/src/packs/treeSitter.js +107 -0
- package/dist/src/queries/flow.d.ts +42 -0
- package/dist/src/queries/flow.js +391 -0
- package/dist/src/queries/map.d.ts +28 -0
- package/dist/src/queries/map.js +102 -0
- package/dist/src/queries/neighborhood.d.ts +35 -0
- package/dist/src/queries/neighborhood.js +60 -0
- package/dist/src/scan/audit.d.ts +2 -0
- package/dist/src/scan/audit.js +41 -0
- package/dist/src/scan/records.d.ts +12 -0
- package/dist/src/scan/records.js +42 -0
- package/dist/src/scan/resolveImports.d.ts +3 -0
- package/dist/src/scan/resolveImports.js +47 -0
- package/dist/src/scan/roles.d.ts +5 -0
- package/dist/src/scan/roles.js +87 -0
- package/dist/src/scan/scan.d.ts +24 -0
- package/dist/src/scan/scan.js +72 -0
- package/dist/src/text/briefs.d.ts +13 -0
- package/dist/src/text/briefs.js +43 -0
- package/dist/src/text/collapse.d.ts +10 -0
- package/dist/src/text/collapse.js +58 -0
- package/dist/src/text/declarations.d.ts +12 -0
- package/dist/src/text/declarations.js +120 -0
- package/dist/src/text/structure.d.ts +2 -0
- package/dist/src/text/structure.js +15 -0
- package/dist/src/text/tree.d.ts +20 -0
- package/dist/src/text/tree.js +49 -0
- package/dist/src/tokens.d.ts +4 -0
- package/dist/src/tokens.js +9 -0
- package/dist/src/version.d.ts +10 -0
- package/dist/src/version.js +11 -0
- package/dist/src/walk/ignoreRules.d.ts +17 -0
- package/dist/src/walk/ignoreRules.js +72 -0
- package/dist/src/walk/walk.d.ts +11 -0
- package/dist/src/walk/walk.js +29 -0
- package/grammars/LICENSE-tree-sitter-dart +20 -0
- package/grammars/README.md +11 -0
- package/grammars/tree-sitter-dart.wasm +0 -0
- package/package.json +68 -4
- package/packs/csharp/README.md +9 -0
- package/packs/csharp/imports.scm +3 -0
- package/packs/csharp/symbols.scm +17 -0
- package/packs/dart/imports.scm +5 -0
- package/packs/dart/symbols.scm +13 -0
- package/packs/msbuild/README.md +11 -0
- package/packs/typescript/exports.scm +16 -0
- package/packs/typescript/imports.scm +9 -0
- package/packs/typescript/symbols.scm +19 -0
- package/schema/ast_code_map.schema.json +144 -0
- package/schema/schema-lock.json +7 -0
- package/schema/topology.schema.json +87 -0
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
import { cleanTypeName, fieldTypeMap } from '../packs/calls.js';
|
|
2
|
+
import { CONTAINER_KINDS, FUNCTION_KINDS } from '../packs/symbols.js';
|
|
3
|
+
import { isTestFile } from './map.js';
|
|
4
|
+
export class UnknownSymbolError extends Error {
|
|
5
|
+
}
|
|
6
|
+
function normKey(key) {
|
|
7
|
+
const tail = key.includes('.') ? key.split('.').pop() : key;
|
|
8
|
+
return tail.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
|
|
9
|
+
}
|
|
10
|
+
function simpleType(ref) {
|
|
11
|
+
return ref.replace(/<.*$/s, '').split('.').pop().trim();
|
|
12
|
+
}
|
|
13
|
+
const MEDIATR_HANDLER_RX = /^I(?:Request|Notification)Handler<\s*([^,>\s]+)/;
|
|
14
|
+
class FlowGraph {
|
|
15
|
+
callables = new Map();
|
|
16
|
+
virtuals = new Map();
|
|
17
|
+
containersByName = new Map();
|
|
18
|
+
containersByFileAndName = new Map();
|
|
19
|
+
functionsByName = new Map();
|
|
20
|
+
implsByInterface = new Map();
|
|
21
|
+
eventHandlers = new Map();
|
|
22
|
+
jobHandlers = new Map();
|
|
23
|
+
mediatrHandlers = new Map();
|
|
24
|
+
outgoing = new Map();
|
|
25
|
+
incoming = new Map();
|
|
26
|
+
constructor(map, includeTests) {
|
|
27
|
+
const files = includeTests ? map.files : map.files.filter((f) => !isTestFile(f.path));
|
|
28
|
+
// 1. Index all containers, callables, implements, and virtual handler entrypoints
|
|
29
|
+
for (const record of files) {
|
|
30
|
+
for (const sym of record.symbols) {
|
|
31
|
+
if (CONTAINER_KINDS.has(sym.kind)) {
|
|
32
|
+
this.indexContainer(record, sym);
|
|
33
|
+
}
|
|
34
|
+
else if (FUNCTION_KINDS.has(sym.kind)) {
|
|
35
|
+
this.indexCallable(record, sym, undefined);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// 2. Index interface implementations and MediatR handlers
|
|
40
|
+
for (const list of this.containersByName.values()) {
|
|
41
|
+
for (const c of list) {
|
|
42
|
+
for (const iface of c.symbol.implements ?? []) {
|
|
43
|
+
const base = simpleType(iface);
|
|
44
|
+
const arr = this.implsByInterface.get(base) ?? [];
|
|
45
|
+
arr.push(c);
|
|
46
|
+
this.implsByInterface.set(base, arr);
|
|
47
|
+
const mediatr = MEDIATR_HANDLER_RX.exec(iface.trim());
|
|
48
|
+
if (mediatr) {
|
|
49
|
+
const cmd = simpleType(mediatr[1]);
|
|
50
|
+
const handle = (c.symbol.members ?? []).find((m) => FUNCTION_KINDS.has(m.kind) && (m.name === 'Handle' || m.name === 'HandleAsync'));
|
|
51
|
+
if (handle) {
|
|
52
|
+
const entry = this.callables.get(`${c.file}#${c.name}.${handle.name}`);
|
|
53
|
+
if (entry) {
|
|
54
|
+
const key = normKey(cmd);
|
|
55
|
+
const handlers = this.mediatrHandlers.get(key) ?? [];
|
|
56
|
+
handlers.push(entry);
|
|
57
|
+
this.mediatrHandlers.set(key, handlers);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// 3. Resolve all call sites and implements edges
|
|
65
|
+
for (const entry of this.callables.values()) {
|
|
66
|
+
for (const call of entry.symbol.calls ?? []) {
|
|
67
|
+
this.resolveCallSite(entry, call);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
indexContainer(record, sym) {
|
|
72
|
+
const entry = {
|
|
73
|
+
id: `${record.path}#${sym.name}`,
|
|
74
|
+
name: sym.name,
|
|
75
|
+
file: record.path,
|
|
76
|
+
symbol: sym,
|
|
77
|
+
record,
|
|
78
|
+
fields: fieldTypeMap(sym),
|
|
79
|
+
};
|
|
80
|
+
const list = this.containersByName.get(sym.name) ?? [];
|
|
81
|
+
list.push(entry);
|
|
82
|
+
this.containersByName.set(sym.name, list);
|
|
83
|
+
this.containersByFileAndName.set(`${record.path}#${sym.name}`, entry);
|
|
84
|
+
for (const m of sym.members ?? []) {
|
|
85
|
+
if (FUNCTION_KINDS.has(m.kind))
|
|
86
|
+
this.indexCallable(record, m, sym.name);
|
|
87
|
+
else if (CONTAINER_KINDS.has(m.kind))
|
|
88
|
+
this.indexContainer(record, m);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
indexCallable(record, sym, owner) {
|
|
92
|
+
const label = owner ? `${owner}.${sym.name}` : sym.name;
|
|
93
|
+
const id = `${record.path}#${label}`;
|
|
94
|
+
const entry = { id, label, file: record.path, line: sym.lineStart, owner, symbol: sym, record };
|
|
95
|
+
this.callables.set(id, entry);
|
|
96
|
+
if (!owner) {
|
|
97
|
+
const list = this.functionsByName.get(sym.name) ?? [];
|
|
98
|
+
list.push(entry);
|
|
99
|
+
this.functionsByName.set(sym.name, list);
|
|
100
|
+
}
|
|
101
|
+
for (const ep of sym.entrypoints ?? []) {
|
|
102
|
+
if (ep.startsWith('event:')) {
|
|
103
|
+
const key = normKey(ep.slice('event:'.length));
|
|
104
|
+
const list = this.eventHandlers.get(key) ?? [];
|
|
105
|
+
list.push(entry);
|
|
106
|
+
this.eventHandlers.set(key, list);
|
|
107
|
+
}
|
|
108
|
+
else if (ep.startsWith('job:')) {
|
|
109
|
+
const key = normKey(ep.slice('job:'.length));
|
|
110
|
+
const list = this.jobHandlers.get(key) ?? [];
|
|
111
|
+
list.push(entry);
|
|
112
|
+
this.jobHandlers.set(key, list);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
addEdge(edge) {
|
|
117
|
+
const out = this.outgoing.get(edge.from) ?? [];
|
|
118
|
+
if (!out.some((e) => e.to === edge.to && e.kind === edge.kind)) {
|
|
119
|
+
out.push(edge);
|
|
120
|
+
this.outgoing.set(edge.from, out);
|
|
121
|
+
}
|
|
122
|
+
const inc = this.incoming.get(edge.to) ?? [];
|
|
123
|
+
if (!inc.some((e) => e.from === edge.from && e.kind === edge.kind)) {
|
|
124
|
+
inc.push(edge);
|
|
125
|
+
this.incoming.set(edge.to, inc);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
ensureVirtual(id, label, boundary) {
|
|
129
|
+
if (!this.virtuals.has(id))
|
|
130
|
+
this.virtuals.set(id, { id, label, ...(boundary ? { boundary } : {}) });
|
|
131
|
+
return id;
|
|
132
|
+
}
|
|
133
|
+
/** Looks up a field's type on `container` or any ancestor class along its `extends` chain. */
|
|
134
|
+
resolveFieldType(container, fieldName, visited = new Set()) {
|
|
135
|
+
if (!container || visited.has(container.id))
|
|
136
|
+
return undefined;
|
|
137
|
+
visited.add(container.id);
|
|
138
|
+
const local = container.fields.get(fieldName);
|
|
139
|
+
if (local)
|
|
140
|
+
return local;
|
|
141
|
+
for (const ext of container.symbol.extends ?? []) {
|
|
142
|
+
const parent = this.findContainer(container.record, simpleType(ext));
|
|
143
|
+
const inherited = this.resolveFieldType(parent, fieldName, visited);
|
|
144
|
+
if (inherited)
|
|
145
|
+
return inherited;
|
|
146
|
+
}
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
/** Finds a container by name, preferring files directly imported by `fromRecord`. */
|
|
150
|
+
findContainer(fromRecord, name) {
|
|
151
|
+
const sameFile = this.containersByFileAndName.get(`${fromRecord.path}#${name}`);
|
|
152
|
+
if (sameFile)
|
|
153
|
+
return sameFile;
|
|
154
|
+
const importedTargets = new Set(fromRecord.imports.filter((i) => i.kind === 'file' && i.target).map((i) => i.target));
|
|
155
|
+
const candidates = this.containersByName.get(name);
|
|
156
|
+
if (!candidates?.length)
|
|
157
|
+
return undefined;
|
|
158
|
+
const imported = candidates.find((c) => importedTargets.has(c.file));
|
|
159
|
+
return imported ?? candidates[0];
|
|
160
|
+
}
|
|
161
|
+
/** Finds a method on a container or its `extends` chain. */
|
|
162
|
+
findMethodOnContainer(container, methodName, visited = new Set()) {
|
|
163
|
+
if (visited.has(container.id))
|
|
164
|
+
return undefined;
|
|
165
|
+
visited.add(container.id);
|
|
166
|
+
const direct = this.callables.get(`${container.file}#${container.name}.${methodName}`);
|
|
167
|
+
if (direct)
|
|
168
|
+
return direct;
|
|
169
|
+
for (const ext of container.symbol.extends ?? []) {
|
|
170
|
+
const parent = this.findContainer(container.record, simpleType(ext));
|
|
171
|
+
if (parent) {
|
|
172
|
+
const inherited = this.findMethodOnContainer(parent, methodName, visited);
|
|
173
|
+
if (inherited)
|
|
174
|
+
return inherited;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
resolveCallSite(from, call) {
|
|
180
|
+
// 1. Virtual async/indirect bridges: events, jobs, MediatR
|
|
181
|
+
if (call.kind === 'emits' && call.targetKey) {
|
|
182
|
+
const vid = this.ensureVirtual(`event:${normKey(call.targetKey)}`, `event:${call.targetKey}`);
|
|
183
|
+
this.addEdge({ from: from.id, to: vid, kind: 'emits', callLine: call.line });
|
|
184
|
+
for (const h of this.eventHandlers.get(normKey(call.targetKey)) ?? []) {
|
|
185
|
+
this.addEdge({ from: vid, to: h.id, kind: 'handles' });
|
|
186
|
+
}
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
if (call.kind === 'queues' && call.targetKey) {
|
|
190
|
+
const vid = this.ensureVirtual(`job:${normKey(call.targetKey)}`, `job:${call.targetKey}`, 'queue');
|
|
191
|
+
this.addEdge({ from: from.id, to: vid, kind: 'queues', callLine: call.line, boundary: 'queue' });
|
|
192
|
+
for (const h of this.jobHandlers.get(normKey(call.targetKey)) ?? []) {
|
|
193
|
+
this.addEdge({ from: vid, to: h.id, kind: 'handles' });
|
|
194
|
+
}
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
if (call.kind === 'sends' && call.targetKey) {
|
|
198
|
+
const vid = this.ensureVirtual(`mediatr:${normKey(call.targetKey)}`, `mediatr:${call.targetKey}`);
|
|
199
|
+
this.addEdge({ from: from.id, to: vid, kind: 'sends', callLine: call.line });
|
|
200
|
+
for (const h of this.mediatrHandlers.get(normKey(call.targetKey)) ?? []) {
|
|
201
|
+
this.addEdge({ from: vid, to: h.id, kind: 'handles' });
|
|
202
|
+
}
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
// 2. Direct member call: resolve receiverType (local, inherited field, or static class name)
|
|
206
|
+
if (call.receiver) {
|
|
207
|
+
const ownerContainer = from.owner ? this.containersByFileAndName.get(`${from.file}#${from.owner}`) : undefined;
|
|
208
|
+
let receiverType = call.receiverType;
|
|
209
|
+
if (!receiverType) {
|
|
210
|
+
if (call.receiver === 'this')
|
|
211
|
+
receiverType = from.owner;
|
|
212
|
+
else
|
|
213
|
+
receiverType = this.resolveFieldType(ownerContainer, call.receiver) ?? cleanTypeName(call.receiver);
|
|
214
|
+
}
|
|
215
|
+
if (receiverType) {
|
|
216
|
+
const targetContainer = this.findContainer(from.record, receiverType);
|
|
217
|
+
if (targetContainer) {
|
|
218
|
+
const targetMethod = this.findMethodOnContainer(targetContainer, call.method);
|
|
219
|
+
if (targetMethod && targetMethod.id !== from.id) {
|
|
220
|
+
this.addEdge({ from: from.id, to: targetMethod.id, kind: 'calls', callLine: call.line, boundary: call.boundary });
|
|
221
|
+
// Follow `implements` edges when calling an interface method
|
|
222
|
+
if (targetContainer.symbol.kind === 'interface') {
|
|
223
|
+
for (const impl of this.implsByInterface.get(targetContainer.name) ?? []) {
|
|
224
|
+
const implMethod = this.findMethodOnContainer(impl, call.method);
|
|
225
|
+
if (implMethod && implMethod.id !== targetMethod.id) {
|
|
226
|
+
this.addEdge({ from: targetMethod.id, to: implMethod.id, kind: 'impl' });
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// Unresolved call that crosses an external boundary ([db], [http], [queue], [fs])
|
|
235
|
+
if (call.boundary) {
|
|
236
|
+
const vid = this.ensureVirtual(`ext:${call.boundary}:${call.callee}`, `${call.callee}()`, call.boundary);
|
|
237
|
+
this.addEdge({ from: from.id, to: vid, kind: 'calls', callLine: call.line, boundary: call.boundary });
|
|
238
|
+
}
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
// 3. Bare function call: same file -> imported files -> unique match in CodeMap
|
|
242
|
+
const sameFileFn = this.callables.get(`${from.file}#${call.method}`);
|
|
243
|
+
if (sameFileFn && sameFileFn.id !== from.id) {
|
|
244
|
+
this.addEdge({ from: from.id, to: sameFileFn.id, kind: 'calls', callLine: call.line, boundary: call.boundary });
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
const importedTargets = new Set(from.record.imports.filter((i) => i.kind === 'file' && i.target).map((i) => i.target));
|
|
248
|
+
const fns = this.functionsByName.get(call.method);
|
|
249
|
+
if (fns?.length) {
|
|
250
|
+
const imported = fns.find((f) => importedTargets.has(f.file));
|
|
251
|
+
const target = imported ?? (fns.length === 1 ? fns[0] : undefined);
|
|
252
|
+
if (target && target.id !== from.id) {
|
|
253
|
+
this.addEdge({ from: from.id, to: target.id, kind: 'calls', callLine: call.line, boundary: call.boundary });
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (call.boundary) {
|
|
258
|
+
const vid = this.ensureVirtual(`ext:${call.boundary}:${call.callee}`, `${call.callee}()`, call.boundary);
|
|
259
|
+
this.addEdge({ from: from.id, to: vid, kind: 'calls', callLine: call.line, boundary: call.boundary });
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
/** Matches a user-supplied symbol query (`Class.method`, `file#Class.method`, `event:X`, `job:X`, `Class`, `fn`). */
|
|
263
|
+
resolveQuery(raw) {
|
|
264
|
+
const q = raw.trim();
|
|
265
|
+
if (q.startsWith('event:')) {
|
|
266
|
+
const key = `event:${normKey(q.slice('event:'.length))}`;
|
|
267
|
+
if (this.virtuals.has(key))
|
|
268
|
+
return [key];
|
|
269
|
+
const handlers = this.eventHandlers.get(normKey(q.slice('event:'.length)));
|
|
270
|
+
if (handlers?.length)
|
|
271
|
+
return handlers.map((h) => h.id);
|
|
272
|
+
}
|
|
273
|
+
if (q.startsWith('job:')) {
|
|
274
|
+
const key = `job:${normKey(q.slice('job:'.length))}`;
|
|
275
|
+
if (this.virtuals.has(key))
|
|
276
|
+
return [key];
|
|
277
|
+
const handlers = this.jobHandlers.get(normKey(q.slice('job:'.length)));
|
|
278
|
+
if (handlers?.length)
|
|
279
|
+
return handlers.map((h) => h.id);
|
|
280
|
+
}
|
|
281
|
+
// Exact id: `path#Symbol` or `path:Symbol`
|
|
282
|
+
const normalizedId = q.includes('#') ? q : q.includes(':') ? q.replace(':', '#') : undefined;
|
|
283
|
+
if (normalizedId && this.callables.has(normalizedId))
|
|
284
|
+
return [normalizedId];
|
|
285
|
+
// Exact `Class.method` or `functionName`
|
|
286
|
+
const exactLabel = [...this.callables.values()].filter((c) => c.label === q);
|
|
287
|
+
if (exactLabel.length)
|
|
288
|
+
return exactLabel.map((c) => c.id);
|
|
289
|
+
// Ends with `#Class.method` or `/file.ts#Class.method`
|
|
290
|
+
if (normalizedId) {
|
|
291
|
+
const suffix = [...this.callables.values()].filter((c) => c.id.endsWith(normalizedId));
|
|
292
|
+
if (suffix.length)
|
|
293
|
+
return suffix.map((c) => c.id);
|
|
294
|
+
}
|
|
295
|
+
// Match by bare method name or class name
|
|
296
|
+
const byClass = [...this.callables.values()].filter((c) => c.owner === q && c.symbol.visibility !== 'private');
|
|
297
|
+
if (byClass.length)
|
|
298
|
+
return byClass.map((c) => c.id);
|
|
299
|
+
const byMethod = [...this.callables.values()].filter((c) => c.symbol.name === q);
|
|
300
|
+
if (byMethod.length)
|
|
301
|
+
return byMethod.map((c) => c.id);
|
|
302
|
+
throw new UnknownSymbolError(`Symbol not found in code map: ${raw}`);
|
|
303
|
+
}
|
|
304
|
+
buildTree(nodeId, direction, depth, pathVisited, edge) {
|
|
305
|
+
const callable = this.callables.get(nodeId);
|
|
306
|
+
const virtual = this.virtuals.get(nodeId);
|
|
307
|
+
const base = callable
|
|
308
|
+
? {
|
|
309
|
+
id: callable.id,
|
|
310
|
+
label: callable.label,
|
|
311
|
+
file: callable.file,
|
|
312
|
+
line: callable.line,
|
|
313
|
+
...(callable.symbol.entrypoints?.length ? { entrypoints: callable.symbol.entrypoints } : {}),
|
|
314
|
+
...(callable.symbol.throws?.length ? { throws: callable.symbol.throws } : {}),
|
|
315
|
+
...(edge?.boundary ? { boundary: edge.boundary } : {}),
|
|
316
|
+
...(edge ? { edgeKind: edge.kind, ...(edge.callLine ? { callLine: edge.callLine } : {}) } : {}),
|
|
317
|
+
children: [],
|
|
318
|
+
}
|
|
319
|
+
: {
|
|
320
|
+
id: nodeId,
|
|
321
|
+
label: virtual?.label ?? nodeId,
|
|
322
|
+
virtual: true,
|
|
323
|
+
...(virtual?.boundary || edge?.boundary ? { boundary: virtual?.boundary ?? edge?.boundary } : {}),
|
|
324
|
+
...(edge ? { edgeKind: edge.kind, ...(edge.callLine ? { callLine: edge.callLine } : {}) } : {}),
|
|
325
|
+
children: [],
|
|
326
|
+
};
|
|
327
|
+
if (pathVisited.has(nodeId)) {
|
|
328
|
+
base.cycle = true;
|
|
329
|
+
return base;
|
|
330
|
+
}
|
|
331
|
+
if (depth <= 0)
|
|
332
|
+
return base;
|
|
333
|
+
const nextVisited = new Set(pathVisited);
|
|
334
|
+
nextVisited.add(nodeId);
|
|
335
|
+
const edges = direction === 'downstream' ? (this.outgoing.get(nodeId) ?? []) : (this.incoming.get(nodeId) ?? []);
|
|
336
|
+
for (const e of edges) {
|
|
337
|
+
const nextId = direction === 'downstream' ? e.to : e.from;
|
|
338
|
+
base.children.push(this.buildTree(nextId, direction, depth - 1, nextVisited, e));
|
|
339
|
+
}
|
|
340
|
+
return base;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
/** Downstream execution flow from `<symbol>` up to `depth` hops. */
|
|
344
|
+
export function flow(map, symbolQuery, options = {}) {
|
|
345
|
+
const depth = options.depth ?? 4;
|
|
346
|
+
const graph = new FlowGraph(map, options.includeTests === true);
|
|
347
|
+
const rootIds = graph.resolveQuery(symbolQuery);
|
|
348
|
+
const roots = rootIds.map((id) => graph.buildTree(id, 'downstream', depth, new Set()));
|
|
349
|
+
return { query: symbolQuery, direction: 'downstream', depth, roots };
|
|
350
|
+
}
|
|
351
|
+
/** Upstream impact (callers and entrypoints reaching `<symbol>`) up to `depth` hops. */
|
|
352
|
+
export function impact(map, symbolQuery, options = {}) {
|
|
353
|
+
const depth = options.depth ?? 4;
|
|
354
|
+
const graph = new FlowGraph(map, options.includeTests === true);
|
|
355
|
+
const rootIds = graph.resolveQuery(symbolQuery);
|
|
356
|
+
const roots = rootIds.map((id) => graph.buildTree(id, 'upstream', depth, new Set()));
|
|
357
|
+
return { query: symbolQuery, direction: 'upstream', depth, roots };
|
|
358
|
+
}
|
|
359
|
+
function formatNodeMeta(node) {
|
|
360
|
+
const parts = [];
|
|
361
|
+
if (node.file && node.line)
|
|
362
|
+
parts.push(`(${node.file}:${node.line})`);
|
|
363
|
+
if (node.entrypoints?.length)
|
|
364
|
+
parts.push(`[${node.entrypoints.join(', ')}]`);
|
|
365
|
+
if (node.throws?.length)
|
|
366
|
+
parts.push(`[throws: ${node.throws.join(', ')}]`);
|
|
367
|
+
if (node.boundary)
|
|
368
|
+
parts.push(`[${node.boundary}]`);
|
|
369
|
+
if (node.cycle)
|
|
370
|
+
parts.push('(cycle)');
|
|
371
|
+
return parts.length ? ` ${parts.join(' ')}` : '';
|
|
372
|
+
}
|
|
373
|
+
function renderFlowNode(node, direction, indent, lines) {
|
|
374
|
+
const arrow = node.edgeKind
|
|
375
|
+
? direction === 'downstream'
|
|
376
|
+
? `──${node.edgeKind}──▶ `
|
|
377
|
+
: `◀──${node.edgeKind}── `
|
|
378
|
+
: '';
|
|
379
|
+
lines.push(`${indent}${arrow}${node.label}${formatNodeMeta(node)}`);
|
|
380
|
+
for (const child of node.children) {
|
|
381
|
+
renderFlowNode(child, direction, `${indent} `, lines);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
/** Renders a `flow` or `impact` result as a compact indented tree with `file:line` addresses and edge kinds. */
|
|
385
|
+
export function renderFlow(result) {
|
|
386
|
+
const lines = [];
|
|
387
|
+
for (const root of result.roots) {
|
|
388
|
+
renderFlowNode(root, result.direction, '', lines);
|
|
389
|
+
}
|
|
390
|
+
return lines.join('\n');
|
|
391
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CodeMap } from '../model.js';
|
|
2
|
+
/**
|
|
3
|
+
* The repo map text of a scanned folder, one of its subfolders, or one file, read from the stored code map (CIN-07).
|
|
4
|
+
* Supports layered zoom levels:
|
|
5
|
+
* - zoom 0: folders/modules with file counts and dominant roles
|
|
6
|
+
* - zoom 1: files + top-level declarations only (no method lists), excluding test files by default
|
|
7
|
+
* - zoom 2: full method-level compact map (default)
|
|
8
|
+
*/
|
|
9
|
+
export type ZoomLevel = 0 | 1 | 2;
|
|
10
|
+
export interface MapOptions {
|
|
11
|
+
/** A subfolder or a file of the scanned folder; the whole folder when absent. */
|
|
12
|
+
folder?: string;
|
|
13
|
+
/** Include fields and getters in the one-line summaries (zoom 2). */
|
|
14
|
+
fields?: boolean;
|
|
15
|
+
/** Print every public declaration under its file (the TopologyFULL text) instead of a one-line summary. */
|
|
16
|
+
full?: boolean;
|
|
17
|
+
/** Folder levels to print below the root. */
|
|
18
|
+
depth?: number;
|
|
19
|
+
/** Print the folders only. */
|
|
20
|
+
foldersOnly?: boolean;
|
|
21
|
+
/** Layered zoom level: 0 (modules), 1 (files + top-level symbols), 2 (method-level compact map, default). */
|
|
22
|
+
zoom?: ZoomLevel;
|
|
23
|
+
/** Include test files in zoom 0 and zoom 1 maps (excluded by default). */
|
|
24
|
+
includeTests?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/** Whether a project-relative path looks like a test or spec file. */
|
|
27
|
+
export declare function isTestFile(filePath: string): boolean;
|
|
28
|
+
export declare function renderMap(map: CodeMap, options?: MapOptions): string;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import { ROLE_TAGS } from '../scan/roles.js';
|
|
3
|
+
import { byName } from '../text/collapse.js';
|
|
4
|
+
import { fullBlock, mapSummary, mapSummaryZoom1 } from '../text/declarations.js';
|
|
5
|
+
import { renderTree } from '../text/tree.js';
|
|
6
|
+
import { UnknownFileError } from './neighborhood.js';
|
|
7
|
+
const TEST_DIR = /(^|\/)(test|tests|__tests__|__mocks__|e2e)\//i;
|
|
8
|
+
const TEST_FILE = /(\.(spec|test|e2e-spec|e2e-test)\.[^.]+$|_test\.dart$|(Tests?|Specs?)\.cs$)/i;
|
|
9
|
+
/** Whether a project-relative path looks like a test or spec file. */
|
|
10
|
+
export function isTestFile(filePath) {
|
|
11
|
+
return TEST_DIR.test(filePath) || TEST_FILE.test(path.posix.basename(filePath));
|
|
12
|
+
}
|
|
13
|
+
function renderFile(record, options) {
|
|
14
|
+
if (options.full)
|
|
15
|
+
return fullBlock(record).join('\n');
|
|
16
|
+
if (options.zoom === 1)
|
|
17
|
+
return mapSummaryZoom1(record) ?? '';
|
|
18
|
+
return mapSummary(record, options.fields === true) ?? '';
|
|
19
|
+
}
|
|
20
|
+
function createZoom0Folder(name) {
|
|
21
|
+
return { name, folders: new Map(), ownFiles: 0, totalFiles: 0, roles: new Map() };
|
|
22
|
+
}
|
|
23
|
+
function dominantRoles(counts, limit = 4) {
|
|
24
|
+
return [...counts.entries()]
|
|
25
|
+
.sort((a, b) => b[1] - a[1] || ROLE_TAGS.indexOf(a[0]) - ROLE_TAGS.indexOf(b[0]))
|
|
26
|
+
.slice(0, limit)
|
|
27
|
+
.map(([role]) => role);
|
|
28
|
+
}
|
|
29
|
+
function formatZoom0Suffix(totalFiles, roles) {
|
|
30
|
+
const count = `(${totalFiles} ${totalFiles === 1 ? 'file' : 'files'})`;
|
|
31
|
+
const dom = dominantRoles(roles);
|
|
32
|
+
return dom.length ? `${count} [${dom.join(', ')}]` : count;
|
|
33
|
+
}
|
|
34
|
+
function renderZoom0(rootName, records, maxDepth) {
|
|
35
|
+
const root = createZoom0Folder(rootName);
|
|
36
|
+
for (const [relPath, record] of records) {
|
|
37
|
+
const parts = relPath.split('/');
|
|
38
|
+
let cur = root;
|
|
39
|
+
const chain = [cur];
|
|
40
|
+
for (const part of parts.slice(0, -1)) {
|
|
41
|
+
if (!cur.folders.has(part))
|
|
42
|
+
cur.folders.set(part, createZoom0Folder(part));
|
|
43
|
+
cur = cur.folders.get(part);
|
|
44
|
+
chain.push(cur);
|
|
45
|
+
}
|
|
46
|
+
cur.ownFiles++;
|
|
47
|
+
for (const f of chain) {
|
|
48
|
+
f.totalFiles++;
|
|
49
|
+
for (const role of record.roles ?? [])
|
|
50
|
+
f.roles.set(role, (f.roles.get(role) ?? 0) + 1);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const lines = [`${rootName}/ ${formatZoom0Suffix(root.totalFiles, root.roles)}`];
|
|
54
|
+
if (records.length === 0) {
|
|
55
|
+
lines.push(' (no files)');
|
|
56
|
+
return lines.join('\n');
|
|
57
|
+
}
|
|
58
|
+
const walk = (folder, depth, prefix) => {
|
|
59
|
+
if (maxDepth !== undefined && depth > maxDepth)
|
|
60
|
+
return;
|
|
61
|
+
const subs = [...folder.folders.values()].sort((a, b) => byName(a.name, b.name));
|
|
62
|
+
for (const sub of subs) {
|
|
63
|
+
lines.push(`${prefix}${sub.name}/ ${formatZoom0Suffix(sub.totalFiles, sub.roles)}`);
|
|
64
|
+
walk(sub, depth + 1, `${prefix} `);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
walk(root, 0, ' ');
|
|
68
|
+
return lines.join('\n');
|
|
69
|
+
}
|
|
70
|
+
export function renderMap(map, options = {}) {
|
|
71
|
+
const zoom = options.zoom ?? 2;
|
|
72
|
+
const folder = (options.folder ?? '').replace(/^\.?\/+|\/+$/g, '');
|
|
73
|
+
const file = map.files.find((f) => f.path === folder);
|
|
74
|
+
if (file)
|
|
75
|
+
return renderFile(file, options);
|
|
76
|
+
const inFolder = map.files.filter((f) => f.language !== 'other' && (!folder || f.path.startsWith(`${folder}/`)));
|
|
77
|
+
if (folder && inFolder.length === 0 && !map.files.some((f) => f.path.startsWith(`${folder}/`))) {
|
|
78
|
+
throw new UnknownFileError(`Not in the code map: ${folder}`);
|
|
79
|
+
}
|
|
80
|
+
const excludeTests = (zoom === 0 || zoom === 1) && !options.includeTests;
|
|
81
|
+
const records = excludeTests ? inFolder.filter((f) => !isTestFile(f.path)) : inFolder;
|
|
82
|
+
const rootName = folder ? path.posix.basename(folder) : map.rootName;
|
|
83
|
+
const entries = records.map((r) => [folder ? r.path.slice(folder.length + 1) : r.path, r]);
|
|
84
|
+
if (zoom === 0)
|
|
85
|
+
return renderZoom0(rootName, entries, options.depth);
|
|
86
|
+
const byPath = new Map(entries);
|
|
87
|
+
return renderTree([...byPath.keys()], {
|
|
88
|
+
rootName,
|
|
89
|
+
maxDepth: options.depth,
|
|
90
|
+
foldersOnly: options.foldersOnly,
|
|
91
|
+
describe: (p) => {
|
|
92
|
+
const record = byPath.get(p);
|
|
93
|
+
if (options.full) {
|
|
94
|
+
const block = fullBlock(record);
|
|
95
|
+
return block.length ? { block } : {};
|
|
96
|
+
}
|
|
97
|
+
if (zoom === 1)
|
|
98
|
+
return { inline: mapSummaryZoom1(record) };
|
|
99
|
+
return { inline: mapSummary(record, options.fields === true) };
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { CodeMap, Topology } from '../model.js';
|
|
2
|
+
/**
|
|
3
|
+
* The files around a change (CIN-08): the targets, their direct imports and importers (tier 2), then what those
|
|
4
|
+
* import (tier 1). Each file comes with its brief (its skeleton), or is counted at its full size when the caller reads
|
|
5
|
+
* contents instead. Without a budget every tier-2 file is taken and tier 1 is only counted; with one, files are taken
|
|
6
|
+
* tier by tier, most central first, while they fit.
|
|
7
|
+
*/
|
|
8
|
+
export interface NeighborhoodEntry {
|
|
9
|
+
path: string;
|
|
10
|
+
tier: 1 | 2 | 3;
|
|
11
|
+
reason: string;
|
|
12
|
+
/** Brief tokens, or raw tokens when contents were asked for. */
|
|
13
|
+
tokens: number;
|
|
14
|
+
/** The file's brief; absent when contents were asked for. */
|
|
15
|
+
brief?: string;
|
|
16
|
+
}
|
|
17
|
+
export interface NeighborhoodOptions {
|
|
18
|
+
/** The most tokens the related files may take; absent: every direct import and importer, and no tier 1. */
|
|
19
|
+
budget?: number;
|
|
20
|
+
/** Count files at their full size, for a caller that reads their contents instead of their briefs. */
|
|
21
|
+
contents?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface Neighborhood {
|
|
24
|
+
targets: NeighborhoodEntry[];
|
|
25
|
+
selected: NeighborhoodEntry[];
|
|
26
|
+
budget?: number;
|
|
27
|
+
selectedTokens: number;
|
|
28
|
+
omitted: {
|
|
29
|
+
files: number;
|
|
30
|
+
tokens: number;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export declare class UnknownFileError extends Error {
|
|
34
|
+
}
|
|
35
|
+
export declare function neighborhood(map: CodeMap, topology: Topology, targetPaths: string[], options?: NeighborhoodOptions): Neighborhood;
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { fileId } from '../graph/build.js';
|
|
2
|
+
export class UnknownFileError extends Error {
|
|
3
|
+
}
|
|
4
|
+
function adjacency(topology) {
|
|
5
|
+
const out = new Map();
|
|
6
|
+
const into = new Map();
|
|
7
|
+
for (const e of topology.edges.filter((e) => e.type === 'imports')) {
|
|
8
|
+
out.set(e.source, [...(out.get(e.source) ?? []), e.target]);
|
|
9
|
+
into.set(e.target, [...(into.get(e.target) ?? []), e.source]);
|
|
10
|
+
}
|
|
11
|
+
const strip = (ids) => (ids ?? []).map((id) => id.slice('file:'.length)).sort();
|
|
12
|
+
return { imports: (p) => strip(out.get(fileId(p))), importers: (p) => strip(into.get(fileId(p))) };
|
|
13
|
+
}
|
|
14
|
+
function candidates(targets, topology) {
|
|
15
|
+
const graph = adjacency(topology);
|
|
16
|
+
const seen = new Map(targets.map((t) => [t, { path: t, tier: 2, reason: 'target' }]));
|
|
17
|
+
const add = (path, tier, reason) => seen.has(path) || seen.set(path, { path, tier, reason });
|
|
18
|
+
for (const t of targets) {
|
|
19
|
+
graph.imports(t).forEach((p) => add(p, 2, `imported by ${t}`));
|
|
20
|
+
graph.importers(t).forEach((p) => add(p, 2, `imports ${t}`));
|
|
21
|
+
}
|
|
22
|
+
const ring = [...seen.values()].filter((c) => c.tier === 2 && c.reason !== 'target');
|
|
23
|
+
for (const c of ring)
|
|
24
|
+
graph.imports(c.path).forEach((p) => add(p, 1, `imported by ${c.path}`));
|
|
25
|
+
return [...seen.values()].filter((c) => c.reason !== 'target');
|
|
26
|
+
}
|
|
27
|
+
function entryOf(record, candidate, contents) {
|
|
28
|
+
if (contents)
|
|
29
|
+
return { ...candidate, tokens: record.rawTokens };
|
|
30
|
+
return { ...candidate, tokens: record.skeletonTokens, brief: record.skeleton };
|
|
31
|
+
}
|
|
32
|
+
export function neighborhood(map, topology, targetPaths, options = {}) {
|
|
33
|
+
const records = new Map(map.files.map((f) => [f.path, f]));
|
|
34
|
+
const targets = targetPaths.map((p) => p.replace(/^\.\//, ''));
|
|
35
|
+
const unknown = targets.filter((t) => !records.has(t));
|
|
36
|
+
if (unknown.length)
|
|
37
|
+
throw new UnknownFileError(`Not in the code map: ${unknown.join(', ')}`);
|
|
38
|
+
const { budget, contents = false } = options;
|
|
39
|
+
const nodes = new Map(topology.nodes.filter((n) => n.type === 'file').map((n) => [n.path, n]));
|
|
40
|
+
const score = (p) => nodes.get(p)?.centralityScore ?? 0;
|
|
41
|
+
const ordered = candidates(targets, topology).sort((a, b) => b.tier - a.tier || score(b.path) - score(a.path) || (a.path < b.path ? -1 : 1));
|
|
42
|
+
const selected = [];
|
|
43
|
+
let used = 0;
|
|
44
|
+
let omittedTokens = 0;
|
|
45
|
+
for (const c of ordered) {
|
|
46
|
+
const entry = entryOf(records.get(c.path), c, contents);
|
|
47
|
+
const fits = budget === undefined ? c.tier === 2 : used + entry.tokens <= budget;
|
|
48
|
+
if (fits) {
|
|
49
|
+
selected.push(entry);
|
|
50
|
+
used += entry.tokens;
|
|
51
|
+
}
|
|
52
|
+
else
|
|
53
|
+
omittedTokens += entry.tokens;
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
targets: targets.map((t) => entryOf(records.get(t), { path: t, tier: 3, reason: 'target' }, contents)),
|
|
57
|
+
selected, ...(budget === undefined ? {} : { budget }), selectedTokens: used,
|
|
58
|
+
omitted: { files: ordered.length - selected.length, tokens: omittedTokens },
|
|
59
|
+
};
|
|
60
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
/**
|
|
3
|
+
* The dependency audit (CIN-09): every dependency a manifest declares, with the files under the manifest's folder
|
|
4
|
+
* that import it. A runtime dependency nothing imports is dormant. Development tools, type packages (`@types/...`)
|
|
5
|
+
* and SDK dependencies are used without an import, so they are never dormant.
|
|
6
|
+
*/
|
|
7
|
+
function folderOf(manifest) {
|
|
8
|
+
const dir = path.posix.dirname(manifest);
|
|
9
|
+
return dir === '.' ? '' : dir;
|
|
10
|
+
}
|
|
11
|
+
/** Whether `file` belongs to the manifest: under its folder and not under a nested manifest of the same name. */
|
|
12
|
+
function belongsTo(file, manifest, sameName) {
|
|
13
|
+
const dir = folderOf(manifest);
|
|
14
|
+
const inside = (d) => d === '' || file.startsWith(`${d}/`);
|
|
15
|
+
if (!inside(dir))
|
|
16
|
+
return false;
|
|
17
|
+
return !sameName.some((other) => other !== manifest && folderOf(other).length > dir.length && inside(folderOf(other)));
|
|
18
|
+
}
|
|
19
|
+
/** The packages each file imports. */
|
|
20
|
+
function usage(records) {
|
|
21
|
+
const byFile = new Map();
|
|
22
|
+
for (const r of records) {
|
|
23
|
+
const packages = r.imports.filter((i) => i.kind === 'package' && i.package).map((i) => i.package);
|
|
24
|
+
if (packages.length)
|
|
25
|
+
byFile.set(r.path, new Set(packages));
|
|
26
|
+
}
|
|
27
|
+
return byFile;
|
|
28
|
+
}
|
|
29
|
+
export function dependencyAudit(records) {
|
|
30
|
+
const manifests = records.filter((r) => r.declares?.length);
|
|
31
|
+
const imports = usage(records);
|
|
32
|
+
return manifests.flatMap((m) => {
|
|
33
|
+
const sameName = manifests.filter((o) => path.posix.basename(o.path) === path.posix.basename(m.path)).map((o) => o.path);
|
|
34
|
+
const files = [...imports.keys()].filter((f) => belongsTo(f, m.path, sameName));
|
|
35
|
+
return m.declares.map((dep) => {
|
|
36
|
+
const using = files.filter((f) => imports.get(f).has(dep.name)).sort();
|
|
37
|
+
const usedImplicitly = dep.dev || dep.sdk || dep.name.startsWith('@types/');
|
|
38
|
+
return { name: dep.name, manifest: m.path, version: dep.version, dev: dep.dev, dormant: !usedImplicitly && using.length === 0, importCount: using.length, files: using };
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|