devlensio 1.1.0 → 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.
Files changed (56) hide show
  1. package/dist/extractors/runner.js +1 -0
  2. package/dist/extractors/types.d.ts +2 -1
  3. package/dist/graph/buildLookup.d.ts +30 -0
  4. package/dist/graph/buildLookup.js +36 -1
  5. package/dist/graph/edges/aliasChain.d.ts +7 -0
  6. package/dist/graph/edges/aliasChain.js +63 -0
  7. package/dist/graph/edges/aliasResolution.test.d.ts +1 -0
  8. package/dist/graph/edges/aliasResolution.test.js +351 -0
  9. package/dist/graph/edges/callEdges.js +101 -15
  10. package/dist/graph/edges/callEdges.test.js +119 -0
  11. package/dist/graph/edges/importEdges.js +124 -0
  12. package/dist/parser/declarationIds.d.ts +23 -0
  13. package/dist/parser/declarationIds.js +123 -0
  14. package/dist/parser/exportsMap.d.ts +9 -0
  15. package/dist/parser/exportsMap.js +260 -0
  16. package/dist/parser/exportsMap.test.d.ts +1 -0
  17. package/dist/parser/exportsMap.test.js +324 -0
  18. package/dist/parser/extractors/functions.d.ts +11 -0
  19. package/dist/parser/extractors/functions.js +69 -59
  20. package/dist/parser/extractors/moduleScope.d.ts +8 -0
  21. package/dist/parser/extractors/moduleScope.js +80 -0
  22. package/dist/parser/index.d.ts +2 -1
  23. package/dist/parser/index.js +45 -0
  24. package/dist/parser/moduleScope.test.d.ts +1 -0
  25. package/dist/parser/moduleScope.test.js +174 -0
  26. package/dist/pipeline/index.d.ts +2 -1
  27. package/dist/pipeline/index.js +1 -0
  28. package/dist/storage/fileStorage.d.ts +2 -1
  29. package/dist/storage/fileStorage.js +5 -1
  30. package/dist/storage/fileStorage.test.d.ts +1 -0
  31. package/dist/storage/fileStorage.test.js +112 -0
  32. package/dist/types.d.ts +4 -0
  33. package/extractors/go/bin/darwin-amd64/devlens_go_extractor +0 -0
  34. package/extractors/go/bin/darwin-arm64/devlens_go_extractor +0 -0
  35. package/extractors/go/bin/linux-amd64/devlens_go_extractor +0 -0
  36. package/extractors/go/bin/linux-arm64/devlens_go_extractor +0 -0
  37. package/extractors/go/bin/windows-amd64/devlens_go_extractor.exe +0 -0
  38. package/extractors/go/calls.go +45 -3
  39. package/extractors/go/parser.go +81 -36
  40. package/extractors/java/devlens_java_extractor.jar +0 -0
  41. package/extractors/java/src/devlens/extractor/Extractor.java +16 -2
  42. package/extractors/java/src/devlens/extractor/LookupMaps.java +2 -0
  43. package/extractors/java/src/devlens/extractor/Parser.java +33 -1
  44. package/extractors/java/src/devlens/extractor/edges/Calls.java +44 -0
  45. package/extractors/python/devlens_extractors_python/edges/calls.py +173 -33
  46. package/extractors/python/devlens_extractors_python/edges/imports.py +21 -1
  47. package/extractors/python/devlens_extractors_python/lookup.py +9 -0
  48. package/extractors/python/devlens_extractors_python/parser/__init__.py +10 -1
  49. package/extractors/rust/bin/darwin-amd64/devlens_rust_extractor +0 -0
  50. package/extractors/rust/bin/darwin-arm64/devlens_rust_extractor +0 -0
  51. package/extractors/rust/bin/linux-amd64/devlens_rust_extractor +0 -0
  52. package/extractors/rust/bin/linux-arm64/devlens_rust_extractor +0 -0
  53. package/extractors/rust/bin/windows-amd64/devlens_rust_extractor.exe +0 -0
  54. package/extractors/rust/src/calls.rs +32 -0
  55. package/extractors/rust/src/lookup.rs +66 -0
  56. package/package.json +1 -1
@@ -0,0 +1,260 @@
1
+ // Exports Map builder — the flattened export surface of the repo, shipped in
2
+ // the graph artifact for future cross-repo linking (see docs/EXPORTS_MAP.md).
3
+ //
4
+ // Architecture notes:
5
+ // - Must live in the parser because binding resolution needs the ts-morph
6
+ // Project created inside parseRepo(); a second Project would double parse
7
+ // cost. parseRepo() calls buildExportsMap() after the node loop, so the
8
+ // map is validated against the FINAL node list (only surviving, deduped,
9
+ // overload-suffixed ids can appear as values).
10
+ // - Values are ALWAYS arrays of proper nodeIds — never bare names. The
11
+ // cloud joins against them in one hash lookup; no second resolution step.
12
+ // - Overloads: multiple nodes sharing one base id (path::name) are an
13
+ // overload group — emitted as a plain array. Distinct base ids under one
14
+ // exported name are a TRUE ambiguity — also flagged in ambiguousNames.
15
+ // - TS resolves `export *` conflicts by EXCLUDING the name from the module's
16
+ // exports (same as the JS spec), so star re-exports need no special code.
17
+ // - Anything without a node representation (plain consts, interfaces,
18
+ // type aliases, anonymous defaults, namespace re-exports) is OMITTED —
19
+ // never fabricate an id that won't exist in the nodes array.
20
+ // - package.json "exports" conditions: v1 resolves one condition per subpath
21
+ // (import → default → require); "types"-only subpaths are skipped; wildcard
22
+ // patterns are skipped. Entry targets pointing into dist/build are mapped
23
+ // back to src/ so ids stay in the same coordinate system as the nodes.
24
+ // - Failures never break analysis: any error yields an absent map.
25
+ import path from "path";
26
+ import fs from "fs";
27
+ import { buildNodeIndex, resolveDeclarationToNode, } from "./declarationIds.js";
28
+ const ENTRY_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx"];
29
+ const INDEX_NAMES = ["index.ts", "index.tsx", "index.js", "index.jsx"];
30
+ export function buildExportsMap(project, repoPath, nodes) {
31
+ try {
32
+ const pkgPath = path.join(repoPath, "package.json");
33
+ if (!fs.existsSync(pkgPath))
34
+ return undefined;
35
+ let pkg;
36
+ try {
37
+ pkg = JSON.parse(fs.readFileSync(pkgPath, "utf-8"));
38
+ }
39
+ catch {
40
+ return undefined;
41
+ }
42
+ const entries = resolveEntryPoints(pkg, repoPath, project);
43
+ if (entries.length === 0)
44
+ return undefined;
45
+ const nodeIndex = buildNodeIndex(nodes);
46
+ const starCache = new Map();
47
+ const exports = {};
48
+ const ambiguousNames = {};
49
+ for (const entry of entries) {
50
+ let decls;
51
+ try {
52
+ decls = entry.file.getExportedDeclarations();
53
+ }
54
+ catch {
55
+ continue;
56
+ }
57
+ const explicitNames = explicitExportNames(entry.file);
58
+ const entryPath = entry.file.getFilePath();
59
+ const stars = starContributors(entry.file, starCache);
60
+ const perPath = {};
61
+ const ambiguous = [];
62
+ for (const [name, declList] of decls) {
63
+ const isExplicit = explicitNames.has(name) || declList.some((d) => d.getSourceFile().getFilePath() === entryPath);
64
+ if (!isExplicit) {
65
+ const contributors = stars.get(name);
66
+ if (contributors && contributors.size > 1) {
67
+ ambiguous.push(name);
68
+ continue;
69
+ }
70
+ }
71
+ const resolved = [];
72
+ for (const decl of declList) {
73
+ const hit = resolveDeclarationToNode(decl, repoPath, nodeIndex);
74
+ if (hit)
75
+ resolved.push(hit);
76
+ }
77
+ const ids = [...new Set(resolved.map((r) => r.id))].sort();
78
+ if (ids.length === 0)
79
+ continue;
80
+ const bases = [...new Set(resolved.map((r) => r.base))];
81
+ if (bases.length > 1)
82
+ ambiguous.push(name);
83
+ perPath[name] = ids;
84
+ }
85
+ if (Object.keys(perPath).length > 0)
86
+ exports[entry.subpath] = perPath;
87
+ if (ambiguous.length > 0)
88
+ ambiguousNames[entry.subpath] = ambiguous;
89
+ }
90
+ if (Object.keys(exports).length === 0)
91
+ return undefined;
92
+ return { exports, ambiguousNames };
93
+ }
94
+ catch {
95
+ return undefined;
96
+ }
97
+ }
98
+ function explicitExportNames(sf) {
99
+ const names = new Set();
100
+ for (const ed of sf.getExportDeclarations()) {
101
+ for (const spec of ed.getNamedExports())
102
+ names.add(spec.getName());
103
+ }
104
+ return names;
105
+ }
106
+ function starContributors(sf, cache) {
107
+ const key = sf.getFilePath();
108
+ const cached = cache.get(key);
109
+ if (cached)
110
+ return cached;
111
+ const result = new Map();
112
+ const seen = new Set([key]);
113
+ const stack = [sf];
114
+ while (stack.length > 0) {
115
+ const current = stack.pop();
116
+ for (const ed of current.getExportDeclarations()) {
117
+ if (ed.getNamedExports().length > 0)
118
+ continue;
119
+ const target = ed.getModuleSpecifierSourceFile();
120
+ if (!target || seen.has(target.getFilePath()))
121
+ continue;
122
+ seen.add(target.getFilePath());
123
+ const targetPath = target.getFilePath();
124
+ let targetDecls;
125
+ try {
126
+ targetDecls = target.getExportedDeclarations();
127
+ }
128
+ catch {
129
+ continue;
130
+ }
131
+ for (const [name, declList] of targetDecls) {
132
+ const isLocal = declList.some((d) => d.getSourceFile().getFilePath() === targetPath);
133
+ if (isLocal) {
134
+ if (!result.has(name))
135
+ result.set(name, new Set());
136
+ result.get(name).add(targetPath);
137
+ }
138
+ else {
139
+ const deeper = starContributors(target, cache);
140
+ const contributors = deeper.get(name);
141
+ if (contributors) {
142
+ if (!result.has(name))
143
+ result.set(name, new Set());
144
+ for (const f of contributors)
145
+ result.get(name).add(f);
146
+ }
147
+ }
148
+ }
149
+ stack.push(target);
150
+ }
151
+ }
152
+ cache.set(key, result);
153
+ return result;
154
+ }
155
+ function resolveEntryPoints(pkg, repoPath, project) {
156
+ const entries = [];
157
+ if (pkg.exports !== undefined) {
158
+ const normalized = normalizeExportsField(pkg.exports);
159
+ for (const [subpath, target] of normalized) {
160
+ const file = resolveTargetFile(repoPath, target, project);
161
+ if (file)
162
+ entries.push({ subpath, file });
163
+ }
164
+ return entries;
165
+ }
166
+ const fallback = pkg.main ?? pkg.module;
167
+ if (fallback) {
168
+ const file = resolveTargetFile(repoPath, fallback, project);
169
+ if (file)
170
+ entries.push({ subpath: ".", file });
171
+ }
172
+ else {
173
+ for (const candidate of INDEX_NAMES.map((n) => path.join("src", n)).concat(INDEX_NAMES)) {
174
+ const file = resolveTargetFile(repoPath, candidate, project);
175
+ if (file) {
176
+ entries.push({ subpath: ".", file });
177
+ break;
178
+ }
179
+ }
180
+ }
181
+ return entries;
182
+ }
183
+ function normalizeExportsField(field) {
184
+ const out = [];
185
+ if (typeof field === "string") {
186
+ out.push([".", field]);
187
+ return out;
188
+ }
189
+ if (typeof field !== "object" || field === null)
190
+ return out;
191
+ for (const [subpath, value] of Object.entries(field)) {
192
+ if (subpath.includes("*"))
193
+ continue;
194
+ if (typeof value === "string") {
195
+ out.push([subpath, value]);
196
+ continue;
197
+ }
198
+ if (typeof value !== "object" || value === null)
199
+ continue;
200
+ const conditions = value;
201
+ const chosen = (typeof conditions.import === "string" && conditions.import) ||
202
+ (typeof conditions.default === "string" && conditions.default) ||
203
+ (typeof conditions.require === "string" && conditions.require);
204
+ if (typeof chosen === "string" && chosen)
205
+ out.push([subpath, chosen]);
206
+ }
207
+ return out;
208
+ }
209
+ function resolveTargetFile(repoPath, target, project) {
210
+ if (typeof target !== "string" || !target)
211
+ return undefined;
212
+ let rel = target.replace(/^\.\//, "").split(/[?#]/)[0];
213
+ if (rel.endsWith(".d.ts"))
214
+ return undefined;
215
+ const candidates = [rel];
216
+ const distMatch = rel.match(/^(dist|build)(\/|$)/);
217
+ if (distMatch) {
218
+ candidates.push("src" + rel.slice(distMatch[1].length));
219
+ }
220
+ for (const candidate of candidates) {
221
+ const stem = candidate.replace(/\.[^.]+$/, "");
222
+ const tries = new Set([candidate]);
223
+ for (const ext of ENTRY_EXTENSIONS)
224
+ tries.add(stem + ext);
225
+ for (const idx of INDEX_NAMES)
226
+ tries.add(path.posix.join(candidate, idx));
227
+ for (const tryPath of tries) {
228
+ if (tryPath.endsWith(".d.ts"))
229
+ continue;
230
+ const abs = path.resolve(repoPath, tryPath);
231
+ if (!fs.existsSync(abs) || fs.statSync(abs).isDirectory())
232
+ continue;
233
+ const sf = project.getSourceFile(abs);
234
+ if (sf)
235
+ return sf;
236
+ }
237
+ }
238
+ return undefined;
239
+ }
240
+ export function validateExportsMap(map, nodeIds) {
241
+ const dangling = [];
242
+ for (const perPath of Object.values(map.exports)) {
243
+ for (const ids of Object.values(perPath)) {
244
+ for (const id of ids) {
245
+ if (!nodeIds.has(id))
246
+ dangling.push(id);
247
+ }
248
+ }
249
+ }
250
+ return [...new Set(dangling)];
251
+ }
252
+ export function exportsMapStats(map) {
253
+ let names = 0;
254
+ let ambiguous = 0;
255
+ for (const perPath of Object.values(map.exports))
256
+ names += Object.keys(perPath).length;
257
+ for (const list of Object.values(map.ambiguousNames))
258
+ ambiguous += list.length;
259
+ return { subpaths: Object.keys(map.exports).length, names, ambiguous };
260
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,324 @@
1
+ import fs from "fs";
2
+ import os from "os";
3
+ import path from "path";
4
+ import { parseRepo } from "./index.js";
5
+ import { buildLookupMaps } from "../graph/buildLookup.js";
6
+ import { detectImportEdges } from "../graph/edges/importEdges.js";
7
+ import { detectCallEdges } from "../graph/edges/callEdges.js";
8
+ function createFakeRepo(files) {
9
+ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), "devlens-exports-map-test-"));
10
+ for (const [filePath, content] of Object.entries(files)) {
11
+ const fullPath = path.join(tmpDir, filePath);
12
+ fs.mkdirSync(path.dirname(fullPath), { recursive: true });
13
+ fs.writeFileSync(fullPath, content);
14
+ }
15
+ return tmpDir;
16
+ }
17
+ function deleteFakeRepo(repoPath) {
18
+ fs.rmSync(repoPath, { recursive: true, force: true });
19
+ }
20
+ describe("exports map (cross-repo linkage)", () => {
21
+ it("returns undefined when the repo has no package.json", () => {
22
+ const repo = createFakeRepo({
23
+ "src/a.ts": `export function alpha() { return 1; }\n`,
24
+ });
25
+ const result = parseRepo(repo);
26
+ expect(result.exports).toBeUndefined();
27
+ deleteFakeRepo(repo);
28
+ });
29
+ it("maps direct exports to single-element arrays of proper nodeIds", () => {
30
+ const repo = createFakeRepo({
31
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
32
+ "src/index.ts": `export { alpha } from './a';\n`,
33
+ "src/a.ts": `export function alpha() { return 1; }\n`,
34
+ });
35
+ const { exports } = parseRepo(repo);
36
+ expect(exports).toBeDefined();
37
+ expect(exports.exports["."]["alpha"]).toEqual(["src/a.ts::alpha"]);
38
+ expect(exports.ambiguousNames).toEqual({});
39
+ deleteFakeRepo(repo);
40
+ });
41
+ it("flattens chained re-exports to the final definition node", () => {
42
+ const repo = createFakeRepo({
43
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
44
+ "src/index.ts": `export { beta } from './helpers';\n`,
45
+ "src/helpers.ts": `export { beta } from './dateUtils';\n`,
46
+ "src/dateUtils.ts": `export function beta(d: string) { return d; }\n`,
47
+ });
48
+ const { exports } = parseRepo(repo);
49
+ expect(exports.exports["."]["beta"]).toEqual(["src/dateUtils.ts::beta"]);
50
+ deleteFakeRepo(repo);
51
+ });
52
+ it("keys the map by the name the importer sees when aliases are involved", () => {
53
+ const repo = createFakeRepo({
54
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
55
+ "src/index.ts": `export { formatDate as fmtDate } from './dateUtils';\n`,
56
+ "src/dateUtils.ts": `export function formatDate(d: string) { return d; }\n`,
57
+ });
58
+ const { exports } = parseRepo(repo);
59
+ expect(exports.exports["."]["fmtDate"]).toEqual(["src/dateUtils.ts::formatDate"]);
60
+ expect(exports.exports["."]["formatDate"]).toBeUndefined();
61
+ deleteFakeRepo(repo);
62
+ });
63
+ it("handles the name-swap case via bindings, never by string matching", () => {
64
+ const repo = createFakeRepo({
65
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
66
+ "src/index.ts": `
67
+ export { formatDate as fmtDate } from './dateUtils';
68
+ export { fmtDate as formatDate } from './misc';
69
+ `,
70
+ "src/dateUtils.ts": `export function formatDate(d: string) { return d; }\n`,
71
+ "src/misc.ts": `export function fmtDate(x: number) { return x; }\n`,
72
+ });
73
+ const { exports } = parseRepo(repo);
74
+ expect(exports.exports["."]["fmtDate"]).toEqual(["src/dateUtils.ts::formatDate"]);
75
+ expect(exports.exports["."]["formatDate"]).toEqual(["src/misc.ts::fmtDate"]);
76
+ expect(exports.ambiguousNames).toEqual({});
77
+ deleteFakeRepo(repo);
78
+ });
79
+ it("emits one map key per package.json exports subpath", () => {
80
+ const repo = createFakeRepo({
81
+ "package.json": JSON.stringify({
82
+ name: "fakepkg",
83
+ exports: {
84
+ ".": "./src/index.ts",
85
+ "./server": "./src/server/index.ts",
86
+ },
87
+ }),
88
+ "src/index.ts": `export { alpha } from './a';\n`,
89
+ "src/a.ts": `export function alpha() { return 1; }\n`,
90
+ "src/server/index.ts": `export { serve } from './serve';\n`,
91
+ "src/server/serve.ts": `export function serve() { return 2; }\n`,
92
+ });
93
+ const { exports } = parseRepo(repo);
94
+ expect(exports.exports["."]["alpha"]).toEqual(["src/a.ts::alpha"]);
95
+ expect(exports.exports["./server"]["serve"]).toEqual(["src/server/serve.ts::serve"]);
96
+ deleteFakeRepo(repo);
97
+ });
98
+ it("resolves dist entry targets back to src (same coordinate system as nodes)", () => {
99
+ const repo = createFakeRepo({
100
+ "package.json": JSON.stringify({
101
+ name: "fakepkg",
102
+ exports: { ".": { import: "./dist/index.js", types: "./dist/index.d.ts" } },
103
+ }),
104
+ "src/index.ts": `export { alpha } from './a';\n`,
105
+ "src/a.ts": `export function alpha() { return 1; }\n`,
106
+ });
107
+ const { exports } = parseRepo(repo);
108
+ expect(exports.exports["."]["alpha"]).toEqual(["src/a.ts::alpha"]);
109
+ deleteFakeRepo(repo);
110
+ });
111
+ it("falls back to main/module when the exports field is absent", () => {
112
+ const repo = createFakeRepo({
113
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./dist/index.js" }),
114
+ "src/index.ts": `export { alpha } from './a';\n`,
115
+ "src/a.ts": `export function alpha() { return 1; }\n`,
116
+ });
117
+ const { exports } = parseRepo(repo);
118
+ expect(exports.exports["."]["alpha"]).toEqual(["src/a.ts::alpha"]);
119
+ deleteFakeRepo(repo);
120
+ });
121
+ it("emits distinct #arity suffixed nodeIds for same-name different-arity exports", () => {
122
+ const repo = createFakeRepo({
123
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
124
+ "src/index.ts": `export { formatValue } from './arity';\n`,
125
+ "src/arity.ts": `
126
+ export function formatValue(v: string) { return v.trim(); }
127
+ export function formatValue(v: string, n: number) { return v.repeat(n); }
128
+ `,
129
+ });
130
+ const { exports } = parseRepo(repo);
131
+ const ids = exports.exports["."]["formatValue"].sort();
132
+ expect(ids).toEqual(["src/arity.ts::formatValue#1", "src/arity.ts::formatValue#2"]);
133
+ expect(exports.ambiguousNames["."]).toBeUndefined();
134
+ deleteFakeRepo(repo);
135
+ });
136
+ it("emits distinct sig8-suffixed nodeIds for same-arity different-type exports", () => {
137
+ const repo = createFakeRepo({
138
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
139
+ "src/index.ts": `export { decode } from './types';\n`,
140
+ "src/types.ts": `
141
+ export function decode(input: string, mode: string) { return input; }
142
+ export function decode(input: number, mode: string) { return String(input); }
143
+ `,
144
+ });
145
+ const { exports } = parseRepo(repo);
146
+ const ids = exports.exports["."]["decode"].sort();
147
+ expect(ids.length).toBe(2);
148
+ expect(ids[0]).toMatch(/^src\/types\.ts::decode#2#[0-9a-f]{8}$/);
149
+ expect(ids[1]).toMatch(/^src\/types\.ts::decode#2#[0-9a-f]{8}$/);
150
+ expect(ids[0]).not.toBe(ids[1]);
151
+ deleteFakeRepo(repo);
152
+ });
153
+ it("excludes star re-export conflicts per the JS module spec (name absent, not guessed)", () => {
154
+ const repo = createFakeRepo({
155
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
156
+ "src/index.ts": `export * from './a';\nexport * from './b';\n`,
157
+ "src/a.ts": `export function clash() { return 1; }\nexport function onlyA() { return 2; }\n`,
158
+ "src/b.ts": `export function clash() { return 3; }\n`,
159
+ });
160
+ const { exports } = parseRepo(repo);
161
+ expect(exports.exports["."]["clash"]).toBeUndefined();
162
+ expect(exports.exports["."]["onlyA"]).toEqual(["src/a.ts::onlyA"]);
163
+ expect(exports.ambiguousNames["."]).toEqual(["clash"]);
164
+ deleteFakeRepo(repo);
165
+ });
166
+ it("resolves star re-exports like named ones", () => {
167
+ const repo = createFakeRepo({
168
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
169
+ "src/index.ts": `export * from './barrel';\n`,
170
+ "src/barrel.ts": `export * from './a';\n`,
171
+ "src/a.ts": `export function alpha() { return 1; }\n`,
172
+ });
173
+ const { exports } = parseRepo(repo);
174
+ expect(exports.exports["."]["alpha"]).toEqual(["src/a.ts::alpha"]);
175
+ deleteFakeRepo(repo);
176
+ });
177
+ it("maps named default exports and omits anonymous ones", () => {
178
+ const repo = createFakeRepo({
179
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
180
+ "src/index.ts": `
181
+ export { default as named } from './named';
182
+ export { default as anon } from './anon';
183
+ `,
184
+ "src/named.ts": `export default function loader() { return 1; }\n`,
185
+ "src/anon.ts": `export default () => 2;\n`,
186
+ });
187
+ const { exports } = parseRepo(repo);
188
+ expect(exports.exports["."]["named"]).toEqual(["src/named.ts::loader"]);
189
+ expect(exports.exports["."]["anon"]).toBeUndefined();
190
+ deleteFakeRepo(repo);
191
+ });
192
+ it("omits exports that have no node representation (consts, types, interfaces)", () => {
193
+ const repo = createFakeRepo({
194
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
195
+ "src/index.ts": `export * from './mixed';\n`,
196
+ "src/mixed.ts": `
197
+ export const config = { a: 1 };
198
+ export type Maybe = string | null;
199
+ export interface Opts { a: number }
200
+ export function run(o: Opts) { return o.a; }
201
+ `,
202
+ });
203
+ const { exports } = parseRepo(repo);
204
+ expect(exports.exports["."]["run"]).toEqual(["src/mixed.ts::run"]);
205
+ expect(exports.exports["."]["config"]).toBeUndefined();
206
+ expect(exports.exports["."]["Maybe"]).toBeUndefined();
207
+ deleteFakeRepo(repo);
208
+ });
209
+ it("survives re-export cycles without hanging and omits the undefined name", () => {
210
+ const repo = createFakeRepo({
211
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
212
+ "src/index.ts": `export { gone } from './a';\n`,
213
+ "src/a.ts": `export { gone } from './b';\n`,
214
+ "src/b.ts": `export { gone } from './a';\n`,
215
+ });
216
+ const { exports } = parseRepo(repo);
217
+ const gone = exports ? exports.exports["."]?.["gone"] : undefined;
218
+ expect(gone).toBeUndefined();
219
+ deleteFakeRepo(repo);
220
+ });
221
+ it("maps exported classes and exported const arrow functions to their nodes", () => {
222
+ const repo = createFakeRepo({
223
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
224
+ "src/index.ts": `export * from './things';\n`,
225
+ "src/things.ts": `
226
+ export class Widget { render() { return 1; } }
227
+ export const handler = () => 2;
228
+ `,
229
+ });
230
+ const { exports } = parseRepo(repo);
231
+ expect(exports.exports["."]["Widget"]).toEqual(["src/things.ts::Widget"]);
232
+ expect(exports.exports["."]["handler"]).toEqual(["src/things.ts::handler"]);
233
+ deleteFakeRepo(repo);
234
+ });
235
+ it("never references a nodeId that is absent from the nodes array", () => {
236
+ const repo = createFakeRepo({
237
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
238
+ "src/index.ts": `export * from './a';\nexport { beta } from './b';\n`,
239
+ "src/a.ts": `export function alpha() { return 1; }\n`,
240
+ "src/b.ts": `export function beta() { return 2; }\n`,
241
+ });
242
+ const result = parseRepo(repo);
243
+ const nodeIds = new Set(result.nodes.map((n) => n.id));
244
+ for (const perPath of Object.values(result.exports.exports)) {
245
+ for (const ids of Object.values(perPath)) {
246
+ for (const id of ids)
247
+ expect(nodeIds.has(id)).toBe(true);
248
+ }
249
+ }
250
+ deleteFakeRepo(repo);
251
+ });
252
+ it("resolves one condition per subpath: import beats require and types", () => {
253
+ const repo = createFakeRepo({
254
+ "package.json": JSON.stringify({
255
+ name: "fakepkg",
256
+ exports: {
257
+ ".": { import: "./src/index.ts", require: "./src/legacy.ts", types: "./src/index.d.ts" },
258
+ "./legacy": { require: "./src/legacy.ts" },
259
+ "./types-only": { types: "./src/index.d.ts" },
260
+ },
261
+ }),
262
+ "src/index.ts": `export { alpha } from './a';\n`,
263
+ "src/a.ts": `export function alpha() { return 1; }\n`,
264
+ "src/legacy.ts": `export function alpha() { return 2; }\n`,
265
+ "src/index.d.ts": `export declare function alpha(): number;\n`,
266
+ });
267
+ const { exports } = parseRepo(repo);
268
+ expect(exports.exports["."]["alpha"]).toEqual(["src/a.ts::alpha"]);
269
+ expect(exports.exports["./legacy"]["alpha"]).toEqual(["src/legacy.ts::alpha"]);
270
+ expect(exports.exports["./types-only"]).toBeUndefined();
271
+ deleteFakeRepo(repo);
272
+ });
273
+ it("skips unparseable files and still builds the map from valid ones", () => {
274
+ const repo = createFakeRepo({
275
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
276
+ "src/index.ts": `export { alpha } from './a';\n`,
277
+ "src/a.ts": `export function alpha() { return 1; }\n`,
278
+ "src/broken.ts": `export function broken( {\n`,
279
+ });
280
+ const result = parseRepo(repo);
281
+ expect(result.exports).toBeDefined();
282
+ expect(result.exports.exports["."]["alpha"]).toEqual(["src/a.ts::alpha"]);
283
+ expect(result.exports.exports["."]["broken"]).toBeUndefined();
284
+ deleteFakeRepo(repo);
285
+ });
286
+ it("call-site narrowing picks within an exported overload group (map ids = edge targets)", () => {
287
+ const repo = createFakeRepo({
288
+ "package.json": JSON.stringify({ name: "fakepkg", main: "./src/index.ts" }),
289
+ "src/index.ts": `export { formatValue } from './overloads';\n`,
290
+ "src/overloads.ts": `
291
+ export function formatValue(v: string) { return v.trim(); }
292
+ export function formatValue(v: string, n: number) { return v.repeat(n); }
293
+ `,
294
+ "src/caller.ts": `
295
+ import { formatValue } from './index';
296
+ export function useOne() { return formatValue("x"); }
297
+ export function useTwo() { return formatValue("x", 3); }
298
+ `,
299
+ });
300
+ const parsed = parseRepo(repo);
301
+ const lookup = buildLookupMaps(parsed.nodes);
302
+ detectImportEdges(lookup, repo);
303
+ const callResult = detectCallEdges(parsed.nodes, lookup);
304
+ const { exports } = parsed;
305
+ const group = exports.exports["."]["formatValue"];
306
+ expect(group.length).toBe(2);
307
+ const targets = callResult.edges
308
+ .filter((e) => e.type === "CALLS" && e.from === "src/caller.ts::useOne")
309
+ .map((e) => e.to);
310
+ expect(targets.length).toBe(1);
311
+ expect(group).toContain(targets[0]);
312
+ const targetsTwo = callResult.edges
313
+ .filter((e) => e.type === "CALLS" && e.from === "src/caller.ts::useTwo")
314
+ .map((e) => e.to);
315
+ expect(targetsTwo.length).toBe(1);
316
+ expect(group).toContain(targetsTwo[0]);
317
+ expect(targets[0]).not.toBe(targetsTwo[0]);
318
+ // every narrowed target is a real node the map points at — no phantom ids
319
+ const nodeIds = new Set(parsed.nodes.map((n) => n.id));
320
+ for (const t of [...targets, ...targetsTwo])
321
+ expect(nodeIds.has(t)).toBe(true);
322
+ deleteFakeRepo(repo);
323
+ });
324
+ });
@@ -10,6 +10,17 @@ export interface ExtractedCallData {
10
10
  dependencyNames: string[];
11
11
  contextRefs: string[];
12
12
  }
13
+ export interface CallAccumulator {
14
+ sites: CallSite[];
15
+ hookCalls: string[];
16
+ apiCalls: string[];
17
+ dependencyNames: string[];
18
+ contextRefs: string[];
19
+ seenSites: Set<string>;
20
+ }
21
+ export declare function createCallAccumulator(): CallAccumulator;
22
+ export declare function classifyCall(call: any, acc: CallAccumulator): void;
23
+ export declare function finishCalls(acc: CallAccumulator): ExtractedCallData;
13
24
  export declare function extractCallsWithSites(node: any): ExtractedCallData;
14
25
  export declare function extractFunctionCalls(node: any): string[];
15
26
  export declare function hasErrorHandling(node: any): boolean;