@draht/coding-agent 2026.11.18 → 2026.11.19-1
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/CHANGELOG.md +36 -0
- package/bin/draht-kg.cjs +4782 -0
- package/bin/draht-tools.cjs +747 -47
- package/dist/bundle/chunks/{anthropic-messages-AC3PJZGW.js → anthropic-messages-7PTOA4ZA.js} +1 -1
- package/dist/bundle/chunks/{azure-openai-responses-2J7IW2GS.js → azure-openai-responses-CDW6FCMN.js} +1 -1
- package/dist/bundle/chunks/bedrock-converse-stream.js +21 -21
- package/dist/bundle/chunks/chunk-2JZLKNZ4.js +3 -0
- package/dist/bundle/chunks/{chunk-BE7TLAK6.js → chunk-3F4OOENR.js} +1 -1
- package/dist/bundle/chunks/{chunk-G7DJ6PIU.js → chunk-4FPBO5KP.js} +1 -1
- package/dist/bundle/chunks/chunk-6M3D4XVD.js +2 -0
- package/dist/bundle/chunks/{chunk-KVH4HR57.js → chunk-6WESOD4H.js} +1 -1
- package/dist/bundle/chunks/{chunk-CJE4DCYO.js → chunk-7HQNDYWE.js} +1 -1
- package/dist/bundle/chunks/{chunk-4CTNOZEB.js → chunk-B6OJ7QX4.js} +1 -1
- package/dist/bundle/chunks/chunk-BIXKXDGW.js +9 -0
- package/dist/bundle/chunks/{chunk-ZTXZVREE.js → chunk-BQMYLQSG.js} +8 -8
- package/dist/bundle/chunks/{chunk-AQ4POXF4.js → chunk-CAOOVEOM.js} +1 -1
- package/dist/bundle/chunks/{chunk-5K3TBDND.js → chunk-GAABJAG4.js} +3 -3
- package/dist/bundle/chunks/chunk-GBFVS2SF.js +12 -0
- package/dist/bundle/chunks/{chunk-ITP6IDBA.js → chunk-GQXRTK54.js} +2 -2
- package/dist/bundle/chunks/{chunk-XJIEDSG4.js → chunk-HKQ3PU5T.js} +1 -1
- package/dist/bundle/chunks/{chunk-4M4EEFE4.js → chunk-INKDO5TZ.js} +3 -3
- package/dist/bundle/chunks/{chunk-SFCIAZKL.js → chunk-JI5YDTBB.js} +1 -1
- package/dist/bundle/chunks/{chunk-QVGSXIKT.js → chunk-KFREX3FS.js} +1 -1
- package/dist/bundle/chunks/chunk-LO6CVU2L.js +6 -0
- package/dist/bundle/chunks/{chunk-H76YEAD6.js → chunk-MHNW355A.js} +1 -1
- package/dist/bundle/chunks/{chunk-6QA6EP5G.js → chunk-O2P5SYXT.js} +1 -1
- package/dist/bundle/chunks/{chunk-XY64T2H6.js → chunk-RI7HJ6AF.js} +10 -10
- package/dist/bundle/chunks/{chunk-XEZZ7MVH.js → chunk-WOIC265R.js} +31 -28
- package/dist/bundle/chunks/{chunk-GU2IUEJX.js → chunk-YOPX5URH.js} +1 -1
- package/dist/bundle/chunks/{cli-23A5TA2S.js → cli-QUYXUKRF.js} +1 -1
- package/dist/bundle/chunks/{cloudflare-workers-ai-system-one-XYXX3HPQ.js → cloudflare-workers-ai-system-one-W63EWPDW.js} +1 -1
- package/dist/bundle/chunks/{execute-YOMYOVUT.js → execute-KGJOWTPG.js} +1 -1
- package/dist/bundle/chunks/{google-generative-ai-KPJSGUNM.js → google-generative-ai-M5WKK5CW.js} +1 -1
- package/dist/bundle/chunks/{google-vertex-O2RTR7GI.js → google-vertex-UYNOGPMI.js} +1 -1
- package/dist/bundle/chunks/https-proxy-agent-WQIRL24U.js +13 -0
- package/dist/bundle/chunks/lib-ULKZYRGD.js +9 -0
- package/dist/bundle/chunks/{llama-cpp-classify-654QJMMO.js → llama-cpp-classify-SAUGLDPT.js} +1 -1
- package/dist/bundle/chunks/{mistral-conversations-RNE6RWOB.js → mistral-conversations-VQ3J6W5K.js} +1 -1
- package/dist/bundle/chunks/{multipart-parser-AZP5BX2W.js → multipart-parser-HMCXZ4BD.js} +1 -1
- package/dist/bundle/chunks/{node-BZAVEWJ3.js → node-DXZFQBEO.js} +1 -1
- package/dist/bundle/chunks/{node-C2JGH2OB.js → node-LBZLUIBE.js} +1 -1
- package/dist/bundle/chunks/{openai-codex-responses-XBJLRZBV.js → openai-codex-responses-JTGQLYC7.js} +1 -1
- package/dist/bundle/chunks/{openai-completions-FJF3ZAAN.js → openai-completions-WAW7BG4E.js} +1 -1
- package/dist/bundle/chunks/{openai-decisions-V3M7K4QS.js → openai-decisions-KHHHHMGW.js} +1 -1
- package/dist/bundle/chunks/{openai-responses-N3KEN2UE.js → openai-responses-GWADST54.js} +1 -1
- package/dist/bundle/chunks/{openrouter-images-N7JGL3Y5.js → openrouter-images-AP626CWG.js} +1 -1
- package/dist/bundle/chunks/{pi-messages-RYZ4IMCU.js → pi-messages-H5TYIEJT.js} +1 -1
- package/dist/bundle/chunks/{runtime-3ZSEARRN.js → runtime-73OLLYOI.js} +1 -1
- package/dist/bundle/chunks/{runtime-S5WBSLTE.js → runtime-FZKS2BWI.js} +1 -1
- package/dist/bundle/chunks/{settings-manager-H3QEOPQ3.js → settings-manager-CWRQW6PZ.js} +1 -1
- package/dist/bundle/chunks/src-KDGO7EGF.js +4 -0
- package/dist/bundle/chunks/{typesafe-system-one-Z3V75OVM.js → typesafe-system-one-RLPGOT75.js} +1 -1
- package/dist/bundle/chunks/{virtual-modules-E543D4YT.js → virtual-modules-5V4G5GQH.js} +1 -1
- package/dist/bundle/cli-runtime.js +1 -1
- package/dist/bundle/index.js +1 -1
- package/dist/bundle/rpc-entry.js +1 -1
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +3 -0
- package/dist/cli/args.js.map +1 -1
- package/dist/prompts/commands/map-codebase.md +3 -1
- package/docs/environment-variables.md +2 -0
- package/package.json +9 -9
- package/prompts/commands/map-codebase.md +3 -1
- package/dist/bundle/chunks/chunk-44YIE6WF.js +0 -9
- package/dist/bundle/chunks/chunk-6MSE6LZ3.js +0 -6
- package/dist/bundle/chunks/chunk-736QBNUZ.js +0 -12
- package/dist/bundle/chunks/chunk-BIBUE2PO.js +0 -2
- package/dist/bundle/chunks/chunk-TIA2T6EO.js +0 -3
- package/dist/bundle/chunks/https-proxy-agent-O2PVUIOI.js +0 -13
- package/dist/bundle/chunks/lib-XOG5PI3Y.js +0 -9
- package/dist/bundle/chunks/src-TIBHS42X.js +0 -4
- /package/dist/bundle/chunks/{chunk-OPLOTJXH.js → chunk-ATYP3OSV.js} +0 -0
- /package/dist/bundle/chunks/{chunk-PX3CDFUB.js → chunk-ITKDPD4D.js} +0 -0
- /package/dist/bundle/chunks/{chunk-ZZD35WKM.js → chunk-MOOADNVS.js} +0 -0
package/bin/draht-kg.cjs
ADDED
|
@@ -0,0 +1,4782 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* draht-kg — graphify-parity deterministic symbol-level knowledge graph.
|
|
6
|
+
*
|
|
7
|
+
* Second engine beside `map-graph` (which builds the module-level living map):
|
|
8
|
+
* here NODES are symbols (files, classes, functions, methods, types) and EDGES
|
|
9
|
+
* carry graphify's relation set (imports/imports_from/re_exports/dynamic_import/
|
|
10
|
+
* contains/defines/method/calls/indirect_call/inherits/implements/extends/
|
|
11
|
+
* instantiates/references) with the EXTRACTED / INFERRED / AMBIGUOUS confidence
|
|
12
|
+
* rubric. Everything is deterministic — no LLM, no network, no timestamps in
|
|
13
|
+
* output — and `graph.json` is schema-compatible with graphify's node-link
|
|
14
|
+
* format (directed, `nodes` + `edges`).
|
|
15
|
+
*
|
|
16
|
+
* Ports of graphify internals (Graphify-Labs/graphify @ main):
|
|
17
|
+
* - ids.py → normalizeId/makeId (NFKC, non-word→_, collapse, casefold)
|
|
18
|
+
* - serve.py → query stopwords, label scoring for `path` endpoints,
|
|
19
|
+
* BFS/DFS with hub guard, NODE/EDGE output (`query` ranks
|
|
20
|
+
* files itself: rankFiles)
|
|
21
|
+
* - affected.py → reverse traversal over the default relation set
|
|
22
|
+
* - analyze.py → god nodes, surprising connections, suggested questions,
|
|
23
|
+
* import cycles
|
|
24
|
+
* - cluster.py → deterministic communities + cohesion + hub labels
|
|
25
|
+
* - report.py → KG_REPORT.md section list
|
|
26
|
+
* - extract.py → node/edge emission contract (hand-written scanners per
|
|
27
|
+
* language stand in for tree-sitter)
|
|
28
|
+
*
|
|
29
|
+
* A Go port of this file lives in go/internal/kg (the `kg` command of the
|
|
30
|
+
* `draht-graph` binary, which draht-tools.cjs prefers when one is installed).
|
|
31
|
+
* The two must stay identical in output: go/internal/kg/parity_test.go compares
|
|
32
|
+
* graph.json, reports, exports and every subcommand's output byte for byte, so
|
|
33
|
+
* a change here needs the same change there.
|
|
34
|
+
*
|
|
35
|
+
* Zero runtime dependencies; Node >= 20. Loaded by draht-tools.cjs (`kg` command)
|
|
36
|
+
* and runnable standalone: `node draht-kg.cjs build`.
|
|
37
|
+
*/
|
|
38
|
+
|
|
39
|
+
const childProcess = require("node:child_process");
|
|
40
|
+
const crypto = require("node:crypto");
|
|
41
|
+
const fs = require("node:fs");
|
|
42
|
+
const path = require("node:path");
|
|
43
|
+
|
|
44
|
+
// ============================================================================
|
|
45
|
+
// Constants
|
|
46
|
+
// ============================================================================
|
|
47
|
+
|
|
48
|
+
const OUT_DIRNAME = path.join(".planning", "codebase");
|
|
49
|
+
const GRAPH_BASENAME = "graph.json";
|
|
50
|
+
|
|
51
|
+
const KG_IGNORES = new Set([
|
|
52
|
+
"node_modules", ".git", "dist", "build", "out", "target", ".next",
|
|
53
|
+
".turbo", ".cache", "coverage", ".nyc_output", ".planning",
|
|
54
|
+
".venv", "venv", "__pycache__", ".pytest_cache", ".idea", ".vscode",
|
|
55
|
+
".DS_Store", ".sst", ".astro", ".svelte-kit", "vendor", "tmp", ".tmp",
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
const KG_LANG_BY_EXT = {
|
|
59
|
+
".ts": "typescript", ".tsx": "typescript", ".mts": "typescript", ".cts": "typescript",
|
|
60
|
+
".js": "javascript", ".jsx": "javascript", ".mjs": "javascript", ".cjs": "javascript",
|
|
61
|
+
".py": "python", ".go": "go", ".rs": "rust",
|
|
62
|
+
};
|
|
63
|
+
const TS_LIKE = new Set(["typescript", "javascript"]);
|
|
64
|
+
const LANG_FAMILY = { typescript: "js", javascript: "js", python: "py", go: "go", rust: "rs" };
|
|
65
|
+
|
|
66
|
+
// graphify export.py:159 — filled when an edge carries no explicit score.
|
|
67
|
+
const CONFIDENCE_SCORE_DEFAULTS = { EXTRACTED: 1.0, INFERRED: 0.5, AMBIGUOUS: 0.2 };
|
|
68
|
+
// graphify extract.py — inferred cross-file call binding carries an explicit 0.8.
|
|
69
|
+
const INFERRED_CALL_SCORE = 0.8;
|
|
70
|
+
|
|
71
|
+
// graphify affected.py DEFAULT_AFFECTED_RELATIONS
|
|
72
|
+
const DEFAULT_AFFECTED_RELATIONS = [
|
|
73
|
+
"calls", "indirect_call", "references", "imports", "imports_from", "re_exports",
|
|
74
|
+
"inherits", "extends", "implements", "uses", "mixes_in", "embeds",
|
|
75
|
+
];
|
|
76
|
+
|
|
77
|
+
// graphify serve.py _QUERY_STOPWORDS (verbatim)
|
|
78
|
+
const QUERY_STOPWORDS = new Set([
|
|
79
|
+
"how", "what", "why", "when", "where", "which", "who", "whom", "whose",
|
|
80
|
+
"does", "did", "is", "are", "was", "were", "be", "been", "being",
|
|
81
|
+
"can", "could", "should", "would", "will", "shall", "may", "might", "must",
|
|
82
|
+
"has", "have", "had", "the", "and", "but", "not", "for", "from", "with",
|
|
83
|
+
"without", "into", "onto", "off", "that", "this", "these", "those", "there",
|
|
84
|
+
"here", "its", "their", "them", "they", "about", "any", "all", "some",
|
|
85
|
+
"work", "works", "working",
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
const EXACT_MATCH_BONUS = 1000.0;
|
|
89
|
+
const PREFIX_MATCH_BONUS = 100.0;
|
|
90
|
+
const SUBSTRING_MATCH_BONUS = 1.0;
|
|
91
|
+
const SOURCE_MATCH_BONUS = 0.5;
|
|
92
|
+
|
|
93
|
+
// Methods the languages' own objects have (arrays, strings, maps, promises,
|
|
94
|
+
// streams, emitters, dicts, files): a call to one of these on a receiver of
|
|
95
|
+
// unknown type says nothing about a repo class that happens to declare the
|
|
96
|
+
// same name.
|
|
97
|
+
const COMMON_METHOD_NAMES = new Set([
|
|
98
|
+
"push", "pop", "shift", "unshift", "map", "filter", "reduce", "forEach", "find", "findIndex", "findLast", "some", "every", "includes",
|
|
99
|
+
"indexOf", "lastIndexOf", "join", "split", "slice", "splice", "concat", "sort", "reverse", "keys", "values", "entries", "get", "set",
|
|
100
|
+
"has", "delete", "add", "clear", "then", "catch", "finally", "toString", "valueOf", "toJSON", "trim", "trimStart", "trimEnd", "replace",
|
|
101
|
+
"replaceAll", "match", "matchAll", "test", "exec", "startsWith", "endsWith", "padStart", "padEnd", "toLowerCase", "toUpperCase",
|
|
102
|
+
"substring", "substr", "charAt", "charCodeAt", "codePointAt", "flat", "flatMap", "fill", "from", "call", "apply", "bind", "on", "off",
|
|
103
|
+
"once", "emit", "addEventListener", "removeEventListener", "dispatchEvent", "addListener", "removeListener", "log", "error", "warn",
|
|
104
|
+
"info", "debug", "trace", "write", "read", "end", "close", "open", "send", "next", "return", "throw", "resolve", "reject", "parse",
|
|
105
|
+
"stringify", "assign", "freeze", "append", "remove", "update", "insert", "extend", "items", "copy", "format", "encode", "decode",
|
|
106
|
+
"strip", "lstrip", "rstrip", "lower", "upper", "count", "index", "json", "text", "blob", "arrayBuffer", "abort", "cancel", "destroy",
|
|
107
|
+
"pipe", "unref", "ref", "kill", "exit", "start", "stop", "run", "reset", "init", "setup", "render", "focus", "blur", "click",
|
|
108
|
+
"toFixed", "toISOString", "getTime", "localeCompare", "normalize", "repeat", "search", "isdigit", "isalpha", "splitlines", "title",
|
|
109
|
+
"setdefault", "popitem", "discard", "union", "intersection", "difference", "seek", "tell", "flush", "readline", "readlines", "writelines",
|
|
110
|
+
"subscribe", "unsubscribe", "connect", "disconnect", "listen", "getAttribute", "setAttribute", "querySelector", "querySelectorAll",
|
|
111
|
+
"appendChild", "removeChild", "preventDefault", "stopPropagation", "toBe", "toEqual", "mockReturnValue", "mockResolvedValue",
|
|
112
|
+
"address", "clone", "formData", "getReader", "releaseLock", "enqueue", "terminate", "postMessage", "toLocaleString", "getItem",
|
|
113
|
+
"setItem", "removeItem", "command", "option", "argument", "group", "route", "headers", "status", "redirect", "cookie", "header",
|
|
114
|
+
]);
|
|
115
|
+
|
|
116
|
+
// Control-flow keywords that look like calls in `name(` scans.
|
|
117
|
+
const CALL_KEYWORDS = new Set([
|
|
118
|
+
"if", "else", "for", "while", "switch", "catch", "try", "do", "return", "throw",
|
|
119
|
+
"new", "await", "async", "typeof", "instanceof", "void", "delete", "yield", "case",
|
|
120
|
+
"function", "class", "const", "let", "var", "constructor", "super", "this", "self",
|
|
121
|
+
"def", "elif", "except", "with", "assert", "lambda", "raise", "match", "loop", "fn",
|
|
122
|
+
"impl", "pub", "use", "mod", "func", "go", "defer", "select", "chan", "interface",
|
|
123
|
+
"struct", "enum", "trait", "type", "in", "of", "as", "satisfies", "declare", "export",
|
|
124
|
+
]);
|
|
125
|
+
|
|
126
|
+
// graphify analyze.py noise labels for god-node filtering (subset).
|
|
127
|
+
const BUILTIN_NOISE_LABELS = new Set([
|
|
128
|
+
"str", "int", "float", "bool", "list", "dict", "set", "tuple", "Path", "Mock",
|
|
129
|
+
"string", "number", "boolean", "object", "any", "unknown", "void", "null", "undefined",
|
|
130
|
+
"Error", "Option", "Result", "Vec", "String", "error",
|
|
131
|
+
]);
|
|
132
|
+
|
|
133
|
+
// ============================================================================
|
|
134
|
+
// ID canonicalization — port of graphify ids.py
|
|
135
|
+
// ============================================================================
|
|
136
|
+
|
|
137
|
+
function normalizeId(s) {
|
|
138
|
+
s = String(s).normalize("NFKC");
|
|
139
|
+
s = s.replace(/[^\p{L}\p{N}_]+/gu, "_");
|
|
140
|
+
s = s.replace(/_+/g, "_");
|
|
141
|
+
return s.replace(/^_+|_+$/g, "").toLowerCase();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function makeId(...parts) {
|
|
145
|
+
return normalizeId(parts.filter(Boolean).map((p) => String(p).replace(/^[_.]+|[_.]+$/g, "")).join("_"));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Full repo-relative path, extension dropped (extractors/base.py _file_stem).
|
|
149
|
+
function fileStem(rel) {
|
|
150
|
+
return rel.replace(/\.[^/.]+$/, "");
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function fileNodeId(rel) {
|
|
154
|
+
return makeId(fileStem(rel));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function symbolNodeId(rel, name) {
|
|
158
|
+
return makeId(fileStem(rel), name);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Method name → node id for one class. makeId drops `_`, `$` and `#`, so
|
|
162
|
+
// `close()` / `#close()` or `load()` / `_load()` in one class would share
|
|
163
|
+
// an id and the second declaration would vanish. graphify's rule
|
|
164
|
+
// (extractors/engine.py _underscore_salted_nid, _js_member_nid): the one
|
|
165
|
+
// public name of such a group keeps the plain id, every other name gets a
|
|
166
|
+
// suffix taken from its own name, so the result never depends on declaration
|
|
167
|
+
// order. Accessor pairs and overloads are one name, not a collision. Applies
|
|
168
|
+
// where graphify's ids were checked: JS/TS and Python.
|
|
169
|
+
const PRIVATE_MEMBER_PREFIX = { js: /^[_$#]/, py: /^_/, go: /^[^A-Z]/ };
|
|
170
|
+
function methodNodeIds(rel, cls, lang) {
|
|
171
|
+
const privatePrefix = PRIVATE_MEMBER_PREFIX[LANG_FAMILY[lang]];
|
|
172
|
+
const groups = new Map(); // plain id -> distinct names
|
|
173
|
+
for (const meth of cls.methods) {
|
|
174
|
+
const id = makeId(fileStem(rel), cls.name, meth.name);
|
|
175
|
+
if (!groups.has(id)) groups.set(id, new Set());
|
|
176
|
+
groups.get(id).add(meth.name);
|
|
177
|
+
}
|
|
178
|
+
const ids = new Map();
|
|
179
|
+
for (const [id, names] of groups) {
|
|
180
|
+
const isPublic = privatePrefix ? [...names].filter((n) => !privatePrefix.test(n)) : [];
|
|
181
|
+
for (const name of names) {
|
|
182
|
+
const plain = !privatePrefix || names.size < 2 || (isPublic.length === 1 && name === isPublic[0]);
|
|
183
|
+
ids.set(name, plain ? id : makeId(id, crypto.createHash("sha1").update(name, "utf8").digest("hex").slice(0, 6)));
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return ids;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// File-level names → node ids, by the same rule: names of one file that would
|
|
190
|
+
// share an id (Go's `ResponseWriter` / `responseWriter`, a class and a
|
|
191
|
+
// same-named constant in another case) each get their own.
|
|
192
|
+
function fileSymbolIds(rel, names, lang) {
|
|
193
|
+
const privatePrefix = PRIVATE_MEMBER_PREFIX[LANG_FAMILY[lang]];
|
|
194
|
+
const groups = new Map();
|
|
195
|
+
for (const name of names) {
|
|
196
|
+
const id = symbolNodeId(rel, name);
|
|
197
|
+
if (!groups.has(id)) groups.set(id, new Set());
|
|
198
|
+
groups.get(id).add(name);
|
|
199
|
+
}
|
|
200
|
+
const ids = new Map();
|
|
201
|
+
for (const [id, set] of groups) {
|
|
202
|
+
const isPublic = privatePrefix ? [...set].filter((n) => !privatePrefix.test(n)) : [];
|
|
203
|
+
for (const name of set) {
|
|
204
|
+
const plain = set.size < 2 || (isPublic.length === 1 && name === isPublic[0]);
|
|
205
|
+
ids.set(name, plain ? id : makeId(id, crypto.createHash("sha1").update(name, "utf8").digest("hex").slice(0, 6)));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return ids;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ============================================================================
|
|
212
|
+
// Repo walk
|
|
213
|
+
// ============================================================================
|
|
214
|
+
|
|
215
|
+
function findRepoRoot(startDir) {
|
|
216
|
+
let dir = path.resolve(startDir);
|
|
217
|
+
for (;;) {
|
|
218
|
+
if (fs.existsSync(path.join(dir, ".git"))) return dir;
|
|
219
|
+
const parent = path.dirname(dir);
|
|
220
|
+
if (parent === dir) break;
|
|
221
|
+
dir = parent;
|
|
222
|
+
}
|
|
223
|
+
dir = path.resolve(startDir);
|
|
224
|
+
for (;;) {
|
|
225
|
+
try {
|
|
226
|
+
const pj = JSON.parse(fs.readFileSync(path.join(dir, "package.json"), "utf-8"));
|
|
227
|
+
if (pj && pj.workspaces) return dir;
|
|
228
|
+
} catch { /* empty */ }
|
|
229
|
+
if (fs.existsSync(path.join(dir, "pnpm-workspace.yaml"))) return dir;
|
|
230
|
+
const parent = path.dirname(dir);
|
|
231
|
+
if (parent === dir) break;
|
|
232
|
+
dir = parent;
|
|
233
|
+
}
|
|
234
|
+
return path.resolve(startDir);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Paths ordered by their "/"-normalized form. Sorting the OS form would order
|
|
238
|
+
// `a\b.ts` against `a0.ts` differently from `a/b.ts` (backslash sorts after
|
|
239
|
+
// digits and capitals, slash before), and the first file wins an id collision
|
|
240
|
+
// — so the same repo must not index differently per OS.
|
|
241
|
+
function sortByPosixPath(paths, sep = path.sep) {
|
|
242
|
+
return paths
|
|
243
|
+
.map((p) => [sep === "/" ? p : p.split(sep).join("/"), p])
|
|
244
|
+
.sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0))
|
|
245
|
+
.map((x) => x[1]);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
// Code files of a git checkout: tracked files and untracked ones git does not
|
|
249
|
+
// ignore, so a tracked dot-directory counts and ignored build output does
|
|
250
|
+
// not. Null outside a git checkout (or without git): the directory walk and
|
|
251
|
+
// its ignore list take over. A checkout with no code file of its own is an
|
|
252
|
+
// empty list, not null: the walk would read what git ignores.
|
|
253
|
+
function gitFiles(root, maxFiles) {
|
|
254
|
+
if (!fs.existsSync(path.join(root, ".git"))) return null;
|
|
255
|
+
let out;
|
|
256
|
+
try {
|
|
257
|
+
const r = childProcess.spawnSync("git", ["-C", root, "ls-files", "-co", "--exclude-standard", "-z"], { encoding: "utf-8", maxBuffer: 1 << 28 });
|
|
258
|
+
if (r.status !== 0 || typeof r.stdout !== "string") return null;
|
|
259
|
+
out = r.stdout;
|
|
260
|
+
} catch { return null; }
|
|
261
|
+
const files = [];
|
|
262
|
+
for (const rel of out.split("\0")) {
|
|
263
|
+
if (!rel || rel.endsWith("/") || !KG_LANG_BY_EXT[path.extname(rel).toLowerCase()]) continue;
|
|
264
|
+
const parts = rel.split("/");
|
|
265
|
+
if (parts.includes("node_modules") || parts.includes(".git")) continue;
|
|
266
|
+
const full = path.join(root, ...parts);
|
|
267
|
+
try { if (!fs.statSync(full).isFile()) continue; } catch { continue; }
|
|
268
|
+
files.push(full);
|
|
269
|
+
if (files.length >= maxFiles) break;
|
|
270
|
+
}
|
|
271
|
+
return sortByPosixPath(files);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Write through a temporary file in the same directory and rename it into
|
|
275
|
+
// place: an interrupted build leaves the previous file whole, and a reader
|
|
276
|
+
// never sees a half-written one. The temporary name is short and does not
|
|
277
|
+
// build on the target's, which may already be as long as a name can be.
|
|
278
|
+
function writeFileAtomic(file, data) {
|
|
279
|
+
const tmp = path.join(path.dirname(file), ".kg-" + process.pid + ".tmp");
|
|
280
|
+
try {
|
|
281
|
+
fs.writeFileSync(tmp, data, "utf-8");
|
|
282
|
+
fs.renameSync(tmp, file);
|
|
283
|
+
} catch (err) {
|
|
284
|
+
try { fs.unlinkSync(tmp); } catch { /* empty */ }
|
|
285
|
+
throw err;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function walk(root, maxFiles = 20000) {
|
|
290
|
+
const tracked = gitFiles(root, maxFiles);
|
|
291
|
+
if (tracked) return tracked;
|
|
292
|
+
const files = [];
|
|
293
|
+
const stack = [root];
|
|
294
|
+
while (stack.length && files.length < maxFiles) {
|
|
295
|
+
const dir = stack.pop();
|
|
296
|
+
let entries;
|
|
297
|
+
try {
|
|
298
|
+
entries = fs.readdirSync(dir, { withFileTypes: true })
|
|
299
|
+
.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
300
|
+
} catch { continue; }
|
|
301
|
+
for (const e of entries) {
|
|
302
|
+
if (KG_IGNORES.has(e.name)) continue;
|
|
303
|
+
if (e.name.startsWith(".") && e.isDirectory()) continue;
|
|
304
|
+
const full = path.join(dir, e.name);
|
|
305
|
+
if (e.isDirectory()) stack.push(full);
|
|
306
|
+
else if (e.isFile() && KG_LANG_BY_EXT[path.extname(e.name).toLowerCase()]) files.push(full);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return sortByPosixPath(files);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// ============================================================================
|
|
313
|
+
// Source masking — ONE lexer pass (comments, strings, templates, regex literals)
|
|
314
|
+
// ============================================================================
|
|
315
|
+
|
|
316
|
+
// Keywords after which a `/` starts a regex literal instead of dividing.
|
|
317
|
+
const REGEX_PREV_WORDS = new Set([
|
|
318
|
+
"return", "typeof", "instanceof", "in", "of", "new", "delete", "void", "throw",
|
|
319
|
+
"case", "do", "else", "yield", "await",
|
|
320
|
+
]);
|
|
321
|
+
// Punctuation after which an expression may start (so `/` opens a regex).
|
|
322
|
+
const REGEX_PREV_CHARS = new Set(["(", ",", "=", ":", "[", "!", "&", "|", "?", "{", ";"]);
|
|
323
|
+
|
|
324
|
+
// Comments and string/regex/template TEXT become spaces — same length, same
|
|
325
|
+
// line breaks — so offsets, line numbers and indentation survive. Quote
|
|
326
|
+
// delimiters stay, and so does the code inside a template's `${ … }`
|
|
327
|
+
// (nested templates included). One pass decides what each character is: a
|
|
328
|
+
// `"src/*"` string cannot open a comment, a backtick inside a string or regex
|
|
329
|
+
// cannot open a template, and a `//` in a URL string is not a comment, which
|
|
330
|
+
// separate comment-then-string passes cannot guarantee across lines.
|
|
331
|
+
// Python: `#` comments and ''' / """ strings. Go: raw backtick strings and
|
|
332
|
+
// runes. Rust: nested block comments, raw strings, chars (a lifetime `'a` is
|
|
333
|
+
// not one). When `multiline` is an array, [start, end] offset pairs of
|
|
334
|
+
// multi-line (triple-quoted) strings are appended to it.
|
|
335
|
+
function maskSource(src, lang, multiline) {
|
|
336
|
+
const js = TS_LIKE.has(lang), py = lang === "python", go = lang === "go", rust = lang === "rust";
|
|
337
|
+
const n = src.length;
|
|
338
|
+
const out = [];
|
|
339
|
+
let flushed = 0;
|
|
340
|
+
const blank = (a, b) => {
|
|
341
|
+
if (b <= a) return;
|
|
342
|
+
out.push(src.slice(flushed, a), src.slice(a, b).replace(/[^\r\n]/g, " "));
|
|
343
|
+
flushed = b;
|
|
344
|
+
};
|
|
345
|
+
const eol = (from) => { const e = src.indexOf("\n", from); return e < 0 ? n : e; };
|
|
346
|
+
const tplDepth = []; // brace depth inside each open `${ … }`
|
|
347
|
+
let sig = -1; // index of the last significant code character
|
|
348
|
+
|
|
349
|
+
// Opening quote at `start`; returns the index after the closing quote.
|
|
350
|
+
// An unterminated single-line string ends at its line break.
|
|
351
|
+
const scanString = (start, quote, multi) => {
|
|
352
|
+
let j = start + 1;
|
|
353
|
+
while (j < n) {
|
|
354
|
+
const ch = src[j];
|
|
355
|
+
if (ch === "\\") { j += src[j + 1] === "\r" && src[j + 2] === "\n" ? 3 : 2; continue; }
|
|
356
|
+
if (ch === quote) { blank(start + 1, j); return j + 1; }
|
|
357
|
+
if (ch === "\n" && !multi) break;
|
|
358
|
+
j++;
|
|
359
|
+
}
|
|
360
|
+
j = Math.min(j, n);
|
|
361
|
+
blank(start + 1, j);
|
|
362
|
+
return j;
|
|
363
|
+
};
|
|
364
|
+
const scanPyString = (start, quote) => {
|
|
365
|
+
if (src[start + 1] !== quote || src[start + 2] !== quote) return scanString(start, quote, false);
|
|
366
|
+
let j = start + 3;
|
|
367
|
+
while (j < n) {
|
|
368
|
+
if (src[j] === "\\") { j += 2; continue; }
|
|
369
|
+
if (src[j] === quote && src[j + 1] === quote && src[j + 2] === quote) {
|
|
370
|
+
blank(start + 3, j);
|
|
371
|
+
if (multiline) multiline.push(start, j + 3);
|
|
372
|
+
return j + 3;
|
|
373
|
+
}
|
|
374
|
+
j++;
|
|
375
|
+
}
|
|
376
|
+
blank(start + 3, n);
|
|
377
|
+
if (multiline) multiline.push(start, n);
|
|
378
|
+
return n;
|
|
379
|
+
};
|
|
380
|
+
const scanRaw = (start, close) => {
|
|
381
|
+
const e = src.indexOf(close, start + 1);
|
|
382
|
+
const end = e < 0 ? n : e;
|
|
383
|
+
blank(start + 1, end);
|
|
384
|
+
return e < 0 ? n : e + close.length;
|
|
385
|
+
};
|
|
386
|
+
// Go rune / Rust char literal; -1 when the quote is not one (a Rust lifetime).
|
|
387
|
+
const scanChar = (start) => {
|
|
388
|
+
if (src[start + 1] === "\\") {
|
|
389
|
+
const e = src.indexOf("'", start + 3);
|
|
390
|
+
return e > 0 && e - start <= 12 && !src.slice(start, e).includes("\n") ? e + 1 : -1;
|
|
391
|
+
}
|
|
392
|
+
const hi = src.charCodeAt(start + 1);
|
|
393
|
+
const close = hi >= 0xd800 && hi <= 0xdbff ? start + 3 : start + 2;
|
|
394
|
+
return src[close] === "'" && src[start + 1] !== "\n" ? close + 1 : -1;
|
|
395
|
+
};
|
|
396
|
+
const scanBlock = (start) => {
|
|
397
|
+
let depth = 1, j = start + 2;
|
|
398
|
+
while (j < n && depth) {
|
|
399
|
+
if (rust && src[j] === "/" && src[j + 1] === "*") { depth++; j += 2; }
|
|
400
|
+
else if (src[j] === "*" && src[j + 1] === "/") { depth--; j += 2; }
|
|
401
|
+
else j++;
|
|
402
|
+
}
|
|
403
|
+
j = Math.min(j, n);
|
|
404
|
+
blank(start, j);
|
|
405
|
+
return j;
|
|
406
|
+
};
|
|
407
|
+
// Template text from `from` (after the opening backtick or a closing `}`)
|
|
408
|
+
// up to the closing backtick or the next `${`.
|
|
409
|
+
const scanTemplate = (from) => {
|
|
410
|
+
let j = from;
|
|
411
|
+
while (j < n) {
|
|
412
|
+
const ch = src[j];
|
|
413
|
+
if (ch === "\\") { j += 2; continue; }
|
|
414
|
+
if (ch === "`") { blank(from, j); sig = j; return j + 1; }
|
|
415
|
+
if (ch === "$" && src[j + 1] === "{") { blank(from, j + 2); tplDepth.push(0); return j + 2; }
|
|
416
|
+
j++;
|
|
417
|
+
}
|
|
418
|
+
blank(from, n);
|
|
419
|
+
return n;
|
|
420
|
+
};
|
|
421
|
+
const regexAllowed = (at) => {
|
|
422
|
+
let k = at - 1;
|
|
423
|
+
while (k >= 0 && (src[k] === " " || src[k] === "\t")) k--;
|
|
424
|
+
if (k < 0 || src[k] === "\n" || src[k] === "\r") return true; // first token on its line
|
|
425
|
+
if (sig < 0) return true;
|
|
426
|
+
const p = src[sig];
|
|
427
|
+
if (isIdentChar(p)) {
|
|
428
|
+
let s = sig;
|
|
429
|
+
while (s > 0 && isIdentChar(src[s - 1])) s--;
|
|
430
|
+
return REGEX_PREV_WORDS.has(src.slice(s, sig + 1));
|
|
431
|
+
}
|
|
432
|
+
if (p === ">") return src[sig - 1] === "="; // `=>`
|
|
433
|
+
return REGEX_PREV_CHARS.has(p);
|
|
434
|
+
};
|
|
435
|
+
// Regex literal opening at `start`; returns the index after its flags, or
|
|
436
|
+
// -1 when the line has no closing slash (so it was a division).
|
|
437
|
+
const scanRegex = (start) => {
|
|
438
|
+
let j = start + 1, inClass = false;
|
|
439
|
+
while (j < n) {
|
|
440
|
+
const ch = src[j];
|
|
441
|
+
if (ch === "\n") return -1;
|
|
442
|
+
if (ch === "\\") { j += 2; continue; }
|
|
443
|
+
if (inClass) { if (ch === "]") inClass = false; }
|
|
444
|
+
else if (ch === "[") inClass = true;
|
|
445
|
+
else if (ch === "/") {
|
|
446
|
+
if (j === start + 1) return -1;
|
|
447
|
+
blank(start + 1, j);
|
|
448
|
+
let e = j + 1;
|
|
449
|
+
while (e < n && /[A-Za-z]/.test(src[e])) e++;
|
|
450
|
+
return e;
|
|
451
|
+
}
|
|
452
|
+
j++;
|
|
453
|
+
}
|
|
454
|
+
return -1;
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
let i = 0;
|
|
458
|
+
if (src.startsWith("#!")) { i = eol(0); blank(0, i); }
|
|
459
|
+
while (i < n) {
|
|
460
|
+
const c = src[i];
|
|
461
|
+
if (c === " " || c === "\t" || c === "\n" || c === "\r") { i++; continue; }
|
|
462
|
+
if (isIdentChar(c)) {
|
|
463
|
+
let j = i + 1;
|
|
464
|
+
while (j < n && isIdentChar(src[j])) j++;
|
|
465
|
+
if (rust && (src[j] === '"' || src[j] === "#") && (src.slice(i, j) === "r" || src.slice(i, j) === "br")) {
|
|
466
|
+
let h = j;
|
|
467
|
+
while (src[h] === "#") h++;
|
|
468
|
+
if (src[h] === '"') {
|
|
469
|
+
i = scanRaw(h, '"' + "#".repeat(h - j));
|
|
470
|
+
sig = h;
|
|
471
|
+
continue;
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
sig = j - 1;
|
|
475
|
+
i = j;
|
|
476
|
+
continue;
|
|
477
|
+
}
|
|
478
|
+
if (c === "/" && !py) {
|
|
479
|
+
const d = src[i + 1];
|
|
480
|
+
if (d === "/") { const e = eol(i); blank(i, e); i = e; continue; }
|
|
481
|
+
if (d === "*") { i = scanBlock(i); continue; }
|
|
482
|
+
if (js && regexAllowed(i)) {
|
|
483
|
+
const e = scanRegex(i);
|
|
484
|
+
if (e > 0) { sig = i; i = e; continue; }
|
|
485
|
+
}
|
|
486
|
+
} else if (py && c === "#") {
|
|
487
|
+
const e = eol(i);
|
|
488
|
+
blank(i, e);
|
|
489
|
+
i = e;
|
|
490
|
+
continue;
|
|
491
|
+
} else if (c === '"' || (c === "'" && !rust && !go)) {
|
|
492
|
+
const start = i;
|
|
493
|
+
i = py ? scanPyString(i, c) : scanString(i, c, rust);
|
|
494
|
+
sig = start;
|
|
495
|
+
continue;
|
|
496
|
+
} else if (c === "'" && (rust || go)) {
|
|
497
|
+
const e = scanChar(i);
|
|
498
|
+
if (e > 0) { blank(i + 1, e - 1); sig = i; i = e; continue; }
|
|
499
|
+
} else if (c === "`" && js) {
|
|
500
|
+
i = scanTemplate(i + 1);
|
|
501
|
+
continue;
|
|
502
|
+
} else if (c === "`" && go) {
|
|
503
|
+
const start = i;
|
|
504
|
+
i = scanRaw(i, "`");
|
|
505
|
+
sig = start;
|
|
506
|
+
continue;
|
|
507
|
+
} else if (js && tplDepth.length && (c === "{" || c === "}")) {
|
|
508
|
+
if (c === "{") tplDepth[tplDepth.length - 1]++;
|
|
509
|
+
else if (tplDepth[tplDepth.length - 1] === 0) {
|
|
510
|
+
tplDepth.pop();
|
|
511
|
+
blank(i, i + 1);
|
|
512
|
+
i = scanTemplate(i + 1);
|
|
513
|
+
continue;
|
|
514
|
+
} else tplDepth[tplDepth.length - 1]--;
|
|
515
|
+
}
|
|
516
|
+
sig = i;
|
|
517
|
+
i++;
|
|
518
|
+
}
|
|
519
|
+
out.push(src.slice(flushed));
|
|
520
|
+
return out.join("");
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// Offset ranges of interface bodies and type-alias right-hand sides in masked
|
|
524
|
+
// TypeScript. Calls cannot occur there, so their member signatures
|
|
525
|
+
// (`save(): number;`) must not be scanned as call sites.
|
|
526
|
+
function typeDeclRanges(masked) {
|
|
527
|
+
const n = masked.length;
|
|
528
|
+
const ranges = [];
|
|
529
|
+
const declRe = /^[ \t]*(?:export\s+)?(?:default\s+)?(?:declare\s+)?(interface|type)\s+[A-Za-z_$][\w$]*/gm;
|
|
530
|
+
let m;
|
|
531
|
+
while ((m = declRe.exec(masked)) !== null) {
|
|
532
|
+
let j = declRe.lastIndex;
|
|
533
|
+
// Generic parameters and heritage clauses may hold braces and `=>`.
|
|
534
|
+
let angle = 0;
|
|
535
|
+
if (m[1] === "interface") {
|
|
536
|
+
for (; j < n; j++) {
|
|
537
|
+
const ch = masked[j];
|
|
538
|
+
if (ch === "<") angle++;
|
|
539
|
+
else if (ch === ">" && masked[j - 1] !== "=") angle = Math.max(0, angle - 1);
|
|
540
|
+
else if ((ch === "{" && !angle) || ch === ";") break;
|
|
541
|
+
}
|
|
542
|
+
if (masked[j] !== "{") continue;
|
|
543
|
+
let depth = 0, k = j;
|
|
544
|
+
for (; k < n; k++) {
|
|
545
|
+
if (masked[k] === "{") depth++;
|
|
546
|
+
else if (masked[k] === "}" && --depth === 0) break;
|
|
547
|
+
}
|
|
548
|
+
ranges.push([j + 1, Math.min(k, n)]);
|
|
549
|
+
declRe.lastIndex = Math.max(declRe.lastIndex, Math.min(k, n));
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
for (; j < n; j++) {
|
|
553
|
+
const ch = masked[j];
|
|
554
|
+
if (ch === "<") angle++;
|
|
555
|
+
else if (ch === ">" && masked[j - 1] !== "=") angle = Math.max(0, angle - 1);
|
|
556
|
+
else if (!angle && !/\s/.test(ch)) break;
|
|
557
|
+
}
|
|
558
|
+
if (masked[j] !== "=") continue;
|
|
559
|
+
const rhs = ++j;
|
|
560
|
+
let depth = 0;
|
|
561
|
+
for (; j < n; j++) {
|
|
562
|
+
const ch = masked[j];
|
|
563
|
+
if (ch === "(" || ch === "[" || ch === "{" || ch === "<") depth++;
|
|
564
|
+
else if (ch === ")" || ch === "]" || ch === "}") { if (--depth < 0) break; }
|
|
565
|
+
else if (ch === ">") { if (masked[j - 1] !== "=") depth = Math.max(0, depth - 1); }
|
|
566
|
+
else if (ch === ";" && !depth) break;
|
|
567
|
+
else if (ch === "\n" && !depth) {
|
|
568
|
+
// A line break ends the alias unless the type visibly goes on.
|
|
569
|
+
let p = j - 1;
|
|
570
|
+
while (p > rhs && /\s/.test(masked[p])) p--;
|
|
571
|
+
let q = j + 1;
|
|
572
|
+
while (q < n && /\s/.test(masked[q])) q++;
|
|
573
|
+
if (!"=|&?:,<(".includes(masked[p]) && !"|&?:.)".includes(masked[q] || "") && !(masked[p] === ">" && masked[p - 1] === "=")) break;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
ranges.push([rhs, Math.min(j, n)]);
|
|
577
|
+
declRe.lastIndex = Math.max(declRe.lastIndex, Math.min(j, n));
|
|
578
|
+
}
|
|
579
|
+
return ranges;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
// `text` with the given [start, end) ranges turned to spaces (line breaks kept).
|
|
583
|
+
function blankRanges(text, ranges) {
|
|
584
|
+
if (!ranges.length) return text;
|
|
585
|
+
const out = [];
|
|
586
|
+
let at = 0;
|
|
587
|
+
for (const [a, b] of [...ranges].sort((x, y) => x[0] - y[0] || y[1] - x[1])) {
|
|
588
|
+
if (a < at) continue;
|
|
589
|
+
out.push(text.slice(at, a), text.slice(a, b).replace(/[^\r\n]/g, " "));
|
|
590
|
+
at = b;
|
|
591
|
+
}
|
|
592
|
+
out.push(text.slice(at));
|
|
593
|
+
return out.join("");
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
// Line number (1-based) of a character offset, via precomputed line starts.
|
|
597
|
+
function lineIndex(content) {
|
|
598
|
+
const starts = [0];
|
|
599
|
+
for (let i = content.indexOf("\n"); i >= 0; i = content.indexOf("\n", i + 1)) starts.push(i + 1);
|
|
600
|
+
return (idx) => {
|
|
601
|
+
let lo = 0, hi = starts.length - 1;
|
|
602
|
+
while (lo < hi) {
|
|
603
|
+
const mid = (lo + hi + 1) >> 1;
|
|
604
|
+
if (starts[mid] <= idx) lo = mid; else hi = mid - 1;
|
|
605
|
+
}
|
|
606
|
+
return lo + 1;
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// ============================================================================
|
|
611
|
+
// Per-language declaration extraction (phase 1)
|
|
612
|
+
// ============================================================================
|
|
613
|
+
|
|
614
|
+
const MAX_SYMBOLS_PER_FILE = 5000;
|
|
615
|
+
// Methods one class keeps. A class that declares more is named in the
|
|
616
|
+
// `kg build` output instead of losing the rest silently.
|
|
617
|
+
const MAX_METHODS_PER_CLASS = 1000;
|
|
618
|
+
|
|
619
|
+
// Python words that start a statement which is not an assignment.
|
|
620
|
+
const PY_STATEMENT_WORDS = new Set([
|
|
621
|
+
"if", "elif", "else", "try", "except", "finally", "for", "while", "with", "def", "class", "return", "import", "from",
|
|
622
|
+
"lambda", "pass", "raise", "global", "nonlocal", "assert", "del", "async", "await", "match", "case", "yield", "not", "print",
|
|
623
|
+
]);
|
|
624
|
+
const TS_EXPRESSION_WORDS = new Set([
|
|
625
|
+
"return", "throw", "typeof", "void", "await", "yield", "new", "delete", "import",
|
|
626
|
+
"super", "this", "case", "catch", "do", "else", "try", "in", "instanceof",
|
|
627
|
+
]);
|
|
628
|
+
|
|
629
|
+
// Adds a method to its class; false (and counted) past the limit.
|
|
630
|
+
function addMethod(cls, meth) {
|
|
631
|
+
if (cls.methods.length >= MAX_METHODS_PER_CLASS) {
|
|
632
|
+
cls.methodsOverLimit = (cls.methodsOverLimit || 0) + 1;
|
|
633
|
+
return false;
|
|
634
|
+
}
|
|
635
|
+
cls.methods.push(meth);
|
|
636
|
+
return true;
|
|
637
|
+
}
|
|
638
|
+
function isIdentChar(c) {
|
|
639
|
+
return (c >= "a" && c <= "z") || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9") || c === "_" || c === "$";
|
|
640
|
+
}
|
|
641
|
+
// `implements A, B<T>, ns.C` (text up to the body brace) → ["A", "B", "ns"]:
|
|
642
|
+
// generic arguments removed, then each item's leading identifier.
|
|
643
|
+
function implementsList(text) {
|
|
644
|
+
let s = text || "";
|
|
645
|
+
while (/<[^<>]*>/.test(s)) s = s.replace(/<[^<>]*>/g, "");
|
|
646
|
+
return s.split(",").map((x) => (x.trim().match(/^[A-Za-z_$][\w$]*/) || [""])[0]).filter(Boolean);
|
|
647
|
+
}
|
|
648
|
+
// ============================================================================
|
|
649
|
+
// Bracket helpers (masked text)
|
|
650
|
+
// ============================================================================
|
|
651
|
+
|
|
652
|
+
// Index of the bracket that closes the one at `open`, or -1.
|
|
653
|
+
function matchClose(text, open) {
|
|
654
|
+
const o = text[open];
|
|
655
|
+
const c = o === "{" ? "}" : o === "(" ? ")" : o === "[" ? "]" : o === "<" ? ">" : null;
|
|
656
|
+
if (!c) return -1;
|
|
657
|
+
let depth = 0;
|
|
658
|
+
for (let i = open; i < text.length; i++) {
|
|
659
|
+
const ch = text[i];
|
|
660
|
+
if (ch === o) depth++;
|
|
661
|
+
else if (ch === c && --depth === 0) return i;
|
|
662
|
+
}
|
|
663
|
+
return -1;
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
// `text` split at the commas that separate parameters or pattern elements:
|
|
667
|
+
// outside every bracket, and outside the `<…>` of a type annotation. A `<`
|
|
668
|
+
// counts only between an element's `:` and its `=`: in a default value it
|
|
669
|
+
// is a comparison.
|
|
670
|
+
function splitBindings(text) {
|
|
671
|
+
const out = [];
|
|
672
|
+
let depth = 0, angle = 0, from = 0, mode = 0; // mode: 0 name, 1 type, 2 default value
|
|
673
|
+
for (let i = 0; i <= text.length; i++) {
|
|
674
|
+
const ch = text[i];
|
|
675
|
+
if (i === text.length || (ch === "," && depth === 0 && angle === 0)) {
|
|
676
|
+
out.push(text.slice(from, i));
|
|
677
|
+
from = i + 1;
|
|
678
|
+
mode = 0;
|
|
679
|
+
angle = 0;
|
|
680
|
+
} else if (ch === "(" || ch === "[" || ch === "{") depth++;
|
|
681
|
+
else if (ch === ")" || ch === "]" || ch === "}") depth--;
|
|
682
|
+
else if (depth !== 0) continue;
|
|
683
|
+
else if (ch === ":" && mode === 0) mode = 1;
|
|
684
|
+
else if (ch === "=" && text[i + 1] !== ">" && angle === 0) mode = 2;
|
|
685
|
+
else if (mode === 1 && ch === "<") angle++;
|
|
686
|
+
else if (mode === 1 && ch === ">" && text[i - 1] !== "=") angle = Math.max(0, angle - 1);
|
|
687
|
+
}
|
|
688
|
+
return out;
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
// The names a destructuring pattern binds (`inner` is the text between its
|
|
692
|
+
// brackets): `a`, the target of `key: b`, `...rest`, nested patterns. A
|
|
693
|
+
// default value (`cb = log`) binds nothing.
|
|
694
|
+
function patternNames(inner, out) {
|
|
695
|
+
for (const el of splitBindings(inner)) {
|
|
696
|
+
let depth = 0, eq = -1, colon = -1;
|
|
697
|
+
for (let i = 0; i < el.length && eq < 0; i++) {
|
|
698
|
+
const ch = el[i];
|
|
699
|
+
if (ch === "(" || ch === "[" || ch === "{") depth++;
|
|
700
|
+
else if (ch === ")" || ch === "]" || ch === "}") depth--;
|
|
701
|
+
else if (depth === 0 && ch === "=" && el[i + 1] !== ">") eq = i;
|
|
702
|
+
else if (depth === 0 && ch === ":" && colon < 0) colon = i;
|
|
703
|
+
}
|
|
704
|
+
const target = el.slice(colon + 1, eq < 0 ? el.length : eq);
|
|
705
|
+
const open = target.search(/[^\s.]/);
|
|
706
|
+
if (open < 0) continue;
|
|
707
|
+
if (target[open] === "{" || target[open] === "[") {
|
|
708
|
+
const close = matchClose(target, open);
|
|
709
|
+
patternNames(target.slice(open + 1, close < 0 ? target.length : close), out);
|
|
710
|
+
continue;
|
|
711
|
+
}
|
|
712
|
+
const m = /^[A-Za-z_$][\w$]*/.exec(target.slice(open));
|
|
713
|
+
if (m) out.push([m[0], null]);
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
// The names a TS/JS parameter list binds, each with its written type name
|
|
718
|
+
// (or null): `a`, `b?: Type`, `...rest`, `{ c, d: e }`. A type (`x: Foo | Bar`)
|
|
719
|
+
// and a default value (`cb = log`) bind nothing. The list may be cut short
|
|
720
|
+
// (the callers stop at the first `)`).
|
|
721
|
+
function paramBindings(list) {
|
|
722
|
+
const out = [];
|
|
723
|
+
for (const piece of splitBindings(list)) {
|
|
724
|
+
const m = piece.match(/^\s*(?:\.\.\.)?\s*(?:(?:public|private|protected|readonly|override)\s+)*([A-Za-z_$][\w$]*)\??\s*(?::\s*([A-Za-z_$][\w$.]*))?/);
|
|
725
|
+
if (m) { out.push([m[1], m[2] || null]); continue; }
|
|
726
|
+
const open = piece.search(/[^\s.]/);
|
|
727
|
+
if (open < 0 || (piece[open] !== "{" && piece[open] !== "[")) continue;
|
|
728
|
+
const close = matchClose(piece, open);
|
|
729
|
+
patternNames(piece.slice(open + 1, close < 0 ? piece.length : close), out);
|
|
730
|
+
}
|
|
731
|
+
return out;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
// Pieces of `text` split at commas outside every bracket.
|
|
735
|
+
function splitTopLevel(text) {
|
|
736
|
+
const out = [];
|
|
737
|
+
let depth = 0, from = 0;
|
|
738
|
+
for (let i = 0; i < text.length; i++) {
|
|
739
|
+
const ch = text[i];
|
|
740
|
+
if (ch === "(" || ch === "[" || ch === "{") depth++;
|
|
741
|
+
else if (ch === ")" || ch === "]" || ch === "}") depth--;
|
|
742
|
+
else if (ch === "," && depth === 0) { out.push(text.slice(from, i)); from = i + 1; }
|
|
743
|
+
}
|
|
744
|
+
out.push(text.slice(from));
|
|
745
|
+
return out;
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// ============================================================================
|
|
749
|
+
// Go declarations
|
|
750
|
+
// ============================================================================
|
|
751
|
+
|
|
752
|
+
const GO_TYPE_WORDS = new Set(["func", "map", "chan", "struct", "interface"]);
|
|
753
|
+
const GO_CALL_SKIP = new Set([
|
|
754
|
+
"if", "for", "switch", "select", "return", "func", "go", "defer", "range", "case", "else", "var", "const", "type",
|
|
755
|
+
"make", "new", "len", "cap", "append", "copy", "delete", "panic", "recover", "close", "print", "println", "min", "max", "clear",
|
|
756
|
+
"string", "int", "int8", "int16", "int32", "int64", "uint", "uint8", "uint16", "uint32", "uint64", "uintptr", "byte", "rune",
|
|
757
|
+
"float32", "float64", "complex64", "complex128", "bool", "error", "any",
|
|
758
|
+
]);
|
|
759
|
+
|
|
760
|
+
// A Go type expression reduced to the name a method is looked up on:
|
|
761
|
+
// `*pkg.T[K]` → "pkg.T"; `[]T` and `...T` → "[]T" (the elements are T). Null for
|
|
762
|
+
// maps, channels, function types and type literals.
|
|
763
|
+
function goTypeName(expr) {
|
|
764
|
+
let t = String(expr || "").trim();
|
|
765
|
+
let slice = false;
|
|
766
|
+
for (;;) {
|
|
767
|
+
if (t.startsWith("*")) t = t.slice(1).trim();
|
|
768
|
+
else if (t.startsWith("...")) { slice = true; t = t.slice(3).trim(); }
|
|
769
|
+
else if (t.startsWith("[")) {
|
|
770
|
+
const e = matchClose(t, 0);
|
|
771
|
+
if (e < 0) return null;
|
|
772
|
+
slice = true;
|
|
773
|
+
t = t.slice(e + 1).trim();
|
|
774
|
+
} else break;
|
|
775
|
+
}
|
|
776
|
+
if (t.startsWith("map[")) {
|
|
777
|
+
const e = matchClose(t, 3);
|
|
778
|
+
const v = e > 0 ? goTypeName(t.slice(e + 1)) : null;
|
|
779
|
+
return v ? "[]" + v.replace(/^\[\]/, "") : null;
|
|
780
|
+
}
|
|
781
|
+
const m = t.match(/^([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)?)/);
|
|
782
|
+
if (!m || GO_TYPE_WORDS.has(m[1])) return null;
|
|
783
|
+
return (slice ? "[]" : "") + m[1];
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
// `a, b int, c *T` → [{ name, type }], with Go's shared-type grouping. A list
|
|
787
|
+
// of bare types (unnamed parameters) gives [].
|
|
788
|
+
function goParams(text) {
|
|
789
|
+
const out = [];
|
|
790
|
+
let pending = [];
|
|
791
|
+
for (const raw of splitTopLevel(text)) {
|
|
792
|
+
const piece = raw.trim();
|
|
793
|
+
if (!piece) continue;
|
|
794
|
+
const m = piece.match(/^([A-Za-z_]\w*)\s+(\S[\s\S]*)$/);
|
|
795
|
+
if (m && !GO_TYPE_WORDS.has(m[1])) {
|
|
796
|
+
const type = goTypeName(m[2]);
|
|
797
|
+
for (const n of pending) out.push({ name: n, type });
|
|
798
|
+
pending = [];
|
|
799
|
+
out.push({ name: m[1], type });
|
|
800
|
+
} else if (/^[A-Za-z_]\w*$/.test(piece)) pending.push(piece);
|
|
801
|
+
else pending = [];
|
|
802
|
+
}
|
|
803
|
+
return out;
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// The first result type of a signature's result text (`*T`, `(T, error)`,
|
|
807
|
+
// `(x T, err error)`).
|
|
808
|
+
function goFirstResult(text) {
|
|
809
|
+
let t = String(text || "").trim();
|
|
810
|
+
if (!t) return null;
|
|
811
|
+
if (t.startsWith("(")) {
|
|
812
|
+
const e = matchClose(t, 0);
|
|
813
|
+
const first = (splitTopLevel(t.slice(1, e < 0 ? t.length : e))[0] || "").trim();
|
|
814
|
+
const m = first.match(/^([A-Za-z_]\w*)\s+(\S[\s\S]*)$/);
|
|
815
|
+
t = m && !GO_TYPE_WORDS.has(m[1]) ? m[2] : first;
|
|
816
|
+
}
|
|
817
|
+
return goTypeName(t);
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// Top-level Go declarations from masked source: functions and methods with
|
|
821
|
+
// receiver, parameter and result types; named types (struct fields and
|
|
822
|
+
// embedded types included); package-level constants and variables, single or
|
|
823
|
+
// in `( … )` groups. Offsets index `masked`.
|
|
824
|
+
function parseGoDecls(d, masked, lineOf) {
|
|
825
|
+
const pm = masked.match(/^package\s+([A-Za-z_]\w*)/m);
|
|
826
|
+
d.goPackage = pm ? pm[1] : "";
|
|
827
|
+
d.goMethods = [];
|
|
828
|
+
d.goVars = new Map(); // name -> { type, init }
|
|
829
|
+
const n = masked.length;
|
|
830
|
+
|
|
831
|
+
const addType = (name, at, rest, restAt) => {
|
|
832
|
+
// generic parameters, then an optional alias `=`
|
|
833
|
+
let r = rest, rAt = restAt;
|
|
834
|
+
const lead = r.match(/^\s*/)[0].length;
|
|
835
|
+
r = r.slice(lead); rAt += lead;
|
|
836
|
+
if (r.startsWith("[")) {
|
|
837
|
+
const e = matchClose(masked, rAt);
|
|
838
|
+
// `type A [4]int` is an array type, not generics: generics hold an identifier pair
|
|
839
|
+
if (e > 0 && /^\[\s*[A-Za-z_]\w*\s+[^\]]/.test(masked.slice(rAt, e + 1))) {
|
|
840
|
+
rAt = e + 1;
|
|
841
|
+
r = masked.slice(rAt, masked.indexOf("\n", rAt) < 0 ? n : masked.indexOf("\n", rAt));
|
|
842
|
+
const l2 = r.match(/^\s*/)[0].length;
|
|
843
|
+
r = r.slice(l2); rAt += l2;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
if (r.startsWith("=")) { const l3 = r.slice(1).match(/^\s*/)[0].length + 1; r = r.slice(l3); rAt += l3; }
|
|
847
|
+
const kind = /^struct\b/.test(r) ? "struct" : /^interface\b/.test(r) ? "interface" : "type";
|
|
848
|
+
const cls = { name, line: lineOf(at), end: null, kind, extends: null, implements: [], methods: [], fields: new Map(), embeds: [], bodyStart: -1, bodyEnd: -1 };
|
|
849
|
+
if (kind !== "type") {
|
|
850
|
+
const open = masked.indexOf("{", rAt);
|
|
851
|
+
const close = open >= 0 ? matchClose(masked, open) : -1;
|
|
852
|
+
if (open >= 0 && close > open) {
|
|
853
|
+
cls.bodyStart = open; cls.bodyEnd = close; cls.end = lineOf(close);
|
|
854
|
+
let depth = 0, lineStart = open + 1;
|
|
855
|
+
for (let i = open + 1; i <= close; i++) {
|
|
856
|
+
const ch = masked[i];
|
|
857
|
+
if (ch === "\n" || i === close) {
|
|
858
|
+
if (depth === 0) {
|
|
859
|
+
const ln = masked.slice(lineStart, i).trim();
|
|
860
|
+
const em = ln.match(/^\*?\s*([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)?)(?:\[[^\]]*\])?\s*(?:"\s*"|`\s*`)?$/);
|
|
861
|
+
if (em) cls.embeds.push(em[1]);
|
|
862
|
+
else if (kind === "struct") {
|
|
863
|
+
const fm = ln.match(/^([A-Za-z_]\w*(?:\s*,\s*[A-Za-z_]\w*)*)\s+(\S.*)$/);
|
|
864
|
+
if (fm) { const t = goTypeName(fm[2]); if (t) for (const f of fm[1].split(",")) cls.fields.set(f.trim(), t); }
|
|
865
|
+
}
|
|
866
|
+
}
|
|
867
|
+
lineStart = i + 1;
|
|
868
|
+
}
|
|
869
|
+
if (ch === "{" || ch === "(") depth++;
|
|
870
|
+
else if (ch === "}" || ch === ")") depth--;
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
} else {
|
|
874
|
+
const t = goTypeName(r);
|
|
875
|
+
if (t && !t.startsWith("[]")) cls.underlying = t;
|
|
876
|
+
}
|
|
877
|
+
d.classes.push(cls);
|
|
878
|
+
};
|
|
879
|
+
|
|
880
|
+
const addValues = (names, at, rest) => {
|
|
881
|
+
const list = names.split(",").map((s) => s.trim()).filter((s) => s && s !== "_");
|
|
882
|
+
for (const name of list) d.types.push({ name, line: lineOf(at), kind: "const" });
|
|
883
|
+
if (list.length !== 1) return;
|
|
884
|
+
const eq = rest.indexOf("=");
|
|
885
|
+
const typeText = (eq < 0 ? rest : rest.slice(0, eq)).trim();
|
|
886
|
+
const init = eq < 0 ? "" : rest.slice(eq + 1).trim();
|
|
887
|
+
d.goVars.set(list[0], { type: typeText ? goTypeName(typeText) : null, init });
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
const funcRe = /func\s*(?:\(([^)]*)\)\s*)?([A-Za-z_]\w*)\s*(\[[^\]]*\]\s*)?\(/y;
|
|
891
|
+
const addFunc = (at) => {
|
|
892
|
+
funcRe.lastIndex = at;
|
|
893
|
+
const m = funcRe.exec(masked);
|
|
894
|
+
if (!m) return at;
|
|
895
|
+
const paramsOpen = funcRe.lastIndex - 1;
|
|
896
|
+
const paramsClose = matchClose(masked, paramsOpen);
|
|
897
|
+
if (paramsClose < 0) return at;
|
|
898
|
+
let bodyOpen = -1, paren = 0, i = paramsClose + 1, word = "";
|
|
899
|
+
for (; i < n; i++) {
|
|
900
|
+
const ch = masked[i];
|
|
901
|
+
if (ch === "(" || ch === "[") paren++;
|
|
902
|
+
else if (ch === ")" || ch === "]") paren--;
|
|
903
|
+
else if (ch === "{") {
|
|
904
|
+
if (word === "interface" || word === "struct") { const e = matchClose(masked, i); if (e < 0) break; i = e; word = ""; continue; }
|
|
905
|
+
bodyOpen = i;
|
|
906
|
+
break;
|
|
907
|
+
} else if (ch === "\n" && paren <= 0) break;
|
|
908
|
+
if (isIdentChar(ch)) word = isIdentChar(masked[i - 1]) ? word + ch : ch;
|
|
909
|
+
else if (ch !== " " && ch !== "\t") word = "";
|
|
910
|
+
}
|
|
911
|
+
const bodyClose = bodyOpen >= 0 ? matchClose(masked, bodyOpen) : -1;
|
|
912
|
+
const fn = {
|
|
913
|
+
name: m[2], line: lineOf(at), end: bodyClose > 0 ? lineOf(bodyClose) : lineOf(i < n ? i : n - 1),
|
|
914
|
+
params: goParams(masked.slice(paramsOpen + 1, paramsClose)),
|
|
915
|
+
result: goFirstResult(masked.slice(paramsClose + 1, bodyOpen >= 0 ? bodyOpen : i)),
|
|
916
|
+
declStart: at, bodyStart: bodyOpen, bodyEnd: bodyClose,
|
|
917
|
+
};
|
|
918
|
+
if (m[1] !== undefined) {
|
|
919
|
+
const rm = m[1].match(/^\s*(?:([A-Za-z_]\w*)\s+)?\*?\s*([A-Za-z_]\w*)/);
|
|
920
|
+
if (rm) {
|
|
921
|
+
fn.recvName = rm[1] && rm[1] !== "_" ? rm[1] : null;
|
|
922
|
+
fn.recvType = rm[2];
|
|
923
|
+
d.goMethods.push(fn);
|
|
924
|
+
return bodyClose > 0 ? bodyClose : i;
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
d.functions.push(fn);
|
|
928
|
+
return bodyClose > 0 ? bodyClose : i;
|
|
929
|
+
};
|
|
930
|
+
|
|
931
|
+
// Walk line starts at brace depth 0; `group` is the keyword of an open `( … )` block.
|
|
932
|
+
let brace = 0, paren = 0, group = null, prevEnd = "", paren1Start = false;
|
|
933
|
+
let i = 0;
|
|
934
|
+
while (i < n) {
|
|
935
|
+
const eol = masked.indexOf("\n", i) < 0 ? n : masked.indexOf("\n", i);
|
|
936
|
+
const ln = masked.slice(i, eol);
|
|
937
|
+
let skipTo = -1;
|
|
938
|
+
if (brace === 0) {
|
|
939
|
+
let m;
|
|
940
|
+
if (paren === 0) {
|
|
941
|
+
group = null;
|
|
942
|
+
if (/^func\b/.test(ln)) skipTo = addFunc(i);
|
|
943
|
+
else if ((m = ln.match(/^type\s+([A-Za-z_]\w*)/))) addType(m[1], i, ln.slice(m[0].length), i + m[0].length);
|
|
944
|
+
else if ((m = ln.match(/^(type|const|var)\s*\(/))) { group = m[1]; paren1Start = true; }
|
|
945
|
+
else if ((m = ln.match(/^(?:const|var)\s+([A-Za-z_]\w*(?:\s*,\s*[A-Za-z_]\w*)*)/))) addValues(m[1], i, ln.slice(m[0].length));
|
|
946
|
+
} else if (paren === 1 && group) {
|
|
947
|
+
let m;
|
|
948
|
+
if (group === "type") {
|
|
949
|
+
if ((m = ln.match(/^\s*([A-Za-z_]\w*)\b/))) addType(m[1], i + ln.indexOf(m[1]), ln.slice(m[0].length), i + m[0].length);
|
|
950
|
+
} else if (paren1Start || !/[+\-*/%&|^<>=,]$/.test(prevEnd)) { if ((m = ln.match(/^\s*([A-Za-z_]\w*(?:\s*,\s*[A-Za-z_]\w*)*)\s*(?=$|=|[A-Za-z_*\[]|\/\/)/))) {
|
|
951
|
+
addValues(m[1], i + ln.indexOf(m[1]), ln.slice(m[0].length));
|
|
952
|
+
} }
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
if (skipTo > i) {
|
|
956
|
+
// a function was consumed up to its closing brace: resume on the next line
|
|
957
|
+
const next = masked.indexOf("\n", skipTo);
|
|
958
|
+
i = next < 0 ? n : next + 1;
|
|
959
|
+
prevEnd = "}";
|
|
960
|
+
continue;
|
|
961
|
+
}
|
|
962
|
+
for (let k = i; k < eol; k++) {
|
|
963
|
+
const ch = masked[k];
|
|
964
|
+
if (ch === "{") brace++;
|
|
965
|
+
else if (ch === "}") brace--;
|
|
966
|
+
else if (ch === "(") paren++;
|
|
967
|
+
else if (ch === ")") paren--;
|
|
968
|
+
}
|
|
969
|
+
const trimmed = ln.trimEnd();
|
|
970
|
+
if (trimmed) { prevEnd = trimmed[trimmed.length - 1]; if (!/^(?:type|const|var)\s*\($/.test(trimmed)) paren1Start = false; }
|
|
971
|
+
i = eol + 1;
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
// ============================================================================
|
|
976
|
+
// Rust declarations
|
|
977
|
+
// ============================================================================
|
|
978
|
+
|
|
979
|
+
// Index after the `<…>` group opening at `open` (the `>` of `->` and `=>`
|
|
980
|
+
// closes nothing), or -1.
|
|
981
|
+
function skipAngles(text, open) {
|
|
982
|
+
let depth = 0;
|
|
983
|
+
for (let i = open; i < text.length; i++) {
|
|
984
|
+
const ch = text[i];
|
|
985
|
+
if (ch === "<") depth++;
|
|
986
|
+
else if (ch === ">" && text[i - 1] !== "-" && text[i - 1] !== "=") { if (--depth === 0) return i + 1; }
|
|
987
|
+
else if (ch === "{" || ch === ";") return -1;
|
|
988
|
+
}
|
|
989
|
+
return -1;
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
// `a::b::{c, d as e, f::*, self}` → [{ segs, alias, glob }].
|
|
993
|
+
function rustUseTree(text, prefix, out) {
|
|
994
|
+
const t = text.trim();
|
|
995
|
+
if (!t) return out;
|
|
996
|
+
const brace = t.indexOf("{");
|
|
997
|
+
if (brace >= 0) {
|
|
998
|
+
const close = matchClose(t, brace);
|
|
999
|
+
const head = t.slice(0, brace).replace(/::\s*$/, "").trim();
|
|
1000
|
+
const base = head ? prefix.concat(head.split("::").map((s) => s.trim()).filter(Boolean)) : prefix;
|
|
1001
|
+
for (const piece of splitTopLevel(t.slice(brace + 1, close < 0 ? t.length : close))) rustUseTree(piece, base, out);
|
|
1002
|
+
return out;
|
|
1003
|
+
}
|
|
1004
|
+
const m = t.match(/^((?:[A-Za-z_]\w*\s*::\s*)*)(\*|[A-Za-z_]\w*)(?:\s+as\s+([A-Za-z_]\w*))?$/);
|
|
1005
|
+
if (!m) return out;
|
|
1006
|
+
const head = m[1].split("::").map((s) => s.trim()).filter(Boolean);
|
|
1007
|
+
if (m[2] === "*") out.push({ segs: prefix.concat(head), alias: null, glob: true });
|
|
1008
|
+
else if (m[2] === "self" && (prefix.length || head.length)) {
|
|
1009
|
+
const segs = prefix.concat(head);
|
|
1010
|
+
out.push({ segs, alias: m[3] || segs[segs.length - 1], glob: false });
|
|
1011
|
+
} else out.push({ segs: prefix.concat(head, [m[2]]), alias: m[3] || m[2], glob: false });
|
|
1012
|
+
return out;
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
// The name a type expression is known by: `&'a mut path::Type<T>` → "Type",
|
|
1016
|
+
// with its full path segments (`["path", "Type"]`).
|
|
1017
|
+
function rustTypePath(expr) {
|
|
1018
|
+
let t = String(expr || "").trim();
|
|
1019
|
+
for (;;) {
|
|
1020
|
+
const m = t.match(/^(?:&|\*\s*(?:const|mut)\b|'[A-Za-z_]\w*|mut\b|dyn\b|impl\b)\s*/);
|
|
1021
|
+
if (!m || !m[0]) break;
|
|
1022
|
+
t = t.slice(m[0].length);
|
|
1023
|
+
}
|
|
1024
|
+
const m = t.match(/^((?:[A-Za-z_]\w*\s*::\s*)*[A-Za-z_]\w*)/);
|
|
1025
|
+
return m ? m[1].split("::").map((s) => s.trim()) : null;
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
const RUST_ITEM_RE = /(?:pub(?:\s*\([^)]*\))?\s+)?(?:default\s+)?(?:(?:const|async|unsafe)\s+)*(?:extern\s*(?:"[^"]*"\s*)?)?(fn|struct|enum|union|trait|impl|mod|type|const|static|use|macro_rules\s*!|extern\s+crate)(?![\w])/y;
|
|
1029
|
+
|
|
1030
|
+
// Rust items from masked source, at any module depth: functions, structs,
|
|
1031
|
+
// enums, unions, traits (with their methods), impl blocks (methods grouped by
|
|
1032
|
+
// the type they are for), inline and file modules, type aliases, constants,
|
|
1033
|
+
// statics, macros and `use` trees. Offsets index `masked`.
|
|
1034
|
+
function parseRustDecls(d, masked, lineOf) {
|
|
1035
|
+
d.rsScopes = new Map(); // inline module path ("" | "a::b") -> scope
|
|
1036
|
+
d.rsImpls = [];
|
|
1037
|
+
d.rsNested = [];
|
|
1038
|
+
d.rsFns = [];
|
|
1039
|
+
d.rsSkip = []; // ranges that hold no calls: type declarations, macro definitions
|
|
1040
|
+
const n = masked.length;
|
|
1041
|
+
const scopeOf = (inl, parent) => {
|
|
1042
|
+
let s = d.rsScopes.get(inl);
|
|
1043
|
+
if (!s) d.rsScopes.set(inl, s = { inl, items: new Map(), mods: new Map(), modDecls: [], uses: new Map(), globs: [], parent });
|
|
1044
|
+
return s;
|
|
1045
|
+
};
|
|
1046
|
+
const nameAt = (i) => {
|
|
1047
|
+
const m = /\s*((?:r#)?[A-Za-z_]\w*)/y;
|
|
1048
|
+
m.lastIndex = i;
|
|
1049
|
+
const r = m.exec(masked);
|
|
1050
|
+
return r ? { name: r[1], end: m.lastIndex } : null;
|
|
1051
|
+
};
|
|
1052
|
+
// End of a statement starting at `i`: its `;`, or the brace that closes its first top-level block.
|
|
1053
|
+
const statementEnd = (i, stopAtBlock) => {
|
|
1054
|
+
for (let k = i; k < n; k++) {
|
|
1055
|
+
const ch = masked[k];
|
|
1056
|
+
if (ch === "(" || ch === "[") { const e = matchClose(masked, k); if (e < 0) return n; k = e; }
|
|
1057
|
+
else if (ch === "{") { const e = matchClose(masked, k); if (e < 0) return n; if (stopAtBlock) return e; k = e; }
|
|
1058
|
+
else if (ch === ";") return k;
|
|
1059
|
+
else if (ch === "}") return k - 1;
|
|
1060
|
+
}
|
|
1061
|
+
return n;
|
|
1062
|
+
};
|
|
1063
|
+
// Body of an item whose header starts at `i`: { open, close } or { end } at its `;`.
|
|
1064
|
+
const bodyOf = (i) => {
|
|
1065
|
+
for (let k = i; k < n; k++) {
|
|
1066
|
+
const ch = masked[k];
|
|
1067
|
+
if (ch === "(" || ch === "[") { const e = matchClose(masked, k); if (e < 0) break; k = e; }
|
|
1068
|
+
else if (ch === "<") { const e = skipAngles(masked, k); if (e > 0) k = e - 1; }
|
|
1069
|
+
else if (ch === "{") { const e = matchClose(masked, k); return e < 0 ? { end: n } : { open: k, close: e }; }
|
|
1070
|
+
else if (ch === ";") return { end: k };
|
|
1071
|
+
else if (ch === "}") return { end: k - 1 };
|
|
1072
|
+
}
|
|
1073
|
+
return { end: n };
|
|
1074
|
+
};
|
|
1075
|
+
const nestedFnRe = /\b(fn|struct|enum)\s+((?:r#)?[A-Za-z_]\w*)/g;
|
|
1076
|
+
const addFn = (at, nameInfo, inl, owner, list) => {
|
|
1077
|
+
const b = bodyOf(nameInfo.end);
|
|
1078
|
+
const fn = { name: nameInfo.name, line: lineOf(at), end: lineOf(b.close != null ? b.close : Math.min(b.end, n - 1)), declStart: at, bodyStart: b.open != null ? b.open : -1, bodyEnd: b.close != null ? b.close : -1, inl, owner };
|
|
1079
|
+
list.push(fn);
|
|
1080
|
+
d.rsFns.push(fn);
|
|
1081
|
+
if (fn.bodyStart >= 0) {
|
|
1082
|
+
nestedFnRe.lastIndex = fn.bodyStart;
|
|
1083
|
+
let m;
|
|
1084
|
+
while ((m = nestedFnRe.exec(masked)) !== null && m.index < fn.bodyEnd) {
|
|
1085
|
+
const nb = bodyOf(m.index + m[0].length);
|
|
1086
|
+
d.rsNested.push({ name: m[2], kind: m[1], line: lineOf(m.index), end: lineOf(nb.close != null ? nb.close : Math.min(nb.end, n - 1)), parent: fn });
|
|
1087
|
+
}
|
|
1088
|
+
}
|
|
1089
|
+
return b.close != null ? b.close : b.end;
|
|
1090
|
+
};
|
|
1091
|
+
|
|
1092
|
+
const scanItems = (from, to, inl, parent, ctx) => {
|
|
1093
|
+
const scope = ctx ? null : scopeOf(inl, parent);
|
|
1094
|
+
let i = from;
|
|
1095
|
+
while (i < to) {
|
|
1096
|
+
const ch = masked[i];
|
|
1097
|
+
if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r" || ch === ";") { i++; continue; }
|
|
1098
|
+
if (ch === "#") { // attribute
|
|
1099
|
+
const open = masked.indexOf("[", i);
|
|
1100
|
+
const e = open >= 0 && open <= i + 2 ? matchClose(masked, open) : -1;
|
|
1101
|
+
i = e < 0 ? i + 1 : e + 1;
|
|
1102
|
+
continue;
|
|
1103
|
+
}
|
|
1104
|
+
RUST_ITEM_RE.lastIndex = i;
|
|
1105
|
+
const m = RUST_ITEM_RE.exec(masked);
|
|
1106
|
+
if (!m) { i = statementEnd(i, true) + 1; continue; }
|
|
1107
|
+
const kw = m[1].replace(/\s+/g, " ");
|
|
1108
|
+
const after = RUST_ITEM_RE.lastIndex;
|
|
1109
|
+
const nm = nameAt(after);
|
|
1110
|
+
if (kw === "fn") {
|
|
1111
|
+
if (!nm) { i = statementEnd(after, true) + 1; continue; }
|
|
1112
|
+
if (ctx) i = addFn(i, nm, inl, ctx.owner, ctx.methods) + 1;
|
|
1113
|
+
else {
|
|
1114
|
+
const list = [];
|
|
1115
|
+
i = addFn(i, nm, inl, null, list) + 1;
|
|
1116
|
+
const fn = list[0];
|
|
1117
|
+
d.functions.push(fn);
|
|
1118
|
+
if (!scope.items.has(fn.name)) scope.items.set(fn.name, { kind: "fn", name: fn.name, ref: fn });
|
|
1119
|
+
}
|
|
1120
|
+
} else if (kw === "struct" || kw === "enum" || kw === "union" || kw === "trait") {
|
|
1121
|
+
if (!nm || ctx) { i = statementEnd(after, true) + 1; continue; }
|
|
1122
|
+
const b = bodyOf(nm.end);
|
|
1123
|
+
const cls = { name: nm.name, line: lineOf(i), end: lineOf(b.close != null ? b.close : Math.min(b.end, n - 1)), kind: kw === "trait" ? "trait" : kw === "enum" ? "enum" : "struct", extends: null, implements: [], methods: [], inl, bodyStart: b.open != null ? b.open : -1, bodyEnd: b.close != null ? b.close : -1 };
|
|
1124
|
+
if (kw !== "trait") d.rsSkip.push([i, (b.close != null ? b.close : b.end) + 1]);
|
|
1125
|
+
d.classes.push(cls);
|
|
1126
|
+
if (!scope.items.has(cls.name)) scope.items.set(cls.name, { kind: cls.kind, name: cls.name, ref: cls });
|
|
1127
|
+
if (kw === "trait" && b.open != null) {
|
|
1128
|
+
const impl = { inl, typeName: nm.name, typeSegs: [nm.name], traitSegs: null, line: cls.line, methods: [], isTrait: true };
|
|
1129
|
+
d.rsImpls.push(impl);
|
|
1130
|
+
scanItems(b.open + 1, b.close, inl, parent, { owner: nm.name, methods: impl.methods });
|
|
1131
|
+
}
|
|
1132
|
+
i = (b.close != null ? b.close : b.end) + 1;
|
|
1133
|
+
} else if (kw === "impl") {
|
|
1134
|
+
let k = after;
|
|
1135
|
+
while (k < n && /\s/.test(masked[k])) k++;
|
|
1136
|
+
if (masked[k] === "<") { const e = skipAngles(masked, k); if (e > 0) k = e; }
|
|
1137
|
+
const b = bodyOf(k);
|
|
1138
|
+
if (b.open == null || ctx) { i = (b.close != null ? b.close : b.end) + 1; continue; }
|
|
1139
|
+
let header = masked.slice(k, b.open);
|
|
1140
|
+
const wh = header.search(/\bwhere\b/);
|
|
1141
|
+
if (wh >= 0) header = header.slice(0, wh);
|
|
1142
|
+
// the first ` for ` outside angle brackets separates trait and type
|
|
1143
|
+
let depth = 0, forAt = -1;
|
|
1144
|
+
for (let q = 0; q < header.length; q++) {
|
|
1145
|
+
const c = header[q];
|
|
1146
|
+
if (c === "<") depth++;
|
|
1147
|
+
else if (c === ">" && header[q - 1] !== "-" && header[q - 1] !== "=") depth--;
|
|
1148
|
+
else if (depth === 0 && c === "f" && /^for\s+[^<\s]/.test(header.slice(q)) && /\s/.test(header[q - 1] || " ")) { forAt = q; break; }
|
|
1149
|
+
}
|
|
1150
|
+
const typeSegs = rustTypePath(forAt >= 0 ? header.slice(forAt + 3) : header);
|
|
1151
|
+
const traitSegs = forAt >= 0 ? rustTypePath(header.slice(0, forAt).replace(/^\s*!/, "")) : null;
|
|
1152
|
+
if (typeSegs) {
|
|
1153
|
+
const impl = { inl, typeName: typeSegs[typeSegs.length - 1], typeSegs, traitSegs, line: lineOf(i), methods: [], isTrait: false };
|
|
1154
|
+
d.rsImpls.push(impl);
|
|
1155
|
+
scanItems(b.open + 1, b.close, inl, parent, { owner: impl.typeName, methods: impl.methods });
|
|
1156
|
+
}
|
|
1157
|
+
i = b.close + 1;
|
|
1158
|
+
} else if (kw === "mod") {
|
|
1159
|
+
if (!nm || ctx) { i = statementEnd(after, true) + 1; continue; }
|
|
1160
|
+
const b = bodyOf(nm.end);
|
|
1161
|
+
if (b.open != null) {
|
|
1162
|
+
const sub = inl ? inl + "::" + nm.name : nm.name;
|
|
1163
|
+
scope.mods.set(nm.name, { inline: sub });
|
|
1164
|
+
scanItems(b.open + 1, b.close, sub, inl, null);
|
|
1165
|
+
i = b.close + 1;
|
|
1166
|
+
} else {
|
|
1167
|
+
scope.modDecls.push({ name: nm.name, line: lineOf(i) });
|
|
1168
|
+
i = b.end + 1;
|
|
1169
|
+
}
|
|
1170
|
+
} else if (kw === "type" || kw === "const" || kw === "static") {
|
|
1171
|
+
let name = nm;
|
|
1172
|
+
if (name && name.name === "mut") name = nameAt(name.end);
|
|
1173
|
+
const end = statementEnd(after, false);
|
|
1174
|
+
if (name && name.name !== "_" && !ctx) {
|
|
1175
|
+
const t = { name: name.name, line: lineOf(i), kind: kw === "type" ? "type" : "const", inl };
|
|
1176
|
+
d.types.push(t);
|
|
1177
|
+
if (!scope.items.has(t.name)) scope.items.set(t.name, { kind: t.kind, name: t.name, ref: t });
|
|
1178
|
+
}
|
|
1179
|
+
i = end + 1;
|
|
1180
|
+
} else if (kw === "macro_rules !" || kw === "macro_rules!") {
|
|
1181
|
+
const b = nm ? bodyOf(nm.end) : { end: after };
|
|
1182
|
+
let end = b.close != null ? b.close : b.end;
|
|
1183
|
+
if (nm && b.open == null) { // macro_rules! name ( … );
|
|
1184
|
+
const open = masked.indexOf("(", nm.end);
|
|
1185
|
+
const e = open >= 0 ? matchClose(masked, open) : -1;
|
|
1186
|
+
if (e > 0) end = e;
|
|
1187
|
+
}
|
|
1188
|
+
d.rsSkip.push([i, end + 1]);
|
|
1189
|
+
if (nm && !ctx) {
|
|
1190
|
+
const t = { name: nm.name, line: lineOf(i), kind: "macro", inl };
|
|
1191
|
+
d.types.push(t);
|
|
1192
|
+
if (!scope.items.has(t.name)) scope.items.set(t.name, { kind: "macro", name: t.name, ref: t });
|
|
1193
|
+
}
|
|
1194
|
+
i = end + 1;
|
|
1195
|
+
} else if (kw === "use") {
|
|
1196
|
+
const end = statementEnd(after, false);
|
|
1197
|
+
if (!ctx) {
|
|
1198
|
+
for (const u of rustUseTree(masked.slice(after, end), [], [])) {
|
|
1199
|
+
if (u.glob) scope.globs.push(u.segs);
|
|
1200
|
+
else if (u.alias !== "_") {
|
|
1201
|
+
if (!scope.uses.has(u.alias)) scope.uses.set(u.alias, []);
|
|
1202
|
+
scope.uses.get(u.alias).push(u.segs);
|
|
1203
|
+
}
|
|
1204
|
+
d.imports.push({ spec: u.segs.join("::"), default: null, namespace: u.alias, names: [], reExport: false, dynamic: false, line: lineOf(i), rsInl: inl, rsSegs: u.segs, rsGlob: u.glob });
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
i = end + 1;
|
|
1208
|
+
} else i = statementEnd(after, false) + 1; // extern crate
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
scanItems(0, n, "", null, null);
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
// ============================================================================
|
|
1215
|
+
// TypeScript / JavaScript declarations
|
|
1216
|
+
// ============================================================================
|
|
1217
|
+
|
|
1218
|
+
// Index after the `<…>` group opening at `open` (a generic parameter or
|
|
1219
|
+
// argument list: the `>` of `=>` closes nothing, brackets inside are skipped
|
|
1220
|
+
// whole), or -1 when it is not one.
|
|
1221
|
+
function tsAngles(text, open) {
|
|
1222
|
+
let depth = 0;
|
|
1223
|
+
const limit = Math.min(text.length, open + 4000);
|
|
1224
|
+
for (let i = open; i < limit; i++) {
|
|
1225
|
+
const ch = text[i];
|
|
1226
|
+
if (ch === "(" || ch === "[" || ch === "{") { const e = matchClose(text, i); if (e < 0) return -1; i = e; }
|
|
1227
|
+
else if (ch === "<") depth++;
|
|
1228
|
+
else if (ch === ">" && text[i - 1] !== "=") { if (--depth === 0) return i + 1; }
|
|
1229
|
+
else if (ch === ";" || ch === ")" || ch === "]" || ch === "}") return -1;
|
|
1230
|
+
}
|
|
1231
|
+
return -1;
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1234
|
+
const TS_TYPE_BRACE_PREV = new Set([":", "|", "&", ",", "(", "<", "?", "["]);
|
|
1235
|
+
const TS_TYPE_WORDS = new Set(["is", "extends", "keyof", "typeof", "infer", "readonly", "asserts", "in"]);
|
|
1236
|
+
const TS_LINE_CONTINUES = new Set(["+", "-", "*", "/", "%", "&", "|", "^", "<", ">", "=", "?", ":", ",", ".", "(", "[", "{", "!", "~"]);
|
|
1237
|
+
const TS_CONTINUE_WORDS = new Set(["return", "typeof", "new", "await", "yield", "extends", "in", "of", "instanceof", "as", "satisfies", "void", "delete", "throw", "case", "else"]);
|
|
1238
|
+
const TS_NEXT_CONTINUES = new Set([".", "?", ":", "+", "*", "/", "%", "&", "|", "^", "<", ">", "=", ",", ")", "]"]);
|
|
1239
|
+
const TS_NEXT_WORDS = new Set(["as", "satisfies", "instanceof", "in", "extends"]);
|
|
1240
|
+
const TS_MEMBER_MODIFIERS = new Set(["public", "private", "protected", "static", "readonly", "override", "abstract", "async", "declare", "accessor", "get", "set"]);
|
|
1241
|
+
const TS_NOT_A_NAME = new Set(["extends", "implements"]);
|
|
1242
|
+
|
|
1243
|
+
// Where a function's body opens after its parameter list closed at `from - 1`:
|
|
1244
|
+
// { open } at the body brace, or { end } where a bodiless signature ends. A
|
|
1245
|
+
// brace that belongs to the return type (`): { ok: boolean } {`) is skipped.
|
|
1246
|
+
function tsBodyOpen(text, from) {
|
|
1247
|
+
const n = text.length;
|
|
1248
|
+
let prev = ")", word = "", arrow = false;
|
|
1249
|
+
for (let k = from; k < n; k++) {
|
|
1250
|
+
const ch = text[k];
|
|
1251
|
+
if (ch === " " || ch === "\t" || ch === "\r") continue;
|
|
1252
|
+
if (ch === "\n") {
|
|
1253
|
+
if (TS_TYPE_BRACE_PREV.has(prev) || arrow || TS_TYPE_WORDS.has(word)) continue;
|
|
1254
|
+
let q = k + 1;
|
|
1255
|
+
while (q < n && /\s/.test(text[q])) q++;
|
|
1256
|
+
const c2 = text[q];
|
|
1257
|
+
if (c2 === "{" || c2 === "|" || c2 === "&" || c2 === ":" || c2 === "<" || c2 === "=" || c2 === "." || c2 === "?" || c2 === "[") continue;
|
|
1258
|
+
if (prev === ")" && c2 === undefined) return { end: k };
|
|
1259
|
+
if (/^(?:is|extends|keyof)\b/.test(text.slice(q, q + 8))) continue;
|
|
1260
|
+
return { end: k };
|
|
1261
|
+
}
|
|
1262
|
+
if (ch === "{") {
|
|
1263
|
+
if (TS_TYPE_BRACE_PREV.has(prev) || arrow || TS_TYPE_WORDS.has(word)) {
|
|
1264
|
+
const e = matchClose(text, k);
|
|
1265
|
+
if (e < 0) return { end: n };
|
|
1266
|
+
k = e; prev = "}"; word = ""; arrow = false;
|
|
1267
|
+
continue;
|
|
1268
|
+
}
|
|
1269
|
+
return { open: k };
|
|
1270
|
+
}
|
|
1271
|
+
if (ch === "(" || ch === "[") { const e = matchClose(text, k); if (e < 0) return { end: n }; k = e; prev = ")"; word = ""; arrow = false; continue; }
|
|
1272
|
+
if (ch === "<") { const e = tsAngles(text, k); if (e > 0) { k = e - 1; prev = ">"; word = ""; arrow = false; continue; } }
|
|
1273
|
+
if (ch === ";") return { end: k };
|
|
1274
|
+
if (ch === "}" || ch === ")" || ch === "]") return { end: k - 1 };
|
|
1275
|
+
if (ch === "," && prev !== ")") return { end: k };
|
|
1276
|
+
if (ch === "=" && text[k + 1] === ">") { arrow = true; k++; prev = ">"; word = ""; continue; }
|
|
1277
|
+
arrow = false;
|
|
1278
|
+
word = isIdentChar(ch) ? (isIdentChar(text[k - 1]) ? word + ch : ch) : "";
|
|
1279
|
+
prev = ch;
|
|
1280
|
+
}
|
|
1281
|
+
return { end: n };
|
|
1282
|
+
}
|
|
1283
|
+
|
|
1284
|
+
// End (exclusive of the terminator) of the expression that starts at `from`:
|
|
1285
|
+
// its `;`, a top-level `,`, the bracket that closes the enclosing construct,
|
|
1286
|
+
// or the line break after which a new statement begins.
|
|
1287
|
+
function tsExprEnd(text, from) {
|
|
1288
|
+
const n = text.length;
|
|
1289
|
+
let prev = "", word = "";
|
|
1290
|
+
for (let k = from; k < n; k++) {
|
|
1291
|
+
const ch = text[k];
|
|
1292
|
+
if (ch === " " || ch === "\t" || ch === "\r") continue;
|
|
1293
|
+
if (ch === "(" || ch === "[" || ch === "{") { const e = matchClose(text, k); if (e < 0) return n; k = e; prev = ")"; word = ""; continue; }
|
|
1294
|
+
if (ch === ";" || ch === ",") return k;
|
|
1295
|
+
if (ch === ")" || ch === "]" || ch === "}") return k;
|
|
1296
|
+
if (ch === "\n") {
|
|
1297
|
+
const jsxClose = prev === ">" && k >= 2 && /[\w"'/}\])]/.test(text[k - 1] === ">" ? text[k - 2] : "");
|
|
1298
|
+
if (!prev || (TS_LINE_CONTINUES.has(prev) && !jsxClose) || TS_CONTINUE_WORDS.has(word)) continue;
|
|
1299
|
+
let q = k + 1;
|
|
1300
|
+
while (q < n && /\s/.test(text[q])) q++;
|
|
1301
|
+
if (q >= n) return k;
|
|
1302
|
+
if (TS_NEXT_CONTINUES.has(text[q]) && !(text[q] === "/" && text[q + 1] === "/")) continue;
|
|
1303
|
+
const nw = text.slice(q, q + 12).match(/^[A-Za-z]+/);
|
|
1304
|
+
if (nw && TS_NEXT_WORDS.has(nw[0])) continue;
|
|
1305
|
+
return k;
|
|
1306
|
+
}
|
|
1307
|
+
word = isIdentChar(ch) ? (isIdentChar(text[k - 1]) ? word + ch : ch) : "";
|
|
1308
|
+
prev = ch;
|
|
1309
|
+
}
|
|
1310
|
+
return n;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
// The function or class an initializer is, when it is one: `(a) => …`,
|
|
1314
|
+
// `async <T>(a: T): R => …`, `x => …`, `function (…) {…}`, `class {…}`.
|
|
1315
|
+
// Returns { kind, open, close } with the body's braces (close = the end of a
|
|
1316
|
+
// brace-less arrow body, open = -1), or null.
|
|
1317
|
+
function tsFunctionInit(text, from) {
|
|
1318
|
+
const n = text.length;
|
|
1319
|
+
let k = from;
|
|
1320
|
+
const skipWs = () => { while (k < n && /\s/.test(text[k])) k++; };
|
|
1321
|
+
skipWs();
|
|
1322
|
+
let m = /async\b/y;
|
|
1323
|
+
m.lastIndex = k;
|
|
1324
|
+
if (m.test(text)) { const save = k; k = m.lastIndex; skipWs(); if (text[k] === "=" || text[k] === ";" || text[k] === "," || text[k] === ")") k = save; }
|
|
1325
|
+
m = /(function|class)\b/y;
|
|
1326
|
+
m.lastIndex = k;
|
|
1327
|
+
const kw = m.exec(text);
|
|
1328
|
+
if (kw && kw[1] === "function") {
|
|
1329
|
+
const open = text.indexOf("(", m.lastIndex);
|
|
1330
|
+
const pc = open >= 0 ? matchClose(text, open) : -1;
|
|
1331
|
+
if (pc < 0) return null;
|
|
1332
|
+
const b = tsBodyOpen(text, pc + 1);
|
|
1333
|
+
const close = b.open != null ? matchClose(text, b.open) : -1;
|
|
1334
|
+
return close > 0 ? { kind: "function", open: b.open, close } : null;
|
|
1335
|
+
}
|
|
1336
|
+
if (kw && kw[1] === "class") {
|
|
1337
|
+
const b = tsClassBody(text, m.lastIndex);
|
|
1338
|
+
return b ? { kind: "class", open: b.open, close: b.close, header: text.slice(m.lastIndex, b.open) } : null;
|
|
1339
|
+
}
|
|
1340
|
+
if (text[k] === "<") { const e = tsAngles(text, k); if (e < 0) return null; k = e; skipWs(); if (text[k] !== "(") return null; }
|
|
1341
|
+
let arrowAt = -1;
|
|
1342
|
+
if (text[k] === "(") {
|
|
1343
|
+
const pc = matchClose(text, k);
|
|
1344
|
+
if (pc < 0) return null;
|
|
1345
|
+
k = pc + 1;
|
|
1346
|
+
skipWs();
|
|
1347
|
+
if (text[k] === "=" && text[k + 1] === ">") arrowAt = k;
|
|
1348
|
+
else if (text[k] === ":") {
|
|
1349
|
+
// a return type, then the arrow
|
|
1350
|
+
for (let q = k + 1; q < n; q++) {
|
|
1351
|
+
const ch = text[q];
|
|
1352
|
+
if (ch === "(" || ch === "[" || ch === "{") { const e = matchClose(text, q); if (e < 0) return null; q = e; }
|
|
1353
|
+
else if (ch === "<") { const e = tsAngles(text, q); if (e > 0) q = e - 1; }
|
|
1354
|
+
else if (ch === "=" && text[q + 1] === ">") { arrowAt = q; break; }
|
|
1355
|
+
else if (ch === ";" || ch === ")" || ch === "]" || ch === "}" || (ch === "=" && text[q + 1] !== "=")) return null;
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
} else {
|
|
1359
|
+
m = /[A-Za-z_$][\w$]*/y;
|
|
1360
|
+
m.lastIndex = k;
|
|
1361
|
+
if (!m.test(text)) return null;
|
|
1362
|
+
k = m.lastIndex;
|
|
1363
|
+
skipWs();
|
|
1364
|
+
if (text[k] === "=" && text[k + 1] === ">") arrowAt = k;
|
|
1365
|
+
}
|
|
1366
|
+
if (arrowAt < 0) return null;
|
|
1367
|
+
k = arrowAt + 2;
|
|
1368
|
+
skipWs();
|
|
1369
|
+
if (text[k] === "{") {
|
|
1370
|
+
const close = matchClose(text, k);
|
|
1371
|
+
return close > 0 ? { kind: "arrow", open: k, close } : null;
|
|
1372
|
+
}
|
|
1373
|
+
return { kind: "arrow", open: -1, close: tsExprEnd(text, k) };
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// The body braces of a class whose name (or `class` keyword) ends at `from`.
|
|
1377
|
+
function tsClassBody(text, from) {
|
|
1378
|
+
const n = text.length;
|
|
1379
|
+
for (let k = from; k < n; k++) {
|
|
1380
|
+
const ch = text[k];
|
|
1381
|
+
if (ch === "(" || ch === "[") { const e = matchClose(text, k); if (e < 0) return null; k = e; }
|
|
1382
|
+
else if (ch === "<") { const e = tsAngles(text, k); if (e > 0) k = e - 1; }
|
|
1383
|
+
else if (ch === "{") { const close = matchClose(text, k); return close > 0 ? { open: k, close } : null; }
|
|
1384
|
+
else if (ch === ";" || ch === "}" || ch === ")") return null;
|
|
1385
|
+
}
|
|
1386
|
+
return null;
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
// `extends` / `implements` of a class header (the text between its name and body).
|
|
1390
|
+
function tsClassHeritage(header) {
|
|
1391
|
+
let h = header;
|
|
1392
|
+
const lead = h.match(/^\s*/)[0].length;
|
|
1393
|
+
if (h[lead] === "<") { const e = tsAngles(h, lead); if (e > 0) h = h.slice(e); }
|
|
1394
|
+
const ext = h.match(/\bextends\s+([A-Za-z_$][\w$.]*)/);
|
|
1395
|
+
const impl = h.match(/\bimplements\s+([\s\S]+)$/);
|
|
1396
|
+
return { extends: ext ? ext[1].split(".")[0] : null, implements: implementsList(impl ? impl[1] : "") };
|
|
1397
|
+
}
|
|
1398
|
+
|
|
1399
|
+
// Members of a class body (between its braces): methods, the constructor,
|
|
1400
|
+
// accessors, `#private` names and function-valued properties. A word followed
|
|
1401
|
+
// by `(` at member position is a method header whatever the word is
|
|
1402
|
+
// (`delete(id) {`, `get() {`); statements cannot occur there.
|
|
1403
|
+
function tsClassMembers(cls, text, from, to, lineOf) {
|
|
1404
|
+
const identRe = /#?[A-Za-z_$][\w$]*/y;
|
|
1405
|
+
let i = from;
|
|
1406
|
+
while (i < to) {
|
|
1407
|
+
const ch = text[i];
|
|
1408
|
+
if (ch === " " || ch === "\t" || ch === "\n" || ch === "\r" || ch === ";" || ch === ",") { i++; continue; }
|
|
1409
|
+
if (ch === "@") { // decorator
|
|
1410
|
+
identRe.lastIndex = i + 1;
|
|
1411
|
+
let k = identRe.test(text) ? identRe.lastIndex : i + 1;
|
|
1412
|
+
while (text[k] === ".") { identRe.lastIndex = k + 1; if (!identRe.test(text)) break; k = identRe.lastIndex; }
|
|
1413
|
+
if (text[k] === "(") { const e = matchClose(text, k); k = e < 0 ? to : e + 1; }
|
|
1414
|
+
i = k;
|
|
1415
|
+
continue;
|
|
1416
|
+
}
|
|
1417
|
+
// modifiers, then the name
|
|
1418
|
+
let name = null, nameAt = i, k = i, accessor = false;
|
|
1419
|
+
for (;;) {
|
|
1420
|
+
while (k < to && (text[k] === " " || text[k] === "\t" || text[k] === "\n" || text[k] === "\r")) k++;
|
|
1421
|
+
if (text[k] === "*") { k++; continue; }
|
|
1422
|
+
identRe.lastIndex = k;
|
|
1423
|
+
const w = identRe.exec(text);
|
|
1424
|
+
if (!w) break;
|
|
1425
|
+
let q = identRe.lastIndex;
|
|
1426
|
+
while (q < to && (text[q] === " " || text[q] === "\t")) q++;
|
|
1427
|
+
const next = text[q];
|
|
1428
|
+
const startsName = next === "#" || next === "[" || next === '"' || next === "'" || next === "*" || (next !== undefined && isIdentChar(next)) || (next === "\n" && (w[0] === "static" || w[0] === "async"));
|
|
1429
|
+
if (TS_MEMBER_MODIFIERS.has(w[0]) && startsName) {
|
|
1430
|
+
if (w[0] === "get" || w[0] === "set" || w[0] === "accessor") accessor = true;
|
|
1431
|
+
k = identRe.lastIndex;
|
|
1432
|
+
continue;
|
|
1433
|
+
}
|
|
1434
|
+
name = w[0]; nameAt = k; k = identRe.lastIndex;
|
|
1435
|
+
break;
|
|
1436
|
+
}
|
|
1437
|
+
if (name === null) {
|
|
1438
|
+
if (text[k] === "{") { const e = matchClose(text, k); i = e < 0 ? to : e + 1; continue; } // static block
|
|
1439
|
+
if (text[k] === "[" || text[k] === '"' || text[k] === "'") { // computed or quoted name: not indexed
|
|
1440
|
+
if (text[k] === "[") { const e = matchClose(text, k); k = e < 0 ? to : e + 1; }
|
|
1441
|
+
else { const e = text.indexOf(text[k], k + 1); k = e < 0 ? to : e + 1; }
|
|
1442
|
+
} else if (/[0-9]/.test(text[k] || "")) { while (k < to && /[\w.]/.test(text[k])) k++; }
|
|
1443
|
+
else { i = k + 1; continue; }
|
|
1444
|
+
}
|
|
1445
|
+
while (k < to && (text[k] === " " || text[k] === "\t")) k++;
|
|
1446
|
+
if (text[k] === "?" || text[k] === "!") k++;
|
|
1447
|
+
while (k < to && (text[k] === " " || text[k] === "\t")) k++;
|
|
1448
|
+
let open = k;
|
|
1449
|
+
if (text[open] === "<") { const e = tsAngles(text, open); if (e > 0) { open = e; while (open < to && /\s/.test(text[open])) open++; } }
|
|
1450
|
+
if (text[open] === "(") {
|
|
1451
|
+
const pc = matchClose(text, open);
|
|
1452
|
+
if (pc < 0) return;
|
|
1453
|
+
const b = tsBodyOpen(text, pc + 1);
|
|
1454
|
+
const close = b.open != null ? matchClose(text, b.open) : -1;
|
|
1455
|
+
const end = close > 0 ? close : Math.min(b.end, to);
|
|
1456
|
+
if (name !== null) addMethod(cls, { name, line: lineOf(nameAt), end: lineOf(Math.min(end, text.length - 1)), bodyStart: close > 0 ? b.open : -1, bodyEnd: close });
|
|
1457
|
+
i = end + 1;
|
|
1458
|
+
continue;
|
|
1459
|
+
}
|
|
1460
|
+
// a property: its type annotation, then an initializer that may be a function
|
|
1461
|
+
let eq = -1, stop = to;
|
|
1462
|
+
for (let q = k; q < to; q++) {
|
|
1463
|
+
const c = text[q];
|
|
1464
|
+
if (c === "(" || c === "[" || c === "{") { const e = matchClose(text, q); if (e < 0) { stop = to; break; } q = e; }
|
|
1465
|
+
else if (c === "<") { const e = tsAngles(text, q); if (e > 0) q = e - 1; }
|
|
1466
|
+
else if (c === "=" && text[q + 1] === ">") q++;
|
|
1467
|
+
else if (c === "=") { eq = q; break; }
|
|
1468
|
+
else if (c === ";" || c === "}") { stop = q; break; }
|
|
1469
|
+
else if (c === "\n") {
|
|
1470
|
+
let p = q - 1;
|
|
1471
|
+
while (p > k && (text[p] === " " || text[p] === "\t" || text[p] === "\r")) p--;
|
|
1472
|
+
let nx = q + 1;
|
|
1473
|
+
while (nx < to && /\s/.test(text[nx])) nx++;
|
|
1474
|
+
if (!TS_TYPE_BRACE_PREV.has(text[p]) && !(text[p] === ">" && text[p - 1] === "=") && text[nx] !== "|" && text[nx] !== "&" && text[nx] !== "=" && text[nx] !== "?" && text[nx] !== ":") { stop = q; break; }
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
if (name !== null && text[k] === ":") {
|
|
1478
|
+
const tm = text.slice(k + 1, eq < 0 ? stop : eq).match(/^\s*(?:readonly\s+)?([A-Za-z_$][\w$.]*)/);
|
|
1479
|
+
if (tm) (cls.fields ||= new Map()).set(name, tm[1]);
|
|
1480
|
+
}
|
|
1481
|
+
if (eq < 0) { i = stop + 1; continue; }
|
|
1482
|
+
if (name !== null && !(cls.fields && cls.fields.has(name))) {
|
|
1483
|
+
const nm = text.slice(eq + 1, eq + 200).match(/^\s*new\s+([A-Za-z_$][\w$.]*)/);
|
|
1484
|
+
if (nm) (cls.fields ||= new Map()).set(name, nm[1]);
|
|
1485
|
+
}
|
|
1486
|
+
const fn = accessor ? null : tsFunctionInit(text, eq + 1);
|
|
1487
|
+
if (fn && fn.kind !== "class") {
|
|
1488
|
+
if (name !== null) addMethod(cls, { name, line: lineOf(nameAt), end: lineOf(Math.min(fn.close, text.length - 1)), bodyStart: fn.open, bodyEnd: fn.close, property: true });
|
|
1489
|
+
i = fn.close + 1;
|
|
1490
|
+
} else i = tsExprEnd(text, eq + 1) + 1;
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
|
|
1494
|
+
// Declarations of a TS/JS module from masked source. Top level: classes
|
|
1495
|
+
// (class expressions bound to a variable included), functions, function-valued
|
|
1496
|
+
// variables, other variables, interfaces, enums, type aliases, namespaces.
|
|
1497
|
+
// Below top level: named functions, function-valued variables, classes and
|
|
1498
|
+
// types declared inside function bodies and callbacks (`d.nested`).
|
|
1499
|
+
function parseTsDecls(d, masked, lineOf, lang) {
|
|
1500
|
+
const n = masked.length;
|
|
1501
|
+
d.nested = [];
|
|
1502
|
+
d.cjsFunctions = [];
|
|
1503
|
+
d.localExports = []; // `export { a as b }` without a source
|
|
1504
|
+
d.defaultName = null; // `export default name`
|
|
1505
|
+
const total = () => d.classes.length + d.functions.length + d.types.length;
|
|
1506
|
+
const addClass = (name, at, bodyOpen, bodyClose, header, isDefault) => {
|
|
1507
|
+
const h = tsClassHeritage(header);
|
|
1508
|
+
const cls = { name, line: lineOf(at), end: lineOf(bodyClose), isDefault, extends: h.extends, implements: h.implements, methods: [], bodyStart: bodyOpen, bodyEnd: bodyClose };
|
|
1509
|
+
d.classes.push(cls);
|
|
1510
|
+
tsClassMembers(cls, masked, bodyOpen + 1, bodyClose, lineOf);
|
|
1511
|
+
return cls;
|
|
1512
|
+
};
|
|
1513
|
+
const functionAt = (at, afterKeyword, name, isDefault) => {
|
|
1514
|
+
let k = afterKeyword;
|
|
1515
|
+
while (k < n && /\s/.test(masked[k])) k++;
|
|
1516
|
+
if (masked[k] === "<") { const e = tsAngles(masked, k); if (e > 0) k = e; }
|
|
1517
|
+
const open = masked.indexOf("(", k);
|
|
1518
|
+
const pc = open >= 0 ? matchClose(masked, open) : -1;
|
|
1519
|
+
if (pc < 0) return;
|
|
1520
|
+
const b = tsBodyOpen(masked, pc + 1);
|
|
1521
|
+
const close = b.open != null ? matchClose(masked, b.open) : -1;
|
|
1522
|
+
d.functions.push({ name, line: lineOf(at), end: lineOf(Math.min(close > 0 ? close : b.end, n - 1)), isDefault, bodyStart: close > 0 ? b.open : -1, bodyEnd: close });
|
|
1523
|
+
};
|
|
1524
|
+
// The `=` that starts a variable's initializer, after an optional type annotation.
|
|
1525
|
+
const initializerAt = (from) => {
|
|
1526
|
+
for (let q = from; q < n; q++) {
|
|
1527
|
+
const c = masked[q];
|
|
1528
|
+
if (c === "(" || c === "[" || c === "{") { const e = matchClose(masked, q); if (e < 0) return -1; q = e; }
|
|
1529
|
+
else if (c === "<") { const e = tsAngles(masked, q); if (e > 0) q = e - 1; }
|
|
1530
|
+
else if (c === "=" && masked[q + 1] === ">") q++;
|
|
1531
|
+
else if (c === "=") return q;
|
|
1532
|
+
else if (c === ";" || c === "}" || c === ")" || c === ",") return -1;
|
|
1533
|
+
else if (c === "\n" && masked[from] !== ":") return -1;
|
|
1534
|
+
}
|
|
1535
|
+
return -1;
|
|
1536
|
+
};
|
|
1537
|
+
// kind of `const name …`: { kind: "function" | "class" | "const", … }
|
|
1538
|
+
const variableAt = (nameEnd) => {
|
|
1539
|
+
let k = nameEnd;
|
|
1540
|
+
while (k < n && (masked[k] === " " || masked[k] === "\t")) k++;
|
|
1541
|
+
if (masked[k] === "!") k++;
|
|
1542
|
+
const eq = masked[k] === "=" && masked[k + 1] !== "=" && masked[k + 1] !== ">" ? k : masked[k] === ":" ? initializerAt(k) : -1;
|
|
1543
|
+
if (eq < 0) return { kind: "const", required: false };
|
|
1544
|
+
const fn = tsFunctionInit(masked, eq + 1);
|
|
1545
|
+
if (fn) return fn.kind === "class" ? fn : { kind: "function", open: fn.open, close: fn.close };
|
|
1546
|
+
return { kind: "const", required: /^\s*(?:await\s+)?(?:require|import)\s*\(/.test(masked.slice(eq + 1, eq + 40)) };
|
|
1547
|
+
};
|
|
1548
|
+
|
|
1549
|
+
// -- top level: lines that start a declaration outside every bracket
|
|
1550
|
+
let brace = 0, paren = 0, i = 0, lineNo = 0;
|
|
1551
|
+
while (i < n && total() < MAX_SYMBOLS_PER_FILE) {
|
|
1552
|
+
const nl = masked.indexOf("\n", i);
|
|
1553
|
+
const eol = nl < 0 ? n : nl;
|
|
1554
|
+
if (brace === 0 && paren === 0) {
|
|
1555
|
+
const ln = masked.slice(i, eol);
|
|
1556
|
+
let m;
|
|
1557
|
+
if ((m = ln.match(/^(\s*(?:export\s+)?(?:default\s+)?(?:declare\s+)?(?:abstract\s+)?)class\b\s*([A-Za-z_$][\w$]*)?/)) && (m[2] || /\bdefault\b/.test(m[1]))) {
|
|
1558
|
+
const named = m[2] && !TS_NOT_A_NAME.has(m[2]);
|
|
1559
|
+
const after = named ? i + m[0].length : i + m[1].length + 5;
|
|
1560
|
+
const b = tsClassBody(masked, after);
|
|
1561
|
+
if (b) addClass(named ? m[2] : "default", i + m[1].length, b.open, b.close, masked.slice(after, b.open), /\bdefault\b/.test(m[1]));
|
|
1562
|
+
} else if ((m = ln.match(/^(\s*(?:export\s+)?(?:default\s+)?(?:declare\s+)?(?:async\s+)?)function\b\s*\*?\s*([A-Za-z_$][\w$]*)?/)) && (m[2] || /\bdefault\b/.test(m[1]))) {
|
|
1563
|
+
functionAt(i + m[1].length, i + m[0].length, m[2] || "default", /\bdefault\b/.test(m[1]));
|
|
1564
|
+
} else if ((m = ln.match(/^(\s*(?:export\s+)?(?:declare\s+)?(?:const|let|var)\s+)([A-Za-z_$][\w$]*)/))) {
|
|
1565
|
+
const at = i + m[1].length;
|
|
1566
|
+
const v = variableAt(i + m[0].length);
|
|
1567
|
+
if (v.kind === "function") d.functions.push({ name: m[2], line: lineOf(at), end: lineOf(Math.min(v.close, n - 1)), bodyStart: v.open, bodyEnd: v.close });
|
|
1568
|
+
else if (v.kind === "class") addClass(m[2], at, v.open, v.close, v.header, false);
|
|
1569
|
+
else if (!v.required) d.types.push({ name: m[2], line: lineOf(at), kind: "const" });
|
|
1570
|
+
} else if ((m = ln.match(/^\s*(?:export\s+)?(?:default\s+)?(?:declare\s+)?(?:const\s+)?(interface|enum)\s+([A-Za-z_$][\w$]*)/))) {
|
|
1571
|
+
const t = { name: m[2], line: lineOf(i + ln.indexOf(m[1])), kind: m[1], extends: null, extendsAll: [] };
|
|
1572
|
+
if (m[1] === "interface") {
|
|
1573
|
+
const b = tsClassBody(masked, i + m[0].length);
|
|
1574
|
+
let header = b ? masked.slice(i + m[0].length, b.open) : "";
|
|
1575
|
+
const lead = header.match(/^\s*/)[0].length;
|
|
1576
|
+
if (header[lead] === "<") { const e = tsAngles(header, lead); if (e > 0) header = header.slice(e); }
|
|
1577
|
+
const em = header.match(/\bextends\s+([\s\S]+)$/);
|
|
1578
|
+
t.extendsAll = implementsList(em ? em[1] : "");
|
|
1579
|
+
t.extends = t.extendsAll[0] || null;
|
|
1580
|
+
}
|
|
1581
|
+
d.types.push(t);
|
|
1582
|
+
} else if ((m = ln.match(/^\s*(?:export\s+)?(?:declare\s+)?type\s+([A-Za-z_$][\w$]*)\s*(?:<|=|$)/))) {
|
|
1583
|
+
d.types.push({ name: m[1], line: lineOf(i + ln.indexOf("type")), kind: "type" });
|
|
1584
|
+
} else if ((m = ln.match(/^\s*(?:export\s+)?(?:declare\s+)?(?:namespace|module)\s+([A-Za-z_$][\w$.]*)\s*\{/)) || (m = ln.match(/^\s*declare\s+(global)\s*\{/))) {
|
|
1585
|
+
d.types.push({ name: m[1], line: lineOf(i + ln.search(/\S/)), kind: "namespace" });
|
|
1586
|
+
} else if ((m = ln.match(/^\s*export\s+default\s+([A-Za-z_$][\w$]*)\s*;?\s*$/)) || (m = ln.match(/^\s*(?:exports\s*=\s*)?module\.exports\s*=\s*([A-Za-z_$][\w$]*)\s*;?\s*$/))) {
|
|
1587
|
+
d.defaultName = m[1];
|
|
1588
|
+
} else if ((m = ln.match(/^(\s*(?:module\.)?exports\.)([A-Za-z_$][\w$]*)\s*=(?!=)/))) {
|
|
1589
|
+
// `exports.name = function …` declares `name` when nothing else does
|
|
1590
|
+
const fn = tsFunctionInit(masked, i + m[0].length);
|
|
1591
|
+
if (fn && fn.kind !== "class") d.cjsFunctions.push({ name: m[2], line: lineOf(i + m[1].length), end: lineOf(Math.min(fn.close, n - 1)), bodyStart: fn.open, bodyEnd: fn.close });
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
for (let k = i; k < eol; k++) {
|
|
1595
|
+
const ch = masked[k];
|
|
1596
|
+
if (ch === "{") brace++;
|
|
1597
|
+
else if (ch === "}") brace--;
|
|
1598
|
+
else if (ch === "(" || ch === "[") paren++;
|
|
1599
|
+
else if (ch === ")" || ch === "]") paren--;
|
|
1600
|
+
}
|
|
1601
|
+
i = eol + 1;
|
|
1602
|
+
lineNo++;
|
|
1603
|
+
}
|
|
1604
|
+
for (const fn of d.cjsFunctions) {
|
|
1605
|
+
if (!d.functions.some((f) => f.name === fn.name) && !d.classes.some((c) => c.name === fn.name) && !d.types.some((t) => t.name === fn.name)) d.functions.push(fn);
|
|
1606
|
+
}
|
|
1607
|
+
const exportRe = /\bexport\s*\{([^}]*)\}(?!\s*from\b)/g;
|
|
1608
|
+
let em;
|
|
1609
|
+
while ((em = exportRe.exec(masked)) !== null) {
|
|
1610
|
+
for (const piece of em[1].split(",")) {
|
|
1611
|
+
const pm = piece.trim().replace(/^type\s+/, "").match(/^([A-Za-z_$][\w$]*)(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);
|
|
1612
|
+
if (pm) d.localExports.push({ local: pm[1], exported: pm[2] || pm[1] });
|
|
1613
|
+
}
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
// -- below top level
|
|
1617
|
+
const depthAt = new Int32Array(n + 1);
|
|
1618
|
+
{
|
|
1619
|
+
let depth = 0;
|
|
1620
|
+
for (let k = 0; k < n; k++) {
|
|
1621
|
+
depthAt[k] = depth;
|
|
1622
|
+
if (masked[k] === "{") depth++;
|
|
1623
|
+
else if (masked[k] === "}") depth--;
|
|
1624
|
+
}
|
|
1625
|
+
depthAt[n] = depth;
|
|
1626
|
+
}
|
|
1627
|
+
const nestedRe = /(?<![\w$.])(?:(function)\s*\*?\s*([A-Za-z_$][\w$]*)\s*(?=[<(])|(const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?=[:=])|(class)\s+([A-Za-z_$][\w$]*)|(type|interface|enum)\s+([A-Za-z_$][\w$]*)\s*(?=[<={]|extends\b))/g;
|
|
1628
|
+
let nm;
|
|
1629
|
+
while ((nm = nestedRe.exec(masked)) !== null && d.nested.length < MAX_SYMBOLS_PER_FILE) {
|
|
1630
|
+
if (depthAt[nm.index] === 0) continue;
|
|
1631
|
+
const line = lineOf(nm.index);
|
|
1632
|
+
if (nm[1]) d.nested.push({ name: nm[2], kind: "function", line });
|
|
1633
|
+
else if (nm[3]) {
|
|
1634
|
+
const v = variableAt(nm.index + nm[0].length);
|
|
1635
|
+
if (v.kind === "function") d.nested.push({ name: nm[4], kind: "function", line });
|
|
1636
|
+
else if (v.kind === "class") d.nested.push({ name: nm[4], kind: "class", line });
|
|
1637
|
+
} else if (nm[5]) {
|
|
1638
|
+
if (TS_NOT_A_NAME.has(nm[6])) continue;
|
|
1639
|
+
d.nested.push({ name: nm[6], kind: "class", line });
|
|
1640
|
+
const b = tsClassBody(masked, nm.index + nm[0].length);
|
|
1641
|
+
if (b) {
|
|
1642
|
+
const inner = { methods: [] };
|
|
1643
|
+
tsClassMembers(inner, masked, b.open + 1, b.close, lineOf);
|
|
1644
|
+
for (const meth of inner.methods) d.nested.push({ name: meth.name, kind: "function", line: meth.line });
|
|
1645
|
+
}
|
|
1646
|
+
}
|
|
1647
|
+
else if (lang === "typescript") d.nested.push({ name: nm[8], kind: nm[7], line });
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
function parseDecls(rel, content, lang) {
|
|
1652
|
+
const d = { classes: [], functions: [], types: [], imports: [], docstring: null, masked: "", callText: "" };
|
|
1653
|
+
// ONE masked copy (see maskSource) with the same offsets and line structure
|
|
1654
|
+
// as `content`: declarations, brace depth and import statements are all
|
|
1655
|
+
// found on it, so a `{` inside `const marker = "{";`, an `export function`
|
|
1656
|
+
// inside a template literal or an `import` inside a string cannot
|
|
1657
|
+
// desynchronize `depth` or fabricate a symbol or dependency. Import
|
|
1658
|
+
// specifiers are read back from `content` at the match offsets.
|
|
1659
|
+
const multiline = [];
|
|
1660
|
+
const masked = maskSource(content, lang, multiline);
|
|
1661
|
+
d.masked = masked;
|
|
1662
|
+
const importRanges = []; // import statements are not call sites either
|
|
1663
|
+
const maskedLines = masked.split("\n");
|
|
1664
|
+
const lineOf = lineIndex(content);
|
|
1665
|
+
const specAt = (m, g) => content.slice(m.indices[g][0], m.indices[g][1]);
|
|
1666
|
+
|
|
1667
|
+
if (TS_LIKE.has(lang)) {
|
|
1668
|
+
// Leading /** */ or run of // as module rationale
|
|
1669
|
+
const dm = content.match(/^\s*\/\*\*?([\s\S]*?)\*\//);
|
|
1670
|
+
if (dm) d.docstring = dm[1].replace(/^\s*\*\s?/gm, "").trim().slice(0, 500) || null;
|
|
1671
|
+
|
|
1672
|
+
parseTsDecls(d, masked, lineOf, lang);
|
|
1673
|
+
// imports (content-level, multi-line safe), on the masked copy so
|
|
1674
|
+
// commented-out examples and import text inside strings or templates are
|
|
1675
|
+
// not dependencies. A statement-level `type` (`import type OpenAI from
|
|
1676
|
+
// "openai"`) is skipped unless it is itself the default binding
|
|
1677
|
+
// (`import type from "./type"`).
|
|
1678
|
+
const importNames = (list) => list.split(",").map((x) => x.trim().replace(/^type\s+/, "")).filter(Boolean)
|
|
1679
|
+
.map((t) => { const am = t.match(/^([A-Za-z_$][\w$]*)(?:\s+as\s+([A-Za-z_$][\w$]*))?$/); return am ? { imported: am[1], local: am[2] || am[1] } : null; })
|
|
1680
|
+
.filter(Boolean);
|
|
1681
|
+
const impRe = /import\s+(?:type\s+(?!from\s*["']))?(?:(?:([A-Za-z_$][\w$]*)\s*,?\s*)?(?:\*\s*as\s+([A-Za-z_$][\w$]*)|\{([^}]*)\})?\s*from\s*)?["']([^"']+)["']/gd;
|
|
1682
|
+
let m2;
|
|
1683
|
+
while ((m2 = impRe.exec(masked)) !== null) {
|
|
1684
|
+
importRanges.push([m2.index, m2.index + m2[0].length]);
|
|
1685
|
+
d.imports.push({ spec: specAt(m2, 4), default: m2[1] || null, namespace: m2[2] || null, names: importNames(m2[3] || ""), reExport: false, dynamic: false, line: lineOf(m2.index) });
|
|
1686
|
+
}
|
|
1687
|
+
const reExpRe = /export\s+(?:type\s+)?\{([^}]*)\}\s*from\s*["']([^"']+)["']/gd;
|
|
1688
|
+
while ((m2 = reExpRe.exec(masked)) !== null) {
|
|
1689
|
+
importRanges.push([m2.index, m2.index + m2[0].length]);
|
|
1690
|
+
d.imports.push({ spec: specAt(m2, 2), default: null, namespace: null, names: importNames(m2[1]), reExport: true, dynamic: false, line: lineOf(m2.index) });
|
|
1691
|
+
}
|
|
1692
|
+
// `export * from` re-exports every name (namespace "*"); `export * as ns
|
|
1693
|
+
// from` exposes only `ns`, so it carries that name instead.
|
|
1694
|
+
const starRe = /export\s+(?:type\s+)?\*(?:\s+as\s+([A-Za-z_$][\w$]*))?\s+from\s*["']([^"']+)["']/gd;
|
|
1695
|
+
while ((m2 = starRe.exec(masked)) !== null) {
|
|
1696
|
+
importRanges.push([m2.index, m2.index + m2[0].length]);
|
|
1697
|
+
d.imports.push({ spec: specAt(m2, 2), default: null, namespace: m2[1] || "*", names: [], reExport: true, dynamic: false, line: lineOf(m2.index) });
|
|
1698
|
+
}
|
|
1699
|
+
// CommonJS: `const x = require("m")`, `const { a, b: c } = require("m")`,
|
|
1700
|
+
// `const y = require("m").y`, and the comma-continued form
|
|
1701
|
+
// (`var a = require("a")\n , b = require("b")`) bind like imports.
|
|
1702
|
+
const cjsRe = /(?:\b(?:const|let|var)\s+|,\s*)(?:([A-Za-z_$][\w$]*)|\{([^}]*)\})\s*=\s*require\s*\(\s*["']([^"']+)["']\s*\)(?:\s*\.\s*([A-Za-z_$][\w$]*))?/gd;
|
|
1703
|
+
const cjsRanges = [];
|
|
1704
|
+
while ((m2 = cjsRe.exec(masked)) !== null) {
|
|
1705
|
+
cjsRanges.push([m2.index, m2.index + m2[0].length]);
|
|
1706
|
+
const rec = { spec: specAt(m2, 3), default: null, namespace: null, names: [], reExport: false, dynamic: false, cjs: true, line: lineOf(m2.index + m2[0].search(/[A-Za-z_$\{]/)) };
|
|
1707
|
+
if (m2[2] !== undefined) {
|
|
1708
|
+
for (const piece of m2[2].split(",")) {
|
|
1709
|
+
const pm = piece.trim().match(/^([A-Za-z_$][\w$]*)(?:\s*:\s*([A-Za-z_$][\w$]*))?$/);
|
|
1710
|
+
if (pm) rec.names.push({ imported: pm[1], local: pm[2] || pm[1] });
|
|
1711
|
+
}
|
|
1712
|
+
} else if (m2[4]) rec.names.push({ imported: m2[4], local: m2[1] });
|
|
1713
|
+
else rec.namespace = m2[1];
|
|
1714
|
+
d.imports.push(rec);
|
|
1715
|
+
}
|
|
1716
|
+
const dynRe = /(?:import|require)\s*\(\s*["']([^"']+)["']\s*\)/gd;
|
|
1717
|
+
while ((m2 = dynRe.exec(masked)) !== null) {
|
|
1718
|
+
if (cjsRanges.some(([a, b]) => m2.index >= a && m2.index < b)) continue;
|
|
1719
|
+
d.imports.push({ spec: specAt(m2, 1), default: null, namespace: null, names: [], reExport: false, dynamic: true, line: lineOf(m2.index) });
|
|
1720
|
+
}
|
|
1721
|
+
} else if (lang === "python") {
|
|
1722
|
+
const dm = content.match(/^(?:#[^\n]*\n|\s*\n)*\s*("""|''')([\s\S]*?)\1/);
|
|
1723
|
+
if (dm) d.docstring = dm[2].trim().slice(0, 500) || null;
|
|
1724
|
+
// Scope follows indentation: a class or def ends before the next logical
|
|
1725
|
+
// line at its own indent or less, so module-level code after it (an
|
|
1726
|
+
// `if __name__ == "__main__":` block) belongs to the file, and `self.x()`
|
|
1727
|
+
// can only bind inside the enclosing class. Lines inside brackets, after a
|
|
1728
|
+
// backslash, or inside a triple-quoted string continue the previous
|
|
1729
|
+
// statement and never open or close a scope.
|
|
1730
|
+
const inString = new Set();
|
|
1731
|
+
for (let k = 0; k < multiline.length; k += 2) {
|
|
1732
|
+
for (let l = lineOf(multiline[k]) + 1; l <= lineOf(multiline[k + 1] - 1); l++) inString.add(l - 1);
|
|
1733
|
+
}
|
|
1734
|
+
// Declarations count wherever module-level code can put them: under an
|
|
1735
|
+
// `if TYPE_CHECKING:`, a `try:`, a platform check. Only a def or class
|
|
1736
|
+
// body ends module level. A decorator belongs to what it decorates, so
|
|
1737
|
+
// `start` is the first decorator line.
|
|
1738
|
+
const scopes = [];
|
|
1739
|
+
let bracket = 0, continued = false, lastCode = 0, decoStart = null;
|
|
1740
|
+
const lineStarts = [0];
|
|
1741
|
+
for (let k = masked.indexOf("\n"); k >= 0; k = masked.indexOf("\n", k + 1)) lineStarts.push(k + 1);
|
|
1742
|
+
for (let i = 0; i < maskedLines.length; i++) {
|
|
1743
|
+
const ln = maskedLines[i];
|
|
1744
|
+
const hasCode = /\S/.test(ln);
|
|
1745
|
+
if (hasCode && bracket === 0 && !continued && !inString.has(i)) {
|
|
1746
|
+
const indent = ln.match(/^[ \t]*/)[0].replace(/\t/g, " ").length;
|
|
1747
|
+
while (scopes.length && indent <= scopes[scopes.length - 1].indent) scopes.pop().ref.end = lastCode;
|
|
1748
|
+
const top = scopes[scopes.length - 1];
|
|
1749
|
+
let m;
|
|
1750
|
+
if (/^[ \t]*@/.test(ln)) {
|
|
1751
|
+
if (decoStart === null) decoStart = i + 1;
|
|
1752
|
+
} else {
|
|
1753
|
+
const start = decoStart !== null ? decoStart : i + 1;
|
|
1754
|
+
decoStart = null;
|
|
1755
|
+
if ((m = ln.match(/^[ \t]*class\s+([A-Za-z_][\w]*)\s*(\()?/))) {
|
|
1756
|
+
if (top) scopes.push({ ref: {}, indent, kind: "inner" });
|
|
1757
|
+
else {
|
|
1758
|
+
let basesText = "";
|
|
1759
|
+
if (m[2]) {
|
|
1760
|
+
const open = lineStarts[i] + m[0].length - 1;
|
|
1761
|
+
const close = matchClose(masked, open);
|
|
1762
|
+
if (close > open) basesText = masked.slice(open + 1, close);
|
|
1763
|
+
}
|
|
1764
|
+
const bases = [];
|
|
1765
|
+
for (const piece of splitTopLevel(basesText)) {
|
|
1766
|
+
const bm = piece.trim().match(/^((?:[A-Za-z_]\w*\.)*)([A-Za-z_]\w*)\s*(?:\[[\s\S]*\])?$/);
|
|
1767
|
+
if (bm) bases.push({ module: bm[1].replace(/\.$/, ""), name: bm[2] });
|
|
1768
|
+
}
|
|
1769
|
+
const cls = { name: m[1], line: i + 1, start, end: null, extends: null, bases, implements: [], methods: [] };
|
|
1770
|
+
d.classes.push(cls);
|
|
1771
|
+
scopes.push({ ref: cls, indent, kind: "class" });
|
|
1772
|
+
}
|
|
1773
|
+
} else if ((m = ln.match(/^[ \t]*(?:async\s+)?def\s+([A-Za-z_][\w]*)/))) {
|
|
1774
|
+
if (!top) {
|
|
1775
|
+
const fn = { name: m[1], line: i + 1, start, end: null };
|
|
1776
|
+
d.functions.push(fn);
|
|
1777
|
+
scopes.push({ ref: fn, indent, kind: "def" });
|
|
1778
|
+
} else if (top.kind === "class") {
|
|
1779
|
+
const meth = { name: m[1], line: i + 1, start, end: null };
|
|
1780
|
+
scopes.push({ ref: addMethod(top.ref, meth) ? meth : {}, indent, kind: "def" });
|
|
1781
|
+
} else scopes.push({ ref: {}, indent, kind: "inner" });
|
|
1782
|
+
} else if (!top && (m = ln.match(/^[ \t]*([A-Za-z_]\w*)[ \t]*(?::[^=\n]*\S[^=\n]*(?:=(?!=)|$)|=(?!=))/)) && !PY_STATEMENT_WORDS.has(m[1])) {
|
|
1783
|
+
d.types.push({ name: m[1], line: i + 1, kind: "const" });
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
}
|
|
1787
|
+
for (const ch of ln) {
|
|
1788
|
+
if (ch === "(" || ch === "[" || ch === "{") bracket++;
|
|
1789
|
+
else if ((ch === ")" || ch === "]" || ch === "}") && bracket > 0) bracket--;
|
|
1790
|
+
}
|
|
1791
|
+
continued = /\\\s*$/.test(ln);
|
|
1792
|
+
if (hasCode) lastCode = i + 1;
|
|
1793
|
+
}
|
|
1794
|
+
for (const sc of scopes) sc.ref.end = lastCode;
|
|
1795
|
+
// `import a.b as c, d` binds `c` and `d`; without an alias the dotted path
|
|
1796
|
+
// itself is the name calls go through (`a.b.f()`).
|
|
1797
|
+
const impRe = /^[ \t]*from\s+(\S+)\s+import\s+([^\n(]+|\([^)]*\))|^[ \t]*import\s+([^\n]+)/gm;
|
|
1798
|
+
let m2;
|
|
1799
|
+
while ((m2 = impRe.exec(masked)) !== null) {
|
|
1800
|
+
importRanges.push([m2.index, m2.index + m2[0].length]);
|
|
1801
|
+
const line = lineOf(m2.index + m2[0].search(/\S/));
|
|
1802
|
+
if (m2[3]) {
|
|
1803
|
+
for (const piece of m2[3].split(",")) {
|
|
1804
|
+
const im = piece.trim().match(/^([\w.]+)(?:\s+as\s+([A-Za-z_]\w*))?$/);
|
|
1805
|
+
if (im) d.imports.push({ spec: im[1], default: null, namespace: im[2] || im[1], names: [], reExport: false, dynamic: false, line });
|
|
1806
|
+
}
|
|
1807
|
+
} else {
|
|
1808
|
+
const names = m2[2].replace(/[()]/g, "").split(",").map((x) => x.trim().split(/\s+as\s+/)).filter((q) => q[0])
|
|
1809
|
+
.map((q) => ({ imported: q[0], local: q[1] || q[0] }));
|
|
1810
|
+
d.imports.push({ spec: m2[1], default: null, namespace: null, names, reExport: false, dynamic: false, line });
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
} else if (lang === "go") {
|
|
1814
|
+
parseGoDecls(d, masked, lineOf);
|
|
1815
|
+
// Named (`import foo "path"`), blank (`_`) and dot (`.`) imports all count;
|
|
1816
|
+
// a named alias becomes the local namespace so call binding can resolve
|
|
1817
|
+
// through it. Applies to both single-line and block forms.
|
|
1818
|
+
const impRe = /^import\s+(?:([\w.]+)\s+)?"([^"]+)"|^import\s+\(([^)]*)\)/gmd;
|
|
1819
|
+
let m2;
|
|
1820
|
+
while ((m2 = impRe.exec(masked)) !== null) {
|
|
1821
|
+
const entries = [];
|
|
1822
|
+
if (m2[2]) entries.push({ alias: m2[1] || null, spec: specAt(m2, 2) });
|
|
1823
|
+
else if (m2[3]) {
|
|
1824
|
+
const block = m2.indices[3][0];
|
|
1825
|
+
for (const lm of m2[3].matchAll(/^\s*(?:([\w.]+)\s+)?"([^"]+)"/gmd)) {
|
|
1826
|
+
entries.push({ alias: lm[1] || null, spec: content.slice(block + lm.indices[2][0], block + lm.indices[2][1]) });
|
|
1827
|
+
}
|
|
1828
|
+
}
|
|
1829
|
+
for (const e of entries) {
|
|
1830
|
+
const ns = e.alias && e.alias !== "_" && e.alias !== "." ? e.alias : e.spec.split("/").pop();
|
|
1831
|
+
d.imports.push({ spec: e.spec, default: null, namespace: ns, names: [], reExport: false, dynamic: false, line: lineOf(m2.index), goAlias: e.alias });
|
|
1832
|
+
}
|
|
1833
|
+
}
|
|
1834
|
+
} else if (lang === "rust") {
|
|
1835
|
+
parseRustDecls(d, masked, lineOf);
|
|
1836
|
+
}
|
|
1837
|
+
// What the call scans read: the masked text minus import statements and, in
|
|
1838
|
+
// TypeScript, interface bodies and type aliases.
|
|
1839
|
+
d.callText = blankRanges(masked, lang === "typescript" ? importRanges.concat(typeDeclRanges(masked)) : importRanges);
|
|
1840
|
+
return d;
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
// ============================================================================
|
|
1844
|
+
// Build (phases 2-8)
|
|
1845
|
+
// ============================================================================
|
|
1846
|
+
|
|
1847
|
+
// Every target string of a package.json `exports` value (string, conditions
|
|
1848
|
+
// object or fallback array), in declaration order.
|
|
1849
|
+
function exportTargets(v, out = []) {
|
|
1850
|
+
if (typeof v === "string") out.push(v);
|
|
1851
|
+
else if (v && typeof v === "object") for (const k of Object.values(v)) exportTargets(k, out);
|
|
1852
|
+
return out;
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
// Targets of a package's root export ("." entry), in declaration order. Only
|
|
1856
|
+
// the conditions a Node/TypeScript consumer of the package can take are read:
|
|
1857
|
+
// others (`browser`, `bun`, `development`, …) may point at a different entry.
|
|
1858
|
+
const ROOT_EXPORT_CONDITIONS = new Set(["source", "types", "import", "module", "node", "require", "default"]);
|
|
1859
|
+
function rootExportTargets(v, out = []) {
|
|
1860
|
+
if (typeof v === "string") out.push(v);
|
|
1861
|
+
else if (Array.isArray(v)) for (const x of v) rootExportTargets(x, out);
|
|
1862
|
+
else if (v && typeof v === "object") {
|
|
1863
|
+
for (const [k, x] of Object.entries(v)) if (ROOT_EXPORT_CONDITIONS.has(k)) rootExportTargets(x, out);
|
|
1864
|
+
}
|
|
1865
|
+
return out;
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
// A package's `exports` as a subpath map ("." and "./sub" keys); null when
|
|
1869
|
+
// the field is absent. A string or a bare conditions object exports the
|
|
1870
|
+
// root alone.
|
|
1871
|
+
function subpathExports(exports) {
|
|
1872
|
+
if (exports === undefined || exports === null) return null;
|
|
1873
|
+
if (typeof exports === "object" && !Array.isArray(exports) && Object.keys(exports).some((k) => k.startsWith("."))) return exports;
|
|
1874
|
+
return { ".": exports };
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
// The source path a build output was compiled from (`./dist/x.js` or
|
|
1878
|
+
// `./dist/x.d.ts` → `./src/x`): `dist/` is never walked, so an export that
|
|
1879
|
+
// points only at build output is found through its source.
|
|
1880
|
+
function distToSrc(target) {
|
|
1881
|
+
return target.replace(/^(\.\/)?dist\//, "$1src/").replace(/\.d\.[mc]?ts$/, "");
|
|
1882
|
+
}
|
|
1883
|
+
|
|
1884
|
+
function buildGraph(root) {
|
|
1885
|
+
const files = walk(root);
|
|
1886
|
+
const nodes = [];
|
|
1887
|
+
const nodeById = new Map();
|
|
1888
|
+
const edges = [];
|
|
1889
|
+
const perFile = new Map(); // rel -> { lang, decls, stripped, localDefs: Map(name -> nid), importedNames: Map(local -> {spec, resolvedRel|null, imported}) }
|
|
1890
|
+
|
|
1891
|
+
// graphify ids.py canonicalization can collide (src/foo-bar.ts and
|
|
1892
|
+
// src/foo_bar.ts share an id). Parity requires keeping the id scheme, so
|
|
1893
|
+
// collisions merge deterministically (first wins) — but they are counted
|
|
1894
|
+
// and surfaced as a build warning instead of being silent.
|
|
1895
|
+
const idCollisions = [];
|
|
1896
|
+
const methodLimitHits = []; // classes that declare more than MAX_METHODS_PER_CLASS methods
|
|
1897
|
+
const textStats = new Map(); // rel -> { node, tf, count }
|
|
1898
|
+
const classMembers = new Map(); // class node id -> Map(method name -> node id)
|
|
1899
|
+
const baseOf = new Map(); // class node id -> node ids of the classes it extends
|
|
1900
|
+
const addNode = (n) => {
|
|
1901
|
+
const existing = nodeById.get(n.id);
|
|
1902
|
+
if (existing) {
|
|
1903
|
+
if (existing.source_file && n.source_file && existing.source_file !== n.source_file) {
|
|
1904
|
+
idCollisions.push({ id: n.id, kept: existing.source_file, dropped: n.source_file });
|
|
1905
|
+
}
|
|
1906
|
+
return existing;
|
|
1907
|
+
}
|
|
1908
|
+
nodeById.set(n.id, n);
|
|
1909
|
+
nodes.push(n);
|
|
1910
|
+
return n;
|
|
1911
|
+
};
|
|
1912
|
+
const addEdge = (source, target, relation, opts = {}) => {
|
|
1913
|
+
const confidence = opts.confidence || "EXTRACTED";
|
|
1914
|
+
edges.push({
|
|
1915
|
+
source, target, relation, confidence,
|
|
1916
|
+
confidence_score: opts.score != null ? opts.score : CONFIDENCE_SCORE_DEFAULTS[confidence],
|
|
1917
|
+
context: opts.context || undefined,
|
|
1918
|
+
source_file: opts.file || undefined,
|
|
1919
|
+
source_location: opts.line ? "L" + opts.line : undefined,
|
|
1920
|
+
weight: 1.0,
|
|
1921
|
+
_deferred: opts.deferred || undefined,
|
|
1922
|
+
});
|
|
1923
|
+
};
|
|
1924
|
+
|
|
1925
|
+
// -- phase 1+2: decls → nodes, contains/defines/method edges
|
|
1926
|
+
for (const abs of files) {
|
|
1927
|
+
const rel = path.relative(root, abs).split(path.sep).join("/");
|
|
1928
|
+
const ext = path.extname(rel).toLowerCase();
|
|
1929
|
+
const lang = KG_LANG_BY_EXT[ext];
|
|
1930
|
+
let content;
|
|
1931
|
+
try { content = fs.readFileSync(abs, "utf-8"); } catch { continue; }
|
|
1932
|
+
if (content.length > 2_000_000) continue;
|
|
1933
|
+
const decls = parseDecls(rel, content, lang);
|
|
1934
|
+
for (const c of decls.classes) {
|
|
1935
|
+
if (c.methodsOverLimit) methodLimitHits.push({ file: rel, name: c.name, declared: c.methods.length + c.methodsOverLimit });
|
|
1936
|
+
}
|
|
1937
|
+
const stripped = decls.callText;
|
|
1938
|
+
const fileNid = fileNodeId(rel);
|
|
1939
|
+
const fileNode = addNode({ id: fileNid, label: path.basename(rel), file_type: "code", source_file: rel, source_location: "L1" });
|
|
1940
|
+
if (decls.docstring) fileNode.rationale = decls.docstring;
|
|
1941
|
+
// words of the file's text (comments and strings included), for `kg query`
|
|
1942
|
+
if (fileNode.source_file === rel && !textStats.has(rel)) {
|
|
1943
|
+
const tf = new Map();
|
|
1944
|
+
let count = 0;
|
|
1945
|
+
for (const m of content.matchAll(/[A-Z]+(?![a-z])|[A-Z]?[a-z]+|[0-9]+/g)) {
|
|
1946
|
+
if (m[0].length < 3) continue;
|
|
1947
|
+
const t = m[0].toLowerCase();
|
|
1948
|
+
tf.set(t, (tf.get(t) || 0) + 1);
|
|
1949
|
+
count++;
|
|
1950
|
+
}
|
|
1951
|
+
textStats.set(rel, { node: fileNode, tf, count });
|
|
1952
|
+
}
|
|
1953
|
+
const localDefs = new Map();
|
|
1954
|
+
const symIds = fileSymbolIds(rel, [...decls.classes, ...decls.functions, ...decls.types].map((x) => x.name), lang);
|
|
1955
|
+
|
|
1956
|
+
for (const c of decls.classes) {
|
|
1957
|
+
const nid = symIds.get(c.name);
|
|
1958
|
+
addNode({ id: nid, label: c.name, file_type: "code", source_file: rel, source_location: "L" + c.line, type: c.kind && c.kind !== "struct" ? c.kind : "class" });
|
|
1959
|
+
addEdge(fileNid, nid, "contains", { file: rel, line: c.line });
|
|
1960
|
+
localDefs.set(c.name, nid);
|
|
1961
|
+
c.methodIds = methodNodeIds(rel, c, lang);
|
|
1962
|
+
if (c.methods.length) classMembers.set(nid, c.methodIds);
|
|
1963
|
+
for (const meth of c.methods) {
|
|
1964
|
+
const mid = c.methodIds.get(meth.name);
|
|
1965
|
+
addNode({ id: mid, label: meth.name + "()", file_type: "code", source_file: rel, source_location: "L" + meth.line, _callable: true });
|
|
1966
|
+
addEdge(nid, mid, "method", { file: rel, line: meth.line });
|
|
1967
|
+
}
|
|
1968
|
+
}
|
|
1969
|
+
for (const f of decls.functions) {
|
|
1970
|
+
if (localDefs.has(f.name)) continue;
|
|
1971
|
+
const nid = symIds.get(f.name);
|
|
1972
|
+
addNode({ id: nid, label: f.name + "()", file_type: "code", source_file: rel, source_location: "L" + f.line, _callable: true });
|
|
1973
|
+
addEdge(fileNid, nid, "defines", { file: rel, line: f.line });
|
|
1974
|
+
localDefs.set(f.name, nid);
|
|
1975
|
+
}
|
|
1976
|
+
for (const t of decls.types) {
|
|
1977
|
+
if (localDefs.has(t.name)) continue;
|
|
1978
|
+
const nid = symIds.get(t.name);
|
|
1979
|
+
addNode({ id: nid, label: t.name, file_type: "code", source_file: rel, source_location: "L" + t.line, type: t.kind });
|
|
1980
|
+
addEdge(fileNid, nid, "defines", { file: rel, line: t.line });
|
|
1981
|
+
localDefs.set(t.name, nid);
|
|
1982
|
+
}
|
|
1983
|
+
// The file's default binding: `export default function original()` is
|
|
1984
|
+
// imported under ANY local name (`import renamed from …`), so default
|
|
1985
|
+
// imports must resolve through this, never through the importer's name.
|
|
1986
|
+
let defaultNid = null;
|
|
1987
|
+
for (const c of decls.classes) if (c.isDefault && localDefs.has(c.name)) defaultNid = localDefs.get(c.name);
|
|
1988
|
+
for (const f of decls.functions) if (f.isDefault && localDefs.has(f.name)) defaultNid = localDefs.get(f.name);
|
|
1989
|
+
if (!defaultNid && decls.defaultName && localDefs.has(decls.defaultName)) defaultNid = localDefs.get(decls.defaultName);
|
|
1990
|
+
// Declarations below top level (inside function bodies and callbacks):
|
|
1991
|
+
// one node each under the declaration that holds them, repeats numbered.
|
|
1992
|
+
if (decls.nested && decls.nested.length) {
|
|
1993
|
+
const holders = [];
|
|
1994
|
+
for (const c of decls.classes) {
|
|
1995
|
+
holders.push({ nid: localDefs.get(c.name), line: c.line, end: c.end });
|
|
1996
|
+
for (const meth of c.methods) holders.push({ nid: c.methodIds.get(meth.name), line: meth.line, end: meth.end });
|
|
1997
|
+
}
|
|
1998
|
+
for (const f of decls.functions) holders.push({ nid: localDefs.get(f.name), line: f.line, end: f.end });
|
|
1999
|
+
holders.sort((a, b) => a.line - b.line);
|
|
2000
|
+
const used = new Map();
|
|
2001
|
+
for (const nd of decls.nested) {
|
|
2002
|
+
let parent = fileNid;
|
|
2003
|
+
for (const h of holders) {
|
|
2004
|
+
if (h.line > nd.line) break;
|
|
2005
|
+
if (h.nid && h.end != null && nd.line <= h.end) parent = h.nid;
|
|
2006
|
+
}
|
|
2007
|
+
const base = makeId(parent, nd.name);
|
|
2008
|
+
const seen = (used.get(base) || 0) + 1;
|
|
2009
|
+
used.set(base, seen);
|
|
2010
|
+
const nid = seen === 1 && !nodeById.has(base) ? base : makeId(base, String(seen));
|
|
2011
|
+
if (nodeById.has(nid)) continue;
|
|
2012
|
+
if (nd.kind === "function") addNode({ id: nid, label: nd.name + "()", file_type: "code", source_file: rel, source_location: "L" + nd.line, _callable: true });
|
|
2013
|
+
else addNode({ id: nid, label: nd.name, file_type: "code", source_file: rel, source_location: "L" + nd.line, type: nd.kind });
|
|
2014
|
+
addEdge(parent, nid, "contains", { file: rel, line: nd.line });
|
|
2015
|
+
}
|
|
2016
|
+
}
|
|
2017
|
+
perFile.set(rel, { lang, decls, stripped, localDefs, defaultNid, importedNames: new Map(), fileNid, lineOf: lineIndex(content) });
|
|
2018
|
+
}
|
|
2019
|
+
|
|
2020
|
+
// -- Go: package scopes. A package is every file of one directory with one
|
|
2021
|
+
// package clause; functions, types and values are visible across its files,
|
|
2022
|
+
// and a method joins its receiver type from whichever file declares it.
|
|
2023
|
+
const goScopes = new Map();
|
|
2024
|
+
const goScopeOf = (dir, name) => {
|
|
2025
|
+
const k = dir + "\n" + name;
|
|
2026
|
+
let s = goScopes.get(k);
|
|
2027
|
+
if (!s) goScopes.set(k, s = { dir, name, files: [], funcs: new Map(), types: new Map(), values: new Map(), vars: new Map(), methods: new Map() });
|
|
2028
|
+
return s;
|
|
2029
|
+
};
|
|
2030
|
+
const goMethodsByName = new Map(); // repo-wide: method name -> entries
|
|
2031
|
+
for (const [rel, pf] of perFile) {
|
|
2032
|
+
if (pf.lang !== "go") continue;
|
|
2033
|
+
const d = pf.decls;
|
|
2034
|
+
const scope = goScopeOf(path.posix.dirname(rel), d.goPackage);
|
|
2035
|
+
pf.goScope = scope;
|
|
2036
|
+
scope.files.push(rel);
|
|
2037
|
+
for (const f of d.functions) if (!scope.funcs.has(f.name)) scope.funcs.set(f.name, { nid: pf.localDefs.get(f.name), fn: f, rel });
|
|
2038
|
+
for (const c of d.classes) if (!scope.types.has(c.name)) scope.types.set(c.name, { nid: pf.localDefs.get(c.name), cls: c, rel, scope });
|
|
2039
|
+
for (const t of d.types) if (!scope.values.has(t.name)) scope.values.set(t.name, { nid: pf.localDefs.get(t.name), rel });
|
|
2040
|
+
for (const [name, v] of d.goVars) if (!scope.vars.has(name)) scope.vars.set(name, { type: v.type, init: v.init, rel });
|
|
2041
|
+
}
|
|
2042
|
+
for (const [rel, pf] of perFile) {
|
|
2043
|
+
if (pf.lang !== "go") continue;
|
|
2044
|
+
const byType = new Map();
|
|
2045
|
+
for (const m of pf.decls.goMethods) {
|
|
2046
|
+
if (!byType.has(m.recvType)) byType.set(m.recvType, []);
|
|
2047
|
+
byType.get(m.recvType).push(m);
|
|
2048
|
+
}
|
|
2049
|
+
for (const [typeName, ms] of byType) {
|
|
2050
|
+
const ids = methodNodeIds(rel, { name: typeName, methods: ms }, "go");
|
|
2051
|
+
const owner = pf.goScope.types.get(typeName);
|
|
2052
|
+
let table = pf.goScope.methods.get(typeName);
|
|
2053
|
+
if (!table) pf.goScope.methods.set(typeName, table = new Map());
|
|
2054
|
+
for (const m of ms) {
|
|
2055
|
+
m.nid = ids.get(m.name);
|
|
2056
|
+
addNode({ id: m.nid, label: m.name + "()", file_type: "code", source_file: rel, source_location: "L" + m.line, _callable: true });
|
|
2057
|
+
if (owner) addEdge(owner.nid, m.nid, "method", { file: rel, line: m.line });
|
|
2058
|
+
else addEdge(pf.fileNid, m.nid, "defines", { file: rel, line: m.line });
|
|
2059
|
+
const entry = { nid: m.nid, fn: m, rel };
|
|
2060
|
+
if (!table.has(m.name)) table.set(m.name, entry);
|
|
2061
|
+
if (!goMethodsByName.has(m.name)) goMethodsByName.set(m.name, []);
|
|
2062
|
+
goMethodsByName.get(m.name).push(entry);
|
|
2063
|
+
}
|
|
2064
|
+
}
|
|
2065
|
+
}
|
|
2066
|
+
// Module paths (go.mod) map an import path to a repo directory; the package a
|
|
2067
|
+
// directory exports is its non-test one.
|
|
2068
|
+
const goModules = [];
|
|
2069
|
+
const goDirScope = new Map();
|
|
2070
|
+
if (goScopes.size) {
|
|
2071
|
+
const dirs = new Set();
|
|
2072
|
+
for (const s of goScopes.values()) {
|
|
2073
|
+
const parts = s.dir === "." ? [] : s.dir.split("/");
|
|
2074
|
+
for (let i = 0; i <= parts.length; i++) dirs.add(parts.slice(0, i).join("/"));
|
|
2075
|
+
if (s.name.endsWith("_test")) continue;
|
|
2076
|
+
const cur = goDirScope.get(s.dir);
|
|
2077
|
+
if (!cur || s.files.length > cur.files.length) goDirScope.set(s.dir, s);
|
|
2078
|
+
}
|
|
2079
|
+
for (const dir of [...dirs].sort()) {
|
|
2080
|
+
try {
|
|
2081
|
+
const mod = fs.readFileSync(path.join(root, dir, "go.mod"), "utf-8").match(/^module\s+(\S+)/m);
|
|
2082
|
+
if (mod) goModules.push({ dir, path: mod[1] });
|
|
2083
|
+
} catch { /* no go.mod here */ }
|
|
2084
|
+
}
|
|
2085
|
+
goModules.sort((a, b) => b.path.length - a.path.length || (a.dir < b.dir ? -1 : 1));
|
|
2086
|
+
}
|
|
2087
|
+
const resolveGoImport = (spec) => {
|
|
2088
|
+
for (const m of goModules) {
|
|
2089
|
+
if (spec !== m.path && !spec.startsWith(m.path + "/")) continue;
|
|
2090
|
+
return goDirScope.get(path.posix.join(m.dir || ".", spec.slice(m.path.length + 1))) || null;
|
|
2091
|
+
}
|
|
2092
|
+
return null;
|
|
2093
|
+
};
|
|
2094
|
+
for (const pf of perFile.values()) {
|
|
2095
|
+
if (pf.lang !== "go") continue;
|
|
2096
|
+
pf.goImports = new Map(); // local name -> package scope, or null for a package outside the repo
|
|
2097
|
+
pf.goDot = [];
|
|
2098
|
+
for (const imp of pf.decls.imports) {
|
|
2099
|
+
imp.goScope = resolveGoImport(imp.spec);
|
|
2100
|
+
if (imp.goAlias === "_") continue;
|
|
2101
|
+
if (imp.goAlias === ".") { if (imp.goScope) pf.goDot.push(imp.goScope); continue; }
|
|
2102
|
+
const segs = imp.spec.split("/");
|
|
2103
|
+
let base = segs[segs.length - 1];
|
|
2104
|
+
if (/^v\d+$/.test(base) && segs.length > 1) base = segs[segs.length - 2];
|
|
2105
|
+
imp.goLocal = imp.goAlias || (imp.goScope ? imp.goScope.name : base.replace(/\.v\d+$/, "").replace(/^go-/, ""));
|
|
2106
|
+
pf.goImports.set(imp.goLocal, imp.goScope);
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
// An in-repo import depends on the files that declare what the importer
|
|
2110
|
+
// names through it (`pkg.Symbol`), not on the whole package; a package used
|
|
2111
|
+
// for nothing nameable (blank import) falls back to its entry file.
|
|
2112
|
+
const goImportEdges = (rel, pf, imp) => {
|
|
2113
|
+
const scope = imp.goScope;
|
|
2114
|
+
if (!scope) return false;
|
|
2115
|
+
const targets = new Set();
|
|
2116
|
+
if (imp.goLocal) {
|
|
2117
|
+
const re = new RegExp("(?<![\\w.])" + imp.goLocal.replace(/[^\w]/g, "\\$&") + "\\s*\\.\\s*([A-Za-z_]\\w*)", "g");
|
|
2118
|
+
const seen = new Set();
|
|
2119
|
+
let m;
|
|
2120
|
+
while ((m = re.exec(pf.decls.masked)) !== null) {
|
|
2121
|
+
if (seen.has(m[1])) continue;
|
|
2122
|
+
seen.add(m[1]);
|
|
2123
|
+
const e = scope.funcs.get(m[1]) || scope.types.get(m[1]) || scope.values.get(m[1]);
|
|
2124
|
+
if (!e || !e.nid) continue;
|
|
2125
|
+
targets.add(e.rel);
|
|
2126
|
+
addEdge(pf.fileNid, e.nid, "imports", { file: rel, line: imp.line, context: "import" });
|
|
2127
|
+
}
|
|
2128
|
+
}
|
|
2129
|
+
if (!targets.size) {
|
|
2130
|
+
const own = scope.files.filter((f) => !f.endsWith("_test.go")).sort();
|
|
2131
|
+
const named = own.find((f) => path.posix.basename(f) === scope.name + ".go") || own.find((f) => path.posix.basename(f) === "doc.go");
|
|
2132
|
+
const entry = named || own[0] || scope.files[0];
|
|
2133
|
+
if (entry) targets.add(entry);
|
|
2134
|
+
}
|
|
2135
|
+
for (const t of [...targets].sort()) addEdge(pf.fileNid, perFile.get(t).fileNid, "imports_from", { file: rel, line: imp.line, context: "import" });
|
|
2136
|
+
return true;
|
|
2137
|
+
};
|
|
2138
|
+
// Type expression (as written in `ctxRel`) → the repo type it names, or null.
|
|
2139
|
+
const goTypeRef = (typeStr, ctxRel) => {
|
|
2140
|
+
if (!typeStr) return null;
|
|
2141
|
+
const name = typeStr.startsWith("[]") ? typeStr.slice(2) : typeStr;
|
|
2142
|
+
const pf = perFile.get(ctxRel);
|
|
2143
|
+
const dot = name.indexOf(".");
|
|
2144
|
+
if (dot < 0) return pf.goScope.types.get(name) || null;
|
|
2145
|
+
const scope = pf.goImports.get(name.slice(0, dot));
|
|
2146
|
+
return (scope && scope.types.get(name.slice(dot + 1))) || null;
|
|
2147
|
+
};
|
|
2148
|
+
// Methods and fields are looked up on the type, then on what it embeds
|
|
2149
|
+
// (Go's promotion), a few levels deep.
|
|
2150
|
+
const goFindMethod = (t, name, depth = 0) => {
|
|
2151
|
+
if (!t || depth > 4) return null;
|
|
2152
|
+
const table = t.scope.methods.get(t.cls.name);
|
|
2153
|
+
const hit = table && table.get(name);
|
|
2154
|
+
if (hit) return hit;
|
|
2155
|
+
for (const e of t.cls.embeds) {
|
|
2156
|
+
const r = goFindMethod(goTypeRef(e, t.rel), name, depth + 1);
|
|
2157
|
+
if (r) return r;
|
|
2158
|
+
}
|
|
2159
|
+
if (t.cls.underlying && t.cls.kind === "type") return null;
|
|
2160
|
+
return null;
|
|
2161
|
+
};
|
|
2162
|
+
const goFieldType = (t, field, depth = 0) => {
|
|
2163
|
+
if (!t || depth > 4) return null;
|
|
2164
|
+
const f = t.cls.fields.get(field);
|
|
2165
|
+
if (f) return { type: f, rel: t.rel };
|
|
2166
|
+
for (const e of t.cls.embeds) {
|
|
2167
|
+
if (e.split(".").pop() === field) return { type: e, rel: t.rel };
|
|
2168
|
+
const r = goFieldType(goTypeRef(e, t.rel), field, depth + 1);
|
|
2169
|
+
if (r) return r;
|
|
2170
|
+
}
|
|
2171
|
+
return null;
|
|
2172
|
+
};
|
|
2173
|
+
const goVarTypes = new Map();
|
|
2174
|
+
const goVarType = (v) => {
|
|
2175
|
+
if (v.type) return { type: v.type, rel: v.rel };
|
|
2176
|
+
if (goVarTypes.has(v)) return goVarTypes.get(v);
|
|
2177
|
+
goVarTypes.set(v, null); // cycle guard
|
|
2178
|
+
const t = goInitType(v.init, v.rel, new Map());
|
|
2179
|
+
goVarTypes.set(v, t);
|
|
2180
|
+
return t;
|
|
2181
|
+
};
|
|
2182
|
+
const GO_EXTERNAL = { kind: "external" };
|
|
2183
|
+
const goFuncNamed = (pf, name) => {
|
|
2184
|
+
const f = pf.goScope.funcs.get(name);
|
|
2185
|
+
if (f) return f;
|
|
2186
|
+
for (const ds of pf.goDot) { const g = ds.funcs.get(name); if (g) return g; }
|
|
2187
|
+
return null;
|
|
2188
|
+
};
|
|
2189
|
+
// What `name` selects on the value, package or type reached so far. A member
|
|
2190
|
+
// the repo does not declare (a type from outside, an interface method, a
|
|
2191
|
+
// func-typed field) is "external": nothing binds to it, and nothing is
|
|
2192
|
+
// guessed for it.
|
|
2193
|
+
const goMember = (cur, name) => {
|
|
2194
|
+
if (cur.kind === "pkg") {
|
|
2195
|
+
const f = cur.scope.funcs.get(name);
|
|
2196
|
+
if (f) return { kind: "fn", entry: f };
|
|
2197
|
+
const v = cur.scope.vars.get(name);
|
|
2198
|
+
if (v) { const t = goVarType(v); return t ? { kind: "value", type: t.type, rel: t.rel } : GO_EXTERNAL; }
|
|
2199
|
+
const t = cur.scope.types.get(name);
|
|
2200
|
+
return t ? { kind: "typename", t } : GO_EXTERNAL;
|
|
2201
|
+
}
|
|
2202
|
+
if (cur.kind === "value" || cur.kind === "typename") {
|
|
2203
|
+
if (cur.kind === "value" && !cur.t && cur.type.startsWith("[]")) return GO_EXTERNAL;
|
|
2204
|
+
const t = cur.t || goTypeRef(cur.type, cur.rel);
|
|
2205
|
+
if (!t) return GO_EXTERNAL;
|
|
2206
|
+
const m = goFindMethod(t, name);
|
|
2207
|
+
if (m) return { kind: "fn", entry: m };
|
|
2208
|
+
if (cur.kind === "typename") return GO_EXTERNAL;
|
|
2209
|
+
const f = goFieldType(t, name);
|
|
2210
|
+
return f ? { kind: "value", type: f.type, rel: f.rel } : GO_EXTERNAL;
|
|
2211
|
+
}
|
|
2212
|
+
return GO_EXTERNAL;
|
|
2213
|
+
};
|
|
2214
|
+
const goIdentRe = /[A-Za-z_]\w*/y;
|
|
2215
|
+
// Evaluates the selector chain that starts with the identifier at `start`
|
|
2216
|
+
// (`x.field.Method(args)[i].Other()`): every call it makes to a repo
|
|
2217
|
+
// function or method is reported through `onCall`, and the result is what
|
|
2218
|
+
// the chain yields (a typed value, where the types are known) with its end
|
|
2219
|
+
// offset.
|
|
2220
|
+
const goEval = (text, start, env, pf, rel, onCall) => {
|
|
2221
|
+
goIdentRe.lastIndex = start;
|
|
2222
|
+
const hm = goIdentRe.exec(text);
|
|
2223
|
+
if (!hm) return null;
|
|
2224
|
+
const head = hm[0];
|
|
2225
|
+
let i = start + head.length;
|
|
2226
|
+
let cur;
|
|
2227
|
+
if (env.has(head)) { const v = env.get(head); cur = v.type ? { kind: "value", type: v.type, rel: v.rel } : GO_EXTERNAL; }
|
|
2228
|
+
else if (pf.goImports.has(head)) { const s = pf.goImports.get(head); cur = s ? { kind: "pkg", scope: s } : GO_EXTERNAL; }
|
|
2229
|
+
else if (pf.goScope.vars.has(head)) { const t = goVarType(pf.goScope.vars.get(head)); cur = t ? { kind: "value", type: t.type, rel: t.rel } : GO_EXTERNAL; }
|
|
2230
|
+
else if (!GO_CALL_SKIP.has(head) && goFuncNamed(pf, head)) cur = { kind: "fn", entry: goFuncNamed(pf, head) };
|
|
2231
|
+
else if (pf.goScope.types.has(head)) cur = { kind: "typename", t: pf.goScope.types.get(head) };
|
|
2232
|
+
else cur = GO_EXTERNAL;
|
|
2233
|
+
for (;;) {
|
|
2234
|
+
let j = i;
|
|
2235
|
+
while (text[j] === " " || text[j] === "\t") j++;
|
|
2236
|
+
const ch = text[j];
|
|
2237
|
+
if (ch === ".") {
|
|
2238
|
+
let k = j + 1;
|
|
2239
|
+
while (k < text.length && /\s/.test(text[k])) k++;
|
|
2240
|
+
if (text[k] === "(") { // type assertion
|
|
2241
|
+
const e = matchClose(text, k);
|
|
2242
|
+
if (e < 0) break;
|
|
2243
|
+
const tn = goTypeName(text.slice(k + 1, e));
|
|
2244
|
+
cur = tn && tn !== "type" ? { kind: "value", type: tn, rel } : GO_EXTERNAL;
|
|
2245
|
+
i = e + 1;
|
|
2246
|
+
continue;
|
|
2247
|
+
}
|
|
2248
|
+
goIdentRe.lastIndex = k;
|
|
2249
|
+
const fm = goIdentRe.exec(text);
|
|
2250
|
+
if (!fm) break;
|
|
2251
|
+
cur = goMember(cur, fm[0]);
|
|
2252
|
+
i = k + fm[0].length;
|
|
2253
|
+
continue;
|
|
2254
|
+
}
|
|
2255
|
+
if (ch === "(") {
|
|
2256
|
+
const e = matchClose(text, j);
|
|
2257
|
+
if (e < 0) break;
|
|
2258
|
+
if (cur.kind === "fn") {
|
|
2259
|
+
onCall(cur.entry, j);
|
|
2260
|
+
cur = cur.entry.fn.result ? { kind: "value", type: cur.entry.fn.result, rel: cur.entry.rel } : GO_EXTERNAL;
|
|
2261
|
+
} else if (cur.kind === "typename") cur = { kind: "value", t: cur.t, type: cur.t.cls.name, rel: cur.t.rel }; // conversion
|
|
2262
|
+
else cur = GO_EXTERNAL;
|
|
2263
|
+
i = e + 1;
|
|
2264
|
+
continue;
|
|
2265
|
+
}
|
|
2266
|
+
if (ch === "[") {
|
|
2267
|
+
const e = matchClose(text, j);
|
|
2268
|
+
if (e < 0) break;
|
|
2269
|
+
if (cur.kind === "value" && !cur.t && cur.type.startsWith("[]")) cur = { kind: "value", type: cur.type.slice(2), rel: cur.rel };
|
|
2270
|
+
else if (cur.kind !== "fn" && cur.kind !== "typename") cur = GO_EXTERNAL; // generic arguments keep the function
|
|
2271
|
+
i = e + 1;
|
|
2272
|
+
continue;
|
|
2273
|
+
}
|
|
2274
|
+
break;
|
|
2275
|
+
}
|
|
2276
|
+
return { cur, end: i };
|
|
2277
|
+
};
|
|
2278
|
+
const goNoCall = () => {};
|
|
2279
|
+
// The type an initializer yields: a composite literal, `new(T)`, or a
|
|
2280
|
+
// selector chain (conversion, call result, field) that is the whole expression.
|
|
2281
|
+
const goInitType = (init, rel, env) => {
|
|
2282
|
+
const text = String(init || "").trim().replace(/^[&*]\s*/, "");
|
|
2283
|
+
let m;
|
|
2284
|
+
if ((m = text.match(/^([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)?)\s*\{/))) return goTypeRef(m[1], rel) ? { type: m[1], rel } : null;
|
|
2285
|
+
if ((m = text.match(/^new\(\s*([A-Za-z_]\w*(?:\.[A-Za-z_]\w*)?)\s*\)/))) return goTypeRef(m[1], rel) ? { type: m[1], rel } : null;
|
|
2286
|
+
if (!/^[A-Za-z_]/.test(text)) return null;
|
|
2287
|
+
const r = goEval(text, 0, env, perFile.get(rel), rel, goNoCall);
|
|
2288
|
+
if (!r || r.cur.kind !== "value" || !/^\s*(?:$|[,;{)])/.test(text.slice(r.end))) return null;
|
|
2289
|
+
return r.cur.t ? { type: r.cur.t.cls.name, rel: r.cur.t.rel } : { type: r.cur.type, rel: r.cur.rel };
|
|
2290
|
+
};
|
|
2291
|
+
// Variable types inside one function: receiver, parameters (closures'
|
|
2292
|
+
// included) and the local declarations whose type is written or follows
|
|
2293
|
+
// from the initializer. Any other local is recorded with no type, so it
|
|
2294
|
+
// shadows a package-level name without a guess.
|
|
2295
|
+
const goEnvOf = (fn, pf, rel, text) => {
|
|
2296
|
+
const env = new Map();
|
|
2297
|
+
if (fn.recvName) env.set(fn.recvName, { type: fn.recvType, rel });
|
|
2298
|
+
for (const p of fn.params) env.set(p.name, { type: p.type, rel });
|
|
2299
|
+
if (fn.bodyStart < 0) return env;
|
|
2300
|
+
const body = text.slice(fn.bodyStart, fn.bodyEnd);
|
|
2301
|
+
for (const m of body.matchAll(/\bfunc\s*\(([^)]*)\)/g)) {
|
|
2302
|
+
for (const p of goParams(m[1])) if (!env.has(p.name)) env.set(p.name, { type: p.type, rel });
|
|
2303
|
+
}
|
|
2304
|
+
const bind = (name, t) => { if (name !== "_" && !env.has(name)) env.set(name, t || { type: null, rel }); };
|
|
2305
|
+
for (const ln of body.split("\n")) {
|
|
2306
|
+
let m;
|
|
2307
|
+
if ((m = ln.match(/\bvar\s+([A-Za-z_]\w*)\s+([^=\n]+?)\s*(?:=|$)/))) bind(m[1], { type: goTypeName(m[2]), rel });
|
|
2308
|
+
else if ((m = ln.match(/\bfor\s+(\w+)\s*(?:,\s*([A-Za-z_]\w*)\s*)?:=\s*range\s+(\S.*)$/))) {
|
|
2309
|
+
bind(m[1], null);
|
|
2310
|
+
if (m[2]) {
|
|
2311
|
+
const tt = /^[A-Za-z_]/.test(m[3]) ? goEval(m[3], 0, env, pf, rel, goNoCall) : null;
|
|
2312
|
+
const v = tt && tt.cur.kind === "value" && !tt.cur.t && tt.cur.type.startsWith("[]") ? { type: tt.cur.type.slice(2), rel: tt.cur.rel } : null;
|
|
2313
|
+
bind(m[2], v);
|
|
2314
|
+
}
|
|
2315
|
+
} else if ((m = ln.match(/\b([A-Za-z_]\w*)\s*((?:,\s*[A-Za-z_]\w*\s*)*):=\s*(\S.*)$/))) {
|
|
2316
|
+
bind(m[1], goInitType(m[3], rel, env));
|
|
2317
|
+
for (const other of m[2].split(",")) if (other.trim()) bind(other.trim(), null);
|
|
2318
|
+
}
|
|
2319
|
+
}
|
|
2320
|
+
return env;
|
|
2321
|
+
};
|
|
2322
|
+
const goHeadRe = /(?<![\w.)\]}])[A-Za-z_]\w*/g;
|
|
2323
|
+
const goArgRe = /[(,]\s*([A-Za-z_]\w*)\s*(?=[,)])/g;
|
|
2324
|
+
const goCalls = (rel, pf) => {
|
|
2325
|
+
const text = pf.stripped;
|
|
2326
|
+
const seen = new Set();
|
|
2327
|
+
const emit = (caller, callee, offset, relation) => {
|
|
2328
|
+
if (!callee || !callee.nid || callee.nid === caller) return;
|
|
2329
|
+
const key = caller + "|" + callee.nid;
|
|
2330
|
+
if (seen.has(key + "|calls") || seen.has(key + "|" + relation)) return;
|
|
2331
|
+
seen.add(key + "|" + relation);
|
|
2332
|
+
const inferred = relation === "indirect_call";
|
|
2333
|
+
addEdge(caller, callee.nid, relation, { file: rel, line: pf.lineOf(offset), confidence: inferred ? "INFERRED" : "EXTRACTED", score: inferred ? INFERRED_CALL_SCORE : undefined, context: "call" });
|
|
2334
|
+
};
|
|
2335
|
+
const scan = (from, to, caller, env, source) => {
|
|
2336
|
+
let m;
|
|
2337
|
+
goHeadRe.lastIndex = from;
|
|
2338
|
+
while ((m = goHeadRe.exec(source)) !== null && m.index < to) {
|
|
2339
|
+
goEval(source, m.index, env, pf, rel, (callee, at) => emit(caller, callee, at, "calls"));
|
|
2340
|
+
}
|
|
2341
|
+
const segment = source.slice(from, to);
|
|
2342
|
+
goArgRe.lastIndex = 0;
|
|
2343
|
+
while ((m = goArgRe.exec(segment)) !== null) {
|
|
2344
|
+
if (env.has(m[1]) || GO_CALL_SKIP.has(m[1])) continue;
|
|
2345
|
+
const f = goFuncNamed(pf, m[1]);
|
|
2346
|
+
if (f) emit(caller, f, from + m.index, "indirect_call");
|
|
2347
|
+
}
|
|
2348
|
+
};
|
|
2349
|
+
const taken = [];
|
|
2350
|
+
for (const fn of [...pf.decls.functions, ...pf.decls.goMethods]) {
|
|
2351
|
+
const eol = text.indexOf("\n", fn.declStart);
|
|
2352
|
+
taken.push([fn.declStart, fn.bodyEnd > 0 ? fn.bodyEnd + 1 : eol < 0 ? text.length : eol]);
|
|
2353
|
+
if (fn.bodyStart < 0) continue;
|
|
2354
|
+
const caller = fn.nid || pf.localDefs.get(fn.name);
|
|
2355
|
+
if (caller) scan(fn.bodyStart + 1, fn.bodyEnd, caller, goEnvOf(fn, pf, rel, text), text);
|
|
2356
|
+
}
|
|
2357
|
+
for (const c of pf.decls.classes) if (c.bodyStart >= 0) taken.push([c.bodyStart, c.bodyEnd + 1]);
|
|
2358
|
+
const outside = blankRanges(text, taken);
|
|
2359
|
+
scan(0, outside.length, pf.fileNid, new Map(), outside);
|
|
2360
|
+
};
|
|
2361
|
+
// Embedded types (struct in struct, interface in interface).
|
|
2362
|
+
for (const scope of goScopes.values()) {
|
|
2363
|
+
for (const t of scope.types.values()) {
|
|
2364
|
+
for (const e of t.cls.embeds) {
|
|
2365
|
+
const target = goTypeRef(e, t.rel);
|
|
2366
|
+
if (target && target.nid !== t.nid) addEdge(t.nid, target.nid, "embeds", { file: t.rel, line: t.cls.line });
|
|
2367
|
+
}
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
|
|
2371
|
+
// -- Rust: the module tree (`mod x;` → file, inline `mod x { … }`), each
|
|
2372
|
+
// module's items and `use` bindings, crates of the workspace, and impl
|
|
2373
|
+
// methods joined to the type they are for.
|
|
2374
|
+
const rsKey = (rel, inl) => rel + "\n" + inl;
|
|
2375
|
+
const rsScopes = new Map(); // key -> { rel, inl, items, mods: name -> key, modDecls, uses, globs, parent: key | null }
|
|
2376
|
+
const rsCrates = new Map(); // crate name -> its lib root file
|
|
2377
|
+
const rsChildOf = new Map(); // file -> key of the module that declares it
|
|
2378
|
+
const rsMethods = new Map(); // owner node id -> Map(method name -> [node ids])
|
|
2379
|
+
const RS_EXT = { ext: true };
|
|
2380
|
+
for (const [rel, pf] of perFile) {
|
|
2381
|
+
if (pf.lang !== "rust") continue;
|
|
2382
|
+
for (const sc of pf.decls.rsScopes.values()) {
|
|
2383
|
+
const mods = new Map();
|
|
2384
|
+
for (const [name, m] of sc.mods) mods.set(name, rsKey(rel, m.inline));
|
|
2385
|
+
for (const item of sc.items.values()) { item.rel = rel; item.nid = pf.localDefs.get(item.name) || null; }
|
|
2386
|
+
rsScopes.set(rsKey(rel, sc.inl), { rel, inl: sc.inl, items: sc.items, mods, modDecls: sc.modDecls, uses: sc.uses, globs: sc.globs, parent: sc.parent === null ? null : rsKey(rel, sc.parent) });
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
if (rsScopes.size) {
|
|
2390
|
+
const dirs = new Set();
|
|
2391
|
+
for (const sc of rsScopes.values()) {
|
|
2392
|
+
const parts = sc.rel.split("/");
|
|
2393
|
+
for (let i = 0; i < parts.length; i++) dirs.add(parts.slice(0, i).join("/"));
|
|
2394
|
+
}
|
|
2395
|
+
for (const dir of [...dirs].sort()) {
|
|
2396
|
+
let toml;
|
|
2397
|
+
try { toml = fs.readFileSync(path.join(root, dir, "Cargo.toml"), "utf-8"); } catch { continue; }
|
|
2398
|
+
let section = "", name = null, libPath = null;
|
|
2399
|
+
for (const raw of toml.split("\n")) {
|
|
2400
|
+
const ln = raw.trim();
|
|
2401
|
+
if (ln.startsWith("[")) { section = ln; continue; }
|
|
2402
|
+
const kv = ln.match(/^(name|path)\s*=\s*"([^"]*)"/);
|
|
2403
|
+
if (!kv) continue;
|
|
2404
|
+
if (section === "[package]" && kv[1] === "name" && name === null) name = kv[2];
|
|
2405
|
+
else if (section === "[lib]" && kv[1] === "name") name = kv[2];
|
|
2406
|
+
else if (section === "[lib]" && kv[1] === "path") libPath = kv[2];
|
|
2407
|
+
}
|
|
2408
|
+
const lib = path.posix.normalize(path.posix.join(dir || ".", libPath || "src/lib.rs"));
|
|
2409
|
+
if (name && perFile.has(lib)) rsCrates.set(name.replace(/-/g, "_"), lib);
|
|
2410
|
+
}
|
|
2411
|
+
// `mod x;` in a.rs names a/x.rs or a/x/mod.rs; in mod.rs, lib.rs, main.rs
|
|
2412
|
+
// and any other crate root it names a sibling.
|
|
2413
|
+
const keys = [...rsScopes.keys()].sort();
|
|
2414
|
+
for (const key of keys) {
|
|
2415
|
+
const sc = rsScopes.get(key);
|
|
2416
|
+
if (!sc.modDecls.length) continue;
|
|
2417
|
+
const dir = path.posix.dirname(sc.rel);
|
|
2418
|
+
const base = path.posix.basename(sc.rel);
|
|
2419
|
+
const own = path.posix.join(dir, base.replace(/\.rs$/, ""));
|
|
2420
|
+
const bases = base === "mod.rs" || base === "lib.rs" || base === "main.rs" ? [dir] : [own, dir];
|
|
2421
|
+
const inl = sc.inl ? sc.inl.split("::") : [];
|
|
2422
|
+
const pf = perFile.get(sc.rel);
|
|
2423
|
+
for (const decl of sc.modDecls) {
|
|
2424
|
+
let hit = null;
|
|
2425
|
+
for (const b of bases) {
|
|
2426
|
+
const here = path.posix.join(b, ...inl);
|
|
2427
|
+
for (const c of [path.posix.join(here, decl.name + ".rs"), path.posix.join(here, decl.name, "mod.rs")]) {
|
|
2428
|
+
const cand = path.posix.normalize(c);
|
|
2429
|
+
if (perFile.has(cand) && cand !== sc.rel) { hit = cand; break; }
|
|
2430
|
+
}
|
|
2431
|
+
if (hit) break;
|
|
2432
|
+
}
|
|
2433
|
+
if (!hit) continue;
|
|
2434
|
+
sc.mods.set(decl.name, rsKey(hit, ""));
|
|
2435
|
+
if (!rsChildOf.has(hit)) rsChildOf.set(hit, key);
|
|
2436
|
+
addEdge(pf.fileNid, perFile.get(hit).fileNid, "imports_from", { file: sc.rel, line: decl.line, context: "module" });
|
|
2437
|
+
}
|
|
2438
|
+
}
|
|
2439
|
+
}
|
|
2440
|
+
const rsCrateRoot = (rel) => {
|
|
2441
|
+
const seen = new Set();
|
|
2442
|
+
while (rsChildOf.has(rel) && !seen.has(rel)) { seen.add(rel); rel = rsScopes.get(rsChildOf.get(rel)).rel; }
|
|
2443
|
+
return rel;
|
|
2444
|
+
};
|
|
2445
|
+
const rsParent = (key) => {
|
|
2446
|
+
const sc = rsScopes.get(key);
|
|
2447
|
+
if (!sc) return null;
|
|
2448
|
+
return sc.parent !== null ? sc.parent : rsChildOf.get(sc.rel) || null;
|
|
2449
|
+
};
|
|
2450
|
+
// A name in a module: { mod: key } | { item } | RS_EXT | null. `use`
|
|
2451
|
+
// bindings and glob imports are followed.
|
|
2452
|
+
const rsLookup = (key, name, depth = 0) => {
|
|
2453
|
+
const sc = rsScopes.get(key);
|
|
2454
|
+
if (!sc || depth > 8) return null;
|
|
2455
|
+
if (sc.mods.has(name)) return { mod: sc.mods.get(name) };
|
|
2456
|
+
if (sc.items.has(name)) return { item: sc.items.get(name) };
|
|
2457
|
+
if (sc.uses.has(name)) return rsUse(key, sc.uses.get(name), null, depth + 1);
|
|
2458
|
+
for (const g of sc.globs) {
|
|
2459
|
+
const r = rsResolve(key, g, depth + 1, true);
|
|
2460
|
+
if (!r || !r.mod) continue;
|
|
2461
|
+
const sub = rsScopes.get(r.mod);
|
|
2462
|
+
if (sub && (sub.mods.has(name) || sub.items.has(name) || sub.uses.has(name))) return rsLookup(r.mod, name, depth + 1);
|
|
2463
|
+
}
|
|
2464
|
+
return null;
|
|
2465
|
+
};
|
|
2466
|
+
// A path from a module: additionally { member: [item, name] } for
|
|
2467
|
+
// `Type::name`. Null when unknown, RS_EXT for a path outside the repo.
|
|
2468
|
+
const rsResolve = (key, segs, depth = 0, viaUse = false) => {
|
|
2469
|
+
if (!segs.length || depth > 8) return null;
|
|
2470
|
+
const first = segs[0];
|
|
2471
|
+
let cur, i = 1;
|
|
2472
|
+
if (first === "crate") cur = { mod: rsKey(rsCrateRoot(rsScopes.get(key).rel), "") };
|
|
2473
|
+
else if (first === "self") cur = { mod: key };
|
|
2474
|
+
else if (first === "super") {
|
|
2475
|
+
let k = key;
|
|
2476
|
+
i = 0;
|
|
2477
|
+
while (i < segs.length && segs[i] === "super") { k = rsParent(k); if (k === null) return null; i++; }
|
|
2478
|
+
cur = { mod: k };
|
|
2479
|
+
} else if (first === "std" || first === "core" || first === "alloc") return RS_EXT;
|
|
2480
|
+
else {
|
|
2481
|
+
const sc = rsScopes.get(key);
|
|
2482
|
+
cur = null;
|
|
2483
|
+
if (sc) {
|
|
2484
|
+
if (sc.mods.has(first)) cur = { mod: sc.mods.get(first) };
|
|
2485
|
+
else if (sc.items.has(first)) cur = { item: sc.items.get(first) };
|
|
2486
|
+
else if (sc.uses.has(first)) cur = rsUse(key, sc.uses.get(first), viaUse ? segs : null, depth + 1);
|
|
2487
|
+
}
|
|
2488
|
+
if (!cur && rsCrates.has(first)) cur = { mod: rsKey(rsCrates.get(first), "") };
|
|
2489
|
+
if (!cur && !viaUse) cur = rsLookup(key, first, depth + 1);
|
|
2490
|
+
if (!cur) return null;
|
|
2491
|
+
}
|
|
2492
|
+
for (; i < segs.length; i++) {
|
|
2493
|
+
if (!cur || !cur.mod) {
|
|
2494
|
+
if (cur && cur.item && i === segs.length - 1) return { member: [cur.item, segs[i]] };
|
|
2495
|
+
return cur && cur.ext ? RS_EXT : null;
|
|
2496
|
+
}
|
|
2497
|
+
cur = rsLookup(cur.mod, segs[i], depth + 1);
|
|
2498
|
+
}
|
|
2499
|
+
return cur;
|
|
2500
|
+
};
|
|
2501
|
+
// One `use` name can bind a type, a value and a macro (three namespaces):
|
|
2502
|
+
// the binding that names a module or an item of this repo wins.
|
|
2503
|
+
function rsUse(key, candidates, self, depth) {
|
|
2504
|
+
let fallback = null;
|
|
2505
|
+
for (const segs of candidates) {
|
|
2506
|
+
if (self && segs.join("::") === self.join("::")) continue;
|
|
2507
|
+
const r = rsResolve(key, segs, depth, true);
|
|
2508
|
+
if (r && (r.mod || r.item)) return r;
|
|
2509
|
+
if (r && !fallback) fallback = r;
|
|
2510
|
+
}
|
|
2511
|
+
return fallback;
|
|
2512
|
+
}
|
|
2513
|
+
const RS_OWNER_KINDS = new Set(["struct", "enum", "trait", "type"]);
|
|
2514
|
+
for (const [rel, pf] of perFile) {
|
|
2515
|
+
if (pf.lang !== "rust") continue;
|
|
2516
|
+
const d = pf.decls;
|
|
2517
|
+
for (const impl of d.rsImpls) {
|
|
2518
|
+
const key = rsKey(rel, impl.inl);
|
|
2519
|
+
const r = impl.typeSegs.length === 1 ? rsLookup(key, impl.typeName) : rsResolve(key, impl.typeSegs);
|
|
2520
|
+
let ownerNid = r && r.item && RS_OWNER_KINDS.has(r.item.kind) ? r.item.nid : null;
|
|
2521
|
+
if (!ownerNid) {
|
|
2522
|
+
// an impl for a type this repo does not declare: the block itself stands for it
|
|
2523
|
+
ownerNid = makeId(fileStem(rel), "impl", impl.typeName);
|
|
2524
|
+
addNode({ id: ownerNid, label: impl.typeName, file_type: "code", source_file: rel, source_location: "L" + impl.line, type: "impl" });
|
|
2525
|
+
addEdge(pf.fileNid, ownerNid, "contains", { file: rel, line: impl.line });
|
|
2526
|
+
}
|
|
2527
|
+
impl.ownerNid = ownerNid;
|
|
2528
|
+
let table = rsMethods.get(ownerNid);
|
|
2529
|
+
if (!table) rsMethods.set(ownerNid, table = new Map());
|
|
2530
|
+
for (const m of impl.methods) {
|
|
2531
|
+
m.nid = makeId(fileStem(rel), impl.typeName, m.name);
|
|
2532
|
+
m.ownerNid = ownerNid;
|
|
2533
|
+
addNode({ id: m.nid, label: m.name + "()", file_type: "code", source_file: rel, source_location: "L" + m.line, _callable: true });
|
|
2534
|
+
addEdge(ownerNid, m.nid, "method", { file: rel, line: m.line });
|
|
2535
|
+
if (!table.has(m.name)) table.set(m.name, []);
|
|
2536
|
+
if (!table.get(m.name).includes(m.nid)) table.get(m.name).push(m.nid);
|
|
2537
|
+
}
|
|
2538
|
+
if (impl.traitSegs && !impl.isTrait) {
|
|
2539
|
+
const tr = rsResolve(key, impl.traitSegs);
|
|
2540
|
+
if (tr && tr.item && tr.item.kind === "trait" && tr.item.nid && tr.item.nid !== ownerNid) addEdge(ownerNid, tr.item.nid, "implements", { file: rel, line: impl.line });
|
|
2541
|
+
}
|
|
2542
|
+
}
|
|
2543
|
+
for (const f of d.functions) f.nid = pf.localDefs.get(f.name);
|
|
2544
|
+
for (const nf of d.rsNested) {
|
|
2545
|
+
const parent = nf.parent.nid;
|
|
2546
|
+
if (!parent) continue;
|
|
2547
|
+
const nid = makeId(parent, nf.name);
|
|
2548
|
+
if (nodeById.has(nid)) continue;
|
|
2549
|
+
if (nf.kind === "fn") addNode({ id: nid, label: nf.name + "()", file_type: "code", source_file: rel, source_location: "L" + nf.line, _callable: true });
|
|
2550
|
+
else addNode({ id: nid, label: nf.name, file_type: "code", source_file: rel, source_location: "L" + nf.line, type: nf.kind === "enum" ? "enum" : "class" });
|
|
2551
|
+
addEdge(parent, nid, "contains", { file: rel, line: nf.line });
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
// `use` paths: the file that declares the module or item, and the item itself.
|
|
2555
|
+
const rustImports = (rel, pf) => {
|
|
2556
|
+
const seen = new Set();
|
|
2557
|
+
const once = (target, relation, line) => {
|
|
2558
|
+
const k = target + "|" + relation;
|
|
2559
|
+
if (seen.has(k) || target === pf.fileNid) return;
|
|
2560
|
+
seen.add(k);
|
|
2561
|
+
addEdge(pf.fileNid, target, relation, { file: rel, line, context: "import" });
|
|
2562
|
+
};
|
|
2563
|
+
for (const imp of pf.decls.imports) {
|
|
2564
|
+
const r = rsResolve(rsKey(rel, imp.rsInl), imp.rsSegs, 0, true);
|
|
2565
|
+
if (r && r.mod) {
|
|
2566
|
+
const target = rsScopes.get(r.mod);
|
|
2567
|
+
if (target && target.rel !== rel) once(perFile.get(target.rel).fileNid, "imports_from", imp.line);
|
|
2568
|
+
} else if (r && r.item) {
|
|
2569
|
+
if (r.item.rel === rel) continue;
|
|
2570
|
+
once(perFile.get(r.item.rel).fileNid, "imports_from", imp.line);
|
|
2571
|
+
if (r.item.nid) once(r.item.nid, "imports", imp.line);
|
|
2572
|
+
} else if (r && r.member) {
|
|
2573
|
+
if (r.member[0].rel !== rel) once(perFile.get(r.member[0].rel).fileNid, "imports_from", imp.line);
|
|
2574
|
+
} else {
|
|
2575
|
+
const head = imp.rsSegs[0];
|
|
2576
|
+
if (!head || head === "crate" || head === "self" || head === "super" || rsCrates.has(head)) continue;
|
|
2577
|
+
let nid = makeId(head);
|
|
2578
|
+
const taken = nodeById.get(nid);
|
|
2579
|
+
if (taken && taken.source_file) nid = makeId("external", head);
|
|
2580
|
+
if (!nid) continue;
|
|
2581
|
+
addNode({ id: nid, label: head, file_type: "code", source_file: "", source_location: "", type: "module" });
|
|
2582
|
+
once(nid, "imports_from", imp.line);
|
|
2583
|
+
}
|
|
2584
|
+
}
|
|
2585
|
+
};
|
|
2586
|
+
const RUST_CALL_SKIP = new Set([
|
|
2587
|
+
"if", "while", "match", "for", "loop", "return", "fn", "let", "in", "as", "move", "async", "await", "unsafe", "where", "impl", "dyn",
|
|
2588
|
+
"ref", "mut", "self", "super", "crate", "Some", "Ok", "Err", "None", "Box", "Vec", "String", "drop", "else", "break", "continue", "pub", "use", "mod",
|
|
2589
|
+
]);
|
|
2590
|
+
const rsPathCallRe = /(?<![\w.:!])((?:[A-Za-z_]\w*\s*::\s*)*[A-Za-z_]\w*)\s*(?:::\s*<[^<>()]*>\s*)?\(/g;
|
|
2591
|
+
const rsSelfCallRe = /\bself\s*\.\s*([A-Za-z_]\w*)\s*(?:::\s*<[^<>()]*>\s*)?\(/g;
|
|
2592
|
+
// `Name(…)` that destructures (after `let`, before `=>` or a single `=`,
|
|
2593
|
+
// in closure parameters) instead of constructing.
|
|
2594
|
+
const rsIsPattern = (segment, at, open) => {
|
|
2595
|
+
if (/(?:\blet\s+(?:mut\s+)?|\|\s*)$/.test(segment.slice(Math.max(0, at - 24), at))) return true;
|
|
2596
|
+
let k = matchClose(segment, open);
|
|
2597
|
+
if (k < 0) return false;
|
|
2598
|
+
for (k++; k < segment.length; k++) {
|
|
2599
|
+
const ch = segment[k];
|
|
2600
|
+
if (ch === ")" || ch === " " || ch === "\t" || ch === "\n" || ch === "\r") continue;
|
|
2601
|
+
if (ch === "=") return segment[k + 1] !== "=";
|
|
2602
|
+
if (ch === "|") return segment[k + 1] !== "|";
|
|
2603
|
+
return ch === "i" && /^if\b/.test(segment.slice(k, k + 3)) ? true : ch === ":" && segment[k + 1] !== ":";
|
|
2604
|
+
}
|
|
2605
|
+
return false;
|
|
2606
|
+
};
|
|
2607
|
+
const rsUniqueMethod = (ownerNid, name) => {
|
|
2608
|
+
const list = ownerNid && rsMethods.get(ownerNid) && rsMethods.get(ownerNid).get(name);
|
|
2609
|
+
return list && list.length === 1 ? list[0] : null;
|
|
2610
|
+
};
|
|
2611
|
+
// Calls whose target the source names outright: a function in scope, a
|
|
2612
|
+
// path (`module::f`, `Type::f`), `Self::f` and `self.f()` on a method the
|
|
2613
|
+
// type declares once. A receiver that needs type inference is left alone.
|
|
2614
|
+
const rustCalls = (rel, pf) => {
|
|
2615
|
+
const text = pf.stripped;
|
|
2616
|
+
const seen = new Set();
|
|
2617
|
+
const emit = (caller, target, offset, relation) => {
|
|
2618
|
+
if (!target || target === caller) return;
|
|
2619
|
+
const k = caller + "|" + target + "|" + relation;
|
|
2620
|
+
if (seen.has(k)) return;
|
|
2621
|
+
seen.add(k);
|
|
2622
|
+
addEdge(caller, target, relation, { file: rel, line: pf.lineOf(offset), context: "call" });
|
|
2623
|
+
};
|
|
2624
|
+
const scan = (segment, base, caller, key, ownerNid, locals) => {
|
|
2625
|
+
let m;
|
|
2626
|
+
rsPathCallRe.lastIndex = 0;
|
|
2627
|
+
while ((m = rsPathCallRe.exec(segment)) !== null) {
|
|
2628
|
+
const segs = m[1].split("::").map((s) => s.trim());
|
|
2629
|
+
const name = segs[segs.length - 1];
|
|
2630
|
+
if (segs.length === 1) {
|
|
2631
|
+
if (RUST_CALL_SKIP.has(name) || locals.has(name)) continue;
|
|
2632
|
+
const r = rsLookup(key, name);
|
|
2633
|
+
if (!r || !r.item || !r.item.nid) continue;
|
|
2634
|
+
if (r.item.kind === "fn") emit(caller, r.item.nid, base + m.index, "calls");
|
|
2635
|
+
else if (r.item.kind === "struct" && !rsIsPattern(segment, m.index, m.index + m[0].length - 1)) emit(caller, r.item.nid, base + m.index, "instantiates");
|
|
2636
|
+
} else if (segs[0] === "Self") {
|
|
2637
|
+
if (segs.length === 2) emit(caller, rsUniqueMethod(ownerNid, name), base + m.index, "calls");
|
|
2638
|
+
} else {
|
|
2639
|
+
const r = rsResolve(key, segs);
|
|
2640
|
+
if (!r) continue;
|
|
2641
|
+
if (r.item && r.item.nid) {
|
|
2642
|
+
if (r.item.kind === "fn") emit(caller, r.item.nid, base + m.index, "calls");
|
|
2643
|
+
else if (r.item.kind === "struct" && !rsIsPattern(segment, m.index, m.index + m[0].length - 1)) emit(caller, r.item.nid, base + m.index, "instantiates");
|
|
2644
|
+
} else if (r.member && r.member[0].nid) emit(caller, rsUniqueMethod(r.member[0].nid, r.member[1]), base + m.index, "calls");
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
if (!ownerNid) return;
|
|
2648
|
+
rsSelfCallRe.lastIndex = 0;
|
|
2649
|
+
while ((m = rsSelfCallRe.exec(segment)) !== null) emit(caller, rsUniqueMethod(ownerNid, m[1]), base + m.index, "calls");
|
|
2650
|
+
};
|
|
2651
|
+
const taken = [];
|
|
2652
|
+
for (const fn of pf.decls.rsFns) {
|
|
2653
|
+
const eol = text.indexOf("\n", fn.declStart);
|
|
2654
|
+
taken.push([fn.declStart, fn.bodyEnd > 0 ? fn.bodyEnd + 1 : eol < 0 ? text.length : eol]);
|
|
2655
|
+
if (fn.bodyStart < 0 || !fn.nid) continue;
|
|
2656
|
+
const locals = new Set();
|
|
2657
|
+
for (const m of text.slice(fn.declStart, fn.bodyStart).matchAll(/([A-Za-z_]\w*)\s*:/g)) locals.add(m[1]);
|
|
2658
|
+
const body = text.slice(fn.bodyStart + 1, fn.bodyEnd);
|
|
2659
|
+
for (const m of body.matchAll(/\blet\s+([^=;]*)/g)) for (const id of m[1].split(":")[0].matchAll(/[a-z_]\w*/g)) locals.add(id[0]);
|
|
2660
|
+
for (const m of body.matchAll(/\|([^|\n]*)\|/g)) for (const id of m[1].matchAll(/([a-z_]\w*)\s*(?=[,:|]|$)/g)) locals.add(id[1]);
|
|
2661
|
+
for (const m of body.matchAll(/\bfor\s+([^{]*?)\s+in\b/g)) for (const id of m[1].matchAll(/[a-z_]\w*/g)) locals.add(id[0]);
|
|
2662
|
+
scan(body, fn.bodyStart + 1, fn.nid, rsKey(rel, fn.inl), fn.ownerNid || null, locals);
|
|
2663
|
+
}
|
|
2664
|
+
scan(blankRanges(text, taken.concat(pf.decls.rsSkip)), 0, pf.fileNid, rsKey(rel, ""), null, new Set());
|
|
2665
|
+
};
|
|
2666
|
+
|
|
2667
|
+
// -- workspace package resolution (js/ts monorepos): each package's root
|
|
2668
|
+
// entry, plus its directory and `exports` map for subpath imports
|
|
2669
|
+
const rels = [...perFile.keys()];
|
|
2670
|
+
const relSet = new Set(rels);
|
|
2671
|
+
const workspaceEntry = new Map();
|
|
2672
|
+
const workspacePkg = new Map(); // package name -> { dir, exports: subpath map | null }
|
|
2673
|
+
|
|
2674
|
+
// The file a module path names: as written, with a `.js`-family extension
|
|
2675
|
+
// swapped for a source one, or a directory's index.
|
|
2676
|
+
const probeModule = (base) => {
|
|
2677
|
+
const stems = [base];
|
|
2678
|
+
if (/\.(js|mjs|cjs|jsx)$/.test(base)) stems.push(base.replace(/\.(js|mjs|cjs|jsx)$/, ""));
|
|
2679
|
+
for (const stem of stems) {
|
|
2680
|
+
for (const e of ["", ".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".mts", ".cts", ".py", ".go", ".rs"]) {
|
|
2681
|
+
if (relSet.has(stem + e)) return stem + e;
|
|
2682
|
+
}
|
|
2683
|
+
for (const e of [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs"]) {
|
|
2684
|
+
if (relSet.has(path.posix.join(stem, "index" + e))) return path.posix.join(stem, "index" + e);
|
|
2685
|
+
}
|
|
2686
|
+
}
|
|
2687
|
+
return null;
|
|
2688
|
+
};
|
|
2689
|
+
const probeTarget = (dir, target) => {
|
|
2690
|
+
const hit = probeModule(path.posix.normalize(path.posix.join(dir, target)));
|
|
2691
|
+
if (hit) return hit;
|
|
2692
|
+
const source = distToSrc(target);
|
|
2693
|
+
return source !== target ? probeModule(path.posix.normalize(path.posix.join(dir, source))) : null;
|
|
2694
|
+
};
|
|
2695
|
+
|
|
2696
|
+
{
|
|
2697
|
+
const pkgDirs = new Set([""]);
|
|
2698
|
+
for (const rel of rels) {
|
|
2699
|
+
const parts = rel.split("/");
|
|
2700
|
+
for (let i = 1; i < parts.length; i++) pkgDirs.add(parts.slice(0, i).join("/"));
|
|
2701
|
+
}
|
|
2702
|
+
for (const dir of [...pkgDirs].sort()) {
|
|
2703
|
+
const pjPath = path.join(root, dir, "package.json");
|
|
2704
|
+
try {
|
|
2705
|
+
const pj = JSON.parse(fs.readFileSync(pjPath, "utf-8"));
|
|
2706
|
+
if (!pj.name) continue;
|
|
2707
|
+
const exportMap = subpathExports(pj.exports);
|
|
2708
|
+
if (!workspacePkg.has(pj.name)) workspacePkg.set(pj.name, { dir, exports: exportMap });
|
|
2709
|
+
if (workspaceEntry.has(pj.name)) continue;
|
|
2710
|
+
// The root import comes from `exports["."]` alone — another subpath's
|
|
2711
|
+
// target (`./extension`) is not the package's entry — then the legacy
|
|
2712
|
+
// fields, then a conventional index. Build output (`dist/`) is mapped
|
|
2713
|
+
// back to the source it was compiled from (probeTarget).
|
|
2714
|
+
const rootTargets = [];
|
|
2715
|
+
if (exportMap && Object.hasOwn(exportMap, ".")) rootExportTargets(exportMap["."], rootTargets);
|
|
2716
|
+
for (const f of [pj.module, pj.main, pj.types, pj.typings]) if (typeof f === "string") rootTargets.push(f);
|
|
2717
|
+
rootTargets.push("src/index.ts", "src/index.tsx", "src/index.js", "index.ts", "index.js");
|
|
2718
|
+
for (const t of rootTargets) {
|
|
2719
|
+
const hit = probeTarget(dir, t.replace(/^\.\//, ""));
|
|
2720
|
+
if (hit) { workspaceEntry.set(pj.name, hit); break; }
|
|
2721
|
+
}
|
|
2722
|
+
} catch { /* empty */ }
|
|
2723
|
+
}
|
|
2724
|
+
}
|
|
2725
|
+
|
|
2726
|
+
// A workspace subpath import (`@scope/pkg/testing`) resolves through the
|
|
2727
|
+
// package's `exports`: the exact key, else the `*` pattern with the
|
|
2728
|
+
// longest prefix (Node's rule), its targets tried in condition order. Never
|
|
2729
|
+
// the package root — the root does not export the subpath's names, so its
|
|
2730
|
+
// symbol imports and calls would be lost. A subpath the map does not
|
|
2731
|
+
// export is unresolved (Node refuses it); without an `exports` field the
|
|
2732
|
+
// subpath is a path inside the package.
|
|
2733
|
+
const resolveWorkspaceSubpath = (pkg, sub) => {
|
|
2734
|
+
if (!pkg.exports) return probeTarget(pkg.dir, sub);
|
|
2735
|
+
let key = Object.hasOwn(pkg.exports, sub) ? sub : null;
|
|
2736
|
+
let star = null;
|
|
2737
|
+
if (key === null) {
|
|
2738
|
+
let prefixLen = -1;
|
|
2739
|
+
for (const k of Object.keys(pkg.exports)) {
|
|
2740
|
+
const at = k.indexOf("*");
|
|
2741
|
+
if (at < 0 || at < prefixLen || (at === prefixLen && k.length <= key.length)) continue;
|
|
2742
|
+
const prefix = k.slice(0, at);
|
|
2743
|
+
const suffix = k.slice(at + 1);
|
|
2744
|
+
if (sub.length < k.length || !sub.startsWith(prefix) || !sub.endsWith(suffix)) continue;
|
|
2745
|
+
key = k;
|
|
2746
|
+
prefixLen = at;
|
|
2747
|
+
star = sub.slice(prefix.length, sub.length - suffix.length);
|
|
2748
|
+
}
|
|
2749
|
+
if (key === null) return null;
|
|
2750
|
+
}
|
|
2751
|
+
for (const target of exportTargets(pkg.exports[key])) {
|
|
2752
|
+
const hit = probeTarget(pkg.dir, star === null ? target : target.split("*").join(star));
|
|
2753
|
+
if (hit) return hit;
|
|
2754
|
+
}
|
|
2755
|
+
return null;
|
|
2756
|
+
};
|
|
2757
|
+
|
|
2758
|
+
// Python module path → file: `pkg/__init__.py` (a package wins over a
|
|
2759
|
+
// same-named module, as in the import system) or `pkg/mod.py`.
|
|
2760
|
+
const probePyModule = (base) => {
|
|
2761
|
+
for (const c of [path.posix.join(base, "__init__.py"), base + ".py"]) if (relSet.has(c)) return c;
|
|
2762
|
+
return null;
|
|
2763
|
+
};
|
|
2764
|
+
// Dots count package levels from the importing file's own package: `.` is
|
|
2765
|
+
// its directory, `..` the parent. Absolute dotted paths start at the repo root.
|
|
2766
|
+
// Import roots: the repo root and every directory that holds a top-level
|
|
2767
|
+
// package (a `src/` layout, an example project). The importing file's own
|
|
2768
|
+
// root is tried first.
|
|
2769
|
+
const pyOwnRoot = (rel) => {
|
|
2770
|
+
const dir = path.posix.dirname(rel);
|
|
2771
|
+
let d = dir;
|
|
2772
|
+
while (d !== "." && relSet.has(d + "/__init__.py")) d = path.posix.dirname(d);
|
|
2773
|
+
return d === "." ? "" : d;
|
|
2774
|
+
};
|
|
2775
|
+
const pyRoots = [...new Set([""].concat(rels.filter((r) => r.endsWith(".py")).map(pyOwnRoot)))].sort();
|
|
2776
|
+
const probePyAbsolute = (fromRel, mod) => {
|
|
2777
|
+
// the file's own root, then each directory above it (a namespace package
|
|
2778
|
+
// has no __init__.py to mark where it starts), then the other roots
|
|
2779
|
+
const own = pyOwnRoot(fromRel);
|
|
2780
|
+
const order = [own];
|
|
2781
|
+
for (let d = own ? path.posix.dirname(own) : "."; d !== "."; d = path.posix.dirname(d)) order.push(d);
|
|
2782
|
+
order.push(...pyRoots);
|
|
2783
|
+
for (const r of new Set(order)) {
|
|
2784
|
+
const hit = probePyModule(r ? r + "/" + mod : mod);
|
|
2785
|
+
if (hit) return hit;
|
|
2786
|
+
}
|
|
2787
|
+
return null;
|
|
2788
|
+
};
|
|
2789
|
+
const resolvePySpec = (fromRel, spec) => {
|
|
2790
|
+
const dots = spec.match(/^\.*/)[0].length;
|
|
2791
|
+
const rest = spec.slice(dots);
|
|
2792
|
+
if (!/^[\w.]*$/.test(rest)) return null;
|
|
2793
|
+
const mod = rest.replace(/\./g, "/");
|
|
2794
|
+
if (!dots) return mod ? probePyAbsolute(fromRel, mod) : null;
|
|
2795
|
+
let dir = path.posix.dirname(fromRel);
|
|
2796
|
+
for (let k = 1; k < dots; k++) dir = path.posix.dirname(dir);
|
|
2797
|
+
if (mod) return probePyModule(path.posix.join(dir, mod));
|
|
2798
|
+
const init = path.posix.join(dir, "__init__.py"); // `from . import x`: the package itself
|
|
2799
|
+
return relSet.has(init) ? init : null;
|
|
2800
|
+
};
|
|
2801
|
+
|
|
2802
|
+
const specMemo = new Map();
|
|
2803
|
+
const resolveSpecUncached = (fromRel, spec) => {
|
|
2804
|
+
if (perFile.get(fromRel).lang === "python") return resolvePySpec(fromRel, spec);
|
|
2805
|
+
if (spec.startsWith(".")) return probeModule(path.posix.normalize(path.posix.join(path.posix.dirname(fromRel), spec)));
|
|
2806
|
+
if (workspaceEntry.has(spec)) return workspaceEntry.get(spec);
|
|
2807
|
+
const slash = spec.indexOf("/", spec.startsWith("@") ? spec.indexOf("/") + 1 : 0);
|
|
2808
|
+
if (slash > 0 && workspacePkg.has(spec.slice(0, slash))) return resolveWorkspaceSubpath(workspacePkg.get(spec.slice(0, slash)), "." + spec.slice(slash));
|
|
2809
|
+
return null;
|
|
2810
|
+
};
|
|
2811
|
+
const resolveSpec = (fromRel, spec) => {
|
|
2812
|
+
const key = fromRel + "\n" + spec;
|
|
2813
|
+
if (!specMemo.has(key)) specMemo.set(key, resolveSpecUncached(fromRel, spec));
|
|
2814
|
+
return specMemo.get(key);
|
|
2815
|
+
};
|
|
2816
|
+
|
|
2817
|
+
// A module's exported names → defining symbol node, following re-export
|
|
2818
|
+
// bindings: named (`export { helper } from`, aliases, `default as X`) and
|
|
2819
|
+
// star (`export * from`, which never carries `default`). `export * as ns`
|
|
2820
|
+
// exposes only `ns`, no inner name. A barrel has no localDef for
|
|
2821
|
+
// pass-through names, so consumers importing through it would otherwise
|
|
2822
|
+
// lose their symbol-import and call edges. Built once per module (a
|
|
2823
|
+
// 1500-entry barrel costs one pass, not one per importer), with a cycle
|
|
2824
|
+
// guard: a map computed while a cycle cut part of it away is incomplete
|
|
2825
|
+
// for every module on that cycle except the one the cut points at, so only
|
|
2826
|
+
// the latter is kept.
|
|
2827
|
+
const reExportsOf = new Map();
|
|
2828
|
+
const reExportList = (rel) => {
|
|
2829
|
+
let list = reExportsOf.get(rel);
|
|
2830
|
+
if (!list) {
|
|
2831
|
+
list = [];
|
|
2832
|
+
for (const imp of perFile.get(rel).decls.imports) {
|
|
2833
|
+
if (!imp.reExport) continue;
|
|
2834
|
+
const target = resolveSpec(rel, imp.spec);
|
|
2835
|
+
if (target) list.push({ target, star: imp.namespace === "*", names: imp.names });
|
|
2836
|
+
}
|
|
2837
|
+
reExportsOf.set(rel, list);
|
|
2838
|
+
}
|
|
2839
|
+
return list;
|
|
2840
|
+
};
|
|
2841
|
+
const NO_EXPORTS = new Map();
|
|
2842
|
+
const exportMaps = new Map();
|
|
2843
|
+
const exportsInProgress = new Map(); // rel -> depth on the resolution stack
|
|
2844
|
+
let cutDepth = Infinity; // shallowest in-progress module a cycle cut pointed at
|
|
2845
|
+
const exportsOf = (rel) => {
|
|
2846
|
+
const done = exportMaps.get(rel);
|
|
2847
|
+
if (done) return done;
|
|
2848
|
+
const pf = perFile.get(rel);
|
|
2849
|
+
if (!pf) return NO_EXPORTS;
|
|
2850
|
+
const depth = exportsInProgress.get(rel);
|
|
2851
|
+
if (depth !== undefined) { cutDepth = Math.min(cutDepth, depth); return NO_EXPORTS; }
|
|
2852
|
+
const myDepth = exportsInProgress.size;
|
|
2853
|
+
exportsInProgress.set(rel, myDepth);
|
|
2854
|
+
const outerCut = cutDepth;
|
|
2855
|
+
cutDepth = Infinity;
|
|
2856
|
+
const map = new Map(pf.localDefs);
|
|
2857
|
+
if (pf.defaultNid) map.set("default", pf.defaultNid);
|
|
2858
|
+
// `export { a as b }`: a local declaration, or a name this module imported
|
|
2859
|
+
for (const le of pf.decls.localExports || []) {
|
|
2860
|
+
if (map.has(le.exported) && le.exported === le.local) continue;
|
|
2861
|
+
let nid = pf.localDefs.get(le.local) || null;
|
|
2862
|
+
if (!nid) {
|
|
2863
|
+
for (const imp of pf.decls.imports) {
|
|
2864
|
+
if (imp.reExport || imp.dynamic) continue;
|
|
2865
|
+
const target = resolveSpec(rel, imp.spec);
|
|
2866
|
+
if (!target) continue;
|
|
2867
|
+
if (imp.default === le.local) nid = perFile.get(target).defaultNid || exportsOf(target).get("default") || null;
|
|
2868
|
+
const named = imp.names.find((x) => x.local === le.local);
|
|
2869
|
+
if (named) nid = exportsOf(target).get(named.imported) || null;
|
|
2870
|
+
if (nid) break;
|
|
2871
|
+
}
|
|
2872
|
+
}
|
|
2873
|
+
if (nid) map.set(le.exported, nid);
|
|
2874
|
+
}
|
|
2875
|
+
for (const re of reExportList(rel)) {
|
|
2876
|
+
const target = exportsOf(re.target);
|
|
2877
|
+
if (re.star) {
|
|
2878
|
+
for (const [name, nid] of target) if (name !== "default" && !map.has(name)) map.set(name, nid);
|
|
2879
|
+
} else {
|
|
2880
|
+
for (const n of re.names) {
|
|
2881
|
+
const nid = !map.has(n.local) && target.get(n.imported);
|
|
2882
|
+
if (nid) map.set(n.local, nid);
|
|
2883
|
+
}
|
|
2884
|
+
}
|
|
2885
|
+
}
|
|
2886
|
+
exportsInProgress.delete(rel);
|
|
2887
|
+
if (cutDepth >= myDepth) {
|
|
2888
|
+
exportMaps.set(rel, map);
|
|
2889
|
+
cutDepth = outerCut;
|
|
2890
|
+
} else cutDepth = Math.min(outerCut, cutDepth);
|
|
2891
|
+
return map;
|
|
2892
|
+
};
|
|
2893
|
+
// A Python module exports every name it imports, and Python packages
|
|
2894
|
+
// import each other in cycles as a matter of course, so a name is followed
|
|
2895
|
+
// on its own (memoized per module and name) instead of through whole
|
|
2896
|
+
// export maps: local declaration, else the import that binds it, else a
|
|
2897
|
+
// star import.
|
|
2898
|
+
const pyExported = new Map();
|
|
2899
|
+
const resolvePyExported = (rel, name, depth = 0) => {
|
|
2900
|
+
const key = rel + "\n" + name;
|
|
2901
|
+
if (pyExported.has(key)) return pyExported.get(key);
|
|
2902
|
+
const pf = perFile.get(rel);
|
|
2903
|
+
if (!pf || depth > 12) return null;
|
|
2904
|
+
pyExported.set(key, null); // cycle guard
|
|
2905
|
+
let nid = pf.localDefs.get(name) || null;
|
|
2906
|
+
if (!nid) {
|
|
2907
|
+
for (const imp of pf.decls.imports) {
|
|
2908
|
+
const bound = imp.names.find((x) => x.local === name);
|
|
2909
|
+
if (!bound) continue;
|
|
2910
|
+
const target = resolveSpec(rel, imp.spec);
|
|
2911
|
+
if (target) nid = resolvePyExported(target, bound.imported, depth + 1);
|
|
2912
|
+
if (nid) break;
|
|
2913
|
+
}
|
|
2914
|
+
}
|
|
2915
|
+
if (!nid) {
|
|
2916
|
+
for (const imp of pf.decls.imports) {
|
|
2917
|
+
if (!imp.names.some((x) => x.imported === "*")) continue;
|
|
2918
|
+
const target = resolveSpec(rel, imp.spec);
|
|
2919
|
+
if (target) nid = resolvePyExported(target, name, depth + 1);
|
|
2920
|
+
if (nid) break;
|
|
2921
|
+
}
|
|
2922
|
+
}
|
|
2923
|
+
pyExported.set(key, nid);
|
|
2924
|
+
return nid;
|
|
2925
|
+
};
|
|
2926
|
+
const resolveExported = (rel, name) => {
|
|
2927
|
+
const pf = perFile.get(rel);
|
|
2928
|
+
if (pf && pf.lang === "python") return resolvePyExported(rel, name);
|
|
2929
|
+
return exportsOf(rel).get(name) || null;
|
|
2930
|
+
};
|
|
2931
|
+
// The symbol a module's default import binds to: its own default
|
|
2932
|
+
// declaration, else what it re-exports as `default`.
|
|
2933
|
+
const defaultOf = (rel) => {
|
|
2934
|
+
const pf = perFile.get(rel);
|
|
2935
|
+
return pf ? pf.defaultNid || resolveExported(rel, "default") : null;
|
|
2936
|
+
};
|
|
2937
|
+
|
|
2938
|
+
// The def (function or method) whose body holds 1-based `line`, innermost.
|
|
2939
|
+
const pyDefRegion = (pf, line) => {
|
|
2940
|
+
let best = null;
|
|
2941
|
+
const consider = (f) => {
|
|
2942
|
+
if (f.line < line && f.end != null && line <= f.end && (!best || f.line > best.start)) best = { start: f.line, end: f.end };
|
|
2943
|
+
};
|
|
2944
|
+
for (const f of pf.decls.functions) consider(f);
|
|
2945
|
+
for (const c of pf.decls.classes) for (const m of c.methods) consider(m);
|
|
2946
|
+
return best;
|
|
2947
|
+
};
|
|
2948
|
+
// What `name` was imported as, seen from `line`: a def's own imports first.
|
|
2949
|
+
const importedAt = (pf, name, line) => {
|
|
2950
|
+
if (line && pf.scopedImports) {
|
|
2951
|
+
let hit = null, hitStart = -1;
|
|
2952
|
+
for (const sc of pf.scopedImports) {
|
|
2953
|
+
if (line >= sc.start && line <= sc.end && sc.start >= hitStart && sc.map.has(name)) { hit = sc.map.get(name); hitStart = sc.start; }
|
|
2954
|
+
}
|
|
2955
|
+
if (hit) return hit;
|
|
2956
|
+
}
|
|
2957
|
+
return pf.importedNames.get(name);
|
|
2958
|
+
};
|
|
2959
|
+
// -- phase 3: import edges
|
|
2960
|
+
// Directory names and module stems the repo itself provides: a top-level
|
|
2961
|
+
// package the repo defines (a `src/` layout the root-relative resolver
|
|
2962
|
+
// misses) is not proof of an external import.
|
|
2963
|
+
const repoNames = new Set();
|
|
2964
|
+
for (const rel of rels) for (const part of rel.split("/")) repoNames.add(part.replace(/\.[^.]+$/, ""));
|
|
2965
|
+
for (const rel of rels) {
|
|
2966
|
+
const pf = perFile.get(rel);
|
|
2967
|
+
if (pf.lang === "rust") { rustImports(rel, pf); continue; }
|
|
2968
|
+
for (const imp of pf.decls.imports) {
|
|
2969
|
+
if (pf.lang === "go" && goImportEdges(rel, pf, imp)) continue;
|
|
2970
|
+
let bind = pf.importedNames;
|
|
2971
|
+
if (pf.lang === "python") {
|
|
2972
|
+
const region = pyDefRegion(pf, imp.line);
|
|
2973
|
+
if (region) {
|
|
2974
|
+
bind = new Map();
|
|
2975
|
+
(pf.scopedImports ||= []).push({ start: region.start, end: region.end, map: bind });
|
|
2976
|
+
}
|
|
2977
|
+
}
|
|
2978
|
+
const resolved = resolveSpec(rel, imp.spec);
|
|
2979
|
+
let targetFileNid;
|
|
2980
|
+
if (resolved) {
|
|
2981
|
+
targetFileNid = perFile.get(resolved).fileNid;
|
|
2982
|
+
} else {
|
|
2983
|
+
// external module node (concept-style: no source_file, like graphify module nodes)
|
|
2984
|
+
targetFileNid = makeId(imp.spec);
|
|
2985
|
+
if (!targetFileNid) continue;
|
|
2986
|
+
// `models` (or `.models`) must not merge with the repo's own models.py.
|
|
2987
|
+
const taken = nodeById.get(targetFileNid);
|
|
2988
|
+
if (taken && taken.source_file) targetFileNid = makeId("external", imp.spec);
|
|
2989
|
+
addNode({ id: targetFileNid, label: imp.spec, file_type: "code", source_file: "", source_location: "", type: "module" });
|
|
2990
|
+
// Names imported from a package outside the repo stay with that
|
|
2991
|
+
// import: they never fall back to a same-named repo symbol.
|
|
2992
|
+
if (!imp.spec.startsWith(".") && !imp.dynamic && !repoNames.has(imp.spec.split(/[./]/)[0])) {
|
|
2993
|
+
for (const n of imp.names) bind.set(n.local, { rel: null, name: n.imported, nid: null, external: true });
|
|
2994
|
+
if (imp.default) bind.set(imp.default, { rel: null, name: "default", nid: null, external: true });
|
|
2995
|
+
if (imp.namespace && imp.namespace !== "*" && !imp.reExport) bind.set(imp.namespace, { rel: null, name: "*", nid: null, external: true });
|
|
2996
|
+
}
|
|
2997
|
+
}
|
|
2998
|
+
const relation = imp.dynamic ? "dynamic_import" : imp.reExport ? "re_exports" : "imports_from";
|
|
2999
|
+
// Python: `from pkg import mod` imports the submodule, `from m import *`
|
|
3000
|
+
// brings every name of m into scope.
|
|
3001
|
+
let names = imp.names;
|
|
3002
|
+
if (pf.lang === "python" && resolved && names.length) {
|
|
3003
|
+
const rest = [];
|
|
3004
|
+
for (const n of names) {
|
|
3005
|
+
if (n.imported === "*") { (pf.starImports ||= []).push(resolved); continue; }
|
|
3006
|
+
const sub = resolved.endsWith("/__init__.py") || resolved === "__init__.py" ? probePyModule(path.posix.join(path.posix.dirname(resolved), n.imported)) : null;
|
|
3007
|
+
if (sub && !perFile.get(resolved).localDefs.has(n.imported)) {
|
|
3008
|
+
addEdge(pf.fileNid, perFile.get(sub).fileNid, relation, { file: rel, line: imp.line, context: "import" });
|
|
3009
|
+
bind.set(n.local, { rel: sub, name: "*", nid: null });
|
|
3010
|
+
} else rest.push(n);
|
|
3011
|
+
}
|
|
3012
|
+
if (!rest.length && names.some((n) => n.imported !== "*")) continue;
|
|
3013
|
+
names = rest;
|
|
3014
|
+
}
|
|
3015
|
+
addEdge(pf.fileNid, targetFileNid, relation, { file: rel, line: imp.line, context: "import", deferred: imp.dynamic || undefined });
|
|
3016
|
+
// symbol-level imports edges (only when target resolves in-repo)
|
|
3017
|
+
if (resolved && !imp.dynamic) {
|
|
3018
|
+
const tpf = perFile.get(resolved);
|
|
3019
|
+
for (const n of names) {
|
|
3020
|
+
const tnid = tpf.localDefs.get(n.imported) || resolveExported(resolved, n.imported);
|
|
3021
|
+
if (tnid) {
|
|
3022
|
+
if (!imp.reExport) addEdge(pf.fileNid, tnid, "imports", { file: rel, line: imp.line, context: "import" });
|
|
3023
|
+
bind.set(n.local, { rel: resolved, name: n.imported, nid: tnid });
|
|
3024
|
+
} else {
|
|
3025
|
+
bind.set(n.local, { rel: resolved, name: n.imported, nid: null });
|
|
3026
|
+
}
|
|
3027
|
+
}
|
|
3028
|
+
if (imp.default) bind.set(imp.default, { rel: resolved, name: "default", nid: defaultOf(resolved) });
|
|
3029
|
+
// `export * as ns from` binds `ns` for importers of this module, not here.
|
|
3030
|
+
if (imp.namespace && imp.namespace !== "*" && !imp.reExport) bind.set(imp.namespace, { rel: resolved, name: "*", nid: null, cjs: imp.cjs || undefined });
|
|
3031
|
+
}
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
|
|
3035
|
+
// -- phase 4: inherits / implements / extends
|
|
3036
|
+
const resolveName = (pf, name, line) => {
|
|
3037
|
+
// local → import evidence → a star import → null. A name nothing in the
|
|
3038
|
+
// file accounts for is not guessed from the rest of the repo.
|
|
3039
|
+
const scoped = line && pf.scopedImports ? importedAt(pf, name, line) : null;
|
|
3040
|
+
if (pf.localDefs.has(name) && (!scoped || scoped === pf.importedNames.get(name))) return { nid: pf.localDefs.get(name), confidence: "EXTRACTED" };
|
|
3041
|
+
const imp = scoped || pf.importedNames.get(name);
|
|
3042
|
+
if (imp && imp.nid) return { nid: imp.nid, confidence: "EXTRACTED" };
|
|
3043
|
+
if (imp && imp.external) return null;
|
|
3044
|
+
if (imp && imp.rel) {
|
|
3045
|
+
const tpf = perFile.get(imp.rel);
|
|
3046
|
+
// A default import binds to the TARGET's default declaration,
|
|
3047
|
+
// whatever local name the importer chose.
|
|
3048
|
+
const t = imp.name === "default"
|
|
3049
|
+
? defaultOf(imp.rel)
|
|
3050
|
+
: (tpf && (tpf.localDefs.get(imp.name) || resolveExported(imp.rel, imp.name)));
|
|
3051
|
+
if (t) return { nid: t, confidence: "EXTRACTED" };
|
|
3052
|
+
return null;
|
|
3053
|
+
}
|
|
3054
|
+
for (const star of pf.starImports || []) {
|
|
3055
|
+
const t = resolveExported(star, name);
|
|
3056
|
+
if (t) return { nid: t, confidence: "EXTRACTED" };
|
|
3057
|
+
}
|
|
3058
|
+
return null;
|
|
3059
|
+
};
|
|
3060
|
+
for (const rel of rels) {
|
|
3061
|
+
const pf = perFile.get(rel);
|
|
3062
|
+
for (const c of pf.decls.classes) {
|
|
3063
|
+
const cnid = pf.localDefs.get(c.name);
|
|
3064
|
+
if (!cnid) continue;
|
|
3065
|
+
for (const base of c.bases || []) {
|
|
3066
|
+
let r = null;
|
|
3067
|
+
if (base.module) {
|
|
3068
|
+
const ns = pf.importedNames.get(base.module);
|
|
3069
|
+
const t = ns && ns.name === "*" && ns.rel ? (perFile.get(ns.rel).localDefs.get(base.name) || resolveExported(ns.rel, base.name)) : null;
|
|
3070
|
+
if (t) r = { nid: t, confidence: "EXTRACTED" };
|
|
3071
|
+
} else if (!BUILTIN_NOISE_LABELS.has(base.name)) r = resolveName(pf, base.name);
|
|
3072
|
+
if (r && r.nid !== cnid && nodeById.get(r.nid) && nodeById.get(r.nid).type === "class") {
|
|
3073
|
+
addEdge(cnid, r.nid, "inherits", { file: rel, line: c.line, confidence: r.confidence });
|
|
3074
|
+
if (!baseOf.has(cnid)) baseOf.set(cnid, []);
|
|
3075
|
+
baseOf.get(cnid).push(r.nid);
|
|
3076
|
+
}
|
|
3077
|
+
}
|
|
3078
|
+
if (c.extends && !BUILTIN_NOISE_LABELS.has(c.extends)) {
|
|
3079
|
+
const r = resolveName(pf, c.extends);
|
|
3080
|
+
if (r) {
|
|
3081
|
+
addEdge(cnid, r.nid, "inherits", { file: rel, line: c.line, confidence: r.confidence, score: r.confidence === "INFERRED" ? INFERRED_CALL_SCORE : undefined });
|
|
3082
|
+
if (!baseOf.has(cnid)) baseOf.set(cnid, []);
|
|
3083
|
+
baseOf.get(cnid).push(r.nid);
|
|
3084
|
+
}
|
|
3085
|
+
}
|
|
3086
|
+
for (const iface of c.implements || []) {
|
|
3087
|
+
if (BUILTIN_NOISE_LABELS.has(iface)) continue;
|
|
3088
|
+
const r = resolveName(pf, iface);
|
|
3089
|
+
if (r) addEdge(cnid, r.nid, "implements", { file: rel, line: c.line, confidence: r.confidence, score: r.confidence === "INFERRED" ? INFERRED_CALL_SCORE : undefined });
|
|
3090
|
+
}
|
|
3091
|
+
}
|
|
3092
|
+
for (const t of pf.decls.types) {
|
|
3093
|
+
if (!pf.localDefs.has(t.name)) continue;
|
|
3094
|
+
for (const base of t.extendsAll || (t.extends ? [t.extends] : [])) {
|
|
3095
|
+
const r = resolveName(pf, base);
|
|
3096
|
+
if (r && r.nid !== pf.localDefs.get(t.name)) addEdge(pf.localDefs.get(t.name), r.nid, "extends", { file: rel, line: t.line, confidence: r.confidence });
|
|
3097
|
+
}
|
|
3098
|
+
}
|
|
3099
|
+
}
|
|
3100
|
+
|
|
3101
|
+
// A member of a class: its own, or the nearest base class's (`from` skips
|
|
3102
|
+
// the class itself).
|
|
3103
|
+
const memberOf = (classNid, name, from, depth = 0) => {
|
|
3104
|
+
if (!classNid || depth > 5) return null;
|
|
3105
|
+
if (depth >= from) {
|
|
3106
|
+
const own = classMembers.get(classNid);
|
|
3107
|
+
if (own && own.has(name)) return own.get(name);
|
|
3108
|
+
}
|
|
3109
|
+
for (const base of baseOf.get(classNid) || []) {
|
|
3110
|
+
const hit = memberOf(base, name, 0, depth + 1);
|
|
3111
|
+
if (hit) return hit;
|
|
3112
|
+
}
|
|
3113
|
+
return null;
|
|
3114
|
+
};
|
|
3115
|
+
// Method names one class of the repo declares and no other, per language
|
|
3116
|
+
// family: what a call on a receiver of unknown type most plausibly reaches.
|
|
3117
|
+
// Names the language's own objects answer to are never guessed.
|
|
3118
|
+
const methodsByName = new Map();
|
|
3119
|
+
for (const [classNid, members] of classMembers) {
|
|
3120
|
+
const node = nodeById.get(classNid);
|
|
3121
|
+
const lang = node && node.source_file ? perFile.get(node.source_file).lang : null;
|
|
3122
|
+
if (!lang) continue;
|
|
3123
|
+
const family = LANG_FAMILY[lang];
|
|
3124
|
+
if (!methodsByName.has(family)) methodsByName.set(family, new Map());
|
|
3125
|
+
const table = methodsByName.get(family);
|
|
3126
|
+
for (const [name, mid] of members) {
|
|
3127
|
+
if (!table.has(name)) table.set(name, new Set());
|
|
3128
|
+
table.get(name).add(mid);
|
|
3129
|
+
}
|
|
3130
|
+
}
|
|
3131
|
+
const uniqueMethod = (lang, name) => {
|
|
3132
|
+
if (name.length < 4 || COMMON_METHOD_NAMES.has(name)) return null;
|
|
3133
|
+
const table = methodsByName.get(LANG_FAMILY[lang]);
|
|
3134
|
+
const set = table && table.get(name);
|
|
3135
|
+
return set && set.size === 1 ? [...set][0] : null;
|
|
3136
|
+
};
|
|
3137
|
+
|
|
3138
|
+
// -- phase 5: calls / instantiates / indirect_call
|
|
3139
|
+
for (const rel of rels) {
|
|
3140
|
+
const pf = perFile.get(rel);
|
|
3141
|
+
if (pf.lang === "go") { goCalls(rel, pf); continue; }
|
|
3142
|
+
if (pf.lang === "rust") { rustCalls(rel, pf); continue; }
|
|
3143
|
+
const lines = pf.stripped.split("\n");
|
|
3144
|
+
// symbol regions: decl line → tracked body end (brace depth), falling
|
|
3145
|
+
// back to open-ended (next-anchor semantics) where no end was tracked.
|
|
3146
|
+
// A top-level call AFTER a function's closing brace belongs to the
|
|
3147
|
+
// file, not to that function.
|
|
3148
|
+
const anchors = [];
|
|
3149
|
+
for (const c of pf.decls.classes) {
|
|
3150
|
+
const classNid = pf.localDefs.get(c.name);
|
|
3151
|
+
for (const meth of c.methods) anchors.push({ nid: c.methodIds.get(meth.name), line: meth.start || meth.line, decl: meth.line, headerEnd: meth.bodyStart > 0 ? pf.lineOf(meth.bodyStart) : meth.line, body: meth.bodyStart, end: meth.end != null ? meth.end : null, cls: c, classNid });
|
|
3152
|
+
anchors.push({ nid: classNid, line: c.start || c.line, decl: c.line, end: c.end != null ? c.end : null, isClass: true, cls: c, classNid });
|
|
3153
|
+
}
|
|
3154
|
+
for (const f of pf.decls.functions) anchors.push({ nid: pf.localDefs.get(f.name), line: f.start || f.line, decl: f.line, headerEnd: f.bodyStart > 0 ? pf.lineOf(f.bodyStart) : f.line, body: f.bodyStart, end: f.end != null ? f.end : null });
|
|
3155
|
+
anchors.sort((a, b) => a.line - b.line);
|
|
3156
|
+
const regionAt = (lineNo) => {
|
|
3157
|
+
let cur = null;
|
|
3158
|
+
for (const a of anchors) {
|
|
3159
|
+
if (a.line > lineNo) break;
|
|
3160
|
+
if (a.end == null || lineNo <= a.end) cur = a;
|
|
3161
|
+
}
|
|
3162
|
+
return cur;
|
|
3163
|
+
};
|
|
3164
|
+
const regionOf = (lineNo) => {
|
|
3165
|
+
const a = regionAt(lineNo);
|
|
3166
|
+
return a ? a.nid : pf.fileNid;
|
|
3167
|
+
};
|
|
3168
|
+
// Names a function binds itself (parameters, local declarations) and the
|
|
3169
|
+
// class types written or constructed for them. A local name shadows a
|
|
3170
|
+
// file-level or imported one; a typed one says which class `x.method()`
|
|
3171
|
+
// is called on.
|
|
3172
|
+
const py = pf.lang === "python";
|
|
3173
|
+
const regionInfo = new Map();
|
|
3174
|
+
const infoOf = (a) => {
|
|
3175
|
+
let info = regionInfo.get(a);
|
|
3176
|
+
if (info) return info;
|
|
3177
|
+
info = { locals: new Set(), types: new Map() };
|
|
3178
|
+
regionInfo.set(a, info);
|
|
3179
|
+
if (a.end == null || a.isClass) return info;
|
|
3180
|
+
const note = (name, type) => {
|
|
3181
|
+
if (!name) return;
|
|
3182
|
+
info.locals.add(name);
|
|
3183
|
+
if (type && !info.types.has(name)) info.types.set(name, type);
|
|
3184
|
+
};
|
|
3185
|
+
// parameters: the first parenthesised list of the header
|
|
3186
|
+
let headerEnd = a.decl;
|
|
3187
|
+
{
|
|
3188
|
+
let text = "", open = -1, close = -1;
|
|
3189
|
+
for (let l = a.decl; l <= a.end && l < a.decl + 60; l++) {
|
|
3190
|
+
text += (lines[l - 1] || "") + "\n";
|
|
3191
|
+
if (open < 0) open = text.indexOf("(");
|
|
3192
|
+
if (open >= 0) close = matchClose(text, open);
|
|
3193
|
+
headerEnd = l;
|
|
3194
|
+
if (close >= 0 || (open < 0 && l >= (a.headerEnd || a.decl))) break;
|
|
3195
|
+
}
|
|
3196
|
+
if (open >= 0 && close > open && !py) {
|
|
3197
|
+
for (const [name, type] of paramBindings(text.slice(open + 1, close))) note(name, type);
|
|
3198
|
+
} else if (open >= 0 && close > open) {
|
|
3199
|
+
for (const piece of splitTopLevel(text.slice(open + 1, close))) {
|
|
3200
|
+
const pm = piece.match(/^\s*(?:\*{1,2}|\.\.\.)?(?:(?:public|private|protected|readonly|override)\s+)*([A-Za-z_$][\w$]*)\??\s*(?::\s*["']?([A-Za-z_$][\w$.]*))?/);
|
|
3201
|
+
if (pm) note(pm[1], pm[2]);
|
|
3202
|
+
else for (const id of piece.split("=")[0].split(":")[0].matchAll(/[A-Za-z_$][\w$]*/g)) note(id[0]); // a destructured parameter
|
|
3203
|
+
}
|
|
3204
|
+
}
|
|
3205
|
+
headerEnd = Math.min(Math.max(headerEnd, a.headerEnd || a.decl), a.end);
|
|
3206
|
+
}
|
|
3207
|
+
for (let l = py ? headerEnd + 1 : a.decl; l <= a.end; l++) {
|
|
3208
|
+
const ln = lines[l - 1] || "";
|
|
3209
|
+
let m;
|
|
3210
|
+
if (py) {
|
|
3211
|
+
if ((m = ln.match(/^\s+([A-Za-z_]\w*)\s*(?::\s*([A-Za-z_][\w.]*))?[^=\n]*=(?!=)\s*(?:([A-Za-z_][\w.]*)\s*\()?/))) note(m[1], m[2] || m[3]);
|
|
3212
|
+
else if ((m = ln.match(/^\s+(?:async\s+)?for\s+([^:]*?)\s+in\b/))) for (const id of m[1].matchAll(/[A-Za-z_]\w*/g)) note(id[0]);
|
|
3213
|
+
for (const w of ln.matchAll(/\bas\s+([A-Za-z_]\w*)/g)) note(w[1]);
|
|
3214
|
+
} else {
|
|
3215
|
+
const declRe = /\b(?:const|let|var)\s+(?:([A-Za-z_$][\w$]*)\s*(?::\s*([A-Za-z_$][\w$.]*)[^=;]*)?(?:=\s*(?:await\s+)?(?:new\s+([A-Za-z_$][\w$.]*))?)?|[[{]([^=]*?)[\]}]\s*=)/g;
|
|
3216
|
+
while ((m = declRe.exec(ln)) !== null) {
|
|
3217
|
+
if (m[1]) note(m[1], m[2] || m[3]);
|
|
3218
|
+
else for (const id of (m[4] || "").matchAll(/(?:[A-Za-z_$][\w$]*\s*:\s*)?([A-Za-z_$][\w$]*)/g)) note(id[1]);
|
|
3219
|
+
}
|
|
3220
|
+
for (const w of ln.matchAll(/\(([^()]*)\)\s*(?::[^=;{]+)?=>/g)) for (const [name, type] of paramBindings(w[1])) note(name, type);
|
|
3221
|
+
// `x => …` binds x; the return type of `(a): Type => …` binds nothing
|
|
3222
|
+
for (const w of ln.matchAll(/(?<![\w$.])(?<!\)\s*:\s*)([A-Za-z_$][\w$]*)\s*=>/g)) note(w[1]);
|
|
3223
|
+
if ((m = ln.match(/\bcatch\s*\(\s*([A-Za-z_$][\w$]*)/))) note(m[1]);
|
|
3224
|
+
for (const w of ln.matchAll(/\bfunction\s*\*?\s*[A-Za-z_$][\w$]*\s*\(([^)]*)\)/g)) for (const [name] of paramBindings(w[1])) note(name);
|
|
3225
|
+
}
|
|
3226
|
+
}
|
|
3227
|
+
return info;
|
|
3228
|
+
};
|
|
3229
|
+
// Outside every indexed declaration (a callback, a function assigned to a
|
|
3230
|
+
// property): `name` declared with var/let/const in a block that encloses
|
|
3231
|
+
// `lineNo`, found by walking outward through less-indented lines.
|
|
3232
|
+
const indentOf = (t) => t.match(/^[ \t]*/)[0].replace(/\t/g, " ").length;
|
|
3233
|
+
const shadowedAbove = (name, lineNo) => {
|
|
3234
|
+
let indent = indentOf(lines[lineNo - 1] || "");
|
|
3235
|
+
if (indent === 0) return false;
|
|
3236
|
+
const re = new RegExp("\\b(?:var|let|const)\\s+(?:[^;=]*,\\s*)?" + name.replace(/[$]/g, "\\$&") + "\\b|^\\s*,\\s*" + name.replace(/[$]/g, "\\$&") + "\\s*=");
|
|
3237
|
+
for (let l = lineNo - 1; l >= 1 && l > lineNo - 600; l--) {
|
|
3238
|
+
const t = lines[l - 1];
|
|
3239
|
+
if (!t || !t.trim()) continue;
|
|
3240
|
+
const ind = indentOf(t);
|
|
3241
|
+
if (ind > indent) continue;
|
|
3242
|
+
if (ind === 0) return false;
|
|
3243
|
+
if (re.test(t)) return true;
|
|
3244
|
+
indent = ind;
|
|
3245
|
+
}
|
|
3246
|
+
return false;
|
|
3247
|
+
};
|
|
3248
|
+
// Field types of the classes in this file: written (`agent: Agent`,
|
|
3249
|
+
// constructor parameter properties) or constructed (`self.pool = Pool()`).
|
|
3250
|
+
const fieldTypes = (c) => {
|
|
3251
|
+
if (c.fieldTypes) return c.fieldTypes;
|
|
3252
|
+
c.fieldTypes = new Map(c.fields || []);
|
|
3253
|
+
if (c.end == null) return c.fieldTypes;
|
|
3254
|
+
for (let l = c.line; l <= c.end; l++) {
|
|
3255
|
+
const ln = lines[l - 1] || "";
|
|
3256
|
+
if (py) {
|
|
3257
|
+
for (const m of ln.matchAll(/\bself\.([A-Za-z_]\w*)\s*(?::\s*["']?([A-Za-z_][\w.]*))?[^=\n]*=(?!=)\s*(?:([A-Za-z_][\w.]*)\s*\()?/g)) {
|
|
3258
|
+
if ((m[2] || m[3]) && !c.fieldTypes.has(m[1])) c.fieldTypes.set(m[1], m[2] || m[3]);
|
|
3259
|
+
}
|
|
3260
|
+
} else {
|
|
3261
|
+
for (const m of ln.matchAll(/\b(?:public|private|protected|readonly)\s+(?:readonly\s+)?([A-Za-z_$][\w$]*)\??\s*:\s*([A-Za-z_$][\w$.]*)/g)) if (!c.fieldTypes.has(m[1])) c.fieldTypes.set(m[1], m[2]);
|
|
3262
|
+
for (const m of ln.matchAll(/\bthis\.(#?[A-Za-z_$][\w$]*)\s*=\s*new\s+([A-Za-z_$][\w$.]*)/g)) if (!c.fieldTypes.has(m[1])) c.fieldTypes.set(m[1], m[2]);
|
|
3263
|
+
}
|
|
3264
|
+
}
|
|
3265
|
+
return c.fieldTypes;
|
|
3266
|
+
};
|
|
3267
|
+
// The class a type name written in this file refers to (node id), or null.
|
|
3268
|
+
const classNamed = (typeName, line) => {
|
|
3269
|
+
if (!typeName) return null;
|
|
3270
|
+
const parts = typeName.split(".");
|
|
3271
|
+
let r = null;
|
|
3272
|
+
if (parts.length === 1) r = resolveName(pf, parts[0], line);
|
|
3273
|
+
else {
|
|
3274
|
+
const ns = importedAt(pf, parts.slice(0, -1).join("."), line);
|
|
3275
|
+
const t = ns && ns.name === "*" && ns.rel ? (perFile.get(ns.rel).localDefs.get(parts[parts.length - 1]) || resolveExported(ns.rel, parts[parts.length - 1])) : null;
|
|
3276
|
+
if (t) r = { nid: t };
|
|
3277
|
+
}
|
|
3278
|
+
const node = r && nodeById.get(r.nid);
|
|
3279
|
+
return node && node.type === "class" ? r.nid : null;
|
|
3280
|
+
};
|
|
3281
|
+
// `name` at `lineNo` is a parameter or local of the enclosing function
|
|
3282
|
+
// (or, outside every declaration, of an enclosing block), not the
|
|
3283
|
+
// file-level or imported name.
|
|
3284
|
+
const shadowedAt = (name, lineNo) => {
|
|
3285
|
+
const region = regionAt(lineNo);
|
|
3286
|
+
if (region && !region.isClass && pf.localDefs.get(name) !== region.nid && infoOf(region).locals.has(name) && !(pf.scopedImports && importedAt(pf, name, lineNo) !== pf.importedNames.get(name))) return true;
|
|
3287
|
+
return !region && !py && shadowedAbove(name, lineNo);
|
|
3288
|
+
};
|
|
3289
|
+
// TypeScript: the type annotations of a declaration's parameters and its
|
|
3290
|
+
// return type, as [from, to) columns per line. A method signature written
|
|
3291
|
+
// there (`cfg: { log(): void }`) is not a call; a default value is not
|
|
3292
|
+
// part of the span.
|
|
3293
|
+
const typeSpans = new Map(); // 0-based line → [[from, to)]
|
|
3294
|
+
if (pf.lang === "typescript") {
|
|
3295
|
+
const text = pf.stripped;
|
|
3296
|
+
const lineStart = [0];
|
|
3297
|
+
for (const ln of lines) lineStart.push(lineStart[lineStart.length - 1] + ln.length + 1);
|
|
3298
|
+
const mark = (from, to) => {
|
|
3299
|
+
for (let l = pf.lineOf(from) - 1; l < lines.length && lineStart[l] < to; l++) {
|
|
3300
|
+
const a = Math.max(from, lineStart[l]) - lineStart[l], b = Math.min(to, lineStart[l + 1]) - lineStart[l];
|
|
3301
|
+
if (a >= b) continue;
|
|
3302
|
+
if (!typeSpans.has(l)) typeSpans.set(l, []);
|
|
3303
|
+
typeSpans.get(l).push([a, b]);
|
|
3304
|
+
}
|
|
3305
|
+
};
|
|
3306
|
+
for (const a of anchors) {
|
|
3307
|
+
if (a.isClass) continue;
|
|
3308
|
+
// the header ends at the body's brace; a brace-less arrow has none
|
|
3309
|
+
// and is bounded by its last line
|
|
3310
|
+
const bound = a.body > 0 ? a.body : a.end != null ? lineStart[Math.min(a.end, lines.length)] : -1;
|
|
3311
|
+
const open = text.indexOf("(", lineStart[a.decl - 1]);
|
|
3312
|
+
if (open < 0 || open >= bound) continue;
|
|
3313
|
+
const close = matchClose(text, open);
|
|
3314
|
+
if (close < 0 || close >= bound) continue;
|
|
3315
|
+
let depth = 0, angle = 0, from = -1, inDefault = false;
|
|
3316
|
+
for (let k = open + 1; k <= close; k++) {
|
|
3317
|
+
const ch = text[k];
|
|
3318
|
+
if (k === close || (depth === 0 && angle === 0 && (ch === "," || (ch === "=" && text[k + 1] !== ">")))) {
|
|
3319
|
+
if (from >= 0) mark(from, k);
|
|
3320
|
+
from = -1;
|
|
3321
|
+
angle = 0;
|
|
3322
|
+
inDefault = ch === "=";
|
|
3323
|
+
} else if (ch === "(" || ch === "[" || ch === "{") depth++;
|
|
3324
|
+
else if (ch === ")" || ch === "]" || ch === "}") depth--;
|
|
3325
|
+
else if (from >= 0 && ch === "<") angle++;
|
|
3326
|
+
else if (from >= 0 && ch === ">" && text[k - 1] !== "=") angle = Math.max(0, angle - 1);
|
|
3327
|
+
else if (ch === ":" && depth === 0 && from < 0 && !inDefault) from = k + 1;
|
|
3328
|
+
}
|
|
3329
|
+
let k = close + 1;
|
|
3330
|
+
while (k < bound && /\s/.test(text[k])) k++;
|
|
3331
|
+
if (text[k] !== ":") continue;
|
|
3332
|
+
let to = a.body > 0 ? a.body : -1;
|
|
3333
|
+
for (let q = k + 1, d = 0; to < 0 && q < bound; q++) {
|
|
3334
|
+
const ch = text[q];
|
|
3335
|
+
if (ch === "(" || ch === "[" || ch === "{") d++;
|
|
3336
|
+
else if (ch === ")" || ch === "]" || ch === "}") d--;
|
|
3337
|
+
else if (d === 0 && ch === "=" && text[q + 1] === ">") to = q;
|
|
3338
|
+
}
|
|
3339
|
+
if (to > k) mark(k + 1, to);
|
|
3340
|
+
}
|
|
3341
|
+
}
|
|
3342
|
+
// A declaration's own name on its header line (`load(): object {`,
|
|
3343
|
+
// `def load(self):`) is not a call to whatever `load` the file imports.
|
|
3344
|
+
const declNames = new Map(); // 1-based line → names declared there
|
|
3345
|
+
const noteDecl = (name, line) => {
|
|
3346
|
+
if (!declNames.has(line)) declNames.set(line, new Set());
|
|
3347
|
+
declNames.get(line).add(name);
|
|
3348
|
+
};
|
|
3349
|
+
for (const c of pf.decls.classes) {
|
|
3350
|
+
noteDecl(c.name, c.line);
|
|
3351
|
+
for (const meth of c.methods) noteDecl(meth.name, meth.line);
|
|
3352
|
+
}
|
|
3353
|
+
for (const f of pf.decls.functions) noteDecl(f.name, f.line);
|
|
3354
|
+
const seenCalls = new Set();
|
|
3355
|
+
// Lookbehind (not a consuming prefix): a consumed delimiter would hide
|
|
3356
|
+
// the NEXT match in nested calls like `outer(inner())`.
|
|
3357
|
+
const callRe = /(?<![\w.$#])([A-Za-z_$][\w$]*)\s*\(/g;
|
|
3358
|
+
const newRe = /\bnew\s+([A-Za-z_$][\w$.]*)\s*[(<]/g;
|
|
3359
|
+
for (let i = 0; i < lines.length; i++) {
|
|
3360
|
+
const ln = lines[i];
|
|
3361
|
+
let m;
|
|
3362
|
+
callRe.lastIndex = 0;
|
|
3363
|
+
const headerNames = declNames.get(i + 1);
|
|
3364
|
+
const headerSeen = headerNames ? new Set() : null;
|
|
3365
|
+
while ((m = callRe.exec(ln)) !== null) {
|
|
3366
|
+
const name = m[1];
|
|
3367
|
+
if (CALL_KEYWORDS.has(name)) continue;
|
|
3368
|
+
if (headerNames && headerNames.has(name) && !headerSeen.has(name)) { headerSeen.add(name); continue; }
|
|
3369
|
+
const spans = typeSpans.get(i);
|
|
3370
|
+
if (spans && spans.some(([a, b]) => m.index >= a && m.index < b)) continue;
|
|
3371
|
+
const region = regionAt(i + 1);
|
|
3372
|
+
const caller = region ? region.nid : pf.fileNid;
|
|
3373
|
+
// a parameter or local of the enclosing function, not the file-level name
|
|
3374
|
+
if (region && !region.isClass && pf.localDefs.get(name) !== region.nid && infoOf(region).locals.has(name) && !(pf.scopedImports && importedAt(pf, name, i + 1) !== pf.importedNames.get(name))) continue;
|
|
3375
|
+
if (!region && !py && shadowedAbove(name, i + 1)) continue;
|
|
3376
|
+
let r = resolveName(pf, name, i + 1);
|
|
3377
|
+
if (!r && !pf.localDefs.has(name)) {
|
|
3378
|
+
const bound = importedAt(pf, name, i + 1);
|
|
3379
|
+
if (bound && bound.cjs && bound.rel) {
|
|
3380
|
+
// the function the module assigns to module.exports, else the module itself
|
|
3381
|
+
const moduleNid = defaultOf(bound.rel) || perFile.get(bound.rel).fileNid;
|
|
3382
|
+
const key = caller + "|" + moduleNid + "|calls";
|
|
3383
|
+
if (moduleNid !== caller && !seenCalls.has(key)) {
|
|
3384
|
+
seenCalls.add(key);
|
|
3385
|
+
addEdge(caller, moduleNid, "calls", { file: rel, line: i + 1, context: "call" });
|
|
3386
|
+
}
|
|
3387
|
+
}
|
|
3388
|
+
}
|
|
3389
|
+
if (!r || r.nid === caller) continue;
|
|
3390
|
+
const key = caller + "|" + r.nid + "|calls";
|
|
3391
|
+
if (seenCalls.has(key)) continue;
|
|
3392
|
+
seenCalls.add(key);
|
|
3393
|
+
const target = nodeById.get(r.nid);
|
|
3394
|
+
if (!target || !target._callable) {
|
|
3395
|
+
// calling a class = instantiation in py; skip non-callables otherwise
|
|
3396
|
+
if (target && target.type === "class" && pf.lang === "python") {
|
|
3397
|
+
addEdge(caller, r.nid, "instantiates", { file: rel, line: i + 1, confidence: r.confidence, context: "call" });
|
|
3398
|
+
}
|
|
3399
|
+
continue;
|
|
3400
|
+
}
|
|
3401
|
+
addEdge(caller, r.nid, "calls", { file: rel, line: i + 1, confidence: r.confidence, score: r.confidence === "INFERRED" ? INFERRED_CALL_SCORE : undefined, context: "call" });
|
|
3402
|
+
}
|
|
3403
|
+
// this.x()/self.x() → the ENCLOSING class's method, never an earlier
|
|
3404
|
+
// unrelated class that happens to share the method name.
|
|
3405
|
+
let sm;
|
|
3406
|
+
const selfRe = /\b(?:this|self)\.(#?[A-Za-z_$][\w$]*)\s*\(/g;
|
|
3407
|
+
while ((sm = selfRe.exec(ln)) !== null) {
|
|
3408
|
+
const caller = regionOf(i + 1);
|
|
3409
|
+
let owner = null;
|
|
3410
|
+
for (const c of pf.decls.classes) {
|
|
3411
|
+
if (c.line > i + 1) continue;
|
|
3412
|
+
if (c.end != null && i + 1 > c.end) continue;
|
|
3413
|
+
if (c.methods.some((x) => x.name === sm[1])) owner = c;
|
|
3414
|
+
}
|
|
3415
|
+
let mid = owner ? owner.methodIds.get(sm[1]) : null;
|
|
3416
|
+
if (!mid) {
|
|
3417
|
+
const region = regionAt(i + 1);
|
|
3418
|
+
if (region && region.classNid) mid = memberOf(region.classNid, sm[1], 1);
|
|
3419
|
+
}
|
|
3420
|
+
if (mid) {
|
|
3421
|
+
const key = caller + "|" + mid + "|calls";
|
|
3422
|
+
if (!seenCalls.has(key) && mid !== caller) { seenCalls.add(key); addEdge(caller, mid, "calls", { file: rel, line: i + 1, context: "call" }); }
|
|
3423
|
+
}
|
|
3424
|
+
}
|
|
3425
|
+
// ns.member() through a known imported namespace — `import * as util
|
|
3426
|
+
// from './util.js'; util.helper()` must record a call edge; the bare
|
|
3427
|
+
// callRe deliberately excludes dotted names. The receiver must START
|
|
3428
|
+
// a reference (`other.util.helper()` is a property chain, not the
|
|
3429
|
+
// import) and must not be shadowed by a module-level declaration.
|
|
3430
|
+
// Lookbehind, not a consuming prefix: `util.outer(util.inner())`
|
|
3431
|
+
// must match the inner call too.
|
|
3432
|
+
const nsCallRe = /(?<![\w.$#])((?:[A-Za-z_$][\w$]*\.)+)([A-Za-z_$][\w$]*)\s*\(/g;
|
|
3433
|
+
let nc;
|
|
3434
|
+
while ((nc = nsCallRe.exec(ln)) !== null) {
|
|
3435
|
+
const recv = nc[1].slice(0, -1);
|
|
3436
|
+
if (recv === "this" || recv === "self") continue;
|
|
3437
|
+
if (pf.localDefs.has(recv.split(".")[0]) && !pf.localDefs.has(recv)) continue;
|
|
3438
|
+
let tnid = null, relation = "calls";
|
|
3439
|
+
const imp = importedAt(pf, recv, i + 1);
|
|
3440
|
+
if (imp && imp.name === "*" && imp.rel && !pf.localDefs.has(recv)) {
|
|
3441
|
+
const tpfNs = perFile.get(imp.rel);
|
|
3442
|
+
tnid = (tpfNs && tpfNs.localDefs.get(nc[2])) || resolveExported(imp.rel, nc[2]);
|
|
3443
|
+
} else {
|
|
3444
|
+
const region = regionAt(i + 1);
|
|
3445
|
+
const segs = recv.split(".");
|
|
3446
|
+
let typeName = null, known = false;
|
|
3447
|
+
if ((segs[0] === "this" || segs[0] === "self") && segs.length === 2 && region && region.cls) {
|
|
3448
|
+
typeName = fieldTypes(region.cls).get(segs[1]) || null;
|
|
3449
|
+
} else if (segs.length === 1) {
|
|
3450
|
+
// `Name.member()` on a class of the repo: a static or class method
|
|
3451
|
+
const owner = region && !region.isClass && infoOf(region).locals.has(recv) ? null : resolveName(pf, recv, i + 1);
|
|
3452
|
+
if (owner) {
|
|
3453
|
+
known = true;
|
|
3454
|
+
tnid = nodeById.get(owner.nid) && nodeById.get(owner.nid).type === "class" ? memberOf(owner.nid, nc[2], 0) : null;
|
|
3455
|
+
} else {
|
|
3456
|
+
const bound = importedAt(pf, recv, i + 1);
|
|
3457
|
+
if (bound && !(region && !region.isClass && infoOf(region).locals.has(recv))) known = true; // an import: its members are not this repo's methods
|
|
3458
|
+
else if (region && !region.isClass) typeName = infoOf(region).types.get(recv) || null;
|
|
3459
|
+
}
|
|
3460
|
+
}
|
|
3461
|
+
if (!tnid && !known) {
|
|
3462
|
+
const cls = classNamed(typeName, i + 1);
|
|
3463
|
+
if (cls) tnid = memberOf(cls, nc[2], 0);
|
|
3464
|
+
else if (!typeName || !importedAt(pf, typeName.split(".")[0], i + 1) || !importedAt(pf, typeName.split(".")[0], i + 1).external) {
|
|
3465
|
+
tnid = uniqueMethod(pf.lang, nc[2]);
|
|
3466
|
+
if (tnid) relation = "inferred";
|
|
3467
|
+
}
|
|
3468
|
+
}
|
|
3469
|
+
}
|
|
3470
|
+
if (!tnid) continue;
|
|
3471
|
+
const target = nodeById.get(tnid);
|
|
3472
|
+
if (!target) continue;
|
|
3473
|
+
if (!target._callable) {
|
|
3474
|
+
if (target.type !== "class" || pf.lang !== "python") continue;
|
|
3475
|
+
relation = "instantiates";
|
|
3476
|
+
}
|
|
3477
|
+
const caller = regionOf(i + 1);
|
|
3478
|
+
if (tnid === caller) continue;
|
|
3479
|
+
const inferred = relation === "inferred";
|
|
3480
|
+
if (inferred) relation = "calls";
|
|
3481
|
+
const key = caller + "|" + tnid + "|" + relation;
|
|
3482
|
+
if (seenCalls.has(key)) continue;
|
|
3483
|
+
seenCalls.add(key);
|
|
3484
|
+
addEdge(caller, tnid, relation, { file: rel, line: i + 1, context: "call", confidence: inferred ? "INFERRED" : undefined, score: inferred ? INFERRED_CALL_SCORE : undefined });
|
|
3485
|
+
}
|
|
3486
|
+
newRe.lastIndex = 0;
|
|
3487
|
+
while ((m = newRe.exec(ln)) !== null) {
|
|
3488
|
+
const name = m[1].split(".")[0];
|
|
3489
|
+
if (shadowedAt(name, i + 1)) continue;
|
|
3490
|
+
const r = resolveName(pf, name, i + 1);
|
|
3491
|
+
if (!r) continue;
|
|
3492
|
+
const caller = regionOf(i + 1);
|
|
3493
|
+
const key = caller + "|" + r.nid + "|instantiates";
|
|
3494
|
+
if (seenCalls.has(key) || r.nid === caller) continue;
|
|
3495
|
+
seenCalls.add(key);
|
|
3496
|
+
addEdge(caller, r.nid, "instantiates", { file: rel, line: i + 1, confidence: r.confidence, context: "call" });
|
|
3497
|
+
}
|
|
3498
|
+
// indirect_call: known callable passed as a bare argument — f(cb) / f(x, cb).
|
|
3499
|
+
// The delimiter after the name is a lookahead: consuming it would hide
|
|
3500
|
+
// the next argument's leading comma (`f(a, b, c)` would lose `b`).
|
|
3501
|
+
const argRe = /[(,]\s*([A-Za-z_$][\w$]*)\s*(?=[,)])/g;
|
|
3502
|
+
while ((m = argRe.exec(ln)) !== null) {
|
|
3503
|
+
const name = m[1];
|
|
3504
|
+
if (CALL_KEYWORDS.has(name)) continue;
|
|
3505
|
+
if (shadowedAt(name, i + 1)) continue;
|
|
3506
|
+
const r = pf.localDefs.has(name) ? { nid: pf.localDefs.get(name) } : (pf.importedNames.get(name) && pf.importedNames.get(name).nid ? { nid: pf.importedNames.get(name).nid } : null);
|
|
3507
|
+
if (!r) continue;
|
|
3508
|
+
const target = nodeById.get(r.nid);
|
|
3509
|
+
if (!target || !target._callable) continue;
|
|
3510
|
+
const caller = regionOf(i + 1);
|
|
3511
|
+
const key = caller + "|" + r.nid + "|indirect_call";
|
|
3512
|
+
if (seenCalls.has(key) || r.nid === caller || seenCalls.has(caller + "|" + r.nid + "|calls")) continue;
|
|
3513
|
+
seenCalls.add(key);
|
|
3514
|
+
addEdge(caller, r.nid, "indirect_call", { file: rel, line: i + 1, confidence: "INFERRED", score: INFERRED_CALL_SCORE, context: "call" });
|
|
3515
|
+
}
|
|
3516
|
+
}
|
|
3517
|
+
}
|
|
3518
|
+
|
|
3519
|
+
// -- file keywords: each file's most distinctive words
|
|
3520
|
+
{
|
|
3521
|
+
const df = new Map();
|
|
3522
|
+
for (const st of textStats.values()) for (const t of st.tf.keys()) df.set(t, (df.get(t) || 0) + 1);
|
|
3523
|
+
const N = textStats.size || 1;
|
|
3524
|
+
const idfOf = (t) => Math.log(1 + (N - df.get(t) + 0.5) / (df.get(t) + 0.5));
|
|
3525
|
+
for (const st of textStats.values()) {
|
|
3526
|
+
if (!st.count) continue;
|
|
3527
|
+
st.node.terms = fileKeywords(st.tf, idfOf);
|
|
3528
|
+
st.node.tokens = st.count;
|
|
3529
|
+
}
|
|
3530
|
+
}
|
|
3531
|
+
|
|
3532
|
+
// -- phase 6: dedupe + sort (determinism)
|
|
3533
|
+
const edgeKey = (e) => e.source + "" + e.target + "" + e.relation;
|
|
3534
|
+
const seenEdge = new Set();
|
|
3535
|
+
const dedupedEdges = [];
|
|
3536
|
+
for (const e of edges) {
|
|
3537
|
+
const k = edgeKey(e);
|
|
3538
|
+
if (seenEdge.has(k)) continue;
|
|
3539
|
+
if (!nodeById.has(e.source) || !nodeById.has(e.target)) continue;
|
|
3540
|
+
seenEdge.add(k);
|
|
3541
|
+
dedupedEdges.push(e);
|
|
3542
|
+
}
|
|
3543
|
+
nodes.sort((a, b) => (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
3544
|
+
dedupedEdges.sort((a, b) => (edgeKey(a) < edgeKey(b) ? -1 : 1));
|
|
3545
|
+
|
|
3546
|
+
// -- phase 7: communities + analysis
|
|
3547
|
+
const { communityOf, communities } = computeCommunities(nodes, dedupedEdges);
|
|
3548
|
+
for (const n of nodes) {
|
|
3549
|
+
n.community = communityOf.get(n.id);
|
|
3550
|
+
n.community_name = communities[n.community] ? communities[n.community].name : undefined;
|
|
3551
|
+
n.norm_label = normalizeId(n.label).replace(/_/g, " ");
|
|
3552
|
+
}
|
|
3553
|
+
|
|
3554
|
+
return { nodes, edges: dedupedEdges, communities, idCollisions, methodLimitHits };
|
|
3555
|
+
}
|
|
3556
|
+
|
|
3557
|
+
// ---------------------------------------------------------------------------
|
|
3558
|
+
// Communities — deterministic LPA + graphify cluster.py post-processing
|
|
3559
|
+
// ---------------------------------------------------------------------------
|
|
3560
|
+
|
|
3561
|
+
function computeCommunities(nodes, edges) {
|
|
3562
|
+
const adj = new Map();
|
|
3563
|
+
for (const n of nodes) adj.set(n.id, new Set());
|
|
3564
|
+
for (const e of edges) {
|
|
3565
|
+
if (e.source === e.target) continue;
|
|
3566
|
+
adj.get(e.source).add(e.target);
|
|
3567
|
+
adj.get(e.target).add(e.source);
|
|
3568
|
+
}
|
|
3569
|
+
const ids = nodes.map((n) => n.id); // already sorted
|
|
3570
|
+
const label = new Map(ids.map((id) => [id, id]));
|
|
3571
|
+
for (let pass = 0; pass < 10; pass++) {
|
|
3572
|
+
let changed = false;
|
|
3573
|
+
for (const id of ids) {
|
|
3574
|
+
const counts = new Map();
|
|
3575
|
+
for (const nb of adj.get(id)) {
|
|
3576
|
+
const l = label.get(nb);
|
|
3577
|
+
counts.set(l, (counts.get(l) || 0) + 1);
|
|
3578
|
+
}
|
|
3579
|
+
if (!counts.size) continue;
|
|
3580
|
+
let best = null, bestC = -1;
|
|
3581
|
+
for (const [l, c] of [...counts.entries()].sort((a, b) => (a[0] < b[0] ? -1 : 1))) {
|
|
3582
|
+
if (c > bestC) { best = l; bestC = c; }
|
|
3583
|
+
}
|
|
3584
|
+
if (best !== label.get(id)) { label.set(id, best); changed = true; }
|
|
3585
|
+
}
|
|
3586
|
+
if (!changed) break;
|
|
3587
|
+
}
|
|
3588
|
+
let groups = new Map();
|
|
3589
|
+
for (const id of ids) {
|
|
3590
|
+
const l = label.get(id);
|
|
3591
|
+
if (!groups.has(l)) groups.set(l, []);
|
|
3592
|
+
groups.get(l).push(id);
|
|
3593
|
+
}
|
|
3594
|
+
// graphify: oversized (>25% of graph, min 10) and low-cohesion (<0.05, >=50)
|
|
3595
|
+
// communities are re-split. Splitter: two-level source-dir prefix (deterministic).
|
|
3596
|
+
const nodeById = new Map(nodes.map((n) => [n.id, n]));
|
|
3597
|
+
const intraEdges = (members) => {
|
|
3598
|
+
const s = new Set(members);
|
|
3599
|
+
let c = 0;
|
|
3600
|
+
for (const id of members) for (const nb of adj.get(id)) if (s.has(nb) && id < nb) c++;
|
|
3601
|
+
return c;
|
|
3602
|
+
};
|
|
3603
|
+
const cohesion = (members) => {
|
|
3604
|
+
const n = members.length;
|
|
3605
|
+
if (n < 2) return 0;
|
|
3606
|
+
return intraEdges(members) / ((n * (n - 1)) / 2);
|
|
3607
|
+
};
|
|
3608
|
+
// In a monorepo a 2-segment prefix is just "packages/<pkg>", which can still be
|
|
3609
|
+
// a third of the graph — recurse deeper (up to 6 segments) until no subgroup
|
|
3610
|
+
// stays oversized. Deterministic: prefix-sorted, fixed depth cap.
|
|
3611
|
+
const total = nodes.length || 1;
|
|
3612
|
+
const oversized = (members) => members.length > total * 0.25 && members.length >= 10;
|
|
3613
|
+
const splitByDir = (members, depth) => {
|
|
3614
|
+
if (depth > 6) return [members];
|
|
3615
|
+
const sub = new Map();
|
|
3616
|
+
for (const id of members) {
|
|
3617
|
+
const src = nodeById.get(id).source_file || "(external)";
|
|
3618
|
+
const key = src.split("/").slice(0, depth).join("/");
|
|
3619
|
+
if (!sub.has(key)) sub.set(key, []);
|
|
3620
|
+
sub.get(key).push(id);
|
|
3621
|
+
}
|
|
3622
|
+
if (sub.size === 1) return splitByDir(members, depth + 1);
|
|
3623
|
+
const out = [];
|
|
3624
|
+
for (const [, v] of [...sub.entries()].sort((a, b) => (a[0] < b[0] ? -1 : 1))) {
|
|
3625
|
+
if (oversized(v)) out.push(...splitByDir(v, depth + 1));
|
|
3626
|
+
else out.push(v);
|
|
3627
|
+
}
|
|
3628
|
+
return out;
|
|
3629
|
+
};
|
|
3630
|
+
let finalGroups = [];
|
|
3631
|
+
for (const members of [...groups.values()]) {
|
|
3632
|
+
if (oversized(members) || (members.length >= 50 && cohesion(members) < 0.05)) {
|
|
3633
|
+
finalGroups.push(...splitByDir(members, 2));
|
|
3634
|
+
} else finalGroups.push(members);
|
|
3635
|
+
}
|
|
3636
|
+
// re-index by size desc, tiebreak on sorted member tuple (cluster.py)
|
|
3637
|
+
finalGroups = finalGroups.map((m) => m.slice().sort());
|
|
3638
|
+
finalGroups.sort((a, b) => b.length - a.length || (a[0] < b[0] ? -1 : 1));
|
|
3639
|
+
const communityOf = new Map();
|
|
3640
|
+
const degree = new Map();
|
|
3641
|
+
for (const id of ids) degree.set(id, adj.get(id).size);
|
|
3642
|
+
const communities = finalGroups.map((members, idx) => {
|
|
3643
|
+
for (const id of members) communityOf.set(id, idx);
|
|
3644
|
+
// hub label: highest-degree member (label_communities_by_hub), lexicographic tiebreak
|
|
3645
|
+
let hub = members[0];
|
|
3646
|
+
for (const id of members) {
|
|
3647
|
+
if (degree.get(id) > degree.get(hub) || (degree.get(id) === degree.get(hub) && id < hub)) hub = id;
|
|
3648
|
+
}
|
|
3649
|
+
return {
|
|
3650
|
+
id: idx,
|
|
3651
|
+
name: nodeById.get(hub).label,
|
|
3652
|
+
size: members.length,
|
|
3653
|
+
cohesion: +cohesion(members).toFixed(4),
|
|
3654
|
+
members,
|
|
3655
|
+
};
|
|
3656
|
+
});
|
|
3657
|
+
return { communityOf, communities };
|
|
3658
|
+
}
|
|
3659
|
+
|
|
3660
|
+
// ---------------------------------------------------------------------------
|
|
3661
|
+
// Analysis — ports of analyze.py
|
|
3662
|
+
// ---------------------------------------------------------------------------
|
|
3663
|
+
|
|
3664
|
+
function buildAdjacency(graph) {
|
|
3665
|
+
const out = new Map(), inn = new Map(), deg = new Map();
|
|
3666
|
+
for (const n of graph.nodes) { out.set(n.id, []); inn.set(n.id, []); deg.set(n.id, 0); }
|
|
3667
|
+
for (const e of graph.edges) {
|
|
3668
|
+
if (!out.has(e.source) || !inn.has(e.target)) continue;
|
|
3669
|
+
out.get(e.source).push(e);
|
|
3670
|
+
inn.get(e.target).push(e);
|
|
3671
|
+
deg.set(e.source, deg.get(e.source) + 1);
|
|
3672
|
+
deg.set(e.target, deg.get(e.target) + 1);
|
|
3673
|
+
}
|
|
3674
|
+
return { out, inn, deg };
|
|
3675
|
+
}
|
|
3676
|
+
|
|
3677
|
+
function isFileNode(n) {
|
|
3678
|
+
return n.source_file && path.basename(n.source_file) === n.label;
|
|
3679
|
+
}
|
|
3680
|
+
function isConceptNode(n) {
|
|
3681
|
+
if (!n.source_file) return true;
|
|
3682
|
+
return !n.source_file.split("/").pop().includes(".");
|
|
3683
|
+
}
|
|
3684
|
+
|
|
3685
|
+
function godNodes(graph, adj, topN = 10) {
|
|
3686
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
3687
|
+
const sorted = [...adj.deg.entries()].sort((a, b) => b[1] - a[1] || (a[0] < b[0] ? -1 : 1));
|
|
3688
|
+
const result = [];
|
|
3689
|
+
for (const [id, deg] of sorted) {
|
|
3690
|
+
const n = byId.get(id);
|
|
3691
|
+
if (isFileNode(n) || isConceptNode(n)) continue;
|
|
3692
|
+
if (BUILTIN_NOISE_LABELS.has(n.label)) continue;
|
|
3693
|
+
if (n.label.endsWith("()") && deg <= 1) continue;
|
|
3694
|
+
result.push({ id, label: n.label, degree: deg, source_file: n.source_file, community: n.community });
|
|
3695
|
+
if (result.length >= topN) break;
|
|
3696
|
+
}
|
|
3697
|
+
return result;
|
|
3698
|
+
}
|
|
3699
|
+
|
|
3700
|
+
function surprisingConnections(graph, adj, topN = 10) {
|
|
3701
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
3702
|
+
const skipRel = new Set(["imports", "imports_from", "contains", "method", "defines"]);
|
|
3703
|
+
const scored = [];
|
|
3704
|
+
for (const e of graph.edges) {
|
|
3705
|
+
if (skipRel.has(e.relation)) continue;
|
|
3706
|
+
const u = byId.get(e.source), v = byId.get(e.target);
|
|
3707
|
+
if (!u || !v || isConceptNode(u) || isConceptNode(v) || isFileNode(u) || isFileNode(v)) continue;
|
|
3708
|
+
if (u.source_file === v.source_file) continue;
|
|
3709
|
+
let score = 0;
|
|
3710
|
+
const reasons = [];
|
|
3711
|
+
const confBonus = { AMBIGUOUS: 3, INFERRED: 2, EXTRACTED: 1 }[e.confidence] || 1;
|
|
3712
|
+
score += confBonus;
|
|
3713
|
+
if (e.confidence === "AMBIGUOUS" || e.confidence === "INFERRED") reasons.push(`${e.confidence.toLowerCase()} connection - not explicitly stated in source`);
|
|
3714
|
+
const topU = u.source_file.split("/")[0], topV = v.source_file.split("/")[0];
|
|
3715
|
+
if (topU !== topV) { score += 2; reasons.push("connects across different repos/directories"); }
|
|
3716
|
+
if (u.community != null && v.community != null && u.community !== v.community) { score += 1; reasons.push("bridges separate communities"); }
|
|
3717
|
+
const degU = adj.deg.get(e.source) || 0, degV = adj.deg.get(e.target) || 0;
|
|
3718
|
+
if (Math.min(degU, degV) <= 2 && Math.max(degU, degV) >= 5) { score += 1; reasons.push("peripheral node connects to a hub"); }
|
|
3719
|
+
if (score >= 3) scored.push({ score, source: u.label, target: v.label, source_id: e.source, target_id: e.target, relation: e.relation, confidence: e.confidence, reasons });
|
|
3720
|
+
}
|
|
3721
|
+
scored.sort((a, b) => b.score - a.score || (a.source_id < b.source_id ? -1 : 1) || (a.target_id < b.target_id ? -1 : 1));
|
|
3722
|
+
// dedupe: one per community pair
|
|
3723
|
+
const seenPair = new Set();
|
|
3724
|
+
const out = [];
|
|
3725
|
+
for (const s of scored) {
|
|
3726
|
+
const cu = byId.get(s.source_id).community, cv = byId.get(s.target_id).community;
|
|
3727
|
+
const key = Math.min(cu, cv) + ":" + Math.max(cu, cv);
|
|
3728
|
+
if (seenPair.has(key)) continue;
|
|
3729
|
+
seenPair.add(key);
|
|
3730
|
+
out.push(s);
|
|
3731
|
+
if (out.length >= topN) break;
|
|
3732
|
+
}
|
|
3733
|
+
return out;
|
|
3734
|
+
}
|
|
3735
|
+
|
|
3736
|
+
function suggestQuestions(graph, adj, communities, gods) {
|
|
3737
|
+
const q = [];
|
|
3738
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
3739
|
+
const ambiguous = graph.edges.filter((e) => e.confidence === "AMBIGUOUS").slice(0, 3);
|
|
3740
|
+
for (const e of ambiguous) {
|
|
3741
|
+
q.push(`Is the ${e.relation} edge ${byId.get(e.source)?.label} → ${byId.get(e.target)?.label} real? (marked AMBIGUOUS)`);
|
|
3742
|
+
}
|
|
3743
|
+
for (const g of gods.slice(0, 3)) {
|
|
3744
|
+
const inferredCount = (adj.out.get(g.id) || []).concat(adj.inn.get(g.id) || []).filter((e) => e.confidence === "INFERRED").length;
|
|
3745
|
+
if (inferredCount >= 2) q.push(`\`${g.label}\` has ${inferredCount} inferred edges — verify how it is actually used.`);
|
|
3746
|
+
else q.push(`Why does \`${g.label}\` have ${g.degree} connections — is it doing too much?`);
|
|
3747
|
+
}
|
|
3748
|
+
const isolated = graph.nodes.filter((n) => (adj.deg.get(n.id) || 0) === 0 && !isConceptNode(n)).slice(0, 2);
|
|
3749
|
+
for (const n of isolated) q.push(`\`${n.label}\` (${n.source_file}) has no connections — dead code or missed extraction?`);
|
|
3750
|
+
for (const c of communities) {
|
|
3751
|
+
if (c.size >= 5 && c.cohesion < 0.15) { q.push(`Community "${c.name}" (${c.size} nodes) has low cohesion ${c.cohesion} — is it really one concept?`); break; }
|
|
3752
|
+
}
|
|
3753
|
+
return q.slice(0, 8);
|
|
3754
|
+
}
|
|
3755
|
+
|
|
3756
|
+
// Strongly connected components (iterative Tarjan): node id → component id,
|
|
3757
|
+
// and component sizes. A cycle lives entirely inside one component, so the
|
|
3758
|
+
// bounded path search below never leaves it — and never starts at all in an
|
|
3759
|
+
// acyclic graph, where it would otherwise walk every path up to maxLen.
|
|
3760
|
+
function stronglyConnected(adj) {
|
|
3761
|
+
const index = new Map(), low = new Map(), comp = new Map(), size = [];
|
|
3762
|
+
const onStack = new Set(), stack = [];
|
|
3763
|
+
let counter = 0;
|
|
3764
|
+
const visit = (root) => {
|
|
3765
|
+
const work = [[root, 0]];
|
|
3766
|
+
index.set(root, counter); low.set(root, counter); counter++;
|
|
3767
|
+
stack.push(root); onStack.add(root);
|
|
3768
|
+
while (work.length) {
|
|
3769
|
+
const frame = work[work.length - 1];
|
|
3770
|
+
const [v] = frame;
|
|
3771
|
+
const nbrs = adj.get(v) || [];
|
|
3772
|
+
if (frame[1] < nbrs.length) {
|
|
3773
|
+
const w = nbrs[frame[1]++];
|
|
3774
|
+
if (!index.has(w)) {
|
|
3775
|
+
index.set(w, counter); low.set(w, counter); counter++;
|
|
3776
|
+
stack.push(w); onStack.add(w);
|
|
3777
|
+
work.push([w, 0]);
|
|
3778
|
+
} else if (onStack.has(w)) low.set(v, Math.min(low.get(v), index.get(w)));
|
|
3779
|
+
continue;
|
|
3780
|
+
}
|
|
3781
|
+
work.pop();
|
|
3782
|
+
if (work.length) {
|
|
3783
|
+
const parent = work[work.length - 1][0];
|
|
3784
|
+
low.set(parent, Math.min(low.get(parent), low.get(v)));
|
|
3785
|
+
}
|
|
3786
|
+
if (low.get(v) === index.get(v)) {
|
|
3787
|
+
const id = size.length;
|
|
3788
|
+
let n = 0, w;
|
|
3789
|
+
do { w = stack.pop(); onStack.delete(w); comp.set(w, id); n++; } while (w !== v);
|
|
3790
|
+
size.push(n);
|
|
3791
|
+
}
|
|
3792
|
+
}
|
|
3793
|
+
};
|
|
3794
|
+
for (const v of adj.keys()) if (!index.has(v)) visit(v);
|
|
3795
|
+
return { comp, size };
|
|
3796
|
+
}
|
|
3797
|
+
|
|
3798
|
+
function importCycles(graph, maxLen = 5) {
|
|
3799
|
+
// file-level digraph over imports_from/re_exports, excluding deferred (dynamic) imports
|
|
3800
|
+
const adj = new Map();
|
|
3801
|
+
for (const e of graph.edges) {
|
|
3802
|
+
if (e.relation !== "imports_from" && e.relation !== "re_exports") continue;
|
|
3803
|
+
if (e._deferred) continue;
|
|
3804
|
+
if (!adj.has(e.source)) adj.set(e.source, []);
|
|
3805
|
+
adj.get(e.source).push(e.target);
|
|
3806
|
+
}
|
|
3807
|
+
const { comp, size } = stronglyConnected(adj);
|
|
3808
|
+
const cycles = [];
|
|
3809
|
+
const seen = new Set();
|
|
3810
|
+
const nodes = [...adj.keys()].sort();
|
|
3811
|
+
for (const start of nodes) {
|
|
3812
|
+
const home = comp.get(start);
|
|
3813
|
+
if (size[home] < 2) continue;
|
|
3814
|
+
const stack = [[start, [start]]];
|
|
3815
|
+
while (stack.length && cycles.length < 20) {
|
|
3816
|
+
const [cur, pathArr] = stack.pop();
|
|
3817
|
+
for (const nb of (adj.get(cur) || [])) {
|
|
3818
|
+
if (comp.get(nb) !== home) continue;
|
|
3819
|
+
if (nb === start && pathArr.length > 1) {
|
|
3820
|
+
const rot = [...pathArr];
|
|
3821
|
+
const minIdx = rot.indexOf([...rot].sort()[0]);
|
|
3822
|
+
const canon = rot.slice(minIdx).concat(rot.slice(0, minIdx)).join("→");
|
|
3823
|
+
if (!seen.has(canon)) { seen.add(canon); cycles.push([...pathArr, start]); }
|
|
3824
|
+
} else if (!pathArr.includes(nb) && pathArr.length < maxLen) {
|
|
3825
|
+
stack.push([nb, [...pathArr, nb]]);
|
|
3826
|
+
}
|
|
3827
|
+
}
|
|
3828
|
+
}
|
|
3829
|
+
}
|
|
3830
|
+
return cycles.slice(0, 10);
|
|
3831
|
+
}
|
|
3832
|
+
|
|
3833
|
+
// ---------------------------------------------------------------------------
|
|
3834
|
+
// graph.json IO
|
|
3835
|
+
// ---------------------------------------------------------------------------
|
|
3836
|
+
|
|
3837
|
+
function outDir(root) {
|
|
3838
|
+
return path.join(root, OUT_DIRNAME);
|
|
3839
|
+
}
|
|
3840
|
+
|
|
3841
|
+
function writeGraph(root, graph) {
|
|
3842
|
+
const dir = outDir(root);
|
|
3843
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
3844
|
+
const nodes = graph.nodes.map((n) => {
|
|
3845
|
+
const { _callable, ...rest } = n;
|
|
3846
|
+
return rest;
|
|
3847
|
+
});
|
|
3848
|
+
const edges = graph.edges.map((e) => {
|
|
3849
|
+
const { _deferred, ...rest } = e;
|
|
3850
|
+
if (_deferred) rest.deferred = true;
|
|
3851
|
+
for (const k of Object.keys(rest)) if (rest[k] === undefined) delete rest[k];
|
|
3852
|
+
return rest;
|
|
3853
|
+
});
|
|
3854
|
+
const payload = {
|
|
3855
|
+
directed: true,
|
|
3856
|
+
multigraph: false,
|
|
3857
|
+
graph: { generated_by: "draht-tools kg build", schema: "graphify-node-link", communities: graph.communities.map(({ members, ...c }) => c) },
|
|
3858
|
+
nodes,
|
|
3859
|
+
edges,
|
|
3860
|
+
hyperedges: [],
|
|
3861
|
+
};
|
|
3862
|
+
const jsonPath = path.join(dir, GRAPH_BASENAME);
|
|
3863
|
+
const next = JSON.stringify(payload, null, 1);
|
|
3864
|
+
let prev = null;
|
|
3865
|
+
try { prev = fs.readFileSync(jsonPath, "utf-8"); } catch { /* empty */ }
|
|
3866
|
+
if (prev !== next) writeFileAtomic(jsonPath, next);
|
|
3867
|
+
return jsonPath;
|
|
3868
|
+
}
|
|
3869
|
+
|
|
3870
|
+
function loadGraph(root) {
|
|
3871
|
+
const jsonPath = path.join(outDir(root), GRAPH_BASENAME);
|
|
3872
|
+
let raw;
|
|
3873
|
+
try { raw = JSON.parse(fs.readFileSync(jsonPath, "utf-8")); } catch { return null; }
|
|
3874
|
+
const edges = raw.edges || raw.links || [];
|
|
3875
|
+
const nodes = raw.nodes || [];
|
|
3876
|
+
// writeGraph strips each community's `members` from graph.json (they are
|
|
3877
|
+
// derivable); rebuild them from node.community so `kg report` and
|
|
3878
|
+
// `kg export wiki` work from a loaded graph. Nodes are sorted by id in
|
|
3879
|
+
// the file, so the rebuilt lists are deterministic.
|
|
3880
|
+
const communities = (raw.graph && raw.graph.communities) || [];
|
|
3881
|
+
const byIdx = new Map(communities.map((c) => [c.id, c]));
|
|
3882
|
+
for (const c of communities) if (!Array.isArray(c.members)) c.members = [];
|
|
3883
|
+
for (const n of nodes) {
|
|
3884
|
+
const c = byIdx.get(n.community);
|
|
3885
|
+
if (c) c.members.push(n.id);
|
|
3886
|
+
}
|
|
3887
|
+
return { nodes, edges, communities, path: jsonPath };
|
|
3888
|
+
}
|
|
3889
|
+
|
|
3890
|
+
// ---------------------------------------------------------------------------
|
|
3891
|
+
// Query engine — ports of serve.py
|
|
3892
|
+
// ---------------------------------------------------------------------------
|
|
3893
|
+
|
|
3894
|
+
function stripDiacritics(s) {
|
|
3895
|
+
return String(s).normalize("NFD").replace(/[̀-ͯ]/g, "");
|
|
3896
|
+
}
|
|
3897
|
+
function searchTokens(text) {
|
|
3898
|
+
return (stripDiacritics(String(text)).toLowerCase().match(/[\p{L}\p{N}_]+/gu) || []);
|
|
3899
|
+
}
|
|
3900
|
+
function isSearchable(term) {
|
|
3901
|
+
if (/^[a-z]+$/.test(term)) return term.length > 2;
|
|
3902
|
+
return true;
|
|
3903
|
+
}
|
|
3904
|
+
function queryTerms(question) {
|
|
3905
|
+
const terms = [];
|
|
3906
|
+
for (const raw of String(question).split(/\s+/)) {
|
|
3907
|
+
for (const tok of (raw.toLowerCase().match(/[\p{L}\p{N}_]+/gu) || [])) {
|
|
3908
|
+
if (isSearchable(tok)) terms.push(tok);
|
|
3909
|
+
}
|
|
3910
|
+
}
|
|
3911
|
+
const content = terms.filter((t) => !QUERY_STOPWORDS.has(t));
|
|
3912
|
+
return content.length ? content : terms;
|
|
3913
|
+
}
|
|
3914
|
+
|
|
3915
|
+
function nodeNormLabel(n) {
|
|
3916
|
+
return (n.norm_label || stripDiacritics(n.label || "").toLowerCase());
|
|
3917
|
+
}
|
|
3918
|
+
|
|
3919
|
+
function computeIdf(graph, terms) {
|
|
3920
|
+
const N = graph.nodes.length || 1;
|
|
3921
|
+
const idf = {};
|
|
3922
|
+
for (const t of terms) {
|
|
3923
|
+
let df = 0;
|
|
3924
|
+
for (const n of graph.nodes) if (nodeNormLabel(n).includes(t)) df++;
|
|
3925
|
+
idf[t] = Math.log(1 + N / (1 + df));
|
|
3926
|
+
}
|
|
3927
|
+
return idf;
|
|
3928
|
+
}
|
|
3929
|
+
|
|
3930
|
+
function scoreNodes(graph, terms) {
|
|
3931
|
+
const normTerms = [...new Set(terms.flatMap((t) => searchTokens(t)))];
|
|
3932
|
+
const nTerms = normTerms.length || 1;
|
|
3933
|
+
const idf = computeIdf(graph, normTerms);
|
|
3934
|
+
const joined = normTerms.join(" ");
|
|
3935
|
+
const joinedW = Math.max(1.0, ...normTerms.map((t) => idf[t] || 1.0));
|
|
3936
|
+
const scored = [];
|
|
3937
|
+
for (const n of graph.nodes) {
|
|
3938
|
+
const normLabel = nodeNormLabel(n);
|
|
3939
|
+
const bareLabel = normLabel.replace(/\(\)+$/, "");
|
|
3940
|
+
const labelTokens = searchTokens(n.label || "").join(" ");
|
|
3941
|
+
const source = (n.source_file || "").toLowerCase();
|
|
3942
|
+
let score = 0;
|
|
3943
|
+
if (joined) {
|
|
3944
|
+
const nidLower = n.id.toLowerCase();
|
|
3945
|
+
if (joined === normLabel || joined === bareLabel || joined === labelTokens || joined === nidLower) {
|
|
3946
|
+
score += EXACT_MATCH_BONUS * 10 * joinedW;
|
|
3947
|
+
} else if (normLabel.startsWith(joined) || bareLabel.startsWith(joined) || labelTokens.startsWith(joined)) {
|
|
3948
|
+
score += PREFIX_MATCH_BONUS * 10 * joinedW;
|
|
3949
|
+
}
|
|
3950
|
+
}
|
|
3951
|
+
let matched = 0, tiered = 0;
|
|
3952
|
+
for (const t of normTerms) {
|
|
3953
|
+
const w = idf[t] || 1.0;
|
|
3954
|
+
if (t === normLabel || t === bareLabel) { tiered += EXACT_MATCH_BONUS * w; matched++; }
|
|
3955
|
+
else if (normLabel.startsWith(t) || bareLabel.startsWith(t)) { tiered += PREFIX_MATCH_BONUS * w; matched++; }
|
|
3956
|
+
else if (normLabel.includes(t)) { score += SUBSTRING_MATCH_BONUS * w; matched++; }
|
|
3957
|
+
if (source.includes(t)) score += SOURCE_MATCH_BONUS * w;
|
|
3958
|
+
}
|
|
3959
|
+
if (tiered) score += tiered * (matched / nTerms) ** 2;
|
|
3960
|
+
if (score > 0) scored.push([score, n.id]);
|
|
3961
|
+
}
|
|
3962
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
3963
|
+
scored.sort((a, b) => b[0] - a[0] || ((byId.get(a[1]).label || a[1]).length - (byId.get(b[1]).label || b[1]).length) || (a[1] < b[1] ? -1 : 1));
|
|
3964
|
+
return scored;
|
|
3965
|
+
}
|
|
3966
|
+
|
|
3967
|
+
function pickScoredEndpoint(graph, scored, query) {
|
|
3968
|
+
const qtokens = new Set(searchTokens(query));
|
|
3969
|
+
if (!qtokens.size) return scored[0][1];
|
|
3970
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
3971
|
+
for (const [, nid] of scored) {
|
|
3972
|
+
const ltokens = new Set(searchTokens(byId.get(nid).label || nid));
|
|
3973
|
+
if ([...qtokens].every((t) => ltokens.has(t))) return nid;
|
|
3974
|
+
}
|
|
3975
|
+
return scored[0][1];
|
|
3976
|
+
}
|
|
3977
|
+
|
|
3978
|
+
function undirectedNeighbors(graph) {
|
|
3979
|
+
const nbrs = new Map();
|
|
3980
|
+
const deg = new Map();
|
|
3981
|
+
for (const n of graph.nodes) { nbrs.set(n.id, new Set()); deg.set(n.id, 0); }
|
|
3982
|
+
for (const e of graph.edges) {
|
|
3983
|
+
if (!nbrs.has(e.source) || !nbrs.has(e.target) || e.source === e.target) continue;
|
|
3984
|
+
nbrs.get(e.source).add(e.target);
|
|
3985
|
+
nbrs.get(e.target).add(e.source);
|
|
3986
|
+
}
|
|
3987
|
+
for (const [id, s] of nbrs) deg.set(id, s.size);
|
|
3988
|
+
return { nbrs, deg };
|
|
3989
|
+
}
|
|
3990
|
+
|
|
3991
|
+
function hubThreshold(deg) {
|
|
3992
|
+
const degrees = [...deg.values()].sort((a, b) => a - b);
|
|
3993
|
+
if (!degrees.length) return 50;
|
|
3994
|
+
const p99 = degrees[Math.min(degrees.length - 1, Math.floor(degrees.length * 0.99))];
|
|
3995
|
+
return Math.max(50, p99);
|
|
3996
|
+
}
|
|
3997
|
+
|
|
3998
|
+
function traverse(graph, startNodes, depth, mode, contextFilter) {
|
|
3999
|
+
let edges = graph.edges;
|
|
4000
|
+
if (contextFilter && contextFilter.length) {
|
|
4001
|
+
const f = new Set(contextFilter);
|
|
4002
|
+
edges = edges.filter((e) => f.has(e.context));
|
|
4003
|
+
}
|
|
4004
|
+
const g = { nodes: graph.nodes, edges };
|
|
4005
|
+
const { nbrs, deg } = undirectedNeighbors(g);
|
|
4006
|
+
const hub = hubThreshold(deg);
|
|
4007
|
+
const seedSet = new Set(startNodes);
|
|
4008
|
+
const visited = new Set(startNodes);
|
|
4009
|
+
const edgesSeen = [];
|
|
4010
|
+
if (mode === "dfs") {
|
|
4011
|
+
visited.clear();
|
|
4012
|
+
const stack = startNodes.slice().reverse().map((n) => [n, 0]);
|
|
4013
|
+
while (stack.length) {
|
|
4014
|
+
const [node, d] = stack.pop();
|
|
4015
|
+
if (visited.has(node) || d > depth) continue;
|
|
4016
|
+
visited.add(node);
|
|
4017
|
+
if (!seedSet.has(node) && (deg.get(node) || 0) >= hub) continue;
|
|
4018
|
+
for (const nb of [...(nbrs.get(node) || [])].sort()) {
|
|
4019
|
+
if (!visited.has(nb)) { stack.push([nb, d + 1]); edgesSeen.push([node, nb]); }
|
|
4020
|
+
}
|
|
4021
|
+
}
|
|
4022
|
+
} else {
|
|
4023
|
+
let frontier = new Set(startNodes);
|
|
4024
|
+
for (let i = 0; i < depth; i++) {
|
|
4025
|
+
const next = new Set();
|
|
4026
|
+
for (const n of [...frontier].sort()) {
|
|
4027
|
+
if (!seedSet.has(n) && (deg.get(n) || 0) >= hub) continue;
|
|
4028
|
+
for (const nb of [...(nbrs.get(n) || [])].sort()) {
|
|
4029
|
+
if (!visited.has(nb)) { next.add(nb); edgesSeen.push([n, nb]); }
|
|
4030
|
+
}
|
|
4031
|
+
}
|
|
4032
|
+
for (const n of next) visited.add(n);
|
|
4033
|
+
frontier = next;
|
|
4034
|
+
}
|
|
4035
|
+
}
|
|
4036
|
+
return { visited, edgesSeen, edges, deg };
|
|
4037
|
+
}
|
|
4038
|
+
|
|
4039
|
+
function edgeBetween(edges, u, v) {
|
|
4040
|
+
for (const e of edges) {
|
|
4041
|
+
if ((e.source === u && e.target === v) || (e.source === v && e.target === u)) return e;
|
|
4042
|
+
}
|
|
4043
|
+
return {};
|
|
4044
|
+
}
|
|
4045
|
+
|
|
4046
|
+
function subgraphToText(graph, visited, edgesSeen, edges, deg, tokenBudget, seeds) {
|
|
4047
|
+
const charBudget = tokenBudget * 3;
|
|
4048
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4049
|
+
const lines = [];
|
|
4050
|
+
const seedSet = new Set(seeds || []);
|
|
4051
|
+
const ordered = (seeds || []).filter((s) => visited.has(s))
|
|
4052
|
+
.concat([...visited].filter((n) => !seedSet.has(n)).sort((a, b) => (deg.get(b) || 0) - (deg.get(a) || 0) || (a < b ? -1 : 1)));
|
|
4053
|
+
for (const nid of ordered) {
|
|
4054
|
+
const d = byId.get(nid) || {};
|
|
4055
|
+
lines.push(`NODE ${d.label || nid} [src=${d.source_file || ""} loc=${d.source_location || ""} community=${d.community_name != null ? d.community_name : (d.community != null ? d.community : "")}]`);
|
|
4056
|
+
}
|
|
4057
|
+
// index edges for fast lookup
|
|
4058
|
+
const eIdx = new Map();
|
|
4059
|
+
for (const e of edges) {
|
|
4060
|
+
eIdx.set(e.source + "" + e.target, e);
|
|
4061
|
+
if (!eIdx.has(e.target + "" + e.source)) eIdx.set(e.target + "" + e.source, e);
|
|
4062
|
+
}
|
|
4063
|
+
// EDGE lines always print the STORED direction (source -> target), never
|
|
4064
|
+
// the traversal direction: a search for a callee must not render
|
|
4065
|
+
// `callee --calls--> caller`.
|
|
4066
|
+
const printed = new Set();
|
|
4067
|
+
const pushEdge = (e) => {
|
|
4068
|
+
if (!e || !e.relation) return;
|
|
4069
|
+
const k = e.source + "" + e.target + "" + e.relation;
|
|
4070
|
+
if (printed.has(k)) return;
|
|
4071
|
+
printed.add(k);
|
|
4072
|
+
const ctx = e.context ? ` context=${e.context}` : "";
|
|
4073
|
+
lines.push(`EDGE ${(byId.get(e.source) || {}).label || e.source} --${e.relation} [${e.confidence || ""}${ctx}]--> ${(byId.get(e.target) || {}).label || e.target}`);
|
|
4074
|
+
};
|
|
4075
|
+
// Edges between seeds first: both endpoints are pre-visited, so the
|
|
4076
|
+
// traversal itself never records them — yet the link between the queried
|
|
4077
|
+
// symbols is usually the answer.
|
|
4078
|
+
if (seedSet.size > 1) {
|
|
4079
|
+
for (const e of edges) {
|
|
4080
|
+
if (e.source !== e.target && seedSet.has(e.source) && seedSet.has(e.target)) pushEdge(e);
|
|
4081
|
+
}
|
|
4082
|
+
}
|
|
4083
|
+
for (const [u, v] of edgesSeen) {
|
|
4084
|
+
if (!visited.has(u) || !visited.has(v)) continue;
|
|
4085
|
+
pushEdge(eIdx.get(u + "" + v));
|
|
4086
|
+
}
|
|
4087
|
+
let output = lines.join("\n");
|
|
4088
|
+
if (output.length > charBudget) {
|
|
4089
|
+
let cutAt = output.slice(0, charBudget).lastIndexOf("\n");
|
|
4090
|
+
if (cutAt <= 0) cutAt = charBudget;
|
|
4091
|
+
const totalNodes = lines.filter((l) => l.startsWith("NODE ")).length;
|
|
4092
|
+
const head = output.slice(0, cutAt);
|
|
4093
|
+
const shownNodes = (head.match(/\nNODE /g) || []).length + (head.startsWith("NODE ") ? 1 : 0);
|
|
4094
|
+
output = head + `\n... (truncated — ${totalNodes - shownNodes} more nodes cut by ~${tokenBudget}-token budget. Narrow with --context call or use kg explain for a specific symbol)`;
|
|
4095
|
+
}
|
|
4096
|
+
return output;
|
|
4097
|
+
}
|
|
4098
|
+
|
|
4099
|
+
// ---------------------------------------------------------------------------
|
|
4100
|
+
// File-first ranking for `kg query`
|
|
4101
|
+
// ---------------------------------------------------------------------------
|
|
4102
|
+
|
|
4103
|
+
// Words of an identifier or a sentence: camelCase, PascalCase, ACRONYMWord,
|
|
4104
|
+
// snake_case and punctuation all split; lowercased.
|
|
4105
|
+
function wordsOf(text) {
|
|
4106
|
+
const out = [];
|
|
4107
|
+
for (const m of String(text).matchAll(/[A-Z]+(?![a-z])|[A-Z]?[a-z]+|[0-9]+/g)) out.push(m[0].toLowerCase());
|
|
4108
|
+
return out;
|
|
4109
|
+
}
|
|
4110
|
+
|
|
4111
|
+
const KEYWORDS_PER_FILE = 100;
|
|
4112
|
+
const BM25_K1 = 1.2;
|
|
4113
|
+
const BM25_B = 0.75;
|
|
4114
|
+
// Signal weights: file text (BM25 over the stored keywords), the best symbol
|
|
4115
|
+
// label, the path. A label that IS the query outranks everything else.
|
|
4116
|
+
const RANK_LABEL_WEIGHT = 4;
|
|
4117
|
+
const RANK_PATH_WEIGHT = 2;
|
|
4118
|
+
const RANK_SUBSTRING = 0.35;
|
|
4119
|
+
const RANK_DEMOTE = 0.3;
|
|
4120
|
+
const RANK_FILES = 10;
|
|
4121
|
+
const DEMOTED_PATH_RE = /(\.test\.|\.spec\.|\.e2e\.|(^|\/)tests?\/|__tests__|(^|\/)testdata\/|(^|\/)fixtures?\/|(^|\/)examples?\/|(^|\/)benches\/|(^|\/)benchmarks?\/|_test\.go$|(^|\/)test_[^/]*\.py$|(^|\/)conftest\.py$|(^|\/)vendor\/|\.min\.[cm]?js$|(^|\/)docs?\/|\.d\.[cm]?ts$)/;
|
|
4122
|
+
const DEMOTE_EXEMPT_WORDS = new Set(["test", "tests", "testing", "spec", "fixture", "fixtures", "example", "examples", "benchmark", "benchmarks", "vendor", "docs"]);
|
|
4123
|
+
|
|
4124
|
+
// The keywords `kg build` stores on a file node: its most distinctive words
|
|
4125
|
+
// (term frequency × rarity across the repo), as "word:count" pairs.
|
|
4126
|
+
function fileKeywords(tf, idfOf) {
|
|
4127
|
+
return [...tf]
|
|
4128
|
+
.map(([t, c]) => [t, c, c * idfOf(t)])
|
|
4129
|
+
.sort((a, b) => b[2] - a[2] || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0))
|
|
4130
|
+
.slice(0, KEYWORDS_PER_FILE)
|
|
4131
|
+
.map(([t, c]) => t + ":" + c)
|
|
4132
|
+
.join(" ");
|
|
4133
|
+
}
|
|
4134
|
+
|
|
4135
|
+
// Files ranked for a question, best first: [{ rel, score, nodes }], where
|
|
4136
|
+
// `nodes` are the file's symbols that match (best first) or its file node.
|
|
4137
|
+
// Three signals per file — its text, its best-matching symbol label, its path
|
|
4138
|
+
// — and one override: a symbol whose name is the whole query. Tests, docs,
|
|
4139
|
+
// vendored and generated code rank below source unless the question names
|
|
4140
|
+
// them.
|
|
4141
|
+
function rankFiles(graph, question) {
|
|
4142
|
+
const allWords = wordsOf(question);
|
|
4143
|
+
const terms = [...new Set(allWords.filter((t) => t.length >= 2 && !QUERY_STOPWORDS.has(t)))];
|
|
4144
|
+
const qs = terms.length ? terms : [...new Set(allWords)];
|
|
4145
|
+
if (!qs.length) return [];
|
|
4146
|
+
const joined = allWords.join("");
|
|
4147
|
+
const rawQuery = /^\S+$/.test(question.trim()) ? question.trim().replace(/\(\)$/, "") : null;
|
|
4148
|
+
const keepDemoted = qs.some((q) => DEMOTE_EXEMPT_WORDS.has(q));
|
|
4149
|
+
|
|
4150
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4151
|
+
const files = new Map(); // rel -> { node, labels, pathWords, tf, len }
|
|
4152
|
+
const fileOf = (rel) => {
|
|
4153
|
+
let f = files.get(rel);
|
|
4154
|
+
if (!f) files.set(rel, f = { node: null, labels: [], pathWords: new Set(wordsOf(rel)), tf: null, len: 0 });
|
|
4155
|
+
return f;
|
|
4156
|
+
};
|
|
4157
|
+
const topLevel = new Set();
|
|
4158
|
+
for (const e of graph.edges) {
|
|
4159
|
+
if (e.relation !== "contains" && e.relation !== "defines") continue;
|
|
4160
|
+
const a = byId.get(e.source);
|
|
4161
|
+
if (a && a.source_file && a.source_location === "L1" && a.label === path.posix.basename(a.source_file)) topLevel.add(e.target);
|
|
4162
|
+
}
|
|
4163
|
+
for (const n of graph.nodes) {
|
|
4164
|
+
if (!n.source_file) continue;
|
|
4165
|
+
const f = fileOf(n.source_file);
|
|
4166
|
+
if (n.source_location === "L1" && n.label === path.posix.basename(n.source_file) && !f.node) {
|
|
4167
|
+
f.node = n;
|
|
4168
|
+
if (typeof n.terms === "string" && n.terms) {
|
|
4169
|
+
f.tf = new Map();
|
|
4170
|
+
for (const pair of n.terms.split(" ")) {
|
|
4171
|
+
const at = pair.lastIndexOf(":");
|
|
4172
|
+
if (at > 0) f.tf.set(pair.slice(0, at), +pair.slice(at + 1) || 1);
|
|
4173
|
+
}
|
|
4174
|
+
f.len = +n.tokens || 0;
|
|
4175
|
+
}
|
|
4176
|
+
continue;
|
|
4177
|
+
}
|
|
4178
|
+
const raw = String(n.label || "").replace(/\(\)$/, "").replace(/^\./, "");
|
|
4179
|
+
const w = wordsOf(raw);
|
|
4180
|
+
if (w.length) f.labels.push({ id: n.id, w: new Set(w), joined: w.join(""), n: w.length, raw, top: topLevel.has(n.id) && n.type !== "impl" });
|
|
4181
|
+
}
|
|
4182
|
+
// rarity of a word: over the stored keywords, else over labels and paths
|
|
4183
|
+
const df = new Map();
|
|
4184
|
+
let withText = 0, totalLen = 0;
|
|
4185
|
+
for (const f of files.values()) {
|
|
4186
|
+
const seen = new Set(f.pathWords);
|
|
4187
|
+
if (f.tf) { withText++; totalLen += f.len; for (const t of f.tf.keys()) seen.add(t); }
|
|
4188
|
+
for (const L of f.labels) for (const t of L.w) seen.add(t);
|
|
4189
|
+
for (const t of seen) df.set(t, (df.get(t) || 0) + 1);
|
|
4190
|
+
}
|
|
4191
|
+
const N = files.size || 1;
|
|
4192
|
+
const avgLen = withText ? totalLen / withText : 1;
|
|
4193
|
+
const idf = (t) => { const d = df.get(t) || 0; return Math.log(1 + (N - d + 0.5) / (d + 0.5)); };
|
|
4194
|
+
|
|
4195
|
+
const ranked = [];
|
|
4196
|
+
for (const [rel, f] of files) {
|
|
4197
|
+
let lex = 0;
|
|
4198
|
+
if (f.tf) {
|
|
4199
|
+
for (const q of qs) {
|
|
4200
|
+
const tf = f.tf.get(q);
|
|
4201
|
+
if (tf) lex += idf(q) * (tf * (BM25_K1 + 1)) / (tf + BM25_K1 * (1 - BM25_B + BM25_B * (f.len || avgLen) / avgLen));
|
|
4202
|
+
}
|
|
4203
|
+
}
|
|
4204
|
+
let best = 0, exact = 0;
|
|
4205
|
+
const hits = [];
|
|
4206
|
+
for (const L of f.labels) {
|
|
4207
|
+
let matched = 0, weight = 0;
|
|
4208
|
+
for (const q of qs) {
|
|
4209
|
+
if (L.w.has(q)) { matched++; weight += idf(q); }
|
|
4210
|
+
else if (q.length >= 4 && L.joined.includes(q)) { matched += RANK_SUBSTRING; weight += RANK_SUBSTRING * idf(q); }
|
|
4211
|
+
}
|
|
4212
|
+
const isExact = L.joined === joined;
|
|
4213
|
+
if (!matched && !isExact) continue;
|
|
4214
|
+
const coverage = matched / qs.length;
|
|
4215
|
+
const value = weight * coverage * (0.5 + 0.5 * (matched / Math.max(L.n, matched)));
|
|
4216
|
+
let ex = 0;
|
|
4217
|
+
if (isExact) ex = (L.top ? 1 : 0.5) + (rawQuery && L.raw === rawQuery ? 0.5 : 0);
|
|
4218
|
+
if (value > best) best = value;
|
|
4219
|
+
if (ex > exact) exact = ex;
|
|
4220
|
+
hits.push([ex, value, L.id]);
|
|
4221
|
+
}
|
|
4222
|
+
let pathScore = 0;
|
|
4223
|
+
for (const q of qs) if (f.pathWords.has(q)) pathScore += idf(q);
|
|
4224
|
+
const base = lex + RANK_LABEL_WEIGHT * best + RANK_PATH_WEIGHT * pathScore;
|
|
4225
|
+
let score = exact ? 1000 * exact - rel.length * 0.01 + base * 0.001 : base;
|
|
4226
|
+
if (score <= 0) continue;
|
|
4227
|
+
if (!keepDemoted && DEMOTED_PATH_RE.test(rel)) score *= RANK_DEMOTE;
|
|
4228
|
+
hits.sort((a, b) => b[0] - a[0] || b[1] - a[1] || (a[2] < b[2] ? -1 : 1));
|
|
4229
|
+
const nodes = hits.slice(0, 2).map((h) => h[2]);
|
|
4230
|
+
if (!nodes.length && f.node) nodes.push(f.node.id);
|
|
4231
|
+
if (nodes.length) ranked.push({ rel, score, nodes });
|
|
4232
|
+
}
|
|
4233
|
+
ranked.sort((a, b) => b.score - a.score || a.rel.length - b.rel.length || (a.rel < b.rel ? -1 : 1));
|
|
4234
|
+
return ranked;
|
|
4235
|
+
}
|
|
4236
|
+
|
|
4237
|
+
function findNode(graph, label) {
|
|
4238
|
+
const term = searchTokens(label).join(" ");
|
|
4239
|
+
if (!term) return [];
|
|
4240
|
+
const bySource = [], exact = [], prefix = [], substr = [];
|
|
4241
|
+
for (const n of graph.nodes) {
|
|
4242
|
+
const normLabel = nodeNormLabel(n);
|
|
4243
|
+
const tokens = searchTokens(n.label || "").join(" ");
|
|
4244
|
+
if ((n.source_file || "").toLowerCase() === label.toLowerCase()) bySource.push(n.id);
|
|
4245
|
+
else if (term === normLabel || term === tokens || term === n.id.toLowerCase()) exact.push(n.id);
|
|
4246
|
+
else if (normLabel.startsWith(term) || tokens.startsWith(term)) prefix.push(n.id);
|
|
4247
|
+
else if (normLabel.includes(term)) substr.push(n.id);
|
|
4248
|
+
}
|
|
4249
|
+
return [...bySource, ...exact, ...prefix, ...substr];
|
|
4250
|
+
}
|
|
4251
|
+
|
|
4252
|
+
// ---------------------------------------------------------------------------
|
|
4253
|
+
// Commands
|
|
4254
|
+
// ---------------------------------------------------------------------------
|
|
4255
|
+
|
|
4256
|
+
// A bad command line: reported with the usage text and a non-zero exit.
|
|
4257
|
+
class KgUsageError extends Error {}
|
|
4258
|
+
|
|
4259
|
+
// A value flag must be followed by its value (never by another flag); a
|
|
4260
|
+
// missing one used to leave `true` behind for the command to choke on.
|
|
4261
|
+
function parseFlags(args, valueFlags = []) {
|
|
4262
|
+
const flags = {}, positional = [];
|
|
4263
|
+
for (let i = 0; i < args.length; i++) {
|
|
4264
|
+
const a = args[i];
|
|
4265
|
+
if (a.startsWith("--")) {
|
|
4266
|
+
const name = a.slice(2);
|
|
4267
|
+
if (valueFlags.includes(name)) {
|
|
4268
|
+
if (i + 1 >= args.length || args[i + 1].startsWith("--")) throw new KgUsageError(`--${name} requires a value`);
|
|
4269
|
+
flags[name] = flags[name] ? [].concat(flags[name], args[++i]) : args[++i];
|
|
4270
|
+
} else flags[name] = true;
|
|
4271
|
+
} else positional.push(a);
|
|
4272
|
+
}
|
|
4273
|
+
return { flags, positional };
|
|
4274
|
+
}
|
|
4275
|
+
|
|
4276
|
+
function requireGraph(root) {
|
|
4277
|
+
const g = loadGraph(root);
|
|
4278
|
+
if (!g) {
|
|
4279
|
+
console.log("no graph — run `draht-tools kg build` first");
|
|
4280
|
+
return null;
|
|
4281
|
+
}
|
|
4282
|
+
return g;
|
|
4283
|
+
}
|
|
4284
|
+
|
|
4285
|
+
const kgCommands = {};
|
|
4286
|
+
|
|
4287
|
+
kgCommands.build = function (root, args) {
|
|
4288
|
+
const { flags } = parseFlags(args, []);
|
|
4289
|
+
const t0 = Date.now();
|
|
4290
|
+
const graph = buildGraph(root);
|
|
4291
|
+
const jsonPath = writeGraph(root, graph);
|
|
4292
|
+
const reportPath = writeReport(root, graph);
|
|
4293
|
+
const codeNodes = graph.nodes.filter((n) => n.source_file).length;
|
|
4294
|
+
const byConf = { EXTRACTED: 0, INFERRED: 0, AMBIGUOUS: 0 };
|
|
4295
|
+
for (const e of graph.edges) byConf[e.confidence] = (byConf[e.confidence] || 0) + 1;
|
|
4296
|
+
if (!flags.quiet) {
|
|
4297
|
+
console.log(`kg build: ${graph.nodes.length} nodes (${codeNodes} in-repo) · ${graph.edges.length} edges · ${graph.communities.length} communities · ${Date.now() - t0}ms`);
|
|
4298
|
+
const pct = (k) => graph.edges.length ? Math.round((byConf[k] / graph.edges.length) * 100) : 0;
|
|
4299
|
+
console.log(`confidence: ${pct("EXTRACTED")}% EXTRACTED · ${pct("INFERRED")}% INFERRED · ${pct("AMBIGUOUS")}% AMBIGUOUS`);
|
|
4300
|
+
if (graph.idCollisions && graph.idCollisions.length) {
|
|
4301
|
+
console.log(`warning: ${graph.idCollisions.length} id collision(s) — graphify ids.py canonicalization merges these (first wins):`);
|
|
4302
|
+
for (const c of graph.idCollisions.slice(0, 5)) console.log(` ${c.id}: kept ${c.kept}, merged ${c.dropped}`);
|
|
4303
|
+
if (graph.idCollisions.length > 5) console.log(` … and ${graph.idCollisions.length - 5} more`);
|
|
4304
|
+
}
|
|
4305
|
+
if (graph.methodLimitHits && graph.methodLimitHits.length) {
|
|
4306
|
+
console.log(`warning: ${graph.methodLimitHits.length} class(es) declare more than ${MAX_METHODS_PER_CLASS} methods — the rest are not indexed:`);
|
|
4307
|
+
for (const c of graph.methodLimitHits.slice(0, 5)) console.log(` ${c.file}: ${c.name} (${c.declared} methods)`);
|
|
4308
|
+
if (graph.methodLimitHits.length > 5) console.log(` … and ${graph.methodLimitHits.length - 5} more`);
|
|
4309
|
+
}
|
|
4310
|
+
console.log(`→ ${jsonPath}\n→ ${reportPath}`);
|
|
4311
|
+
}
|
|
4312
|
+
};
|
|
4313
|
+
|
|
4314
|
+
kgCommands.query = function (root, args) {
|
|
4315
|
+
const { flags, positional } = parseFlags(args, ["depth", "budget", "context"]);
|
|
4316
|
+
const question = positional.join(" ");
|
|
4317
|
+
if (!question) { console.log('usage: kg query "<question>" [--dfs] [--depth N] [--budget N] [--context C]'); return; }
|
|
4318
|
+
const graph = requireGraph(root);
|
|
4319
|
+
if (!graph) return;
|
|
4320
|
+
// The files that answer the question lead the output, best first, each
|
|
4321
|
+
// through its matching symbols; the traversal then starts from the best
|
|
4322
|
+
// three and adds what they connect to.
|
|
4323
|
+
const ranked = rankFiles(graph, question).slice(0, RANK_FILES);
|
|
4324
|
+
const seeds = [];
|
|
4325
|
+
for (const f of ranked) for (const nid of f.nodes) if (!seeds.includes(nid)) seeds.push(nid);
|
|
4326
|
+
if (!seeds.length) { console.log("No matching nodes found."); return; }
|
|
4327
|
+
const startNodes = [];
|
|
4328
|
+
for (const f of ranked.slice(0, 3)) for (const nid of f.nodes) if (!startNodes.includes(nid)) startNodes.push(nid);
|
|
4329
|
+
const mode = flags.dfs ? "dfs" : "bfs";
|
|
4330
|
+
const depth = Math.min(6, parseInt(flags.depth, 10) || 2);
|
|
4331
|
+
const budget = parseInt(flags.budget, 10) || 2000;
|
|
4332
|
+
const contextFilter = flags.context ? [].concat(flags.context) : null;
|
|
4333
|
+
const t = traverse(graph, startNodes, depth, mode, contextFilter);
|
|
4334
|
+
for (const nid of seeds) t.visited.add(nid);
|
|
4335
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4336
|
+
const headerParts = [`Traversal: ${mode.toUpperCase()} depth=${depth}`, `Start: [${startNodes.map((s) => (byId.get(s) || {}).label || s).join(", ")}]`];
|
|
4337
|
+
if (contextFilter) headerParts.push(`Context: ${contextFilter.join(", ")}`);
|
|
4338
|
+
headerParts.push(`${t.visited.size} nodes found`);
|
|
4339
|
+
console.log(headerParts.join(" | ") + "\n");
|
|
4340
|
+
console.log(subgraphToText(graph, t.visited, t.edgesSeen, t.edges, t.deg, budget, seeds));
|
|
4341
|
+
};
|
|
4342
|
+
|
|
4343
|
+
kgCommands.explain = function (root, args) {
|
|
4344
|
+
const { positional } = parseFlags(args, []);
|
|
4345
|
+
const label = positional.join(" ");
|
|
4346
|
+
if (!label) { console.log('usage: kg explain "<node>"'); return; }
|
|
4347
|
+
const graph = requireGraph(root);
|
|
4348
|
+
if (!graph) return;
|
|
4349
|
+
const matches = findNode(graph, label);
|
|
4350
|
+
if (!matches.length) { console.log(`No node matching '${label}' found.`); return; }
|
|
4351
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4352
|
+
const nid = matches[0];
|
|
4353
|
+
const d = byId.get(nid);
|
|
4354
|
+
const adj = buildAdjacency(graph);
|
|
4355
|
+
console.log(`Node: ${d.label || nid}`);
|
|
4356
|
+
console.log(` ID: ${nid}`);
|
|
4357
|
+
console.log(` Source: ${d.source_file || ""} ${d.source_location || ""}`.trimEnd());
|
|
4358
|
+
console.log(` Type: ${d.type || d.file_type || ""}`);
|
|
4359
|
+
console.log(` Community: ${d.community_name != null ? d.community_name : (d.community != null ? d.community : "")}`);
|
|
4360
|
+
console.log(` Degree: ${adj.deg.get(nid) || 0}`);
|
|
4361
|
+
const connections = [];
|
|
4362
|
+
for (const e of adj.out.get(nid) || []) connections.push(["-->", e.target, e]);
|
|
4363
|
+
for (const e of adj.inn.get(nid) || []) connections.push(["<--", e.source, e]);
|
|
4364
|
+
if (connections.length) {
|
|
4365
|
+
console.log(`\nConnections (${connections.length}):`);
|
|
4366
|
+
connections.sort((a, b) => (adj.deg.get(b[1]) || 0) - (adj.deg.get(a[1]) || 0) || (a[1] < b[1] ? -1 : 1));
|
|
4367
|
+
for (const [arrow, nb, e] of connections.slice(0, 20)) {
|
|
4368
|
+
console.log(` ${arrow} ${(byId.get(nb) || {}).label || nb} [${e.relation}] [${e.confidence}]`);
|
|
4369
|
+
}
|
|
4370
|
+
if (connections.length > 20) console.log(` ... and ${connections.length - 20} more`);
|
|
4371
|
+
}
|
|
4372
|
+
if (d.rationale) console.log(`\nRationale: ${d.rationale.slice(0, 300)}`);
|
|
4373
|
+
};
|
|
4374
|
+
|
|
4375
|
+
kgCommands.path = function (root, args) {
|
|
4376
|
+
const { positional } = parseFlags(args, []);
|
|
4377
|
+
if (positional.length < 2) { console.log('usage: kg path "<source>" "<target>"'); return; }
|
|
4378
|
+
const graph = requireGraph(root);
|
|
4379
|
+
if (!graph) return;
|
|
4380
|
+
const [srcLabel, tgtLabel] = [positional[0], positional[1]];
|
|
4381
|
+
const srcScored = scoreNodes(graph, srcLabel.toLowerCase().split(/\s+/));
|
|
4382
|
+
const tgtScored = scoreNodes(graph, tgtLabel.toLowerCase().split(/\s+/));
|
|
4383
|
+
if (!srcScored.length) { console.log(`No node matching '${srcLabel}' found.`); return; }
|
|
4384
|
+
if (!tgtScored.length) { console.log(`No node matching '${tgtLabel}' found.`); return; }
|
|
4385
|
+
const src = pickScoredEndpoint(graph, srcScored, srcLabel);
|
|
4386
|
+
const tgt = pickScoredEndpoint(graph, tgtScored, tgtLabel);
|
|
4387
|
+
if (src === tgt) { console.log(`'${srcLabel}' and '${tgtLabel}' both resolved to the same node '${src}'. Use a more specific label.`); return; }
|
|
4388
|
+
const { nbrs } = undirectedNeighbors(graph);
|
|
4389
|
+
// External module nodes (no source_file) are import hubs shared by everything —
|
|
4390
|
+
// a "path" through node:fs connects nothing meaningfully, so they can't be transit.
|
|
4391
|
+
const byIdT = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4392
|
+
const isTransit = (id) => id === src || id === tgt || Boolean((byIdT.get(id) || {}).source_file);
|
|
4393
|
+
const prev = new Map([[src, null]]);
|
|
4394
|
+
const q = [src];
|
|
4395
|
+
while (q.length) {
|
|
4396
|
+
const cur = q.shift();
|
|
4397
|
+
if (cur === tgt) break;
|
|
4398
|
+
if (!isTransit(cur)) continue;
|
|
4399
|
+
for (const nb of [...(nbrs.get(cur) || [])].sort()) {
|
|
4400
|
+
if (!prev.has(nb)) { prev.set(nb, cur); q.push(nb); }
|
|
4401
|
+
}
|
|
4402
|
+
}
|
|
4403
|
+
if (!prev.has(tgt)) { console.log(`No path found between '${srcLabel}' and '${tgtLabel}'.`); return; }
|
|
4404
|
+
const chain = [];
|
|
4405
|
+
for (let x = tgt; x != null; x = prev.get(x)) chain.unshift(x);
|
|
4406
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4407
|
+
// directed arrow recovery per hop
|
|
4408
|
+
const dirEdge = new Map();
|
|
4409
|
+
for (const e of graph.edges) dirEdge.set(e.source + "" + e.target, e);
|
|
4410
|
+
let line = (byId.get(chain[0]) || {}).label || chain[0];
|
|
4411
|
+
for (let i = 0; i < chain.length - 1; i++) {
|
|
4412
|
+
const fwd = dirEdge.get(chain[i] + "" + chain[i + 1]);
|
|
4413
|
+
const rev = dirEdge.get(chain[i + 1] + "" + chain[i]);
|
|
4414
|
+
const e = fwd || rev || {};
|
|
4415
|
+
const arrow = fwd ? `--${e.relation} [${e.confidence}]-->` : `<--${e.relation} [${e.confidence}]--`;
|
|
4416
|
+
line += ` ${arrow} ${(byId.get(chain[i + 1]) || {}).label || chain[i + 1]}`;
|
|
4417
|
+
}
|
|
4418
|
+
console.log(`Shortest path (${chain.length - 1} hops): ${line}`);
|
|
4419
|
+
};
|
|
4420
|
+
|
|
4421
|
+
kgCommands.affected = function (root, args) {
|
|
4422
|
+
const { flags, positional } = parseFlags(args, ["depth", "relation"]);
|
|
4423
|
+
const label = positional.join(" ");
|
|
4424
|
+
if (!label) { console.log('usage: kg affected "<node>" [--depth N] [--relation R]'); return; }
|
|
4425
|
+
const graph = requireGraph(root);
|
|
4426
|
+
if (!graph) return;
|
|
4427
|
+
const matches = findNode(graph, label);
|
|
4428
|
+
if (!matches.length) { console.log(`No node matching '${label}' found.`); return; }
|
|
4429
|
+
const start = matches[0];
|
|
4430
|
+
const depth = parseInt(flags.depth, 10) || 2;
|
|
4431
|
+
const relations = new Set(flags.relation ? [].concat(flags.relation) : DEFAULT_AFFECTED_RELATIONS);
|
|
4432
|
+
// reverse traversal: who depends on start (edges pointing AT the affected set)
|
|
4433
|
+
const rin = new Map();
|
|
4434
|
+
for (const e of graph.edges) {
|
|
4435
|
+
if (!relations.has(e.relation)) continue;
|
|
4436
|
+
if (!rin.has(e.target)) rin.set(e.target, []);
|
|
4437
|
+
rin.get(e.target).push(e);
|
|
4438
|
+
}
|
|
4439
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4440
|
+
const hits = [];
|
|
4441
|
+
const seen = new Set([start]);
|
|
4442
|
+
let frontier = [start];
|
|
4443
|
+
for (let d = 1; d <= depth && frontier.length; d++) {
|
|
4444
|
+
const next = [];
|
|
4445
|
+
for (const nid of frontier) {
|
|
4446
|
+
for (const e of (rin.get(nid) || [])) {
|
|
4447
|
+
if (seen.has(e.source)) continue;
|
|
4448
|
+
seen.add(e.source);
|
|
4449
|
+
hits.push({ id: e.source, depth: d, via: e.relation });
|
|
4450
|
+
next.push(e.source);
|
|
4451
|
+
}
|
|
4452
|
+
}
|
|
4453
|
+
frontier = next;
|
|
4454
|
+
}
|
|
4455
|
+
console.log(`Affected by changes to '${(byId.get(start) || {}).label || start}' (depth ${depth}): ${hits.length} nodes`);
|
|
4456
|
+
let curDepth = 0;
|
|
4457
|
+
for (const h of hits) {
|
|
4458
|
+
if (h.depth !== curDepth) { curDepth = h.depth; console.log(`\n depth ${curDepth}:`); }
|
|
4459
|
+
const n = byId.get(h.id) || {};
|
|
4460
|
+
console.log(` ${n.label || h.id} [via ${h.via}] (${n.source_file || ""}${n.source_location ? ":" + n.source_location : ""})`);
|
|
4461
|
+
}
|
|
4462
|
+
};
|
|
4463
|
+
|
|
4464
|
+
kgCommands.stats = function (root) {
|
|
4465
|
+
const graph = requireGraph(root);
|
|
4466
|
+
if (!graph) return;
|
|
4467
|
+
const byConf = {}, byRel = {};
|
|
4468
|
+
for (const e of graph.edges) {
|
|
4469
|
+
byConf[e.confidence] = (byConf[e.confidence] || 0) + 1;
|
|
4470
|
+
byRel[e.relation] = (byRel[e.relation] || 0) + 1;
|
|
4471
|
+
}
|
|
4472
|
+
console.log(`nodes: ${graph.nodes.length} · edges: ${graph.edges.length} · communities: ${graph.communities.length}`);
|
|
4473
|
+
console.log("confidence: " + Object.entries(byConf).sort().map(([k, v]) => `${k}=${v}`).join(" · "));
|
|
4474
|
+
console.log("relations:");
|
|
4475
|
+
for (const [k, v] of Object.entries(byRel).sort((a, b) => b[1] - a[1])) console.log(` ${k}: ${v}`);
|
|
4476
|
+
};
|
|
4477
|
+
|
|
4478
|
+
kgCommands.report = function (root) {
|
|
4479
|
+
const graph = requireGraph(root);
|
|
4480
|
+
if (!graph) return;
|
|
4481
|
+
console.log("→ " + writeReport(root, graph));
|
|
4482
|
+
};
|
|
4483
|
+
|
|
4484
|
+
// ---------------------------------------------------------------------------
|
|
4485
|
+
// KG_REPORT.md — graphify report.py section list
|
|
4486
|
+
// ---------------------------------------------------------------------------
|
|
4487
|
+
|
|
4488
|
+
function writeReport(root, graph) {
|
|
4489
|
+
const adj = buildAdjacency(graph);
|
|
4490
|
+
const communities = graph.communities.length ? graph.communities : [];
|
|
4491
|
+
const gods = godNodes(graph, adj, 10);
|
|
4492
|
+
const surprises = surprisingConnections(graph, adj, 10);
|
|
4493
|
+
const cycles = importCycles(graph);
|
|
4494
|
+
const questions = suggestQuestions(graph, adj, communities, gods);
|
|
4495
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4496
|
+
const byConf = { EXTRACTED: 0, INFERRED: 0, AMBIGUOUS: 0 };
|
|
4497
|
+
for (const e of graph.edges) byConf[e.confidence] = (byConf[e.confidence] || 0) + 1;
|
|
4498
|
+
const pct = (k) => graph.edges.length ? Math.round((byConf[k] / graph.edges.length) * 100) : 0;
|
|
4499
|
+
|
|
4500
|
+
const L = [];
|
|
4501
|
+
L.push(`# Graph Report - ${path.basename(root)}`);
|
|
4502
|
+
L.push("");
|
|
4503
|
+
L.push("_Generated by `draht-tools kg build`. Do not edit; regenerated each build._");
|
|
4504
|
+
L.push("");
|
|
4505
|
+
L.push("## Summary");
|
|
4506
|
+
L.push("");
|
|
4507
|
+
L.push(`${graph.nodes.length} nodes · ${graph.edges.length} edges · ${communities.length} communities`);
|
|
4508
|
+
L.push("");
|
|
4509
|
+
L.push(`${pct("EXTRACTED")}% EXTRACTED · ${pct("INFERRED")}% INFERRED · ${pct("AMBIGUOUS")}% AMBIGUOUS`);
|
|
4510
|
+
L.push("");
|
|
4511
|
+
L.push("## Community Hubs (Navigation)");
|
|
4512
|
+
L.push("");
|
|
4513
|
+
for (const c of communities.slice(0, 15)) L.push(`- **${c.name}** — community ${c.id}, ${c.size} nodes, cohesion ${c.cohesion}`);
|
|
4514
|
+
L.push("");
|
|
4515
|
+
L.push("## God Nodes");
|
|
4516
|
+
L.push("");
|
|
4517
|
+
L.push("| Node | Degree | Source |");
|
|
4518
|
+
L.push("| --- | --- | --- |");
|
|
4519
|
+
for (const g of gods) L.push(`| \`${g.label}\` | ${g.degree} | ${g.source_file || "-"} |`);
|
|
4520
|
+
L.push("");
|
|
4521
|
+
L.push("## Surprising Connections");
|
|
4522
|
+
L.push("");
|
|
4523
|
+
if (surprises.length) {
|
|
4524
|
+
for (const s of surprises) L.push(`- \`${s.source}\` --${s.relation} [${s.confidence}]--> \`${s.target}\` (score ${s.score}): ${s.reasons.join("; ")}`);
|
|
4525
|
+
} else L.push("(none detected)");
|
|
4526
|
+
L.push("");
|
|
4527
|
+
L.push("## Import Cycles");
|
|
4528
|
+
L.push("");
|
|
4529
|
+
if (cycles.length) {
|
|
4530
|
+
for (const cyc of cycles) L.push(`- ${cyc.map((id) => (byId.get(id) || {}).label || id).join(" → ")}`);
|
|
4531
|
+
} else L.push("(none — no file-level import cycles)");
|
|
4532
|
+
L.push("");
|
|
4533
|
+
L.push("## Communities");
|
|
4534
|
+
L.push("");
|
|
4535
|
+
for (const c of communities) {
|
|
4536
|
+
if (c.size < 3) continue;
|
|
4537
|
+
const sample = (c.members || []).slice(0, 8).map((id) => (byId.get(id) || {}).label || id);
|
|
4538
|
+
L.push(`### ${c.name} (community ${c.id})`);
|
|
4539
|
+
L.push("");
|
|
4540
|
+
L.push(`${c.size} nodes · cohesion ${c.cohesion}`);
|
|
4541
|
+
if (sample.length) L.push(`Members: ${sample.map((s) => "`" + s + "`").join(", ")}${c.size > 8 ? " …" : ""}`);
|
|
4542
|
+
L.push("");
|
|
4543
|
+
}
|
|
4544
|
+
L.push("## Ambiguous Edges - Review These");
|
|
4545
|
+
L.push("");
|
|
4546
|
+
const amb = graph.edges.filter((e) => e.confidence === "AMBIGUOUS").slice(0, 15);
|
|
4547
|
+
if (amb.length) {
|
|
4548
|
+
for (const e of amb) L.push(`- \`${(byId.get(e.source) || {}).label || e.source}\` --${e.relation}--> \`${(byId.get(e.target) || {}).label || e.target}\` (${e.source_file || ""}${e.source_location ? ":" + e.source_location : ""})`);
|
|
4549
|
+
} else L.push("(none)");
|
|
4550
|
+
L.push("");
|
|
4551
|
+
L.push("## Knowledge Gaps");
|
|
4552
|
+
L.push("");
|
|
4553
|
+
const isolated = graph.nodes.filter((n) => (adj.deg.get(n.id) || 0) === 0 && n.source_file).slice(0, 10);
|
|
4554
|
+
if (isolated.length) {
|
|
4555
|
+
L.push(`Isolated nodes (${isolated.length} shown):`);
|
|
4556
|
+
for (const n of isolated) L.push(`- \`${n.label}\` (${n.source_file})`);
|
|
4557
|
+
} else L.push("(no isolated nodes)");
|
|
4558
|
+
L.push("");
|
|
4559
|
+
L.push("## Suggested Questions");
|
|
4560
|
+
L.push("");
|
|
4561
|
+
for (const q of questions) L.push(`- ${q}`);
|
|
4562
|
+
L.push("");
|
|
4563
|
+
L.push("---");
|
|
4564
|
+
L.push("Query the graph: `draht-tools kg query \"<question>\"` · `kg explain \"<node>\"` · `kg path \"<a>\" \"<b>\"` · `kg affected \"<node>\"` · `kg export tree|wiki|graphml`.");
|
|
4565
|
+
L.push("");
|
|
4566
|
+
const p = path.join(outDir(root), "KG_REPORT.md");
|
|
4567
|
+
const next = L.join("\n");
|
|
4568
|
+
let prev = null;
|
|
4569
|
+
try { prev = fs.readFileSync(p, "utf-8"); } catch { /* empty */ }
|
|
4570
|
+
if (prev !== next) writeFileAtomic(p, next);
|
|
4571
|
+
return p;
|
|
4572
|
+
}
|
|
4573
|
+
|
|
4574
|
+
// ---------------------------------------------------------------------------
|
|
4575
|
+
// Exports: tree html / wiki / graphml
|
|
4576
|
+
// ---------------------------------------------------------------------------
|
|
4577
|
+
|
|
4578
|
+
kgCommands.export = function (root, args) {
|
|
4579
|
+
const { flags, positional } = parseFlags(args, ["output"]);
|
|
4580
|
+
const format = positional[0];
|
|
4581
|
+
const graph = requireGraph(root);
|
|
4582
|
+
if (!graph) return;
|
|
4583
|
+
// A repeated `--output` keeps the last one.
|
|
4584
|
+
const output = Array.isArray(flags.output) ? flags.output[flags.output.length - 1] : flags.output;
|
|
4585
|
+
if (format === "tree") console.log("→ " + exportTree(root, graph, output));
|
|
4586
|
+
else if (format === "wiki") console.log("→ " + exportWiki(root, graph));
|
|
4587
|
+
else if (format === "graphml") console.log("→ " + exportGraphml(root, graph, output));
|
|
4588
|
+
else console.log("usage: kg export tree|wiki|graphml [--output path]");
|
|
4589
|
+
};
|
|
4590
|
+
|
|
4591
|
+
function escapeHtml(s) {
|
|
4592
|
+
return String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
4593
|
+
}
|
|
4594
|
+
|
|
4595
|
+
function exportTree(root, graph, output) {
|
|
4596
|
+
// Hierarchy: directory → file → symbols, with descendant counts. Self-contained
|
|
4597
|
+
// vanilla-JS collapsible tree (offline parity with graphify's GRAPH_TREE.html).
|
|
4598
|
+
const tree = { name: path.basename(root), children: new Map(), symbols: [] };
|
|
4599
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4600
|
+
const fileSymbols = new Map();
|
|
4601
|
+
for (const e of graph.edges) {
|
|
4602
|
+
if (e.relation !== "contains" && e.relation !== "defines" && e.relation !== "method") continue;
|
|
4603
|
+
const src = byId.get(e.source), tgt = byId.get(e.target);
|
|
4604
|
+
if (!src || !tgt || !tgt.source_file) continue;
|
|
4605
|
+
if (!fileSymbols.has(tgt.source_file)) fileSymbols.set(tgt.source_file, new Set());
|
|
4606
|
+
fileSymbols.get(tgt.source_file).add(tgt.label);
|
|
4607
|
+
}
|
|
4608
|
+
const files = [...new Set(graph.nodes.filter((n) => n.source_file).map((n) => n.source_file))].sort();
|
|
4609
|
+
for (const rel of files) {
|
|
4610
|
+
let cur = tree;
|
|
4611
|
+
const parts = rel.split("/");
|
|
4612
|
+
for (let i = 0; i < parts.length - 1; i++) {
|
|
4613
|
+
if (!cur.children.has(parts[i])) cur.children.set(parts[i], { name: parts[i], children: new Map(), symbols: [] });
|
|
4614
|
+
cur = cur.children.get(parts[i]);
|
|
4615
|
+
}
|
|
4616
|
+
const leafName = parts[parts.length - 1];
|
|
4617
|
+
if (!cur.children.has(leafName)) cur.children.set(leafName, { name: leafName, children: new Map(), symbols: [...(fileSymbols.get(rel) || [])].sort() });
|
|
4618
|
+
}
|
|
4619
|
+
const toObj = (node) => ({ name: node.name, symbols: node.symbols || [], children: [...node.children.values()].sort((a, b) => (a.name < b.name ? -1 : 1)).map(toObj) });
|
|
4620
|
+
const data = toObj(tree);
|
|
4621
|
+
const html = `<!DOCTYPE html><html><head><meta charset="utf-8"><title>${escapeHtml(path.basename(root))} — graph tree</title>
|
|
4622
|
+
<style>
|
|
4623
|
+
body{background:#0d1117;color:#c9d1d9;font-family:ui-monospace,monospace;font-size:13px;margin:0;padding:20px}
|
|
4624
|
+
h1{font-size:15px;color:#58a6ff} .node{margin-left:18px} .lbl{cursor:pointer;padding:1px 4px;border-radius:4px}
|
|
4625
|
+
.lbl:hover{background:#161b22} .dir>.lbl{color:#58a6ff} .file>.lbl{color:#c9d1d9} .sym{color:#7ee787;margin-left:36px}
|
|
4626
|
+
.count{color:#8b949e;font-size:11px;margin-left:6px} .hidden{display:none}
|
|
4627
|
+
button{background:#161b22;color:#c9d1d9;border:1px solid #30363d;border-radius:6px;padding:4px 10px;cursor:pointer;margin-right:6px}
|
|
4628
|
+
</style></head><body>
|
|
4629
|
+
<h1>${escapeHtml(path.basename(root))} — module tree (${files.length} files)</h1>
|
|
4630
|
+
<div><button id="expand">expand all</button><button id="collapse">collapse all</button></div>
|
|
4631
|
+
<div id="tree"></div>
|
|
4632
|
+
<script>
|
|
4633
|
+
var DATA=${JSON.stringify(data)};
|
|
4634
|
+
function count(n){var c=(n.symbols||[]).length;(n.children||[]).forEach(function(k){c+=1+count(k)});return c}
|
|
4635
|
+
function render(n,el,depth){
|
|
4636
|
+
var d=document.createElement('div');d.className='node '+((n.children&&n.children.length)?'dir':'file');
|
|
4637
|
+
var l=document.createElement('div');l.className='lbl';l.textContent=(n.children&&n.children.length? '▸ ':'· ')+n.name;
|
|
4638
|
+
var c=document.createElement('span');c.className='count';c.textContent='('+count(n)+')';l.appendChild(c);
|
|
4639
|
+
d.appendChild(l);
|
|
4640
|
+
var kids=document.createElement('div');kids.className='kids'+(depth>1?' hidden':'');
|
|
4641
|
+
(n.children||[]).forEach(function(k){render(k,kids,depth+1)});
|
|
4642
|
+
(n.symbols||[]).forEach(function(s){var sd=document.createElement('div');sd.className='sym';sd.textContent=s;kids.appendChild(sd)});
|
|
4643
|
+
d.appendChild(kids);
|
|
4644
|
+
l.onclick=function(){kids.classList.toggle('hidden');l.textContent=(kids.classList.contains('hidden')?'▸ ':'▾ ')+n.name;l.appendChild(c)};
|
|
4645
|
+
el.appendChild(d);
|
|
4646
|
+
}
|
|
4647
|
+
render(DATA,document.getElementById('tree'),0);
|
|
4648
|
+
document.getElementById('expand').onclick=function(){document.querySelectorAll('.kids').forEach(function(k){k.classList.remove('hidden')})};
|
|
4649
|
+
document.getElementById('collapse').onclick=function(){document.querySelectorAll('.kids').forEach(function(k){k.classList.add('hidden')})};
|
|
4650
|
+
</script></body></html>`;
|
|
4651
|
+
const p = output || path.join(outDir(root), "GRAPH_TREE.html");
|
|
4652
|
+
writeFileAtomic(p, html);
|
|
4653
|
+
return p;
|
|
4654
|
+
}
|
|
4655
|
+
|
|
4656
|
+
// Wiki page file for a node, from its unique id: labels repeat (two `save()`
|
|
4657
|
+
// methods would write, and link to, the same page). The `node-` prefix keeps
|
|
4658
|
+
// pages clear of index.md and community-N.md; very long ids keep a readable
|
|
4659
|
+
// prefix plus a hash.
|
|
4660
|
+
function wikiPageName(id) {
|
|
4661
|
+
const slug = String(id).replace(/_/g, "-");
|
|
4662
|
+
if (slug.length <= 120) return `node-${slug}.md`;
|
|
4663
|
+
return `node-${slug.slice(0, 100)}-${crypto.createHash("sha1").update(String(id)).digest("hex").slice(0, 10)}.md`;
|
|
4664
|
+
}
|
|
4665
|
+
|
|
4666
|
+
function exportWiki(root, graph) {
|
|
4667
|
+
const dir = path.join(outDir(root), "kg-wiki");
|
|
4668
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
4669
|
+
const adj = buildAdjacency(graph);
|
|
4670
|
+
const byId = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
4671
|
+
const gods = godNodes(graph, adj, 10);
|
|
4672
|
+
const communities = graph.communities;
|
|
4673
|
+
const idx = ["# Knowledge Graph Wiki", "", "_Generated by `draht-tools kg export wiki`._", "", "## Communities", ""];
|
|
4674
|
+
for (const c of communities) {
|
|
4675
|
+
if (c.size < 3) continue;
|
|
4676
|
+
idx.push(`- [${c.name}](community-${c.id}.md) — ${c.size} nodes, cohesion ${c.cohesion}`);
|
|
4677
|
+
}
|
|
4678
|
+
idx.push("", "## God Nodes", "");
|
|
4679
|
+
for (const g of gods) idx.push(`- [${g.label}](${wikiPageName(g.id)}) — degree ${g.degree}`);
|
|
4680
|
+
writeFileAtomic(path.join(dir, "index.md"), idx.join("\n") + "\n");
|
|
4681
|
+
|
|
4682
|
+
for (const c of communities) {
|
|
4683
|
+
if (c.size < 3) continue;
|
|
4684
|
+
const L = [`# ${c.name}`, "", `Community ${c.id} · ${c.size} nodes · cohesion ${c.cohesion}`, "", "## Key Concepts", ""];
|
|
4685
|
+
const members = (c.members || []).map((id) => byId.get(id)).filter(Boolean);
|
|
4686
|
+
members.sort((a, b) => (adj.deg.get(b.id) || 0) - (adj.deg.get(a.id) || 0) || (a.id < b.id ? -1 : 1));
|
|
4687
|
+
for (const m of members.slice(0, 15)) L.push(`- \`${m.label}\` (${m.source_file || "external"}${m.source_location ? ":" + m.source_location : ""})`);
|
|
4688
|
+
L.push("", "## Source Files", "");
|
|
4689
|
+
for (const f of [...new Set(members.map((m) => m.source_file).filter(Boolean))].sort().slice(0, 20)) L.push(`- ${f}`);
|
|
4690
|
+
L.push("", "## Connections by Relation", "");
|
|
4691
|
+
const memberSet = new Set(c.members || []);
|
|
4692
|
+
const byRel = new Map();
|
|
4693
|
+
for (const e of graph.edges) {
|
|
4694
|
+
if (!memberSet.has(e.source) && !memberSet.has(e.target)) continue;
|
|
4695
|
+
if (!byRel.has(e.relation)) byRel.set(e.relation, []);
|
|
4696
|
+
if (byRel.get(e.relation).length < 10) byRel.get(e.relation).push(e);
|
|
4697
|
+
}
|
|
4698
|
+
for (const [rel, es] of [...byRel.entries()].sort()) {
|
|
4699
|
+
L.push(`### ${rel}`, "");
|
|
4700
|
+
for (const e of es) L.push(`- \`${(byId.get(e.source) || {}).label || e.source}\` → \`${(byId.get(e.target) || {}).label || e.target}\` [${e.confidence}]`);
|
|
4701
|
+
L.push("");
|
|
4702
|
+
}
|
|
4703
|
+
writeFileAtomic(path.join(dir, `community-${c.id}.md`), L.join("\n") + "\n");
|
|
4704
|
+
}
|
|
4705
|
+
for (const g of gods) {
|
|
4706
|
+
const L = [`# ${g.label}`, "", `Degree ${g.degree} · ${g.source_file || ""}`, "", "## Relationships", ""];
|
|
4707
|
+
const conns = (adj.out.get(g.id) || []).map((e) => ["→", e.target, e]).concat((adj.inn.get(g.id) || []).map((e) => ["←", e.source, e]));
|
|
4708
|
+
conns.sort((a, b) => (adj.deg.get(b[1]) || 0) - (adj.deg.get(a[1]) || 0));
|
|
4709
|
+
for (const [arrow, nb, e] of conns.slice(0, 30)) L.push(`- ${arrow} \`${(byId.get(nb) || {}).label || nb}\` [${e.relation}] [${e.confidence}]`);
|
|
4710
|
+
L.push("", "## Audit Trail", "", `Extraction: deterministic (draht-tools kg). Source: ${g.source_file || "-"}.`);
|
|
4711
|
+
writeFileAtomic(path.join(dir, wikiPageName(g.id)), L.join("\n") + "\n");
|
|
4712
|
+
}
|
|
4713
|
+
return dir;
|
|
4714
|
+
}
|
|
4715
|
+
|
|
4716
|
+
function exportGraphml(root, graph, output) {
|
|
4717
|
+
const esc = (s) => String(s).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
4718
|
+
const L = ['<?xml version="1.0" encoding="UTF-8"?>',
|
|
4719
|
+
'<graphml xmlns="http://graphml.graphdrawing.org/xmlns">',
|
|
4720
|
+
'<key id="label" for="node" attr.name="label" attr.type="string"/>',
|
|
4721
|
+
'<key id="source_file" for="node" attr.name="source_file" attr.type="string"/>',
|
|
4722
|
+
'<key id="community" for="node" attr.name="community" attr.type="int"/>',
|
|
4723
|
+
'<key id="relation" for="edge" attr.name="relation" attr.type="string"/>',
|
|
4724
|
+
'<key id="confidence" for="edge" attr.name="confidence" attr.type="string"/>',
|
|
4725
|
+
'<graph edgedefault="directed">'];
|
|
4726
|
+
for (const n of graph.nodes) {
|
|
4727
|
+
L.push(`<node id="${esc(n.id)}"><data key="label">${esc(n.label)}</data><data key="source_file">${esc(n.source_file || "")}</data><data key="community">${n.community != null ? n.community : -1}</data></node>`);
|
|
4728
|
+
}
|
|
4729
|
+
for (const e of graph.edges) {
|
|
4730
|
+
L.push(`<edge source="${esc(e.source)}" target="${esc(e.target)}"><data key="relation">${esc(e.relation)}</data><data key="confidence">${esc(e.confidence)}</data></edge>`);
|
|
4731
|
+
}
|
|
4732
|
+
L.push("</graph></graphml>");
|
|
4733
|
+
const p = output || path.join(outDir(root), "graph.graphml");
|
|
4734
|
+
writeFileAtomic(p, L.join("\n"));
|
|
4735
|
+
return p;
|
|
4736
|
+
}
|
|
4737
|
+
|
|
4738
|
+
// ---------------------------------------------------------------------------
|
|
4739
|
+
// CLI
|
|
4740
|
+
// ---------------------------------------------------------------------------
|
|
4741
|
+
|
|
4742
|
+
const KG_HELP = `draht-kg — graphify-parity symbol-level knowledge graph (deterministic, no LLM)
|
|
4743
|
+
|
|
4744
|
+
Usage: draht-tools kg <command> [args]
|
|
4745
|
+
|
|
4746
|
+
build [--quiet] Index the repo → .planning/codebase/graph.json + KG_REPORT.md
|
|
4747
|
+
query "<question>" [--dfs] [--depth N] [--budget N] [--context C]
|
|
4748
|
+
Best files for the question first, then a BFS/DFS subgraph from them (NODE/EDGE output)
|
|
4749
|
+
explain "<node>" Node dump: id, source, community, degree, connections
|
|
4750
|
+
path "<a>" "<b>" Shortest path with directed arrows and confidence
|
|
4751
|
+
affected "<node>" [--depth N] [--relation R]
|
|
4752
|
+
Reverse blast-radius over calls/imports/inherits/…
|
|
4753
|
+
stats Node/edge/relation/confidence counts
|
|
4754
|
+
report Regenerate KG_REPORT.md
|
|
4755
|
+
export tree|wiki|graphml GRAPH_TREE.html / kg-wiki/ / graph.graphml
|
|
4756
|
+
|
|
4757
|
+
Languages: TypeScript, JavaScript, Python, Go, Rust. Nodes are symbols (files,
|
|
4758
|
+
classes, functions(), methods(), types, constants); edges carry graphify's
|
|
4759
|
+
relation set with EXTRACTED/INFERRED/AMBIGUOUS confidence. graph.json is
|
|
4760
|
+
node-link format, directed — loadable by graphify's own tooling.`;
|
|
4761
|
+
|
|
4762
|
+
function kgMain(argv) {
|
|
4763
|
+
const [cmd, ...args] = argv;
|
|
4764
|
+
if (!cmd || cmd === "help" || cmd === "--help" || cmd === "-h") { console.log(KG_HELP); return 0; }
|
|
4765
|
+
if (!kgCommands[cmd]) { console.error(`unknown kg command: ${cmd}\n`); console.log(KG_HELP); return 1; }
|
|
4766
|
+
const root = findRepoRoot(process.cwd());
|
|
4767
|
+
try {
|
|
4768
|
+
kgCommands[cmd](root, args);
|
|
4769
|
+
} catch (err) {
|
|
4770
|
+
if (!(err instanceof KgUsageError)) throw err;
|
|
4771
|
+
console.error(`kg ${cmd}: ${err.message}\n`);
|
|
4772
|
+
console.log(KG_HELP);
|
|
4773
|
+
return 1;
|
|
4774
|
+
}
|
|
4775
|
+
return 0;
|
|
4776
|
+
}
|
|
4777
|
+
|
|
4778
|
+
module.exports = { kgMain, buildGraph, writeGraph, loadGraph, queryTerms, scoreNodes, rankFiles, wordsOf, normalizeId, makeId, parseDecls, maskSource, sortByPosixPath, importCycles, KG_HELP };
|
|
4779
|
+
|
|
4780
|
+
if (require.main === module) {
|
|
4781
|
+
process.exit(kgMain(process.argv.slice(2)));
|
|
4782
|
+
}
|