devlensio 1.2.0 → 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/edges/callEdges.js +77 -3
- package/dist/graph/edges/callEdges.test.js +1 -1
- package/dist/graph/edges/callOrder.test.d.ts +1 -0
- package/dist/graph/edges/callOrder.test.js +227 -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/extractors/components.js +18 -28
- package/dist/parser/extractors/functions.d.ts +12 -3
- package/dist/parser/extractors/functions.js +58 -36
- package/dist/parser/extractors/moduleScope.js +1 -1
- package/dist/parser/moduleScope.test.js +5 -3
- 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 +89 -10
- package/extractors/go/nodes.go +29 -11
- package/extractors/go/parser.go +311 -21
- package/extractors/java/devlens_java_extractor.jar +0 -0
- package/extractors/java/src/devlens/extractor/Extractor.java +36 -10
- package/extractors/java/src/devlens/extractor/Parser.java +387 -0
- package/extractors/java/src/devlens/extractor/edges/Calls.java +66 -10
- package/extractors/python/devlens_extractors_python/edges/calls.py +26 -3
- package/extractors/python/devlens_extractors_python/parser/__init__.py +4 -3
- 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 +82 -8
- 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
|
+
});
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { SyntaxKind } from "ts-morph";
|
|
3
3
|
import { detectFunctionDirective } from "../directives.js";
|
|
4
4
|
import { extractReturnTypeAnnotation, extractReferencedInterfaces, } from "../typeUtils.js";
|
|
5
|
-
import {
|
|
5
|
+
import { classifyCall, createCallAccumulator, finishCalls } from "./functions.js";
|
|
6
6
|
// Generates a unique id for a node
|
|
7
7
|
function makeId(filePath, name) {
|
|
8
8
|
return `${filePath}::${name}`;
|
|
@@ -41,10 +41,13 @@ function extractContextRefs(node) {
|
|
|
41
41
|
}
|
|
42
42
|
return [...new Set(refs)];
|
|
43
43
|
}
|
|
44
|
-
//
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
// Component call sites. The legacy `uses` list keeps its exact shape (external
|
|
45
|
+
// calls only: no hooks, no React.*, no builtins, no calls to the component's own
|
|
46
|
+
// inner functions), while callSites keeps every occurrence in source order for
|
|
47
|
+
// callOrder v1. Calls made inside inner functions or JSX handlers are kept but
|
|
48
|
+
// tagged by scope, never renumbered into the render path.
|
|
49
|
+
function extractComponentCalls(node) {
|
|
50
|
+
const innerFunctionNames = new Set(["React"]);
|
|
48
51
|
for (const varDecl of node.getDescendantsOfKind(SyntaxKind.VariableDeclaration)) {
|
|
49
52
|
const name = varDecl.getName();
|
|
50
53
|
const init = varDecl.getInitializer();
|
|
@@ -54,31 +57,16 @@ function extractAllCalls(node) {
|
|
|
54
57
|
innerFunctionNames.add(name);
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
|
-
// Function declarations like function fn() {}
|
|
58
60
|
for (const fn of node.getDescendantsOfKind(SyntaxKind.FunctionDeclaration)) {
|
|
59
61
|
const name = fn.getName();
|
|
60
62
|
if (name)
|
|
61
63
|
innerFunctionNames.add(name);
|
|
62
64
|
}
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
const expr = call.getExpression();
|
|
67
|
-
const fullExpr = expr.getText();
|
|
68
|
-
const rootName = fullExpr.split(".")[0];
|
|
69
|
-
if (rootName.startsWith("use"))
|
|
70
|
-
continue; // skip hooks
|
|
71
|
-
if (rootName.startsWith("React"))
|
|
72
|
-
continue;
|
|
73
|
-
if (JS_BUILTINS.has(rootName))
|
|
74
|
-
continue;
|
|
75
|
-
if (innerFunctionNames.has(rootName))
|
|
76
|
-
continue; // skip calls to inner functions
|
|
77
|
-
// Capture the full expression (e.g. "axios.get" not just "axios") so
|
|
78
|
-
// callEdges.ts can create a per-method third-party node when needed.
|
|
79
|
-
externalCalls.add(fullExpr);
|
|
65
|
+
const acc = createCallAccumulator({ owner: node, skipRoots: innerFunctionNames });
|
|
66
|
+
for (const call of node.getDescendantsOfKind(SyntaxKind.CallExpression)) {
|
|
67
|
+
classifyCall(call, acc);
|
|
80
68
|
}
|
|
81
|
-
return
|
|
69
|
+
return finishCalls(acc);
|
|
82
70
|
}
|
|
83
71
|
// Checks if a component has any state (useState or useReducer)
|
|
84
72
|
function hasState(hooks) {
|
|
@@ -138,7 +126,7 @@ export function extractComponents(file, fileDirective = null) {
|
|
|
138
126
|
if (!returnsJSX(fn))
|
|
139
127
|
continue;
|
|
140
128
|
const hooks = extractHooks(fn);
|
|
141
|
-
const
|
|
129
|
+
const callData = extractComponentCalls(fn);
|
|
142
130
|
const contextRefs = extractContextRefs(fn);
|
|
143
131
|
const renderingBoundary = detectFunctionDirective(fn.getBody()) ?? fileDirective;
|
|
144
132
|
const propTypes = extractPropTypes(fn, file);
|
|
@@ -154,7 +142,8 @@ export function extractComponents(file, fileDirective = null) {
|
|
|
154
142
|
metadata: {
|
|
155
143
|
hooks,
|
|
156
144
|
contextRefs,
|
|
157
|
-
uses:
|
|
145
|
+
uses: callData.calls,
|
|
146
|
+
callSites: callData.callSites,
|
|
158
147
|
hasState: hasState(hooks),
|
|
159
148
|
exportType: fn.isDefaultExport() ? "default" : "named",
|
|
160
149
|
propTypes,
|
|
@@ -204,7 +193,7 @@ export function extractComponents(file, fileDirective = null) {
|
|
|
204
193
|
if (!returnsJSX(nodeToAnalyze))
|
|
205
194
|
continue;
|
|
206
195
|
const hooks = extractHooks(nodeToAnalyze);
|
|
207
|
-
const
|
|
196
|
+
const callData = extractComponentCalls(nodeToAnalyze);
|
|
208
197
|
const contextRefs = extractContextRefs(nodeToAnalyze);
|
|
209
198
|
const renderingBoundary = detectFunctionDirective(nodeToAnalyze.getKind() === SyntaxKind.ArrowFunction
|
|
210
199
|
? nodeToAnalyze.getBody()
|
|
@@ -229,7 +218,8 @@ export function extractComponents(file, fileDirective = null) {
|
|
|
229
218
|
metadata: {
|
|
230
219
|
hooks,
|
|
231
220
|
contextRefs,
|
|
232
|
-
uses:
|
|
221
|
+
uses: callData.calls,
|
|
222
|
+
callSites: callData.callSites,
|
|
233
223
|
hasState: hasState(hooks),
|
|
234
224
|
exportType: isDefault ? "default" : isExported ? "named" : "none",
|
|
235
225
|
isMemoized: isMemoOrForwardRef,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { SourceFile } from "ts-morph";
|
|
1
|
+
import { SourceFile, type Node } from "ts-morph";
|
|
2
2
|
import type { CallSite, CodeNode } from "../../types.js";
|
|
3
3
|
import { type RenderingBoundary } from "../directives.js";
|
|
4
4
|
export declare const JS_BUILTINS: Set<string>;
|
|
@@ -10,15 +10,24 @@ export interface ExtractedCallData {
|
|
|
10
10
|
dependencyNames: string[];
|
|
11
11
|
contextRefs: string[];
|
|
12
12
|
}
|
|
13
|
+
export interface CallAccumulatorOptions {
|
|
14
|
+
owner: Node;
|
|
15
|
+
isFileOwner?: boolean;
|
|
16
|
+
skipRoots?: Set<string>;
|
|
17
|
+
}
|
|
13
18
|
export interface CallAccumulator {
|
|
19
|
+
owner: Node;
|
|
20
|
+
isFileOwner: boolean;
|
|
21
|
+
skipRoots: Set<string>;
|
|
14
22
|
sites: CallSite[];
|
|
15
23
|
hookCalls: string[];
|
|
16
24
|
apiCalls: string[];
|
|
17
25
|
dependencyNames: string[];
|
|
18
26
|
contextRefs: string[];
|
|
19
|
-
|
|
27
|
+
order: number;
|
|
28
|
+
ordinals: Map<Node, number>;
|
|
20
29
|
}
|
|
21
|
-
export declare function createCallAccumulator(): CallAccumulator;
|
|
30
|
+
export declare function createCallAccumulator(options: CallAccumulatorOptions): CallAccumulator;
|
|
22
31
|
export declare function classifyCall(call: any, acc: CallAccumulator): void;
|
|
23
32
|
export declare function finishCalls(acc: CallAccumulator): ExtractedCallData;
|
|
24
33
|
export declare function extractCallsWithSites(node: any): ExtractedCallData;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { SyntaxKind } from "ts-morph";
|
|
2
|
+
import { annotateCall, isIifeCallee } from "../callFlow.js";
|
|
2
3
|
import { returnsJSX } from "./components.js";
|
|
3
4
|
import { detectFunctionDirective } from "../directives.js";
|
|
4
5
|
import { extractParams, extractReturnTypeAnnotation, extractBareTypeNames, extractReferencedInterfaces, } from "../typeUtils.js";
|
|
@@ -43,23 +44,72 @@ function inferArgType(arg) {
|
|
|
43
44
|
// classifier below.
|
|
44
45
|
const API_HOOK_FAMILIES = new Set(["useQuery", "useMutation", "useInfiniteQuery", "useSuspenseQuery", "useSWR", "useSWRMutation"]);
|
|
45
46
|
const AXIOS_METHODS = new Set(["axios.get", "axios.post", "axios.put", "axios.delete", "axios.patch", "axios"]);
|
|
46
|
-
export function createCallAccumulator() {
|
|
47
|
+
export function createCallAccumulator(options) {
|
|
47
48
|
return {
|
|
49
|
+
owner: options.owner,
|
|
50
|
+
isFileOwner: options.isFileOwner ?? false,
|
|
51
|
+
skipRoots: options.skipRoots ?? new Set(),
|
|
48
52
|
sites: [],
|
|
49
53
|
hookCalls: [],
|
|
50
54
|
apiCalls: [],
|
|
51
55
|
dependencyNames: [],
|
|
52
56
|
contextRefs: [],
|
|
53
|
-
|
|
57
|
+
order: 0,
|
|
58
|
+
ordinals: new Map(),
|
|
54
59
|
};
|
|
55
60
|
}
|
|
61
|
+
function pushSite(call, name, acc, kind) {
|
|
62
|
+
const args = call.getArguments();
|
|
63
|
+
let hasSpread = false;
|
|
64
|
+
const argTypes = [];
|
|
65
|
+
for (const arg of args) {
|
|
66
|
+
if (arg.getKind?.() === SyntaxKind.SpreadElement) {
|
|
67
|
+
hasSpread = true;
|
|
68
|
+
argTypes.push("unknown");
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
argTypes.push(inferArgType(arg) ?? "unknown");
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
const order = ++acc.order;
|
|
75
|
+
acc.ordinals.set(call, order);
|
|
76
|
+
const annotations = annotateCall(call, acc.owner, acc.isFileOwner, acc.ordinals);
|
|
77
|
+
acc.sites.push({
|
|
78
|
+
name,
|
|
79
|
+
argCount: args.length,
|
|
80
|
+
argTypes,
|
|
81
|
+
...(hasSpread && { hasSpread: true }),
|
|
82
|
+
kind,
|
|
83
|
+
order,
|
|
84
|
+
line: call.getStartLineNumber(),
|
|
85
|
+
...annotations,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
56
88
|
// SINGLE traversal over all call expressions producing every call-derived
|
|
57
89
|
// output at once: structured call sites + legacy bare names, custom hook
|
|
58
90
|
// calls, and API-call strings. The per-aspect extractors used to walk the
|
|
59
91
|
// same descendants tree four times per function-like; whole-repo cost is now
|
|
60
92
|
// one walk.
|
|
93
|
+
//
|
|
94
|
+
// callOrder v1: every occurrence is kept (no shape de-duplication), hooks are
|
|
95
|
+
// recorded with kind "hook" so their position is known, and metadata.calls is
|
|
96
|
+
// derived as the ordered, name-deduped, non-hook subset.
|
|
61
97
|
export function classifyCall(call, acc) {
|
|
62
|
-
const
|
|
98
|
+
const expression = call.getExpression?.();
|
|
99
|
+
if (!expression)
|
|
100
|
+
return;
|
|
101
|
+
if (isIifeCallee(expression))
|
|
102
|
+
return;
|
|
103
|
+
const name = expression.getText();
|
|
104
|
+
const rootName = name.split(".")[0];
|
|
105
|
+
if (acc.skipRoots.has(rootName))
|
|
106
|
+
return;
|
|
107
|
+
// API-call families: fetch / axios.* / React Query / SWR
|
|
108
|
+
if (name === "fetch" || AXIOS_METHODS.has(name) || API_HOOK_FAMILIES.has(name)) {
|
|
109
|
+
const args = call.getArguments();
|
|
110
|
+
if (args.length > 0)
|
|
111
|
+
acc.apiCalls.push(`${name}(${args[0].getText()})`);
|
|
112
|
+
}
|
|
63
113
|
// use* family: custom hooks (^use[A-Z]) plus ALL use* names (incl.
|
|
64
114
|
// built-ins like useState) for hook dependency tracking
|
|
65
115
|
if (name.startsWith("use")) {
|
|
@@ -72,44 +122,16 @@ export function classifyCall(call, acc) {
|
|
|
72
122
|
if (ctxArg && !acc.contextRefs.includes(ctxArg.getText()))
|
|
73
123
|
acc.contextRefs.push(ctxArg.getText());
|
|
74
124
|
}
|
|
75
|
-
|
|
76
|
-
// API-call families: fetch / axios.* / React Query / SWR
|
|
77
|
-
if (name === "fetch" || AXIOS_METHODS.has(name) || API_HOOK_FAMILIES.has(name)) {
|
|
78
|
-
const args = call.getArguments();
|
|
79
|
-
if (args.length > 0)
|
|
80
|
-
acc.apiCalls.push(`${name}(${args[0].getText()})`);
|
|
81
|
-
}
|
|
82
|
-
// Plain calls/sites skip hooks and builtin noise
|
|
83
|
-
if (name.startsWith("use"))
|
|
125
|
+
pushSite(call, name, acc, "hook");
|
|
84
126
|
return;
|
|
85
|
-
const rootName = name.split(".")[0];
|
|
86
|
-
if (JS_BUILTINS.has(rootName))
|
|
87
|
-
return;
|
|
88
|
-
const args = call.getArguments();
|
|
89
|
-
let hasSpread = false;
|
|
90
|
-
const argTypes = [];
|
|
91
|
-
for (const arg of args) {
|
|
92
|
-
if (arg.getKind?.() === SyntaxKind.SpreadElement) {
|
|
93
|
-
hasSpread = true;
|
|
94
|
-
argTypes.push("unknown");
|
|
95
|
-
}
|
|
96
|
-
else {
|
|
97
|
-
argTypes.push(inferArgType(arg) ?? "unknown");
|
|
98
|
-
}
|
|
99
127
|
}
|
|
100
|
-
|
|
101
|
-
// name with different arg counts OR arg type shapes are both overload
|
|
102
|
-
// signals (same arity + different literal types can hit different
|
|
103
|
-
// overloads, and each deserves its own edge).
|
|
104
|
-
const key = `${name}/${args.length}/${argTypes.join(",")}`;
|
|
105
|
-
if (acc.seenSites.has(key))
|
|
128
|
+
if (JS_BUILTINS.has(rootName))
|
|
106
129
|
return;
|
|
107
|
-
acc
|
|
108
|
-
acc.sites.push({ name, argCount: args.length, argTypes, ...(hasSpread && { hasSpread: true }) });
|
|
130
|
+
pushSite(call, name, acc, "call");
|
|
109
131
|
}
|
|
110
132
|
export function finishCalls(acc) {
|
|
111
133
|
return {
|
|
112
|
-
calls: [...new Set(acc.sites.map((cs) => cs.name))],
|
|
134
|
+
calls: [...new Set(acc.sites.filter((s) => s.kind !== "hook").map((cs) => cs.name))],
|
|
113
135
|
callSites: acc.sites,
|
|
114
136
|
hookCalls: acc.hookCalls,
|
|
115
137
|
apiCalls: acc.apiCalls,
|
|
@@ -118,7 +140,7 @@ export function finishCalls(acc) {
|
|
|
118
140
|
};
|
|
119
141
|
}
|
|
120
142
|
export function extractCallsWithSites(node) {
|
|
121
|
-
const acc = createCallAccumulator();
|
|
143
|
+
const acc = createCallAccumulator({ owner: node });
|
|
122
144
|
for (const call of node.getDescendantsOfKind(SyntaxKind.CallExpression)) {
|
|
123
145
|
classifyCall(call, acc);
|
|
124
146
|
}
|