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
|
@@ -1,4 +1,17 @@
|
|
|
1
1
|
import type { CodeNode } from "../types.js";
|
|
2
|
+
/** Where a local name in a file came from (the import statement, resolved). */
|
|
3
|
+
export interface ImportTarget {
|
|
4
|
+
/** repo-relative file the specifier resolves to */
|
|
5
|
+
targetFile: string;
|
|
6
|
+
/** the name AS EXPORTED by targetFile ("default" / "*" for default+namespace) */
|
|
7
|
+
importedName: string;
|
|
8
|
+
kind: "named" | "default" | "namespace";
|
|
9
|
+
}
|
|
10
|
+
/** One hop of a re-export chain, recorded for provenance only. */
|
|
11
|
+
export interface ReexportLink {
|
|
12
|
+
targetFile: string;
|
|
13
|
+
targetName: string;
|
|
14
|
+
}
|
|
2
15
|
export interface LookupMaps {
|
|
3
16
|
nodesByName: Map<string, CodeNode[]>;
|
|
4
17
|
nodesByFile: Map<string, CodeNode[]>;
|
|
@@ -7,5 +20,22 @@ export interface LookupMaps {
|
|
|
7
20
|
thirdPartyNodesByName: Map<string, CodeNode>;
|
|
8
21
|
thirdPartyImportAliases: Map<string, Map<string, string>>;
|
|
9
22
|
localImportSymbols: Map<string, Map<string, string>>;
|
|
23
|
+
/** filePath → (local name → resolved import target) */
|
|
24
|
+
importTargets: Map<string, Map<string, ImportTarget>>;
|
|
25
|
+
/** filePath → (exported name → nodeIds in the ORIGIN file, overload-suffixed) */
|
|
26
|
+
exportBindings: Map<string, Map<string, string[]>>;
|
|
27
|
+
/** filePath → (exported name → next hop) — provenance only, never resolution */
|
|
28
|
+
reexportLinks: Map<string, Map<string, ReexportLink>>;
|
|
29
|
+
/** filePath → files pulled in by `export * from` — provenance only */
|
|
30
|
+
reexportGlobs: Map<string, string[]>;
|
|
31
|
+
/** every node id in the graph, for O(1) existence checks */
|
|
32
|
+
nodeIds: Set<string>;
|
|
10
33
|
}
|
|
11
34
|
export declare function buildLookupMaps(codeNodes: CodeNode[]): LookupMaps;
|
|
35
|
+
/**
|
|
36
|
+
* Rebuild a flat node list from the lookup maps.
|
|
37
|
+
* Detectors that only receive `LookupMaps` (importEdges) need the node array to
|
|
38
|
+
* index declarations — the maps are a partition of the same nodes, so this is
|
|
39
|
+
* lossless and avoids threading the array through every signature.
|
|
40
|
+
*/
|
|
41
|
+
export declare function nodesFromLookup(lookup: LookupMaps): CodeNode[];
|
|
@@ -6,15 +6,22 @@ export function buildLookupMaps(codeNodes) {
|
|
|
6
6
|
const thirdPartyNodesByName = new Map();
|
|
7
7
|
const thirdPartyImportAliases = new Map();
|
|
8
8
|
const localImportSymbols = new Map();
|
|
9
|
+
const importTargets = new Map();
|
|
10
|
+
const exportBindings = new Map();
|
|
11
|
+
const reexportLinks = new Map();
|
|
12
|
+
const reexportGlobs = new Map();
|
|
13
|
+
const nodeIds = new Set();
|
|
9
14
|
for (const node of codeNodes) {
|
|
10
15
|
if (node.type === "THIRD_PARTY") {
|
|
11
16
|
thirdPartyNodesByName.set(node.name, node);
|
|
17
|
+
nodeIds.add(node.id);
|
|
12
18
|
continue;
|
|
13
19
|
}
|
|
14
20
|
// FILE nodes go into their own dedicated map — kept separate so other
|
|
15
21
|
// detectors (guards, call edges, etc.) only see function/component nodes
|
|
16
22
|
if (["FILE", "TEST", "STORY"].includes(node.type)) {
|
|
17
23
|
fileNodesByPath.set(node.filePath, node);
|
|
24
|
+
nodeIds.add(node.id);
|
|
18
25
|
continue;
|
|
19
26
|
}
|
|
20
27
|
if (!nodesByName.has(node.name)) {
|
|
@@ -28,6 +35,34 @@ export function buildLookupMaps(codeNodes) {
|
|
|
28
35
|
if (node.type === "STATE_STORE") {
|
|
29
36
|
storeNodes.push(node);
|
|
30
37
|
}
|
|
38
|
+
nodeIds.add(node.id);
|
|
31
39
|
}
|
|
32
|
-
return {
|
|
40
|
+
return {
|
|
41
|
+
nodesByName,
|
|
42
|
+
nodesByFile,
|
|
43
|
+
fileNodesByPath,
|
|
44
|
+
storeNodes,
|
|
45
|
+
thirdPartyNodesByName,
|
|
46
|
+
thirdPartyImportAliases,
|
|
47
|
+
localImportSymbols,
|
|
48
|
+
importTargets,
|
|
49
|
+
exportBindings,
|
|
50
|
+
reexportLinks,
|
|
51
|
+
reexportGlobs,
|
|
52
|
+
nodeIds,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Rebuild a flat node list from the lookup maps.
|
|
57
|
+
* Detectors that only receive `LookupMaps` (importEdges) need the node array to
|
|
58
|
+
* index declarations — the maps are a partition of the same nodes, so this is
|
|
59
|
+
* lossless and avoids threading the array through every signature.
|
|
60
|
+
*/
|
|
61
|
+
export function nodesFromLookup(lookup) {
|
|
62
|
+
const out = [];
|
|
63
|
+
for (const nodes of lookup.nodesByName.values())
|
|
64
|
+
out.push(...nodes);
|
|
65
|
+
for (const node of lookup.fileNodesByPath.values())
|
|
66
|
+
out.push(node);
|
|
67
|
+
return out;
|
|
33
68
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { ImportTarget, LookupMaps } from "../buildLookup.js";
|
|
2
|
+
/**
|
|
3
|
+
* Chain of `file::name` hops from the imported module to the origin symbol.
|
|
4
|
+
* Returns undefined when the name is not aliased beyond the importing module
|
|
5
|
+
* (depth 1) — the common case, which must not bloat the edge metadata.
|
|
6
|
+
*/
|
|
7
|
+
export declare function buildAliasChain(lookup: LookupMaps, target: ImportTarget, cache: Map<string, string[] | undefined>): string[] | undefined;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// Alias-chain provenance ("how did this name get here?").
|
|
2
|
+
//
|
|
3
|
+
// Resolution NEVER uses this file. The chain is already collapsed inside
|
|
4
|
+
// `exportBindings` (TypeScript's checker followed every re-export when it
|
|
5
|
+
// produced `getExportedDeclarations()`), so a call site resolves with two map
|
|
6
|
+
// reads no matter how deep the chain is.
|
|
7
|
+
//
|
|
8
|
+
// This module exists only to answer the display question — "the caller wrote
|
|
9
|
+
// `httpResponse`, the edge points at `data`, why?" — by walking the recorded
|
|
10
|
+
// hops (`reexportLinks` / `reexportGlobs`). It is:
|
|
11
|
+
// - memoized per (file, exported name), so a chain shared by many call sites
|
|
12
|
+
// is computed once;
|
|
13
|
+
// - depth-capped and cycle-guarded, so a pathological barrel ring can neither
|
|
14
|
+
// hang nor recurse forever;
|
|
15
|
+
// - best-effort: a missing link simply ends the chain.
|
|
16
|
+
const MAX_CHAIN_HOPS = 24;
|
|
17
|
+
/**
|
|
18
|
+
* Chain of `file::name` hops from the imported module to the origin symbol.
|
|
19
|
+
* Returns undefined when the name is not aliased beyond the importing module
|
|
20
|
+
* (depth 1) — the common case, which must not bloat the edge metadata.
|
|
21
|
+
*/
|
|
22
|
+
export function buildAliasChain(lookup, target, cache) {
|
|
23
|
+
if (target.kind === "namespace")
|
|
24
|
+
return undefined;
|
|
25
|
+
const key = `${target.targetFile}::${target.importedName}`;
|
|
26
|
+
if (cache.has(key))
|
|
27
|
+
return cache.get(key);
|
|
28
|
+
const chain = [];
|
|
29
|
+
const visited = new Set();
|
|
30
|
+
let current = {
|
|
31
|
+
file: target.targetFile,
|
|
32
|
+
name: target.importedName,
|
|
33
|
+
};
|
|
34
|
+
while (current && chain.length < MAX_CHAIN_HOPS) {
|
|
35
|
+
const hopId = `${current.file}::${current.name}`;
|
|
36
|
+
if (visited.has(hopId))
|
|
37
|
+
break; // cycle in the barrel ring
|
|
38
|
+
visited.add(hopId);
|
|
39
|
+
chain.push(hopId);
|
|
40
|
+
current = nextHop(lookup, current.file, current.name);
|
|
41
|
+
}
|
|
42
|
+
// depth 1 = imported straight from the declaring module: not a chain
|
|
43
|
+
const result = chain.length > 1 ? chain : undefined;
|
|
44
|
+
cache.set(key, result);
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
|
+
function nextHop(lookup, file, name) {
|
|
48
|
+
const explicit = lookup.reexportLinks.get(file)?.get(name);
|
|
49
|
+
if (explicit)
|
|
50
|
+
return { file: explicit.targetFile, name: explicit.targetName };
|
|
51
|
+
// `export * from "./x"` keeps the name — follow the first glob that owns it
|
|
52
|
+
const globs = lookup.reexportGlobs.get(file);
|
|
53
|
+
if (globs) {
|
|
54
|
+
for (const glob of globs) {
|
|
55
|
+
const owns = lookup.exportBindings.get(glob)?.has(name) === true ||
|
|
56
|
+
lookup.reexportLinks.get(glob)?.has(name) === true ||
|
|
57
|
+
(lookup.reexportGlobs.get(glob)?.length ?? 0) > 0;
|
|
58
|
+
if (owns)
|
|
59
|
+
return { file: glob, name };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
// Alias / re-export resolution — the class of bug where the name at the call
|
|
2
|
+
// site never exists as a node.
|
|
3
|
+
//
|
|
4
|
+
// Covers (see docs/ENGINE_PIPELINE.html → "Resolving a call"):
|
|
5
|
+
// 1 plain import alias (`import { a as b }`)
|
|
6
|
+
// 2 re-export alias (`export { a as b } from`)
|
|
7
|
+
// 3 export * barrel + alias
|
|
8
|
+
// 4 rename on both hops
|
|
9
|
+
// 5 same exported name in two modules (module-scoped, not closestByPath)
|
|
10
|
+
// 6 default export
|
|
11
|
+
// 7 namespace import member
|
|
12
|
+
// 8 member access through a re-exported class
|
|
13
|
+
// 9 overload group exported under one name (overload-suffixed ids)
|
|
14
|
+
// 10 deep chain (20 hops) — one edge, provenance chain recorded
|
|
15
|
+
// 11 re-export cycle (no hang, no throw)
|
|
16
|
+
// 12 unresolvable module (tier-3 fallback, no throw)
|
|
17
|
+
// 13 module-scope (FILE) caller through a re-export
|
|
18
|
+
// 14 third-party resolution untouched
|
|
19
|
+
import fs from "fs";
|
|
20
|
+
import path from "path";
|
|
21
|
+
import os from "os";
|
|
22
|
+
import { describe, it, expect } from "bun:test";
|
|
23
|
+
import { parseRepo } from "../../parser/index.js";
|
|
24
|
+
import { buildLookupMaps } from "../buildLookup.js";
|
|
25
|
+
import { detectImportEdges } from "./importEdges.js";
|
|
26
|
+
import { detectCallEdges } from "./callEdges.js";
|
|
27
|
+
import { buildThirdPartyNodes } from "../thirdPartyLibs.js";
|
|
28
|
+
function createRepo(files) {
|
|
29
|
+
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "devlens-alias-test-"));
|
|
30
|
+
for (const [rel, content] of Object.entries(files)) {
|
|
31
|
+
const full = path.join(dir, rel);
|
|
32
|
+
fs.mkdirSync(path.dirname(full), { recursive: true });
|
|
33
|
+
fs.writeFileSync(full, content);
|
|
34
|
+
}
|
|
35
|
+
return dir;
|
|
36
|
+
}
|
|
37
|
+
function dropRepo(dir) {
|
|
38
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
39
|
+
}
|
|
40
|
+
/** Mirrors the pipeline: parse → lookup maps → import edges → call edges. */
|
|
41
|
+
function detect(repoPath, extraNodes = []) {
|
|
42
|
+
const { nodes } = parseRepo(repoPath);
|
|
43
|
+
const all = [...nodes, ...extraNodes];
|
|
44
|
+
const lookup = buildLookupMaps(all);
|
|
45
|
+
detectImportEdges(lookup, repoPath);
|
|
46
|
+
const result = detectCallEdges(all, lookup);
|
|
47
|
+
return { ...result, all, lookup };
|
|
48
|
+
}
|
|
49
|
+
const localCalls = (edges) => edges.filter((e) => (e.type === "CALLS" || e.type === "USES") && !e.metadata?.isThirdParty);
|
|
50
|
+
const findCall = (edges, from, to) => edges.find((e) => e.from === from && (!to || e.to === to));
|
|
51
|
+
const TSCONFIG = JSON.stringify({
|
|
52
|
+
compilerOptions: {
|
|
53
|
+
target: "ES2020",
|
|
54
|
+
module: "ESNext",
|
|
55
|
+
moduleResolution: "Bundler",
|
|
56
|
+
baseUrl: ".",
|
|
57
|
+
paths: { "~/*": ["./src/*"] },
|
|
58
|
+
strict: true,
|
|
59
|
+
},
|
|
60
|
+
include: ["src"],
|
|
61
|
+
});
|
|
62
|
+
describe("alias resolution — one hop", () => {
|
|
63
|
+
it("resolves a plain import alias (`import { data as httpResponse }`)", () => {
|
|
64
|
+
const repo = createRepo({
|
|
65
|
+
"src/http.ts": `export function data(body: unknown) { return body; }`,
|
|
66
|
+
"src/route.ts": `import { data as httpResponse } from "./http";
|
|
67
|
+
export function loader() { return httpResponse({ ok: true }); }`,
|
|
68
|
+
});
|
|
69
|
+
const { edges } = detect(repo);
|
|
70
|
+
const edge = findCall(edges, "src/route.ts::loader", "src/http.ts::data");
|
|
71
|
+
expect(edge).toBeDefined();
|
|
72
|
+
expect(edge?.metadata?.calledName).toBe("httpResponse");
|
|
73
|
+
expect(edge?.metadata?.resolvedName).toBe("data");
|
|
74
|
+
expect(edge?.metadata?.aliased).toBe(true);
|
|
75
|
+
// no re-export hop → no chain noise on the edge
|
|
76
|
+
expect(edge?.metadata?.aliasChain).toBeUndefined();
|
|
77
|
+
dropRepo(repo);
|
|
78
|
+
});
|
|
79
|
+
it("resolves a re-export alias (`export { data as httpResponse } from`)", () => {
|
|
80
|
+
const repo = createRepo({
|
|
81
|
+
"tsconfig.json": TSCONFIG,
|
|
82
|
+
"src/http.ts": `export function data(body: unknown) { return body; }`,
|
|
83
|
+
"src/index.ts": `export { data as httpResponse } from "./http";`,
|
|
84
|
+
"src/route.ts": `import { httpResponse } from "~/index";
|
|
85
|
+
export function loader() { return httpResponse({ ok: true }); }`,
|
|
86
|
+
});
|
|
87
|
+
const { edges } = detect(repo);
|
|
88
|
+
const edge = findCall(edges, "src/route.ts::loader", "src/http.ts::data");
|
|
89
|
+
expect(edge).toBeDefined();
|
|
90
|
+
expect(edge?.metadata?.calledName).toBe("httpResponse");
|
|
91
|
+
expect(edge?.metadata?.resolvedName).toBe("data");
|
|
92
|
+
expect(edge?.metadata?.aliased).toBe(true);
|
|
93
|
+
expect(edge?.metadata?.aliasChain).toEqual([
|
|
94
|
+
"src/index.ts::httpResponse",
|
|
95
|
+
"src/http.ts::data",
|
|
96
|
+
]);
|
|
97
|
+
dropRepo(repo);
|
|
98
|
+
});
|
|
99
|
+
it("follows an `export * from` barrel that also renames", () => {
|
|
100
|
+
const repo = createRepo({
|
|
101
|
+
"src/inner.ts": `export function starOriginal() { return 7; }`,
|
|
102
|
+
"src/mid.ts": `export * from "./inner";`,
|
|
103
|
+
"src/outer.ts": `import { starOriginal as starAlias } from "./mid";
|
|
104
|
+
export function useStar() { return starAlias(); }`,
|
|
105
|
+
});
|
|
106
|
+
const { edges } = detect(repo);
|
|
107
|
+
expect(findCall(edges, "src/outer.ts::useStar", "src/inner.ts::starOriginal")).toBeDefined();
|
|
108
|
+
dropRepo(repo);
|
|
109
|
+
});
|
|
110
|
+
it("resolves a rename on both hops (import alias of an export alias)", () => {
|
|
111
|
+
const repo = createRepo({
|
|
112
|
+
"src/impl.ts": `export function realThing(x: number) { return x; }`,
|
|
113
|
+
"src/mid.ts": `export { realThing as publicName } from "./impl";`,
|
|
114
|
+
"src/use.ts": `import { publicName as local } from "./mid";
|
|
115
|
+
export function run() { return local(1); }`,
|
|
116
|
+
});
|
|
117
|
+
const { edges } = detect(repo);
|
|
118
|
+
const edge = findCall(edges, "src/use.ts::run", "src/impl.ts::realThing");
|
|
119
|
+
expect(edge).toBeDefined();
|
|
120
|
+
expect(edge?.metadata?.resolvedName).toBe("realThing");
|
|
121
|
+
dropRepo(repo);
|
|
122
|
+
});
|
|
123
|
+
it("resolves an import followed by a LOCAL re-export (`export { data as httpResponse }`)", () => {
|
|
124
|
+
const repo = createRepo({
|
|
125
|
+
"src/http.ts": `export function data() { return 1; }`,
|
|
126
|
+
"src/index.ts": `import { data } from "./http";
|
|
127
|
+
export { data as httpResponse };`,
|
|
128
|
+
"src/use.ts": `import { httpResponse } from "./index";
|
|
129
|
+
export function run() { return httpResponse(); }`,
|
|
130
|
+
});
|
|
131
|
+
const { edges } = detect(repo);
|
|
132
|
+
const edge = findCall(edges, "src/use.ts::run", "src/http.ts::data");
|
|
133
|
+
expect(edge).toBeDefined();
|
|
134
|
+
expect(edge?.metadata?.resolvedName).toBe("data");
|
|
135
|
+
// the local hop is recorded too: index.ts::httpResponse → index.ts::data
|
|
136
|
+
expect(edge?.metadata?.aliasChain).toEqual([
|
|
137
|
+
"src/index.ts::httpResponse",
|
|
138
|
+
"src/index.ts::data",
|
|
139
|
+
"src/http.ts::data",
|
|
140
|
+
]);
|
|
141
|
+
dropRepo(repo);
|
|
142
|
+
});
|
|
143
|
+
it("follows a default export through a re-export", () => {
|
|
144
|
+
const repo = createRepo({
|
|
145
|
+
"src/client.ts": `export default function httpClient() { return 1; }`,
|
|
146
|
+
"src/index.ts": `export { default as makeClient } from "./client";`,
|
|
147
|
+
"src/use.ts": `import { makeClient } from "./index";
|
|
148
|
+
export function run() { return makeClient(); }`,
|
|
149
|
+
});
|
|
150
|
+
const { edges } = detect(repo);
|
|
151
|
+
expect(findCall(edges, "src/use.ts::run", "src/client.ts::httpClient")).toBeDefined();
|
|
152
|
+
dropRepo(repo);
|
|
153
|
+
});
|
|
154
|
+
it("resolves a member call through a namespace import", () => {
|
|
155
|
+
const repo = createRepo({
|
|
156
|
+
"src/tools.ts": `export function normalize(x: number) { return x; }`,
|
|
157
|
+
"src/use.ts": `import * as Tools from "./tools";
|
|
158
|
+
export function run() { return Tools.normalize(1); }`,
|
|
159
|
+
});
|
|
160
|
+
const { edges } = detect(repo);
|
|
161
|
+
expect(findCall(edges, "src/use.ts::run", "src/tools.ts::normalize")).toBeDefined();
|
|
162
|
+
dropRepo(repo);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
describe("alias resolution — module scoping (no name guessing)", () => {
|
|
166
|
+
it("sends each importer to its OWN module when two files export the same name", () => {
|
|
167
|
+
const repo = createRepo({
|
|
168
|
+
"a/http.ts": `export function data() { return "a"; }`,
|
|
169
|
+
"b/http.ts": `export function data() { return "b"; }`,
|
|
170
|
+
"a/index.ts": `export { data } from "./http";`,
|
|
171
|
+
"b/index.ts": `export { data } from "./http";`,
|
|
172
|
+
// a shallow path trick: the *nearer* file to use.ts is b/http.ts
|
|
173
|
+
"a/use.ts": `import { data } from "./index";
|
|
174
|
+
export function readA() { return data(); }`,
|
|
175
|
+
"b/use.ts": `import { data } from "./index";
|
|
176
|
+
export function readB() { return data(); }`,
|
|
177
|
+
});
|
|
178
|
+
const { edges } = detect(repo);
|
|
179
|
+
expect(findCall(edges, "a/use.ts::readA", "a/http.ts::data")).toBeDefined();
|
|
180
|
+
expect(findCall(edges, "b/use.ts::readB", "b/http.ts::data")).toBeDefined();
|
|
181
|
+
// and the wrong cross-wire must not exist
|
|
182
|
+
expect(findCall(edges, "a/use.ts::readA", "b/http.ts::data")).toBeUndefined();
|
|
183
|
+
expect(findCall(edges, "b/use.ts::readB", "a/http.ts::data")).toBeUndefined();
|
|
184
|
+
dropRepo(repo);
|
|
185
|
+
});
|
|
186
|
+
it("resolves a method on a re-exported class (dotted member id)", () => {
|
|
187
|
+
const repo = createRepo({
|
|
188
|
+
"src/services.ts": `export class UserService {
|
|
189
|
+
static get(id: string) { return id; }
|
|
190
|
+
}`,
|
|
191
|
+
"src/index.ts": `export { UserService as US } from "./services";`,
|
|
192
|
+
"src/use.ts": `import { US } from "./index";
|
|
193
|
+
export function load(id: string) { return US.get(id); }`,
|
|
194
|
+
});
|
|
195
|
+
const { edges } = detect(repo);
|
|
196
|
+
const edge = findCall(edges, "src/use.ts::load", "src/services.ts::UserService.get");
|
|
197
|
+
expect(edge).toBeDefined();
|
|
198
|
+
expect(edge?.metadata?.calledName).toBe("US.get");
|
|
199
|
+
expect(edge?.metadata?.resolvedName).toBe("UserService.get");
|
|
200
|
+
dropRepo(repo);
|
|
201
|
+
});
|
|
202
|
+
it("never invents an edge to the container when the member does not exist", () => {
|
|
203
|
+
const repo = createRepo({
|
|
204
|
+
"src/services.ts": `export class UserService { static get(id: string) { return id; } }`,
|
|
205
|
+
"src/index.ts": `export { UserService } from "./services";`,
|
|
206
|
+
"src/use.ts": `import { UserService } from "./index";
|
|
207
|
+
export function load() { return UserService.missing(); }`,
|
|
208
|
+
});
|
|
209
|
+
const { edges } = detect(repo);
|
|
210
|
+
expect(edges.find((e) => e.from === "src/use.ts::load")).toBeUndefined();
|
|
211
|
+
dropRepo(repo);
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
describe("alias resolution — overloads keep their discriminated ids", () => {
|
|
215
|
+
it("maps an exported overload group to the right `#arity` sibling", () => {
|
|
216
|
+
const repo = createRepo({
|
|
217
|
+
"src/fmt.ts": `export function fmt(x: string) { return x; }
|
|
218
|
+
export function fmt(x: string, y: number) { return x + y; }`,
|
|
219
|
+
"src/index.ts": `export { fmt as format } from "./fmt";`,
|
|
220
|
+
"src/use.ts": `import { format } from "./index";
|
|
221
|
+
export function one() { return format("a"); }
|
|
222
|
+
export function two() { return format("a", 1); }`,
|
|
223
|
+
});
|
|
224
|
+
const { edges, all } = detect(repo);
|
|
225
|
+
const ids = all.filter((n) => n.name === "fmt").map((n) => n.id).sort();
|
|
226
|
+
// the overload scheme owns id generation — the binding table must read it
|
|
227
|
+
expect(ids).toEqual(["src/fmt.ts::fmt#1", "src/fmt.ts::fmt#2"]);
|
|
228
|
+
const one = edges.find((e) => e.from === "src/use.ts::one");
|
|
229
|
+
const two = edges.find((e) => e.from === "src/use.ts::two");
|
|
230
|
+
expect(one?.to).toBe("src/fmt.ts::fmt#1");
|
|
231
|
+
expect(two?.to).toBe("src/fmt.ts::fmt#2");
|
|
232
|
+
dropRepo(repo);
|
|
233
|
+
});
|
|
234
|
+
});
|
|
235
|
+
describe("alias resolution — deep chains", () => {
|
|
236
|
+
function buildChainRepo(hops) {
|
|
237
|
+
const files = {
|
|
238
|
+
"src/chain/c00.ts": `export function realOriginal(x: number) { return x + 1; }`,
|
|
239
|
+
};
|
|
240
|
+
for (let i = 1; i <= hops; i++) {
|
|
241
|
+
const prev = `c${String(i - 1).padStart(2, "0")}`;
|
|
242
|
+
const cur = `c${String(i).padStart(2, "0")}`;
|
|
243
|
+
const prevName = i === 1 ? "realOriginal" : `alias${String(i - 1).padStart(2, "0")}`;
|
|
244
|
+
files[`src/chain/${cur}.ts`] = `export { ${prevName} as alias${String(i).padStart(2, "0")} } from "./${prev}";`;
|
|
245
|
+
}
|
|
246
|
+
const leaf = `c${String(hops).padStart(2, "0")}`;
|
|
247
|
+
files["src/leaf.ts"] = `import { alias${String(hops).padStart(2, "0")} } from "./chain/${leaf}";
|
|
248
|
+
export function useLeaf() { return alias${String(hops).padStart(2, "0")}(41); }`;
|
|
249
|
+
return createRepo(files);
|
|
250
|
+
}
|
|
251
|
+
it("collapses 20 re-export hops into ONE edge to the origin", () => {
|
|
252
|
+
const repo = buildChainRepo(20);
|
|
253
|
+
const { edges } = detect(repo);
|
|
254
|
+
const calls = localCalls(edges).filter((e) => e.from === "src/leaf.ts::useLeaf");
|
|
255
|
+
expect(calls).toHaveLength(1);
|
|
256
|
+
expect(calls[0].to).toBe("src/chain/c00.ts::realOriginal");
|
|
257
|
+
// provenance: every hop is recorded, in order, ending at the origin
|
|
258
|
+
const chain = calls[0].metadata?.aliasChain;
|
|
259
|
+
expect(chain[0]).toBe("src/chain/c20.ts::alias20");
|
|
260
|
+
expect(chain[chain.length - 1]).toBe("src/chain/c00.ts::realOriginal");
|
|
261
|
+
expect(chain).toHaveLength(21);
|
|
262
|
+
dropRepo(repo);
|
|
263
|
+
});
|
|
264
|
+
it("holds for a 3-hop chain too (no depth-specific code path)", () => {
|
|
265
|
+
const repo = buildChainRepo(3);
|
|
266
|
+
const { edges } = detect(repo);
|
|
267
|
+
const calls = localCalls(edges).filter((e) => e.from === "src/leaf.ts::useLeaf");
|
|
268
|
+
expect(calls).toHaveLength(1);
|
|
269
|
+
expect(calls[0].to).toBe("src/chain/c00.ts::realOriginal");
|
|
270
|
+
expect((calls[0].metadata?.aliasChain).length).toBe(4);
|
|
271
|
+
dropRepo(repo);
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
describe("alias resolution — robustness", () => {
|
|
275
|
+
it("survives a re-export cycle without hanging or throwing", () => {
|
|
276
|
+
const repo = createRepo({
|
|
277
|
+
"src/a.ts": `export { aliasB as aliasA } from "./b";`,
|
|
278
|
+
"src/b.ts": `export { aliasA as aliasB } from "./a";`,
|
|
279
|
+
"src/use.ts": `import { aliasA } from "./a";
|
|
280
|
+
export function run() { return aliasA(); }`,
|
|
281
|
+
});
|
|
282
|
+
const { edges } = detect(repo);
|
|
283
|
+
// no declaration exists anywhere → no edge, but the run completes
|
|
284
|
+
expect(edges.find((e) => e.from === "src/use.ts::run")).toBeUndefined();
|
|
285
|
+
dropRepo(repo);
|
|
286
|
+
});
|
|
287
|
+
it("falls back silently when the module cannot be resolved", () => {
|
|
288
|
+
const repo = createRepo({
|
|
289
|
+
"src/use.ts": `import { missing } from "./nope";
|
|
290
|
+
export function run() { return missing(); }`,
|
|
291
|
+
});
|
|
292
|
+
const { edges } = detect(repo);
|
|
293
|
+
expect(edges.find((e) => e.from === "src/use.ts::run")).toBeUndefined();
|
|
294
|
+
dropRepo(repo);
|
|
295
|
+
});
|
|
296
|
+
it("keeps legacy name-based resolution when there is no import at all", () => {
|
|
297
|
+
const repo = createRepo({
|
|
298
|
+
"src/a.ts": `export function helper() { return 1; }
|
|
299
|
+
export function caller() { return helper(); }`,
|
|
300
|
+
});
|
|
301
|
+
const { edges } = detect(repo);
|
|
302
|
+
expect(findCall(edges, "src/a.ts::caller", "src/a.ts::helper")).toBeDefined();
|
|
303
|
+
dropRepo(repo);
|
|
304
|
+
});
|
|
305
|
+
it("resolves module-scope (FILE) callers through a re-export", () => {
|
|
306
|
+
const repo = createRepo({
|
|
307
|
+
"src/http.ts": `export function data() { return 1; }`,
|
|
308
|
+
"src/index.ts": `export { data as httpResponse } from "./http";`,
|
|
309
|
+
"src/main.ts": `import { httpResponse } from "./index";
|
|
310
|
+
const ready = httpResponse();`,
|
|
311
|
+
});
|
|
312
|
+
const { edges } = detect(repo);
|
|
313
|
+
const edge = findCall(edges, "file::src/main.ts", "src/http.ts::data");
|
|
314
|
+
expect(edge).toBeDefined();
|
|
315
|
+
expect(edge?.metadata?.scope).toBe("module");
|
|
316
|
+
dropRepo(repo);
|
|
317
|
+
});
|
|
318
|
+
it("leaves third-party resolution untouched", () => {
|
|
319
|
+
const repo = createRepo({
|
|
320
|
+
"src/api.ts": `import axios from "axios";
|
|
321
|
+
export function fetchUsers() { return axios.get("/users"); }`,
|
|
322
|
+
});
|
|
323
|
+
const thirdParty = buildThirdPartyNodes(repo, ["axios"]);
|
|
324
|
+
const { edges } = detect(repo, thirdParty);
|
|
325
|
+
const edge = edges.find((e) => e.from === "src/api.ts::fetchUsers" && e.metadata?.isThirdParty);
|
|
326
|
+
expect(edge?.to).toBe("[npm]/axios::get");
|
|
327
|
+
dropRepo(repo);
|
|
328
|
+
});
|
|
329
|
+
it("records the binding tables the fix depends on", () => {
|
|
330
|
+
const repo = createRepo({
|
|
331
|
+
"src/http.ts": `export function data() { return 1; }`,
|
|
332
|
+
"src/index.ts": `export { data as httpResponse } from "./http";`,
|
|
333
|
+
"src/use.ts": `import { httpResponse } from "./index";
|
|
334
|
+
export function run() { return httpResponse(); }`,
|
|
335
|
+
});
|
|
336
|
+
const { lookup } = detect(repo);
|
|
337
|
+
expect(lookup.importTargets.get("src/use.ts")?.get("httpResponse")).toEqual({
|
|
338
|
+
targetFile: "src/index.ts",
|
|
339
|
+
importedName: "httpResponse",
|
|
340
|
+
kind: "named",
|
|
341
|
+
});
|
|
342
|
+
expect(lookup.exportBindings.get("src/index.ts")?.get("httpResponse")).toEqual([
|
|
343
|
+
"src/http.ts::data",
|
|
344
|
+
]);
|
|
345
|
+
expect(lookup.reexportLinks.get("src/index.ts")?.get("httpResponse")).toEqual({
|
|
346
|
+
targetFile: "src/http.ts",
|
|
347
|
+
targetName: "data",
|
|
348
|
+
});
|
|
349
|
+
dropRepo(repo);
|
|
350
|
+
});
|
|
351
|
+
});
|