devlensio 1.1.1 → 1.4.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/dist/graph/buildLookup.d.ts +30 -0
- package/dist/graph/buildLookup.js +36 -1
- package/dist/graph/edges/aliasChain.d.ts +7 -0
- package/dist/graph/edges/aliasChain.js +63 -0
- package/dist/graph/edges/aliasResolution.test.d.ts +1 -0
- package/dist/graph/edges/aliasResolution.test.js +351 -0
- package/dist/graph/edges/callEdges.js +178 -18
- package/dist/graph/edges/callEdges.test.js +119 -0
- package/dist/graph/edges/callOrder.test.d.ts +1 -0
- package/dist/graph/edges/callOrder.test.js +227 -0
- package/dist/graph/edges/importEdges.js +124 -0
- package/dist/graph/edges/propEdges.js +6 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/parser/callFlow.d.ts +15 -0
- package/dist/parser/callFlow.js +302 -0
- package/dist/parser/callOrder.test.d.ts +1 -0
- package/dist/parser/callOrder.test.js +407 -0
- package/dist/parser/declarationIds.d.ts +23 -0
- package/dist/parser/declarationIds.js +123 -0
- package/dist/parser/exportsMap.js +1 -90
- package/dist/parser/extractors/components.js +18 -28
- package/dist/parser/extractors/functions.d.ts +21 -1
- package/dist/parser/extractors/functions.js +92 -60
- package/dist/parser/extractors/moduleScope.d.ts +8 -0
- package/dist/parser/extractors/moduleScope.js +80 -0
- package/dist/parser/index.js +12 -0
- package/dist/parser/moduleScope.test.d.ts +1 -0
- package/dist/parser/moduleScope.test.js +176 -0
- package/dist/summarizer/index.js +53 -4
- package/dist/summarizer/prompts.js +111 -50
- package/dist/summarizer/prompts.test.d.ts +1 -0
- package/dist/summarizer/prompts.test.js +112 -0
- package/dist/types.d.ts +63 -0
- package/dist/version.d.ts +1 -0
- package/dist/version.js +2 -0
- package/extractors/go/bin/darwin-amd64/devlens_go_extractor +0 -0
- package/extractors/go/bin/darwin-arm64/devlens_go_extractor +0 -0
- package/extractors/go/bin/linux-amd64/devlens_go_extractor +0 -0
- package/extractors/go/bin/linux-arm64/devlens_go_extractor +0 -0
- package/extractors/go/bin/windows-amd64/devlens_go_extractor.exe +0 -0
- package/extractors/go/calls.go +131 -10
- package/extractors/go/nodes.go +29 -11
- package/extractors/go/parser.go +376 -41
- package/extractors/java/devlens_java_extractor.jar +0 -0
- package/extractors/java/src/devlens/extractor/Extractor.java +52 -12
- package/extractors/java/src/devlens/extractor/LookupMaps.java +2 -0
- package/extractors/java/src/devlens/extractor/Parser.java +420 -1
- package/extractors/java/src/devlens/extractor/edges/Calls.java +105 -5
- package/extractors/python/devlens_extractors_python/edges/calls.py +199 -36
- package/extractors/python/devlens_extractors_python/edges/imports.py +21 -1
- package/extractors/python/devlens_extractors_python/lookup.py +9 -0
- package/extractors/python/devlens_extractors_python/parser/__init__.py +11 -1
- package/extractors/python/devlens_extractors_python/parser/functions.py +199 -42
- package/extractors/rust/bin/darwin-amd64/devlens_rust_extractor +0 -0
- package/extractors/rust/bin/darwin-arm64/devlens_rust_extractor +0 -0
- package/extractors/rust/bin/linux-amd64/devlens_rust_extractor +0 -0
- package/extractors/rust/bin/linux-arm64/devlens_rust_extractor +0 -0
- package/extractors/rust/bin/windows-amd64/devlens_rust_extractor.exe +0 -0
- package/extractors/rust/src/calls.rs +114 -8
- package/extractors/rust/src/lookup.rs +66 -0
- package/extractors/rust/src/nodes.rs +29 -0
- package/extractors/rust/src/parser.rs +362 -27
- package/package.json +77 -73
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
import fs from "fs";
|
|
2
|
+
import path from "path";
|
|
3
|
+
import os from "os";
|
|
4
|
+
import { parseRepo } from "./index.js";
|
|
5
|
+
// callOrder/v1 conformance, TypeScript/JavaScript. docs/CALL_ORDER.md §6.
|
|
6
|
+
function createFakeRepo(files) {
|
|
7
|
+
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "devlens-callorder-ts-"));
|
|
8
|
+
for (const [filePath, content] of Object.entries(files)) {
|
|
9
|
+
const fullPath = path.join(tmpDir, filePath);
|
|
10
|
+
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
|
11
|
+
fs.writeFileSync(fullPath, content);
|
|
12
|
+
}
|
|
13
|
+
return tmpDir;
|
|
14
|
+
}
|
|
15
|
+
function deleteFakeRepo(repoPath) {
|
|
16
|
+
fs.rmSync(repoPath, { recursive: true, force: true });
|
|
17
|
+
}
|
|
18
|
+
function nodeOf(nodes, name) {
|
|
19
|
+
const found = nodes.find((n) => n.name === name);
|
|
20
|
+
if (!found)
|
|
21
|
+
throw new Error(`node not found: ${name}`);
|
|
22
|
+
return found;
|
|
23
|
+
}
|
|
24
|
+
function sitesOf(nodes, name) {
|
|
25
|
+
return nodeOf(nodes, name).metadata.callSites ?? [];
|
|
26
|
+
}
|
|
27
|
+
function orderOf(nodes, nodeName, callName) {
|
|
28
|
+
return sitesOf(nodes, nodeName).find((s) => s.name === callName)?.order;
|
|
29
|
+
}
|
|
30
|
+
function parseOne(source, fileName = "main.ts") {
|
|
31
|
+
const repo = createFakeRepo({ [fileName]: source });
|
|
32
|
+
const result = parseRepo(repo);
|
|
33
|
+
deleteFakeRepo(repo);
|
|
34
|
+
return result.nodes;
|
|
35
|
+
}
|
|
36
|
+
function parseTsx(source) {
|
|
37
|
+
return parseOne(source, "main.tsx");
|
|
38
|
+
}
|
|
39
|
+
describe("callOrder — ordering basics", () => {
|
|
40
|
+
it("numbers sequential calls in source order", () => {
|
|
41
|
+
const nodes = parseOne(`
|
|
42
|
+
function a() {}
|
|
43
|
+
function b() {}
|
|
44
|
+
function run() { a(); b(); a(); }
|
|
45
|
+
`);
|
|
46
|
+
expect(sitesOf(nodes, "run").map((s) => [s.name, s.order])).toEqual([
|
|
47
|
+
["a", 1], ["b", 2], ["a", 3],
|
|
48
|
+
]);
|
|
49
|
+
});
|
|
50
|
+
it("keeps one record per occurrence instead of collapsing identical shapes", () => {
|
|
51
|
+
const nodes = parseOne(`
|
|
52
|
+
function f() {}
|
|
53
|
+
function run() { f(); f(); f(); }
|
|
54
|
+
`);
|
|
55
|
+
const sites = sitesOf(nodes, "run");
|
|
56
|
+
expect(sites).toHaveLength(3);
|
|
57
|
+
expect(sites.map((s) => s.order)).toEqual([1, 2, 3]);
|
|
58
|
+
});
|
|
59
|
+
it("keeps same-name calls with different arity apart", () => {
|
|
60
|
+
const nodes = parseOne(`
|
|
61
|
+
function f(a?: number, b?: number) {}
|
|
62
|
+
function run() { f(1); f(1, 2); }
|
|
63
|
+
`);
|
|
64
|
+
expect(sitesOf(nodes, "run").map((s) => [s.argCount, s.order])).toEqual([[1, 1], [2, 2]]);
|
|
65
|
+
});
|
|
66
|
+
it("links a nested call to its enclosing call", () => {
|
|
67
|
+
const nodes = parseOne(`
|
|
68
|
+
function foo(x: any) {}
|
|
69
|
+
function bar() { return 1; }
|
|
70
|
+
function run() { foo(bar()); }
|
|
71
|
+
`);
|
|
72
|
+
const sites = sitesOf(nodes, "run");
|
|
73
|
+
const foo = sites.find((s) => s.name === "foo");
|
|
74
|
+
const bar = sites.find((s) => s.name === "bar");
|
|
75
|
+
expect(foo.order).toBe(1);
|
|
76
|
+
expect(bar.order).toBe(2);
|
|
77
|
+
expect(bar.nested).toBe(foo.order);
|
|
78
|
+
});
|
|
79
|
+
it("chains nested through three levels", () => {
|
|
80
|
+
const nodes = parseOne(`
|
|
81
|
+
function a(x: any) {}
|
|
82
|
+
function b(x: any) {}
|
|
83
|
+
function c() { return 1; }
|
|
84
|
+
function run() { a(b(c())); }
|
|
85
|
+
`);
|
|
86
|
+
const sites = sitesOf(nodes, "run");
|
|
87
|
+
expect(sites.map((s) => s.name)).toEqual(["a", "b", "c"]);
|
|
88
|
+
expect(sites.map((s) => s.order)).toEqual([1, 2, 3]);
|
|
89
|
+
expect(sites[1].nested).toBe(1);
|
|
90
|
+
expect(sites[2].nested).toBe(2);
|
|
91
|
+
});
|
|
92
|
+
it("distinguishes two calls on one line by column", () => {
|
|
93
|
+
const nodes = parseOne(`
|
|
94
|
+
function f() {}
|
|
95
|
+
function run() { f(); f(); }
|
|
96
|
+
`);
|
|
97
|
+
const sites = sitesOf(nodes, "run");
|
|
98
|
+
expect(sites).toHaveLength(2);
|
|
99
|
+
expect(sites[0].line).toBe(sites[1].line);
|
|
100
|
+
expect(sites[0].col).toBeLessThan(sites[1].col);
|
|
101
|
+
});
|
|
102
|
+
it("orders multi-line arguments by source position", () => {
|
|
103
|
+
const nodes = parseOne(`
|
|
104
|
+
function outer(x: any) {}
|
|
105
|
+
function first() { return 1; }
|
|
106
|
+
function second() { return 2; }
|
|
107
|
+
function run() {
|
|
108
|
+
outer(
|
|
109
|
+
first(),
|
|
110
|
+
second()
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
`);
|
|
114
|
+
expect(sitesOf(nodes, "run").map((s) => [s.name, s.order])).toEqual([
|
|
115
|
+
["outer", 1], ["first", 2], ["second", 3],
|
|
116
|
+
]);
|
|
117
|
+
});
|
|
118
|
+
it("records calls in branches and loops without reordering them", () => {
|
|
119
|
+
const nodes = parseOne(`
|
|
120
|
+
function a() {}
|
|
121
|
+
function b() {}
|
|
122
|
+
function c() {}
|
|
123
|
+
function run(flag: boolean) {
|
|
124
|
+
if (flag) { a(); } else { b(); }
|
|
125
|
+
for (let i = 0; i < 3; i++) { c(); }
|
|
126
|
+
}
|
|
127
|
+
`);
|
|
128
|
+
expect(sitesOf(nodes, "run").map((s) => [s.name, s.order])).toEqual([
|
|
129
|
+
["a", 1], ["b", 2], ["c", 3],
|
|
130
|
+
]);
|
|
131
|
+
});
|
|
132
|
+
it("returns an empty list for a node with no calls", () => {
|
|
133
|
+
const nodes = parseOne(`function run() { return 1; }`);
|
|
134
|
+
expect(sitesOf(nodes, "run")).toEqual([]);
|
|
135
|
+
});
|
|
136
|
+
it("records a self-recursive call", () => {
|
|
137
|
+
const nodes = parseOne(`function run() { run(); }`);
|
|
138
|
+
expect(sitesOf(nodes, "run").map((s) => [s.name, s.order])).toEqual([["run", 1]]);
|
|
139
|
+
});
|
|
140
|
+
it("records a call in a default parameter as inline", () => {
|
|
141
|
+
const nodes = parseOne(`
|
|
142
|
+
function g() { return 1; }
|
|
143
|
+
function run(x = g()) { return x; }
|
|
144
|
+
`);
|
|
145
|
+
const sites = sitesOf(nodes, "run");
|
|
146
|
+
expect(sites.map((s) => [s.name, s.scope])).toEqual([["g", "inline"]]);
|
|
147
|
+
});
|
|
148
|
+
it("does not record the IIFE wrapper but records its body", () => {
|
|
149
|
+
const nodes = parseOne(`
|
|
150
|
+
function inner() {}
|
|
151
|
+
function run() { (function () { inner(); })(); }
|
|
152
|
+
`);
|
|
153
|
+
expect(sitesOf(nodes, "run").map((s) => s.name)).toEqual(["inner"]);
|
|
154
|
+
});
|
|
155
|
+
it("records an optional-chain call", () => {
|
|
156
|
+
const nodes = parseOne(`
|
|
157
|
+
function run(obj: any) { obj?.save(); }
|
|
158
|
+
`);
|
|
159
|
+
expect(sitesOf(nodes, "run").map((s) => s.name)).toEqual(["obj?.save"]);
|
|
160
|
+
});
|
|
161
|
+
it("does not let builtins consume an ordinal", () => {
|
|
162
|
+
const nodes = parseOne(`
|
|
163
|
+
function a() {}
|
|
164
|
+
function run() { console.log("x"); a(); }
|
|
165
|
+
`);
|
|
166
|
+
expect(sitesOf(nodes, "run").map((s) => [s.name, s.order])).toEqual([["a", 1]]);
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
describe("callOrder — hooks", () => {
|
|
170
|
+
it("records hooks in the flow but keeps them out of metadata.calls", () => {
|
|
171
|
+
const nodes = parseTsx(`
|
|
172
|
+
import { useState } from "react";
|
|
173
|
+
function useCustom() { return 1; }
|
|
174
|
+
function doWork() {}
|
|
175
|
+
function Panel() {
|
|
176
|
+
const [v] = useState(0);
|
|
177
|
+
doWork();
|
|
178
|
+
const c = useCustom();
|
|
179
|
+
return <div>{v}</div>;
|
|
180
|
+
}
|
|
181
|
+
`);
|
|
182
|
+
const panel = nodeOf(nodes, "Panel");
|
|
183
|
+
const sites = panel.metadata.callSites;
|
|
184
|
+
expect(sites.map((s) => [s.name, s.kind, s.order])).toEqual([
|
|
185
|
+
["useState", "hook", 1],
|
|
186
|
+
["doWork", "call", 2],
|
|
187
|
+
["useCustom", "hook", 3],
|
|
188
|
+
]);
|
|
189
|
+
expect(panel.metadata.hooks).toEqual(["useState", "useCustom"]);
|
|
190
|
+
expect(panel.metadata.uses).toEqual(["doWork"]);
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
describe("callOrder — execution regions", () => {
|
|
194
|
+
const source = `
|
|
195
|
+
import { useEffect } from "react";
|
|
196
|
+
declare function computeTotal(v: any): number;
|
|
197
|
+
declare function fetchRows(): void;
|
|
198
|
+
declare function apiPost(v: any): void;
|
|
199
|
+
declare function save(v: any): void;
|
|
200
|
+
declare function formatMoney(v: any): string;
|
|
201
|
+
|
|
202
|
+
function Table({ items }: { items: number[] }) {
|
|
203
|
+
const total = computeTotal(items);
|
|
204
|
+
useEffect(() => { fetchRows(); }, []);
|
|
205
|
+
function handleSave() { apiPost(total); }
|
|
206
|
+
return <button onClick={() => save(total)}>{formatMoney(total)}</button>;
|
|
207
|
+
}
|
|
208
|
+
`;
|
|
209
|
+
it("tags render-body calls inline", () => {
|
|
210
|
+
const nodes = parseTsx(source);
|
|
211
|
+
const site = sitesOf(nodes, "Table").find((s) => s.name === "computeTotal");
|
|
212
|
+
expect(site.scope).toBe("inline");
|
|
213
|
+
expect(site.order).toBe(1);
|
|
214
|
+
});
|
|
215
|
+
it("tags a JSX handler call as a handler and never as render order", () => {
|
|
216
|
+
const nodes = parseTsx(source);
|
|
217
|
+
const site = sitesOf(nodes, "Table").find((s) => s.name === "save");
|
|
218
|
+
expect(site.scope).toBe("handler");
|
|
219
|
+
expect(site.defer).toBe("event");
|
|
220
|
+
});
|
|
221
|
+
it("tags a useEffect callback call as postRender", () => {
|
|
222
|
+
const nodes = parseTsx(source);
|
|
223
|
+
const site = sitesOf(nodes, "Table").find((s) => s.name === "fetchRows");
|
|
224
|
+
expect(site.scope).toBe("callback");
|
|
225
|
+
expect(site.defer).toBe("postRender");
|
|
226
|
+
});
|
|
227
|
+
it("tags a call inside an inner function as nested", () => {
|
|
228
|
+
const nodes = parseTsx(source);
|
|
229
|
+
const site = sitesOf(nodes, "Table").find((s) => s.name === "apiPost");
|
|
230
|
+
expect(site.scope).toBe("nested");
|
|
231
|
+
});
|
|
232
|
+
it("tags a call in JSX children (not an attribute) as inline", () => {
|
|
233
|
+
const nodes = parseTsx(source);
|
|
234
|
+
const site = sitesOf(nodes, "Table").find((s) => s.name === "formatMoney");
|
|
235
|
+
expect(site.scope).toBe("inline");
|
|
236
|
+
});
|
|
237
|
+
it("keeps one monotone sequence across all regions", () => {
|
|
238
|
+
const nodes = parseTsx(source);
|
|
239
|
+
const orders = sitesOf(nodes, "Table").map((s) => s.order);
|
|
240
|
+
expect(orders).toEqual([...orders].sort((a, b) => a - b));
|
|
241
|
+
expect(new Set(orders).size).toBe(orders.length);
|
|
242
|
+
});
|
|
243
|
+
it("tags module-scope calls on the FILE node", () => {
|
|
244
|
+
const nodes = parseOne(`
|
|
245
|
+
import { startAll } from "./start";
|
|
246
|
+
const running = startAll();
|
|
247
|
+
`);
|
|
248
|
+
const file = nodes.find((n) => n.id === "file::main.ts");
|
|
249
|
+
const sites = file.metadata.callSites;
|
|
250
|
+
expect(sites[0].scope).toBe("module");
|
|
251
|
+
expect(sites[0].order).toBe(1);
|
|
252
|
+
});
|
|
253
|
+
it("tags an iteration callback as a repeated callback", () => {
|
|
254
|
+
const nodes = parseOne(`
|
|
255
|
+
function save(x: number) {}
|
|
256
|
+
function run(items: number[]) { items.map((x) => save(x)); }
|
|
257
|
+
`);
|
|
258
|
+
const site = sitesOf(nodes, "run").find((s) => s.name === "save");
|
|
259
|
+
expect(site.scope).toBe("callback");
|
|
260
|
+
expect(site.repeat).toBe(true);
|
|
261
|
+
expect(site.defer).toBeUndefined();
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
describe("callOrder — async and concurrency markers", () => {
|
|
265
|
+
it("marks an awaited call and keeps the following order", () => {
|
|
266
|
+
const nodes = parseOne(`
|
|
267
|
+
declare function fetchUser(): Promise<number>;
|
|
268
|
+
declare function saveUser(v: number): void;
|
|
269
|
+
async function load() { const u = await fetchUser(); saveUser(u); }
|
|
270
|
+
`);
|
|
271
|
+
const sites = sitesOf(nodes, "load");
|
|
272
|
+
expect(sites[0]).toMatchObject({ name: "fetchUser", order: 1, awaited: true });
|
|
273
|
+
expect(sites[1]).toMatchObject({ name: "saveUser", order: 2 });
|
|
274
|
+
expect(sites[1].awaited).toBeUndefined();
|
|
275
|
+
});
|
|
276
|
+
it("marks a timer callback", () => {
|
|
277
|
+
const nodes = parseOne(`
|
|
278
|
+
declare function logLater(): void;
|
|
279
|
+
function run() { setTimeout(() => logLater(), 0); }
|
|
280
|
+
`);
|
|
281
|
+
expect(sitesOf(nodes, "run")).toEqual([
|
|
282
|
+
expect.objectContaining({ name: "logLater", scope: "callback", defer: "timer" }),
|
|
283
|
+
]);
|
|
284
|
+
});
|
|
285
|
+
it("marks a promise continuation", () => {
|
|
286
|
+
const nodes = parseOne(`
|
|
287
|
+
declare function after(): void;
|
|
288
|
+
declare const p: Promise<number>;
|
|
289
|
+
function run() { p.then(() => after()); }
|
|
290
|
+
`);
|
|
291
|
+
expect(sitesOf(nodes, "run").find((s) => s.name === "after")).toEqual(expect.objectContaining({ name: "after", scope: "callback", defer: "promise" }));
|
|
292
|
+
});
|
|
293
|
+
it("marks a microtask callback", () => {
|
|
294
|
+
const nodes = parseOne(`
|
|
295
|
+
declare function after(): void;
|
|
296
|
+
function run() { queueMicrotask(() => after()); }
|
|
297
|
+
`);
|
|
298
|
+
expect(sitesOf(nodes, "run").find((s) => s.name === "after")).toEqual(expect.objectContaining({ name: "after", scope: "callback", defer: "microtask" }));
|
|
299
|
+
});
|
|
300
|
+
it("marks an event listener callback", () => {
|
|
301
|
+
const nodes = parseOne(`
|
|
302
|
+
declare function onClick(): void;
|
|
303
|
+
declare const el: any;
|
|
304
|
+
function run() { el.addEventListener("click", () => onClick()); }
|
|
305
|
+
`);
|
|
306
|
+
expect(sitesOf(nodes, "run").find((s) => s.name === "onClick")).toEqual(expect.objectContaining({ name: "onClick", scope: "callback", defer: "event" }));
|
|
307
|
+
});
|
|
308
|
+
it("marks Promise.all members as concurrent", () => {
|
|
309
|
+
const nodes = parseOne(`
|
|
310
|
+
declare function a(): Promise<number>;
|
|
311
|
+
declare function b(): Promise<number>;
|
|
312
|
+
async function run() { await Promise.all([a(), b()]); }
|
|
313
|
+
`);
|
|
314
|
+
const sites = sitesOf(nodes, "run");
|
|
315
|
+
expect(sites.map((s) => [s.name, s.order, s.conc])).toEqual([
|
|
316
|
+
["a", 1, true], ["b", 2, true],
|
|
317
|
+
]);
|
|
318
|
+
});
|
|
319
|
+
it("does not claim detachment for an unrecognised callback helper", () => {
|
|
320
|
+
const nodes = parseOne(`
|
|
321
|
+
declare function after(): void;
|
|
322
|
+
declare const sched: any;
|
|
323
|
+
function run() { sched.runLater(() => after()); }
|
|
324
|
+
`);
|
|
325
|
+
const site = sitesOf(nodes, "run").find((s) => s.name === "after");
|
|
326
|
+
expect(site.scope).toBe("callback");
|
|
327
|
+
expect(site.defer).toBeUndefined();
|
|
328
|
+
});
|
|
329
|
+
it("marks branch and loop sites", () => {
|
|
330
|
+
const nodes = parseOne(`
|
|
331
|
+
declare function a(): void;
|
|
332
|
+
declare function b(): void;
|
|
333
|
+
function run(flag: boolean) {
|
|
334
|
+
if (flag) a();
|
|
335
|
+
while (flag) b();
|
|
336
|
+
}
|
|
337
|
+
`);
|
|
338
|
+
const sites = sitesOf(nodes, "run");
|
|
339
|
+
expect(sites.find((s) => s.name === "a").maybe).toBe(true);
|
|
340
|
+
expect(sites.find((s) => s.name === "b").repeat).toBe(true);
|
|
341
|
+
});
|
|
342
|
+
it("does not mark a call in an if-condition as conditional", () => {
|
|
343
|
+
const nodes = parseOne(`
|
|
344
|
+
declare function a(): boolean;
|
|
345
|
+
declare function b(): void;
|
|
346
|
+
function run() { if (a()) b(); }
|
|
347
|
+
`);
|
|
348
|
+
const sites = sitesOf(nodes, "run");
|
|
349
|
+
expect(sites.find((s) => s.name === "a").maybe).toBeUndefined();
|
|
350
|
+
expect(sites.find((s) => s.name === "b").maybe).toBe(true);
|
|
351
|
+
});
|
|
352
|
+
it("marks a call inside finally as onReturn", () => {
|
|
353
|
+
const nodes = parseOne(`
|
|
354
|
+
declare function cleanup(): void;
|
|
355
|
+
function run() { try { work(); } finally { cleanup(); } }
|
|
356
|
+
declare function work(): void;
|
|
357
|
+
`);
|
|
358
|
+
expect(sitesOf(nodes, "run").find((s) => s.name === "cleanup")).toEqual(expect.objectContaining({ name: "cleanup", defer: "onReturn" }));
|
|
359
|
+
});
|
|
360
|
+
});
|
|
361
|
+
describe("callOrder — invariants", () => {
|
|
362
|
+
const source = `
|
|
363
|
+
declare function alpha(x?: any): void;
|
|
364
|
+
declare function beta(): number;
|
|
365
|
+
declare function gamma(): void;
|
|
366
|
+
function run(items: number[], flag: boolean) {
|
|
367
|
+
alpha(beta());
|
|
368
|
+
if (flag) { gamma(); }
|
|
369
|
+
for (const i of items) alpha(i);
|
|
370
|
+
}
|
|
371
|
+
`;
|
|
372
|
+
it("keeps orders contiguous, unique and monotone by position", () => {
|
|
373
|
+
const nodes = parseOne(source);
|
|
374
|
+
const sites = sitesOf(nodes, "run");
|
|
375
|
+
expect(sites.map((s) => s.order)).toEqual([1, 2, 3, 4]);
|
|
376
|
+
for (let i = 1; i < sites.length; i++) {
|
|
377
|
+
const prev = sites[i - 1];
|
|
378
|
+
const cur = sites[i];
|
|
379
|
+
expect(cur.order).toBeGreaterThan(prev.order);
|
|
380
|
+
expect(cur.line > prev.line || (cur.line === prev.line && cur.col > prev.col)).toBe(true);
|
|
381
|
+
}
|
|
382
|
+
});
|
|
383
|
+
it("only points nested at an existing lower order", () => {
|
|
384
|
+
const nodes = parseOne(source);
|
|
385
|
+
const sites = sitesOf(nodes, "run");
|
|
386
|
+
const orders = new Set(sites.map((s) => s.order));
|
|
387
|
+
for (const site of sites) {
|
|
388
|
+
if (site.nested === undefined)
|
|
389
|
+
continue;
|
|
390
|
+
expect(orders.has(site.nested)).toBe(true);
|
|
391
|
+
expect(site.nested).toBeLessThan(site.order);
|
|
392
|
+
}
|
|
393
|
+
});
|
|
394
|
+
it("keeps metadata.calls identical as a set", () => {
|
|
395
|
+
const nodes = parseOne(source);
|
|
396
|
+
expect(nodeOf(nodes, "run").metadata.calls).toEqual(["alpha", "beta", "gamma"]);
|
|
397
|
+
});
|
|
398
|
+
it("gives every site a line, a column and a scope", () => {
|
|
399
|
+
const nodes = parseOne(source);
|
|
400
|
+
for (const site of sitesOf(nodes, "run")) {
|
|
401
|
+
expect(typeof site.line).toBe("number");
|
|
402
|
+
expect(typeof site.col).toBe("number");
|
|
403
|
+
expect(site.scope).toBeDefined();
|
|
404
|
+
expect(site.kind).toBe("call");
|
|
405
|
+
}
|
|
406
|
+
});
|
|
407
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Node } from "ts-morph";
|
|
2
|
+
import type { CodeNode } from "../types.js";
|
|
3
|
+
/** base id (`file::name`, no overload suffix) → every node sharing that base */
|
|
4
|
+
export type NodeIndex = Map<string, CodeNode[]>;
|
|
5
|
+
export declare function buildNodeIndex(nodes: CodeNode[]): NodeIndex;
|
|
6
|
+
/** Repo-relative path of a declaration's source file, posix separators. */
|
|
7
|
+
export declare function relativePathOf(decl: Node, repoPath: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Name of the declaration behind an exported name.
|
|
10
|
+
* Prefers the declaration's own `getName()` (functions, classes, variables) and
|
|
11
|
+
* falls back to the symbol name. Returns undefined for anonymous declarations —
|
|
12
|
+
* those have no node and are therefore not linkable.
|
|
13
|
+
*/
|
|
14
|
+
export declare function declarationName(decl: Node): string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* Map an exported declaration to the node it produced.
|
|
17
|
+
* `base` is the collection key (useful for ambiguity detection: two different
|
|
18
|
+
* bases under one exported name means the name denotes more than one symbol).
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolveDeclarationToNode(decl: Node, repoPath: string, nodeIndex: NodeIndex): {
|
|
21
|
+
id: string;
|
|
22
|
+
base: string;
|
|
23
|
+
} | undefined;
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Declaration → nodeId resolution (shared by the exports map and the
|
|
2
|
+
// import/export binding table).
|
|
3
|
+
//
|
|
4
|
+
// Node ids are `relativePath::name`, but two constructs break the naive
|
|
5
|
+
// concatenation:
|
|
6
|
+
// 1. OVERLOADS — `parser/overloads.ts` rewrites colliding ids to
|
|
7
|
+
// `path::name#arity` / `path::name#arity#sig8`, so a declaration cannot be
|
|
8
|
+
// mapped by name alone. The node index is keyed by the BASE id
|
|
9
|
+
// (`path::name`) and holds every suffixed node; the overload with the
|
|
10
|
+
// matching parameter signature wins.
|
|
11
|
+
// 2. ALIASING — the name a caller writes is not always the name a declaration
|
|
12
|
+
// has. That is the caller's problem (see `graph/edges/callEdges.ts`); this
|
|
13
|
+
// module always reports the ORIGIN declaration's id.
|
|
14
|
+
//
|
|
15
|
+
// Everything here works on ts-morph declaration nodes, so it must run while a
|
|
16
|
+
// Project is alive — i.e. inside `parseRepo()` (exports map) or
|
|
17
|
+
// `detectImportEdges()` (export bindings). Both call it through this single
|
|
18
|
+
// implementation so overloaded ids are generated in exactly one place.
|
|
19
|
+
import path from "path";
|
|
20
|
+
export function buildNodeIndex(nodes) {
|
|
21
|
+
const index = new Map();
|
|
22
|
+
for (const node of nodes) {
|
|
23
|
+
const base = `${node.filePath}::${node.name}`;
|
|
24
|
+
if (!index.has(base))
|
|
25
|
+
index.set(base, []);
|
|
26
|
+
index.get(base).push(node);
|
|
27
|
+
}
|
|
28
|
+
return index;
|
|
29
|
+
}
|
|
30
|
+
/** Repo-relative path of a declaration's source file, posix separators. */
|
|
31
|
+
export function relativePathOf(decl, repoPath) {
|
|
32
|
+
return path.relative(repoPath, decl.getSourceFile().getFilePath()).replace(/\\/g, "/");
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Name of the declaration behind an exported name.
|
|
36
|
+
* Prefers the declaration's own `getName()` (functions, classes, variables) and
|
|
37
|
+
* falls back to the symbol name. Returns undefined for anonymous declarations —
|
|
38
|
+
* those have no node and are therefore not linkable.
|
|
39
|
+
*/
|
|
40
|
+
export function declarationName(decl) {
|
|
41
|
+
const getName = decl.getName;
|
|
42
|
+
if (typeof getName === "function") {
|
|
43
|
+
try {
|
|
44
|
+
const own = getName.call(decl);
|
|
45
|
+
if (typeof own === "string" && own.length > 0)
|
|
46
|
+
return own;
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
// fall through to symbol
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
const sym = decl.getSymbol()?.getName();
|
|
53
|
+
if (sym && sym !== "default" && !sym.startsWith("__"))
|
|
54
|
+
return sym;
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Map an exported declaration to the node it produced.
|
|
59
|
+
* `base` is the collection key (useful for ambiguity detection: two different
|
|
60
|
+
* bases under one exported name means the name denotes more than one symbol).
|
|
61
|
+
*/
|
|
62
|
+
export function resolveDeclarationToNode(decl, repoPath, nodeIndex) {
|
|
63
|
+
const name = declarationName(decl);
|
|
64
|
+
if (!name)
|
|
65
|
+
return undefined;
|
|
66
|
+
const base = `${relativePathOf(decl, repoPath)}::${name}`;
|
|
67
|
+
const candidates = nodeIndex.get(base);
|
|
68
|
+
if (!candidates || candidates.length === 0)
|
|
69
|
+
return undefined;
|
|
70
|
+
if (candidates.length === 1)
|
|
71
|
+
return { id: candidates[0].id, base };
|
|
72
|
+
const match = matchOverloadedNode(decl, candidates);
|
|
73
|
+
return match ? { id: match.id, base } : undefined;
|
|
74
|
+
}
|
|
75
|
+
function matchOverloadedNode(decl, candidates) {
|
|
76
|
+
const paramTypes = declarationParamTypes(decl);
|
|
77
|
+
if (paramTypes === undefined)
|
|
78
|
+
return undefined;
|
|
79
|
+
const declKey = signatureKeyFromTypes(paramTypes);
|
|
80
|
+
const matches = candidates.filter((node) => {
|
|
81
|
+
const params = node.metadata?.parameters;
|
|
82
|
+
if (!Array.isArray(params))
|
|
83
|
+
return false;
|
|
84
|
+
return (signatureKeyFromNodeParams(params) === declKey);
|
|
85
|
+
});
|
|
86
|
+
return matches.length >= 1 ? matches[0] : undefined;
|
|
87
|
+
}
|
|
88
|
+
function declarationParamTypes(decl) {
|
|
89
|
+
const anyDecl = decl;
|
|
90
|
+
if (typeof anyDecl.getParameters !== "function")
|
|
91
|
+
return undefined;
|
|
92
|
+
try {
|
|
93
|
+
const params = anyDecl.getParameters();
|
|
94
|
+
if (!Array.isArray(params))
|
|
95
|
+
return undefined;
|
|
96
|
+
return params.map((p) => {
|
|
97
|
+
const t = p.getTypeNode?.()?.getText() ?? "unknown";
|
|
98
|
+
if (p.isRest?.())
|
|
99
|
+
return `...${t}`;
|
|
100
|
+
if (p.isOptional?.())
|
|
101
|
+
return `${t}?`;
|
|
102
|
+
return t;
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function signatureKeyFromTypes(types) {
|
|
110
|
+
return `(${types.join(",")})`;
|
|
111
|
+
}
|
|
112
|
+
function signatureKeyFromNodeParams(params) {
|
|
113
|
+
return `(${params
|
|
114
|
+
.map((p) => {
|
|
115
|
+
const t = typeof p?.type === "string" && p.type.length > 0 ? p.type : "unknown";
|
|
116
|
+
if (p?.isRest)
|
|
117
|
+
return `...${t}`;
|
|
118
|
+
if (p?.isOptional)
|
|
119
|
+
return `${t}?`;
|
|
120
|
+
return t;
|
|
121
|
+
})
|
|
122
|
+
.join(",")})`;
|
|
123
|
+
}
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
// - Failures never break analysis: any error yields an absent map.
|
|
25
25
|
import path from "path";
|
|
26
26
|
import fs from "fs";
|
|
27
|
+
import { buildNodeIndex, resolveDeclarationToNode, } from "./declarationIds.js";
|
|
27
28
|
const ENTRY_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx"];
|
|
28
29
|
const INDEX_NAMES = ["index.ts", "index.tsx", "index.js", "index.jsx"];
|
|
29
30
|
export function buildExportsMap(project, repoPath, nodes) {
|
|
@@ -151,16 +152,6 @@ function starContributors(sf, cache) {
|
|
|
151
152
|
cache.set(key, result);
|
|
152
153
|
return result;
|
|
153
154
|
}
|
|
154
|
-
function buildNodeIndex(nodes) {
|
|
155
|
-
const index = new Map();
|
|
156
|
-
for (const node of nodes) {
|
|
157
|
-
const base = `${node.filePath}::${node.name}`;
|
|
158
|
-
if (!index.has(base))
|
|
159
|
-
index.set(base, []);
|
|
160
|
-
index.get(base).push(node);
|
|
161
|
-
}
|
|
162
|
-
return index;
|
|
163
|
-
}
|
|
164
155
|
function resolveEntryPoints(pkg, repoPath, project) {
|
|
165
156
|
const entries = [];
|
|
166
157
|
if (pkg.exports !== undefined) {
|
|
@@ -246,86 +237,6 @@ function resolveTargetFile(repoPath, target, project) {
|
|
|
246
237
|
}
|
|
247
238
|
return undefined;
|
|
248
239
|
}
|
|
249
|
-
function resolveDeclarationToNode(decl, repoPath, nodeIndex) {
|
|
250
|
-
const name = declarationName(decl);
|
|
251
|
-
if (!name)
|
|
252
|
-
return undefined;
|
|
253
|
-
const relPath = path.relative(repoPath, decl.getSourceFile().getFilePath()).replace(/\\/g, "/");
|
|
254
|
-
const base = `${relPath}::${name}`;
|
|
255
|
-
const candidates = nodeIndex.get(base);
|
|
256
|
-
if (!candidates || candidates.length === 0)
|
|
257
|
-
return undefined;
|
|
258
|
-
if (candidates.length === 1)
|
|
259
|
-
return { id: candidates[0].id, base };
|
|
260
|
-
const match = matchOverloadedNode(decl, candidates);
|
|
261
|
-
return match ? { id: match.id, base } : undefined;
|
|
262
|
-
}
|
|
263
|
-
function declarationName(decl) {
|
|
264
|
-
const getName = decl.getName;
|
|
265
|
-
if (typeof getName === "function") {
|
|
266
|
-
try {
|
|
267
|
-
const own = getName.call(decl);
|
|
268
|
-
if (typeof own === "string" && own.length > 0)
|
|
269
|
-
return own;
|
|
270
|
-
}
|
|
271
|
-
catch {
|
|
272
|
-
// fall through to symbol
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
const sym = decl.getSymbol()?.getName();
|
|
276
|
-
if (sym && sym !== "default" && !sym.startsWith("__"))
|
|
277
|
-
return sym;
|
|
278
|
-
return undefined;
|
|
279
|
-
}
|
|
280
|
-
function matchOverloadedNode(decl, candidates) {
|
|
281
|
-
const paramTypes = declarationParamTypes(decl);
|
|
282
|
-
if (paramTypes === undefined)
|
|
283
|
-
return undefined;
|
|
284
|
-
const declKey = signatureKeyFromTypes(paramTypes);
|
|
285
|
-
const matches = candidates.filter((node) => {
|
|
286
|
-
const params = node.metadata?.parameters;
|
|
287
|
-
if (!Array.isArray(params))
|
|
288
|
-
return false;
|
|
289
|
-
return signatureKeyFromNodeParams(params) === declKey;
|
|
290
|
-
});
|
|
291
|
-
return matches.length >= 1 ? matches[0] : undefined;
|
|
292
|
-
}
|
|
293
|
-
function declarationParamTypes(decl) {
|
|
294
|
-
const anyDecl = decl;
|
|
295
|
-
if (typeof anyDecl.getParameters !== "function")
|
|
296
|
-
return undefined;
|
|
297
|
-
try {
|
|
298
|
-
const params = anyDecl.getParameters();
|
|
299
|
-
if (!Array.isArray(params))
|
|
300
|
-
return undefined;
|
|
301
|
-
return params.map((p) => {
|
|
302
|
-
const t = p.getTypeNode?.()?.getText() ?? "unknown";
|
|
303
|
-
if (p.isRest?.())
|
|
304
|
-
return `...${t}`;
|
|
305
|
-
if (p.isOptional?.())
|
|
306
|
-
return `${t}?`;
|
|
307
|
-
return t;
|
|
308
|
-
});
|
|
309
|
-
}
|
|
310
|
-
catch {
|
|
311
|
-
return undefined;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
function signatureKeyFromTypes(types) {
|
|
315
|
-
return `(${types.join(",")})`;
|
|
316
|
-
}
|
|
317
|
-
function signatureKeyFromNodeParams(params) {
|
|
318
|
-
return `(${params
|
|
319
|
-
.map((p) => {
|
|
320
|
-
const t = typeof p?.type === "string" && p.type.length > 0 ? p.type : "unknown";
|
|
321
|
-
if (p?.isRest)
|
|
322
|
-
return `...${t}`;
|
|
323
|
-
if (p?.isOptional)
|
|
324
|
-
return `${t}?`;
|
|
325
|
-
return t;
|
|
326
|
-
})
|
|
327
|
-
.join(",")})`;
|
|
328
|
-
}
|
|
329
240
|
export function validateExportsMap(map, nodeIds) {
|
|
330
241
|
const dangling = [];
|
|
331
242
|
for (const perPath of Object.values(map.exports)) {
|