devlensio 1.1.1 → 1.2.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 +101 -15
- package/dist/graph/edges/callEdges.test.js +119 -0
- package/dist/graph/edges/importEdges.js +124 -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/functions.d.ts +11 -0
- package/dist/parser/extractors/functions.js +69 -59
- 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 +174 -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 +45 -3
- package/extractors/go/parser.go +81 -36
- package/extractors/java/devlens_java_extractor.jar +0 -0
- package/extractors/java/src/devlens/extractor/Extractor.java +16 -2
- package/extractors/java/src/devlens/extractor/LookupMaps.java +2 -0
- package/extractors/java/src/devlens/extractor/Parser.java +33 -1
- package/extractors/java/src/devlens/extractor/edges/Calls.java +44 -0
- package/extractors/python/devlens_extractors_python/edges/calls.py +173 -33
- 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 +10 -1
- 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 +32 -0
- package/extractors/rust/src/lookup.rs +66 -0
- package/package.json +1 -1
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { closestByPath } from "./utils.js";
|
|
2
|
+
import { buildAliasChain } from "./aliasChain.js";
|
|
2
3
|
// Arity compatibility: a candidate matches when the call provides at least all
|
|
3
4
|
// required (non-optional, non-rest) params, and does not exceed the declared
|
|
4
5
|
// count unless the candidate declares a rest parameter. Candidates without
|
|
@@ -88,12 +89,21 @@ export function detectCallEdges(nodes, lookupMp) {
|
|
|
88
89
|
const resolvedCallsMap = new Map();
|
|
89
90
|
// Lazily-created method nodes for default/namespace import member-access calls
|
|
90
91
|
const createdMethodNodes = new Map();
|
|
92
|
+
// id → node, for turning resolved export bindings back into candidates
|
|
93
|
+
const nodesById = new Map();
|
|
94
|
+
for (const n of nodes)
|
|
95
|
+
nodesById.set(n.id, n);
|
|
96
|
+
// memoized alias chains (provenance only; see aliasChain.ts)
|
|
97
|
+
const aliasChainCache = new Map();
|
|
91
98
|
for (const node of nodes) {
|
|
92
|
-
//
|
|
99
|
+
// FILE is a caller for module-scope execution (calls executed at file
|
|
100
|
+
// load, attached by parser/extractors/moduleScope.ts). TEST/STORY are
|
|
101
|
+
// leaves and deliberately excluded.
|
|
93
102
|
if (node.type !== "FUNCTION" &&
|
|
94
103
|
node.type !== "HOOK" &&
|
|
95
104
|
node.type !== "COMPONENT" &&
|
|
96
|
-
node.type !== "METHOD"
|
|
105
|
+
node.type !== "METHOD" &&
|
|
106
|
+
node.type !== "FILE")
|
|
97
107
|
continue;
|
|
98
108
|
const calls = node.metadata.calls;
|
|
99
109
|
const uses = node.metadata.uses;
|
|
@@ -174,7 +184,11 @@ export function detectCallEdges(nodes, lookupMp) {
|
|
|
174
184
|
from: node.id,
|
|
175
185
|
to: tpNodeId,
|
|
176
186
|
type: "CALLS",
|
|
177
|
-
metadata: {
|
|
187
|
+
metadata: {
|
|
188
|
+
calledName,
|
|
189
|
+
isThirdParty: true,
|
|
190
|
+
...(node.type === "FILE" ? { scope: "module" } : {}),
|
|
191
|
+
},
|
|
178
192
|
});
|
|
179
193
|
}
|
|
180
194
|
continue;
|
|
@@ -194,18 +208,66 @@ export function detectCallEdges(nodes, lookupMp) {
|
|
|
194
208
|
// continue;
|
|
195
209
|
// }
|
|
196
210
|
// ── Local node lookup ─────────────────────────────────────────
|
|
197
|
-
//
|
|
198
|
-
//
|
|
199
|
-
//
|
|
200
|
-
|
|
211
|
+
// Tier 2 — module-scoped (binding-aware). The import resolved to a
|
|
212
|
+
// file, and that file's export table already points at the ORIGIN
|
|
213
|
+
// declaration: TypeScript's checker followed every `export … from`,
|
|
214
|
+
// `export *` and `export { a as b }` when the table was built. Two
|
|
215
|
+
// map reads, independent of chain depth.
|
|
216
|
+
let targets;
|
|
201
217
|
let resolvedName = calledName;
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
218
|
+
let aliasChain;
|
|
219
|
+
const rootName = calledName.split(".")[0];
|
|
220
|
+
const importTarget = lookupMp.importTargets.get(node.filePath)?.get(rootName);
|
|
221
|
+
if (importTarget) {
|
|
222
|
+
const suffix = calledName.slice(rootName.length); // "" or ".get"
|
|
223
|
+
let exportName = importTarget.importedName;
|
|
224
|
+
let memberSuffix = suffix;
|
|
225
|
+
if (importTarget.kind === "namespace") {
|
|
226
|
+
// `import * as ns` + `ns.member()` → the member is the export
|
|
227
|
+
const member = suffix.startsWith(".") ? suffix.slice(1) : "";
|
|
228
|
+
exportName = member.split(".")[0];
|
|
229
|
+
memberSuffix = member.slice(exportName.length);
|
|
230
|
+
}
|
|
231
|
+
if (exportName) {
|
|
232
|
+
const ids = lookupMp.exportBindings.get(importTarget.targetFile)?.get(exportName);
|
|
233
|
+
if (ids && ids.length > 0) {
|
|
234
|
+
if (memberSuffix) {
|
|
235
|
+
// Member access on an imported class/object: nodes are
|
|
236
|
+
// dotted (`file::Class.method`). Accept only ids that
|
|
237
|
+
// exist — never invent an edge to the container.
|
|
238
|
+
const memberIds = ids
|
|
239
|
+
.map((id) => `${id}${memberSuffix}`)
|
|
240
|
+
.filter((id) => lookupMp.nodeIds.has(id));
|
|
241
|
+
targets = memberIds
|
|
242
|
+
.map((id) => nodesById.get(id))
|
|
243
|
+
.filter((n) => !!n);
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
targets = ids
|
|
247
|
+
.map((id) => nodesById.get(id))
|
|
248
|
+
.filter((n) => !!n);
|
|
249
|
+
}
|
|
250
|
+
if (targets.length > 0) {
|
|
251
|
+
aliasChain = buildAliasChain(lookupMp, importTarget, aliasChainCache);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// Tier 3 — legacy name-based fallback (unchanged behaviour): direct
|
|
257
|
+
// name match, then the one-hop local alias substitution. Keeps repos
|
|
258
|
+
// where module resolution fails (no config, exotic layouts) exactly
|
|
259
|
+
// as they were.
|
|
260
|
+
if (!targets || targets.length === 0) {
|
|
261
|
+
resolvedName = calledName;
|
|
262
|
+
targets = lookupMp.nodesByName.get(calledName);
|
|
263
|
+
if (!targets || targets.length === 0) {
|
|
264
|
+
const importedName = lookupMp.localImportSymbols.get(node.filePath)?.get(rootName);
|
|
265
|
+
if (importedName) {
|
|
266
|
+
resolvedName = calledName.includes(".")
|
|
267
|
+
? importedName + calledName.slice(rootName.length)
|
|
268
|
+
: importedName;
|
|
269
|
+
targets = lookupMp.nodesByName.get(resolvedName);
|
|
270
|
+
}
|
|
209
271
|
}
|
|
210
272
|
}
|
|
211
273
|
if (!targets || targets.length === 0)
|
|
@@ -213,6 +275,19 @@ export function detectCallEdges(nodes, lookupMp) {
|
|
|
213
275
|
const { target, matchedBy } = resolveOverloadTarget(targets, node, callSite.argCount >= 0 ? callSite : undefined);
|
|
214
276
|
if (target.id === node.id)
|
|
215
277
|
continue; // skip self-reference
|
|
278
|
+
// The origin symbol's own name — for an aliased call this differs
|
|
279
|
+
// from what the caller wrote (`httpResponse` → `data`).
|
|
280
|
+
resolvedName = target.name;
|
|
281
|
+
// A chain that ends inside a module (local `export { a as b }`) gets
|
|
282
|
+
// the origin file appended, so every chain ends at the node the edge
|
|
283
|
+
// points to. Skipped when the last hop already *is* that id (or its
|
|
284
|
+
// overload base, e.g. `fmt::fmt` vs `fmt::fmt#1`).
|
|
285
|
+
if (aliasChain && aliasChain.length > 0) {
|
|
286
|
+
const last = aliasChain[aliasChain.length - 1];
|
|
287
|
+
if (last !== target.id && !target.id.startsWith(last)) {
|
|
288
|
+
aliasChain = [...aliasChain, target.id];
|
|
289
|
+
}
|
|
290
|
+
}
|
|
216
291
|
const localEdgeKey = `${node.id}→${target.id}:${edgeType}`;
|
|
217
292
|
if (createdLocalEdges.has(localEdgeKey))
|
|
218
293
|
continue;
|
|
@@ -221,7 +296,18 @@ export function detectCallEdges(nodes, lookupMp) {
|
|
|
221
296
|
from: node.id,
|
|
222
297
|
to: target.id,
|
|
223
298
|
type: edgeType,
|
|
224
|
-
metadata: {
|
|
299
|
+
metadata: {
|
|
300
|
+
// as written at the call site
|
|
301
|
+
calledName,
|
|
302
|
+
// the declaration the graph edge actually points at
|
|
303
|
+
resolvedName,
|
|
304
|
+
// true when the name was aliased (import alias, re-export, …)
|
|
305
|
+
...(calledName !== target.name ? { aliased: true } : {}),
|
|
306
|
+
// provenance for the UI — only when a re-export chain exists
|
|
307
|
+
...(aliasChain ? { aliasChain } : {}),
|
|
308
|
+
matchedBy,
|
|
309
|
+
...(node.type === "FILE" ? { scope: "module" } : {}),
|
|
310
|
+
},
|
|
225
311
|
});
|
|
226
312
|
if (!resolvedCallsMap.has(node.id))
|
|
227
313
|
resolvedCallsMap.set(node.id, []);
|
|
@@ -352,3 +352,122 @@ describe("detectCallEdges with overloads", () => {
|
|
|
352
352
|
deleteFakeRepo(repo);
|
|
353
353
|
});
|
|
354
354
|
});
|
|
355
|
+
// ─── Module-scope callers (FILE nodes) ────────────────────────────────────────
|
|
356
|
+
//
|
|
357
|
+
// Calls executed at file load are owned by the FILE node (moduleScope.ts).
|
|
358
|
+
// They must produce normal CALLS edges — no new edge type — with
|
|
359
|
+
// metadata.scope = "module" so consumers can label them.
|
|
360
|
+
describe("detectCallEdges with module-scope callers", () => {
|
|
361
|
+
it("emits FILE -> FUNCTION CALLS from a top-level call", () => {
|
|
362
|
+
const repo = createFakeRepo({
|
|
363
|
+
"lib/startAll.ts": `export function startAll() { return () => {}; }`,
|
|
364
|
+
"main.ts": `
|
|
365
|
+
import { startAll } from "./lib/startAll";
|
|
366
|
+
const cleanup = startAll();
|
|
367
|
+
process.on("SIGTERM", cleanup);
|
|
368
|
+
`,
|
|
369
|
+
});
|
|
370
|
+
const { edges, all } = detectCalls(repo);
|
|
371
|
+
expect(edges).toContainEqual(expect.objectContaining({
|
|
372
|
+
from: "file::main.ts",
|
|
373
|
+
to: "lib/startAll.ts::startAll",
|
|
374
|
+
type: "CALLS",
|
|
375
|
+
metadata: expect.objectContaining({ calledName: "startAll", scope: "module" }),
|
|
376
|
+
}));
|
|
377
|
+
const file = all.find((n) => n.id === "file::main.ts");
|
|
378
|
+
expect(file.metadata.resolvedCalls).toEqual([
|
|
379
|
+
{ name: "startAll", nodeId: "lib/startAll.ts::startAll" },
|
|
380
|
+
]);
|
|
381
|
+
deleteFakeRepo(repo);
|
|
382
|
+
});
|
|
383
|
+
it("resolves module-scope calls through a local import alias", () => {
|
|
384
|
+
const repo = createFakeRepo({
|
|
385
|
+
"lib/startAll.ts": `export function startAll() { return 1; }`,
|
|
386
|
+
"main.ts": `
|
|
387
|
+
import { startAll as boot } from "./lib/startAll";
|
|
388
|
+
boot();
|
|
389
|
+
`,
|
|
390
|
+
});
|
|
391
|
+
const { edges } = detectCalls(repo);
|
|
392
|
+
expect(edges).toContainEqual(expect.objectContaining({
|
|
393
|
+
from: "file::main.ts",
|
|
394
|
+
to: "lib/startAll.ts::startAll",
|
|
395
|
+
type: "CALLS",
|
|
396
|
+
// calledName is the text at the call site; resolvedName is the origin
|
|
397
|
+
// symbol the edge points at (they differ for aliased imports).
|
|
398
|
+
metadata: expect.objectContaining({
|
|
399
|
+
calledName: "boot",
|
|
400
|
+
resolvedName: "startAll",
|
|
401
|
+
aliased: true,
|
|
402
|
+
}),
|
|
403
|
+
}));
|
|
404
|
+
deleteFakeRepo(repo);
|
|
405
|
+
});
|
|
406
|
+
it("does not tag function-origin calls with scope=module", () => {
|
|
407
|
+
const repo = createFakeRepo({
|
|
408
|
+
"src/a.ts": `
|
|
409
|
+
export function helper() { return 1; }
|
|
410
|
+
export function caller() { return helper(); }
|
|
411
|
+
`,
|
|
412
|
+
});
|
|
413
|
+
const { edges } = detectCalls(repo);
|
|
414
|
+
const edge = edges.find((e) => e.from === "src/a.ts::caller" && e.to === "src/a.ts::helper");
|
|
415
|
+
expect(edge).toBeDefined();
|
|
416
|
+
expect(edge.metadata?.scope).toBeUndefined();
|
|
417
|
+
deleteFakeRepo(repo);
|
|
418
|
+
});
|
|
419
|
+
it("keeps module scope separate from a same-file function call", () => {
|
|
420
|
+
const repo = createFakeRepo({
|
|
421
|
+
"src/boot.ts": `
|
|
422
|
+
export function helper() { return 1; }
|
|
423
|
+
const sentinel = helper();
|
|
424
|
+
export function caller() { return helper(); }
|
|
425
|
+
`,
|
|
426
|
+
});
|
|
427
|
+
const { edges } = detectCalls(repo);
|
|
428
|
+
const fromFile = edges.filter((e) => e.from === "file::src/boot.ts");
|
|
429
|
+
const fromFn = edges.filter((e) => e.from === "src/boot.ts::caller");
|
|
430
|
+
expect(fromFile).toHaveLength(1);
|
|
431
|
+
expect(fromFile[0].to).toBe("src/boot.ts::helper");
|
|
432
|
+
expect(fromFile[0].metadata?.scope).toBe("module");
|
|
433
|
+
expect(fromFn).toHaveLength(1);
|
|
434
|
+
expect(fromFn[0].metadata?.scope).toBeUndefined();
|
|
435
|
+
deleteFakeRepo(repo);
|
|
436
|
+
});
|
|
437
|
+
});
|
|
438
|
+
// ─── Third-party gate (must be identical to function callers) ────────────────
|
|
439
|
+
describe("detectCallEdges — module-scope third-party gate", () => {
|
|
440
|
+
const files = {
|
|
441
|
+
"package.json": JSON.stringify({
|
|
442
|
+
name: "gate-fixture",
|
|
443
|
+
version: "1.0.0",
|
|
444
|
+
dependencies: { express: "^4.0.0" },
|
|
445
|
+
}),
|
|
446
|
+
"main.ts": `
|
|
447
|
+
import express from "express";
|
|
448
|
+
const app = express();
|
|
449
|
+
`,
|
|
450
|
+
};
|
|
451
|
+
it("emits NO third-party node or CALLS edge when the option is absent", () => {
|
|
452
|
+
const repo = createFakeRepo(files);
|
|
453
|
+
const { edges, all } = detectCalls(repo); // no third-party nodes
|
|
454
|
+
expect(all.some((n) => n.id.startsWith("[npm]"))).toBe(false);
|
|
455
|
+
expect(edges.some((e) => e.to.startsWith("[npm]"))).toBe(false);
|
|
456
|
+
// metadata still records the name (not gated)
|
|
457
|
+
const file = all.find((n) => n.id === "file::main.ts");
|
|
458
|
+
expect(file.metadata.calls).toContain("express");
|
|
459
|
+
deleteFakeRepo(repo);
|
|
460
|
+
});
|
|
461
|
+
it("emits FILE -> [npm]/express CALLS when the option is set", () => {
|
|
462
|
+
const repo = createFakeRepo(files);
|
|
463
|
+
const extra = buildThirdPartyNodes(repo, ["express"]);
|
|
464
|
+
const { edges } = detectCalls(repo, extra);
|
|
465
|
+
expect(edges).toContainEqual(expect.objectContaining({
|
|
466
|
+
from: "file::main.ts",
|
|
467
|
+
to: "[npm]/express",
|
|
468
|
+
type: "CALLS",
|
|
469
|
+
metadata: expect.objectContaining({ isThirdParty: true, scope: "module" }),
|
|
470
|
+
}));
|
|
471
|
+
deleteFakeRepo(repo);
|
|
472
|
+
});
|
|
473
|
+
});
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import fs from "fs";
|
|
3
|
+
import { nodesFromLookup } from "../buildLookup.js";
|
|
3
4
|
import { extractPackageName } from "../thirdPartyLibs.js";
|
|
5
|
+
import { buildNodeIndex, resolveDeclarationToNode } from "../../parser/declarationIds.js";
|
|
4
6
|
import { Project } from "ts-morph";
|
|
5
7
|
// Aliases we consider as local imports
|
|
6
8
|
const LOCAL_PREFIXES = ["./", "../", "@/", "~/", "#/"];
|
|
@@ -169,6 +171,8 @@ export function detectImportEdges(lookupMp, repoPath) {
|
|
|
169
171
|
// Records localAlias → importedName so callEdges can resolve
|
|
170
172
|
// `US.get()` when `import { UserService as US } from "./services"`
|
|
171
173
|
// (the dotted METHOD node is named `UserService.get`).
|
|
174
|
+
// Tier-3 fallback only — the binding-aware maps below carry the
|
|
175
|
+
// resolved file and drive tier-2 resolution.
|
|
172
176
|
const localAliases = lookupMp.localImportSymbols.get(sourceRelative) ?? new Map();
|
|
173
177
|
for (const specifier of importDecl.getNamedImports()) {
|
|
174
178
|
const localAlias = specifier.getAliasNode()?.getText() ?? specifier.getName();
|
|
@@ -220,6 +224,11 @@ export function detectImportEdges(lookupMp, repoPath) {
|
|
|
220
224
|
const targetFileNode = lookupMp.fileNodesByPath.get(targetRelative);
|
|
221
225
|
if (!targetFileNode)
|
|
222
226
|
continue;
|
|
227
|
+
// ─── Binding-aware import targets (drives tier-2 resolution) ──
|
|
228
|
+
// Recorded BEFORE the file-pair dedupe below: two import
|
|
229
|
+
// declarations from the same module (or an aliased second name)
|
|
230
|
+
// would otherwise lose their alias entries.
|
|
231
|
+
recordImportTargets(lookupMp, sourceRelative, targetRelative, importDecl);
|
|
223
232
|
const edgeKey = `${sourceFileNode.id}→${targetFileNode.id}`;
|
|
224
233
|
if (createdEdges.has(edgeKey))
|
|
225
234
|
continue;
|
|
@@ -232,5 +241,120 @@ export function detectImportEdges(lookupMp, repoPath) {
|
|
|
232
241
|
});
|
|
233
242
|
}
|
|
234
243
|
}
|
|
244
|
+
// ─── Export surface per module (one pass, reuses this Project) ────────
|
|
245
|
+
// TypeScript's checker already follows `export … from`, `export *`,
|
|
246
|
+
// defaults and path mappings, so a 20-hop re-export chain collapses into a
|
|
247
|
+
// single (file, name) → origin nodeId entry below.
|
|
248
|
+
buildExportSurface(lookupMp, project, repoPath);
|
|
235
249
|
return { edges, thirdPartyMethodNodes: [...methodNodesMap.values()] };
|
|
236
250
|
}
|
|
251
|
+
/** local name → resolved module + exported name, for every import kind. */
|
|
252
|
+
function recordImportTargets(lookupMp, sourceRelative, targetRelative, importDecl) {
|
|
253
|
+
const targets = lookupMp.importTargets.get(sourceRelative) ?? new Map();
|
|
254
|
+
for (const spec of importDecl.getNamedImports()) {
|
|
255
|
+
const localName = spec.getAliasNode()?.getText() ?? spec.getName();
|
|
256
|
+
targets.set(localName, {
|
|
257
|
+
targetFile: targetRelative,
|
|
258
|
+
importedName: spec.getName(),
|
|
259
|
+
kind: "named",
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
const defaultImport = importDecl.getDefaultImport();
|
|
263
|
+
if (defaultImport) {
|
|
264
|
+
targets.set(defaultImport.getText(), {
|
|
265
|
+
targetFile: targetRelative,
|
|
266
|
+
importedName: "default",
|
|
267
|
+
kind: "default",
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
const namespaceImport = importDecl.getNamespaceImport();
|
|
271
|
+
if (namespaceImport) {
|
|
272
|
+
targets.set(namespaceImport.getText(), {
|
|
273
|
+
targetFile: targetRelative,
|
|
274
|
+
importedName: "*",
|
|
275
|
+
kind: "namespace",
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
if (targets.size > 0)
|
|
279
|
+
lookupMp.importTargets.set(sourceRelative, targets);
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Build the per-module export surface.
|
|
283
|
+
*
|
|
284
|
+
* `exportBindings[file][exportedName] = [origin nodeIds]` — the resolution
|
|
285
|
+
* table. `getExportedDeclarations()` returns the ORIGIN declaration of every
|
|
286
|
+
* exported name (re-exports, `export *`, barrels and defaults already followed
|
|
287
|
+
* by the checker), and `resolveDeclarationToNode` maps it to a node id using
|
|
288
|
+
* the shared overload-aware scheme — so `path::name#arity` ids are produced in
|
|
289
|
+
* exactly one place.
|
|
290
|
+
*
|
|
291
|
+
* `reexportLinks` / `reexportGlobs` record the hops themselves. They are NOT
|
|
292
|
+
* used for resolution (the chain is already collapsed); they exist so callers
|
|
293
|
+
* can show "how did this name get here" without inventing graph edges.
|
|
294
|
+
*/
|
|
295
|
+
function buildExportSurface(lookupMp, project, repoPath) {
|
|
296
|
+
const nodeIndex = buildNodeIndex(nodesFromLookup(lookupMp));
|
|
297
|
+
if (nodeIndex.size === 0)
|
|
298
|
+
return;
|
|
299
|
+
for (const file of project.getSourceFiles()) {
|
|
300
|
+
const rel = path.relative(repoPath, file.getFilePath()).replace(/\\/g, "/");
|
|
301
|
+
// ── resolution table ─────────────────────────────────────────────
|
|
302
|
+
try {
|
|
303
|
+
const bindings = new Map();
|
|
304
|
+
for (const [exportedName, declarations] of file.getExportedDeclarations()) {
|
|
305
|
+
const ids = [];
|
|
306
|
+
for (const declaration of declarations) {
|
|
307
|
+
const hit = resolveDeclarationToNode(declaration, repoPath, nodeIndex);
|
|
308
|
+
if (hit)
|
|
309
|
+
ids.push(hit.id);
|
|
310
|
+
}
|
|
311
|
+
const unique = [...new Set(ids)];
|
|
312
|
+
if (unique.length > 0)
|
|
313
|
+
bindings.set(exportedName, unique);
|
|
314
|
+
}
|
|
315
|
+
if (bindings.size > 0)
|
|
316
|
+
lookupMp.exportBindings.set(rel, bindings);
|
|
317
|
+
}
|
|
318
|
+
catch {
|
|
319
|
+
// One unparseable file must not break edge detection — its imports
|
|
320
|
+
// simply fall through to the name-based tier.
|
|
321
|
+
}
|
|
322
|
+
// ── provenance hops (display only) ───────────────────────────────
|
|
323
|
+
try {
|
|
324
|
+
const links = new Map();
|
|
325
|
+
const globs = [];
|
|
326
|
+
for (const exportDecl of file.getExportDeclarations()) {
|
|
327
|
+
const target = exportDecl.getModuleSpecifierSourceFile();
|
|
328
|
+
const targetRel = target
|
|
329
|
+
? path.relative(repoPath, target.getFilePath()).replace(/\\/g, "/")
|
|
330
|
+
: rel; // `export { a as b }` with no `from` — a local binding hop
|
|
331
|
+
const namespaceExport = exportDecl.getNamespaceExport();
|
|
332
|
+
if (namespaceExport) {
|
|
333
|
+
if (!target)
|
|
334
|
+
continue;
|
|
335
|
+
links.set(namespaceExport.getName(), { targetFile: targetRel, targetName: "*" });
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
const named = exportDecl.getNamedExports();
|
|
339
|
+
if (named.length === 0) {
|
|
340
|
+
if (!target)
|
|
341
|
+
continue;
|
|
342
|
+
// `export * from "./x"` — exported names mirror the target
|
|
343
|
+
globs.push(targetRel);
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
for (const spec of named) {
|
|
347
|
+
const exportedName = spec.getAliasNode()?.getText() ?? spec.getName();
|
|
348
|
+
links.set(exportedName, { targetFile: targetRel, targetName: spec.getName() });
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
if (links.size > 0)
|
|
352
|
+
lookupMp.reexportLinks.set(rel, links);
|
|
353
|
+
if (globs.length > 0)
|
|
354
|
+
lookupMp.reexportGlobs.set(rel, globs);
|
|
355
|
+
}
|
|
356
|
+
catch {
|
|
357
|
+
// provenance is best-effort
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
}
|
|
@@ -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
|
+
}
|