@hardkeel/benthic 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +121 -0
- package/LICENSE +92 -0
- package/NOTICE +16 -0
- package/README.md +424 -2
- package/action.yml +54 -0
- package/dist/agility.js +80 -0
- package/dist/badge.js +62 -0
- package/dist/baseline.js +81 -0
- package/dist/cbom-diff.js +273 -0
- package/dist/cbom.js +418 -0
- package/dist/cli.js +834 -0
- package/dist/cloud-aws-services.js +375 -0
- package/dist/cloud-aws.js +354 -0
- package/dist/cloud-azure-services.js +73 -0
- package/dist/cloud-azure.js +144 -0
- package/dist/cloud-common.js +242 -0
- package/dist/cloud-gcp-services.js +90 -0
- package/dist/cloud-gcp.js +150 -0
- package/dist/comments.js +138 -0
- package/dist/community-rules.js +37 -0
- package/dist/community.js +30 -0
- package/dist/deps.js +406 -0
- package/dist/der.js +571 -0
- package/dist/evidence-cli.js +235 -0
- package/dist/evidence-live.js +109 -0
- package/dist/evidence.js +512 -0
- package/dist/ignore.js +90 -0
- package/dist/image.js +1224 -0
- package/dist/infra.js +635 -0
- package/dist/inventory.js +277 -0
- package/dist/map-activity.js +24 -0
- package/dist/map-areas.js +60 -0
- package/dist/map-cloud-keys.js +110 -0
- package/dist/map-cloud.js +34 -0
- package/dist/map-ct.js +107 -0
- package/dist/map-host.js +335 -0
- package/dist/map-network.js +520 -0
- package/dist/map-people.js +79 -0
- package/dist/map-rank.js +70 -0
- package/dist/map-report.js +304 -0
- package/dist/map-run.js +327 -0
- package/dist/map-sarif.js +61 -0
- package/dist/map-signin.js +247 -0
- package/dist/map-types.js +19 -0
- package/dist/map-web.js +666 -0
- package/dist/native.js +168 -0
- package/dist/policy.js +221 -0
- package/dist/pqc.js +46 -0
- package/dist/profile.js +65 -0
- package/dist/project.js +928 -0
- package/dist/prose.js +159 -0
- package/dist/protocols.js +207 -0
- package/dist/registry.js +326 -0
- package/dist/report.js +190 -0
- package/dist/rules.js +230 -0
- package/dist/sarif-common.js +30 -0
- package/dist/sarif.js +114 -0
- package/dist/scan.js +601 -0
- package/dist/score.js +124 -0
- package/dist/secrets.js +192 -0
- package/dist/share-config.js +129 -0
- package/dist/share-lists.js +19 -0
- package/dist/share.js +125 -0
- package/dist/sonar.js +83 -0
- package/dist/structure.js +218 -0
- package/dist/suites.js +402 -0
- package/dist/tls-check.js +122 -0
- package/dist/types.js +4 -0
- package/dist/walk.js +78 -0
- package/dist/yaml.js +173 -0
- package/package.json +73 -4
package/dist/project.js
ADDED
|
@@ -0,0 +1,928 @@
|
|
|
1
|
+
// SPDX-License-Identifier: PolyForm-Shield-1.0.0
|
|
2
|
+
// Copyright 2026 Hard Keel (https://hardkeel.com)
|
|
3
|
+
// The project pass: what one file can't see on its own.
|
|
4
|
+
//
|
|
5
|
+
// Constants imported from other files. A value set in one file is often used in another:
|
|
6
|
+
//
|
|
7
|
+
// settings.py KEY_BITS = 1024 Crypto.java public static final String T = "DES/ECB/PKCS5Padding";
|
|
8
|
+
// keys.py from settings import KEY_BITS Store.java Cipher.getInstance(Crypto.T)
|
|
9
|
+
// rsa.generate_private_key(..., key_size=KEY_BITS)
|
|
10
|
+
//
|
|
11
|
+
// Values passed into functions. A function that takes the algorithm or key size as a parameter is
|
|
12
|
+
// only as safe as what its callers pass:
|
|
13
|
+
//
|
|
14
|
+
// def make_key(bits): make_key(1024) # in another file
|
|
15
|
+
// return rsa.generate_private_key(65537, bits)
|
|
16
|
+
//
|
|
17
|
+
// For each file this works out the constants it can see from other files (by the name it uses for
|
|
18
|
+
// them) and, for each function body, the literal values its parameters receive from calls across the
|
|
19
|
+
// project (through up to three functions that pass a parameter on, and from default values when a call
|
|
20
|
+
// leaves the argument out). The scanner then reads those lines with the values filled in. It is careful
|
|
21
|
+
// rather than complete: a call is followed only when it can name exactly one function (a unique name,
|
|
22
|
+
// an imported name, self./this. in the same file, or module.function / Class.method / package.Func),
|
|
23
|
+
// and calls from tests and examples don't count, since they often pass weak values on purpose.
|
|
24
|
+
//
|
|
25
|
+
// Languages: Python, JavaScript and TypeScript, Go, Java, Kotlin and C# for both; C and C++ headers
|
|
26
|
+
// for constants.
|
|
27
|
+
import { depth, isLiteral } from "./structure.js";
|
|
28
|
+
export function familyOf(path) {
|
|
29
|
+
if (/\.(py|pyw)$/i.test(path))
|
|
30
|
+
return "python";
|
|
31
|
+
if (/\.(js|mjs|cjs|jsx|ts|mts|cts|tsx)$/i.test(path) && !/\.d\.[cm]?ts$/i.test(path))
|
|
32
|
+
return "js";
|
|
33
|
+
if (/\.go$/i.test(path))
|
|
34
|
+
return "go";
|
|
35
|
+
if (/\.(java|kt|kts)$/i.test(path))
|
|
36
|
+
return "jvm";
|
|
37
|
+
if (/\.cs$/i.test(path))
|
|
38
|
+
return "cs";
|
|
39
|
+
if (/\.(c|cc|cpp|cxx|h|hh|hpp|m|mm)$/i.test(path))
|
|
40
|
+
return "c";
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const MAX_VALUES = 8;
|
|
44
|
+
const ROUNDS = 3;
|
|
45
|
+
// A parameter only matters on a line that does something with encryption.
|
|
46
|
+
const HINT = /rsa|ecdsa|ecdh|\bdsa|\bdh\b|cipher|getinstance|key_?size|keysize|keylength|key_?bits|modulus|bits\b|curve|md5|sha-?1\b|sha1|\bdes\b|3des|tripledes|rc4|rc2|blowfish|jwt|jws|jose|algorithm|\balgo?\b|\balg\b|sign|generate|crypto|hashlib|digest|hash|ssl|tls|elliptic|secp|prime256|p-?256|p-?384|ed25519|x25519|pkcs/i;
|
|
47
|
+
const HINT_FILE = /rsa|ecdsa|ecdh|cipher|getinstance|key_?size|keysize|modulus|curve|md5|sha1|\bdes\b|rc4|jwt|algorithm|crypto|hashlib|digest|elliptic|secp|signing|signature|ssl|tls/i;
|
|
48
|
+
function dirOf(p) {
|
|
49
|
+
const i = p.lastIndexOf("/");
|
|
50
|
+
return i < 0 ? "" : p.slice(0, i);
|
|
51
|
+
}
|
|
52
|
+
function joinPath(dir, rel) {
|
|
53
|
+
const parts = (dir ? dir.split("/") : []).concat(rel.split("/"));
|
|
54
|
+
const out = [];
|
|
55
|
+
for (const p of parts) {
|
|
56
|
+
if (p === "" || p === ".")
|
|
57
|
+
continue;
|
|
58
|
+
if (p === "..") {
|
|
59
|
+
if (!out.length)
|
|
60
|
+
return "\u0000"; // outside the project
|
|
61
|
+
out.pop();
|
|
62
|
+
}
|
|
63
|
+
else
|
|
64
|
+
out.push(p);
|
|
65
|
+
}
|
|
66
|
+
return out.join("/");
|
|
67
|
+
}
|
|
68
|
+
function baseName(p) {
|
|
69
|
+
return p.slice(p.lastIndexOf("/") + 1).replace(/\.[^.]+$/, "");
|
|
70
|
+
}
|
|
71
|
+
/** Split a call's or a signature's text on its top-level commas. */
|
|
72
|
+
export function splitArgs(text) {
|
|
73
|
+
const out = [];
|
|
74
|
+
let d = 0;
|
|
75
|
+
let quote = null;
|
|
76
|
+
let cur = "";
|
|
77
|
+
for (let i = 0; i < text.length; i++) {
|
|
78
|
+
const c = text[i];
|
|
79
|
+
if (quote) {
|
|
80
|
+
cur += c;
|
|
81
|
+
if (c === "\\")
|
|
82
|
+
cur += text[++i] ?? "";
|
|
83
|
+
else if (c === quote)
|
|
84
|
+
quote = null;
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
if (c === '"' || c === "'" || c === "`")
|
|
88
|
+
quote = c;
|
|
89
|
+
else if (c === "(" || c === "[" || c === "{" || c === "<")
|
|
90
|
+
d++;
|
|
91
|
+
else if (c === ")" || c === "]" || c === "}" || c === ">")
|
|
92
|
+
d = Math.max(0, d - 1);
|
|
93
|
+
if (c === "," && d === 0) {
|
|
94
|
+
out.push(cur.trim());
|
|
95
|
+
cur = "";
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
cur += c;
|
|
99
|
+
}
|
|
100
|
+
if (cur.trim())
|
|
101
|
+
out.push(cur.trim());
|
|
102
|
+
return out;
|
|
103
|
+
}
|
|
104
|
+
/** The text between the bracket at code[idx][col] and its partner, over up to 12 lines. */
|
|
105
|
+
function bracketText(code, idx, col) {
|
|
106
|
+
let d = 0;
|
|
107
|
+
let quote = null;
|
|
108
|
+
let text = "";
|
|
109
|
+
for (let j = idx; j < code.length && j < idx + 12; j++) {
|
|
110
|
+
const line = code[j];
|
|
111
|
+
for (let i = j === idx ? col : 0; i < line.length; i++) {
|
|
112
|
+
const c = line[i];
|
|
113
|
+
if (quote) {
|
|
114
|
+
if (c === "\\") {
|
|
115
|
+
text += c + (line[i + 1] ?? "");
|
|
116
|
+
i++;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (c === quote)
|
|
120
|
+
quote = null;
|
|
121
|
+
text += c;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (c === '"' || c === "'" || c === "`")
|
|
125
|
+
quote = c;
|
|
126
|
+
if (c === "(" || c === "[" || c === "{")
|
|
127
|
+
d++;
|
|
128
|
+
else if (c === ")" || c === "]" || c === "}") {
|
|
129
|
+
d--;
|
|
130
|
+
if (d === 0)
|
|
131
|
+
return { text: text.slice(1), endIdx: j, endCol: i };
|
|
132
|
+
}
|
|
133
|
+
text += c;
|
|
134
|
+
if (text.length > 3000)
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
quote = null; // strings don't run on over lines here
|
|
138
|
+
text += " ";
|
|
139
|
+
}
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const CONTROL = new Set(["if", "for", "while", "switch", "catch", "return", "new", "else", "throw", "function", "await", "typeof", "do", "try", "using", "lock", "foreach", "when", "sizeof", "with", "elif", "super", "this", "synchronized"]);
|
|
143
|
+
function paramList(family, text) {
|
|
144
|
+
const params = [];
|
|
145
|
+
let kwOnly = false;
|
|
146
|
+
for (let part of splitArgs(text)) {
|
|
147
|
+
part = part.replace(/^@\w+(?:\([^)]*\))?\s*/, "").trim();
|
|
148
|
+
if (!part)
|
|
149
|
+
continue;
|
|
150
|
+
if (family === "python") {
|
|
151
|
+
if (part === "*") {
|
|
152
|
+
kwOnly = true;
|
|
153
|
+
continue;
|
|
154
|
+
}
|
|
155
|
+
if (part === "/")
|
|
156
|
+
continue;
|
|
157
|
+
if (part.startsWith("*")) {
|
|
158
|
+
kwOnly = true;
|
|
159
|
+
continue;
|
|
160
|
+
}
|
|
161
|
+
const m = part.match(/^([A-Za-z_]\w*)\s*(?::[^=]+)?(?:=\s*(.+))?$/);
|
|
162
|
+
if (!m)
|
|
163
|
+
return { params, ok: false };
|
|
164
|
+
params.push({ name: m[1], ...(m[2] && isLiteral(m[2]) ? { default: m[2].trim() } : {}), ...(kwOnly ? { kwOnly } : {}) });
|
|
165
|
+
}
|
|
166
|
+
else if (family === "js") {
|
|
167
|
+
if (part.startsWith("..."))
|
|
168
|
+
break;
|
|
169
|
+
const m = part.match(/^(?:(?:public|private|protected|readonly)\s+)*([A-Za-z_$][\w$]*)\??\s*(?::[^=]+)?(?:=\s*(.+))?$/);
|
|
170
|
+
if (!m)
|
|
171
|
+
return { params, ok: false }; // destructuring: { a, b }
|
|
172
|
+
params.push({ name: m[1], ...(m[2] && isLiteral(m[2]) ? { default: m[2].trim() } : {}) });
|
|
173
|
+
}
|
|
174
|
+
else if (family === "go") {
|
|
175
|
+
// a, b int → a and b; bits int; opts ...Option
|
|
176
|
+
const m = part.match(/^([A-Za-z_]\w*)(?:\s+(.+))?$/);
|
|
177
|
+
if (!m)
|
|
178
|
+
return { params, ok: false };
|
|
179
|
+
if (m[2]?.startsWith("..."))
|
|
180
|
+
break;
|
|
181
|
+
params.push({ name: m[1] });
|
|
182
|
+
}
|
|
183
|
+
else if (family === "jvm" && /:/.test(part)) {
|
|
184
|
+
// Kotlin: bits: Int = 2048
|
|
185
|
+
const m = part.match(/^(?:vararg\s+)?(?:va[lr]\s+)?([A-Za-z_]\w*)\s*:[^=]+(?:=\s*(.+))?$/);
|
|
186
|
+
if (!m)
|
|
187
|
+
return { params, ok: false };
|
|
188
|
+
params.push({ name: m[1], ...(m[2] && isLiteral(m[2]) ? { default: m[2].trim() } : {}) });
|
|
189
|
+
}
|
|
190
|
+
else {
|
|
191
|
+
// Java and C#: final int bits; String... xs; int bits = 2048 (C#); ref/out/in/params
|
|
192
|
+
if (/\.\.\.|^params\s/.test(part))
|
|
193
|
+
break;
|
|
194
|
+
const m = part.match(/^(?:(?:final|ref|out|in|this|scoped)\s+)*[A-Za-z_][\w<>\[\],.?\s]*?\s+([A-Za-z_]\w*)\s*(?:=\s*(.+))?$/);
|
|
195
|
+
if (!m)
|
|
196
|
+
return { params, ok: false };
|
|
197
|
+
params.push({ name: m[1], ...(m[2] && isLiteral(m[2]) ? { default: m[2].trim() } : {}) });
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return { params, ok: true };
|
|
201
|
+
}
|
|
202
|
+
/** The body of a brace language function whose parameters end at code[idx][col]. */
|
|
203
|
+
function braceBody(code, idx, col) {
|
|
204
|
+
// The opening brace on this line or the next two, before any ; (an interface or abstract method).
|
|
205
|
+
for (let j = idx; j < code.length && j <= idx + 2; j++) {
|
|
206
|
+
const from = j === idx ? col + 1 : 0;
|
|
207
|
+
const rest = code[j].slice(from);
|
|
208
|
+
const semi = rest.search(/;/);
|
|
209
|
+
const brace = rest.indexOf("{");
|
|
210
|
+
const arrow = rest.search(/=>/);
|
|
211
|
+
if (brace < 0 || (semi >= 0 && semi < brace)) {
|
|
212
|
+
if (semi >= 0)
|
|
213
|
+
return null;
|
|
214
|
+
// JS arrow with an expression body, Kotlin "= expr": the rest of the statement.
|
|
215
|
+
if (arrow >= 0 || /^\s*=(?!=)/.test(rest))
|
|
216
|
+
return { start: j, end: Math.min(code.length - 1, j + 3) };
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
let d = depth(code[j].slice(from + brace));
|
|
220
|
+
if (d <= 0)
|
|
221
|
+
return { start: j, end: j };
|
|
222
|
+
let k = j;
|
|
223
|
+
while (d > 0 && k + 1 < code.length && k < j + 600) {
|
|
224
|
+
k++;
|
|
225
|
+
d += depth(code[k]);
|
|
226
|
+
}
|
|
227
|
+
return d <= 0 ? { start: j, end: k } : null;
|
|
228
|
+
}
|
|
229
|
+
return null;
|
|
230
|
+
}
|
|
231
|
+
function pythonBody(code, defIdx, sigEnd) {
|
|
232
|
+
const indent = code[defIdx].match(/^\s*/)[0].length;
|
|
233
|
+
// def f(x): return g(x) — the body is on the same line.
|
|
234
|
+
if (/:\s*\S/.test(code[sigEnd].slice(code[sigEnd].lastIndexOf(")"))))
|
|
235
|
+
return { start: sigEnd, end: sigEnd };
|
|
236
|
+
let end = sigEnd;
|
|
237
|
+
for (let j = sigEnd + 1; j < code.length && j < sigEnd + 600; j++) {
|
|
238
|
+
if (!code[j].trim())
|
|
239
|
+
continue;
|
|
240
|
+
if (code[j].match(/^\s*/)[0].length <= indent)
|
|
241
|
+
break;
|
|
242
|
+
end = j;
|
|
243
|
+
}
|
|
244
|
+
return end > sigEnd ? { start: sigEnd + 1, end } : null;
|
|
245
|
+
}
|
|
246
|
+
const DEF_PATTERNS = {
|
|
247
|
+
python: [/^\s*(?:async\s+)?def\s+([A-Za-z_]\w*)\s*\(/],
|
|
248
|
+
js: [
|
|
249
|
+
/(?:^|[^\w$.])(?:export\s+)?(?:default\s+)?(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)\s*(?:<[^>()]*>)?\s*\(/,
|
|
250
|
+
/^\s*(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*(?::[^=]+)?=\s*(?:async\s+)?(?:function\s*\*?\s*[\w$]*\s*)?\(/,
|
|
251
|
+
/^\s*(?:(?:public|private|protected|static|async|override|readonly|get|set)\s+)*([A-Za-z_$][\w$]*)\s*(?:<[^>()]*>)?\s*\((?=.*\)\s*(?::\s*[^{;=]+)?\{\s*$)/,
|
|
252
|
+
],
|
|
253
|
+
go: [/^\s*func\s+(?:\([^)]*\)\s*)?([A-Za-z_]\w*)\s*(?:\[[^\]]*\])?\s*\(/],
|
|
254
|
+
jvm: [
|
|
255
|
+
/^\s*(?:(?:public|private|protected|internal|override|open|suspend|inline|operator|infix|tailrec|abstract|final)\s+)*fun\s+(?:<[^>]+>\s*)?(?:[\w.]+\.)?([A-Za-z_]\w*)\s*\(/,
|
|
256
|
+
/^\s*(?:@\w+(?:\([^)]*\))?\s+)*(?:(?:public|private|protected|static|final|abstract|synchronized|native|default|strictfp)\s+)*(?:<[^>]+>\s+)?[A-Za-z_][\w<>\[\],.?]*(?:\s*\[\s*\])*\s+([A-Za-z_]\w*)\s*\(/,
|
|
257
|
+
],
|
|
258
|
+
cs: [/^\s*(?:\[[^\]]*\]\s*)*(?:(?:public|private|protected|internal|static|async|override|virtual|sealed|abstract|extern|unsafe|new|partial)\s+)*(?:<[^>]+>\s+)?[A-Za-z_][\w<>\[\],.?]*(?:\s*\[\s*\])*\s+([A-Za-z_]\w*)\s*(?:<[^>]*>)?\s*\(/],
|
|
259
|
+
};
|
|
260
|
+
function classAt(code, idx, family) {
|
|
261
|
+
const re = family === "python" ? /^\s*class\s+([A-Za-z_]\w*)/ : /\b(?:class|object|interface|struct|record|enum)\s+([A-Za-z_]\w*)/;
|
|
262
|
+
for (let j = idx; j >= 0 && j > idx - 2000; j--) {
|
|
263
|
+
const m = code[j].match(re);
|
|
264
|
+
if (m) {
|
|
265
|
+
if (family === "python" && code[j].match(/^\s*/)[0].length >= code[idx].match(/^\s*/)[0].length)
|
|
266
|
+
continue;
|
|
267
|
+
return m[1];
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
return undefined;
|
|
271
|
+
}
|
|
272
|
+
/** Lines inside Python triple-quoted strings (docstrings), which hold examples, not code. */
|
|
273
|
+
const DOC_CACHE = new WeakMap();
|
|
274
|
+
export function docstringLines(path, code) {
|
|
275
|
+
let out = DOC_CACHE.get(code);
|
|
276
|
+
if (out)
|
|
277
|
+
return out;
|
|
278
|
+
out = new Set();
|
|
279
|
+
DOC_CACHE.set(code, out);
|
|
280
|
+
if (familyOf(path) !== "python")
|
|
281
|
+
return out;
|
|
282
|
+
let open = null;
|
|
283
|
+
code.forEach((l, i) => {
|
|
284
|
+
let at = 0;
|
|
285
|
+
if (open)
|
|
286
|
+
out.add(i);
|
|
287
|
+
for (;;) {
|
|
288
|
+
if (open) {
|
|
289
|
+
const end = l.indexOf(open, at);
|
|
290
|
+
if (end < 0)
|
|
291
|
+
break;
|
|
292
|
+
open = null;
|
|
293
|
+
at = end + 3;
|
|
294
|
+
}
|
|
295
|
+
else {
|
|
296
|
+
const m = /"{3}|'{3}/.exec(l.slice(at));
|
|
297
|
+
if (!m)
|
|
298
|
+
break;
|
|
299
|
+
open = m[0];
|
|
300
|
+
at += m.index + 3;
|
|
301
|
+
out.add(i);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
});
|
|
305
|
+
return out;
|
|
306
|
+
}
|
|
307
|
+
export function functionDefs(f) {
|
|
308
|
+
const family = familyOf(f.path);
|
|
309
|
+
if (!family || family === "c")
|
|
310
|
+
return [];
|
|
311
|
+
const out = [];
|
|
312
|
+
const code = f.code;
|
|
313
|
+
const patterns = DEF_PATTERNS[family];
|
|
314
|
+
for (let idx = 0; idx < code.length; idx++) {
|
|
315
|
+
const line = code[idx];
|
|
316
|
+
if (line.length > 600 || !line.includes("("))
|
|
317
|
+
continue;
|
|
318
|
+
for (const re of patterns) {
|
|
319
|
+
const m = line.match(re);
|
|
320
|
+
if (!m || CONTROL.has(m[1]))
|
|
321
|
+
continue;
|
|
322
|
+
if (family !== "python" && /;\s*$/.test(line))
|
|
323
|
+
continue; // a call or a declaration with no body
|
|
324
|
+
if (family === "jvm" && /^\s*(?:return|new|throw|else)\b/.test(line))
|
|
325
|
+
continue;
|
|
326
|
+
const open = (m.index ?? 0) + m[0].length - 1;
|
|
327
|
+
const sig = bracketText(code, idx, open);
|
|
328
|
+
if (!sig)
|
|
329
|
+
continue;
|
|
330
|
+
const { params, ok } = paramList(family, sig.text);
|
|
331
|
+
if (!ok || !params.length)
|
|
332
|
+
continue;
|
|
333
|
+
const body = family === "python" ? pythonBody(code, idx, sig.endIdx) : braceBody(code, sig.endIdx, sig.endCol);
|
|
334
|
+
if (!body)
|
|
335
|
+
continue;
|
|
336
|
+
const self = family === "python" && /^(self|cls)$/.test(params[0].name);
|
|
337
|
+
out.push({ file: f.path, family, name: m[1], owner: classAt(code, idx, family), params: self ? params.slice(1) : params, self, defIdx: idx, start: body.start, end: body.end });
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
return out;
|
|
342
|
+
}
|
|
343
|
+
function buildIndex(files, goMods) {
|
|
344
|
+
const pyModules = new Map();
|
|
345
|
+
const goPackages = new Map();
|
|
346
|
+
const classes = new Map();
|
|
347
|
+
const add = (m, k, v) => {
|
|
348
|
+
const l = m.get(k);
|
|
349
|
+
if (l)
|
|
350
|
+
l.push(v);
|
|
351
|
+
else
|
|
352
|
+
m.set(k, [v]);
|
|
353
|
+
};
|
|
354
|
+
for (const f of files) {
|
|
355
|
+
const fam = familyOf(f.path);
|
|
356
|
+
if (fam === "python") {
|
|
357
|
+
const parts = f.path.replace(/\.pyw?$/i, "").split("/");
|
|
358
|
+
if (parts[parts.length - 1] === "__init__")
|
|
359
|
+
parts.pop();
|
|
360
|
+
for (let i = 0; i < parts.length; i++)
|
|
361
|
+
add(pyModules, parts.slice(i).join("."), f.path);
|
|
362
|
+
}
|
|
363
|
+
else if (fam === "go") {
|
|
364
|
+
const pkg = f.code.slice(0, 60).map((l) => l.match(/^\s*package\s+([A-Za-z_]\w*)/)).find(Boolean);
|
|
365
|
+
if (pkg && pkg[1] !== "main" && !goPackages.has(dirOf(f.path)))
|
|
366
|
+
goPackages.set(dirOf(f.path), pkg[1]);
|
|
367
|
+
else if (pkg && !goPackages.has(dirOf(f.path)))
|
|
368
|
+
goPackages.set(dirOf(f.path), pkg[1]);
|
|
369
|
+
}
|
|
370
|
+
else if (fam === "jvm" || fam === "cs") {
|
|
371
|
+
const names = new Set([baseName(f.path)]);
|
|
372
|
+
for (const l of f.code) {
|
|
373
|
+
const m = l.match(/^\s*(?:(?:public|internal|static|final|abstract|sealed|partial|data|open)\s+)*(?:class|interface|object|enum|record|struct)\s+([A-Za-z_]\w*)/);
|
|
374
|
+
if (m)
|
|
375
|
+
names.add(m[1]);
|
|
376
|
+
}
|
|
377
|
+
for (const n of names)
|
|
378
|
+
add(classes, `${fam}:${n}`, f.path);
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
return { files: new Map(files.map((f) => [f.path, f])), pyModules, goMods, goPackages, classes };
|
|
382
|
+
}
|
|
383
|
+
function pyResolve(ix, from, mod) {
|
|
384
|
+
if (mod.startsWith(".")) {
|
|
385
|
+
const dots = mod.match(/^\.+/)[0].length;
|
|
386
|
+
let dir = dirOf(from);
|
|
387
|
+
for (let i = 1; i < dots; i++)
|
|
388
|
+
dir = dirOf(dir);
|
|
389
|
+
const rest = mod.slice(dots).split(".").filter(Boolean).join("/");
|
|
390
|
+
const base = rest ? joinPath(dir, rest) : dir;
|
|
391
|
+
for (const c of [`${base}.py`, `${base}/__init__.py`])
|
|
392
|
+
if (ix.files.has(c))
|
|
393
|
+
return c;
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
const hit = ix.pyModules.get(mod);
|
|
397
|
+
return hit && hit.length === 1 ? hit[0] : null;
|
|
398
|
+
}
|
|
399
|
+
function jsResolve(ix, from, spec) {
|
|
400
|
+
if (!spec.startsWith("."))
|
|
401
|
+
return null;
|
|
402
|
+
const base = joinPath(dirOf(from), spec);
|
|
403
|
+
const stems = [base, base.replace(/\.(m|c)?js$/, (_, x) => `.${x ?? ""}ts`), base.replace(/\.jsx$/, ".tsx")];
|
|
404
|
+
for (const s of stems) {
|
|
405
|
+
for (const ext of ["", ".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".mts", ".cts", "/index.ts", "/index.tsx", "/index.js", "/index.mjs"]) {
|
|
406
|
+
if (ix.files.has(s + ext))
|
|
407
|
+
return s + ext;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
return null;
|
|
411
|
+
}
|
|
412
|
+
function goPackageFiles(ix, dir) {
|
|
413
|
+
const out = [];
|
|
414
|
+
for (const p of ix.files.keys())
|
|
415
|
+
if (dirOf(p) === dir && /\.go$/.test(p) && !/_test\.go$/.test(p))
|
|
416
|
+
out.push(p);
|
|
417
|
+
return out;
|
|
418
|
+
}
|
|
419
|
+
function importsOf(ix, f) {
|
|
420
|
+
const imp = { names: new Map(), qualifiers: new Map(), star: [] };
|
|
421
|
+
const fam = familyOf(f.path);
|
|
422
|
+
const code = f.code;
|
|
423
|
+
const joinFrom = (idx, stop) => {
|
|
424
|
+
let t = code[idx];
|
|
425
|
+
for (let j = idx + 1; j < code.length && j < idx + 40 && !stop.test(t); j++)
|
|
426
|
+
t += " " + code[j].trim();
|
|
427
|
+
return t;
|
|
428
|
+
};
|
|
429
|
+
if (fam === "python") {
|
|
430
|
+
for (let i = 0; i < code.length; i++) {
|
|
431
|
+
const l = code[i];
|
|
432
|
+
if (!/^\s*(from|import)\s/.test(l))
|
|
433
|
+
continue;
|
|
434
|
+
let m = l.match(/^\s*from\s+([\w.]+)\s+import\s+(.+)$/);
|
|
435
|
+
if (m) {
|
|
436
|
+
const text = m[2].includes("(") && !m[2].includes(")") ? joinFrom(i, /\)/).replace(/^.*?import\s+/, "") : m[2];
|
|
437
|
+
const target = pyResolve(ix, f.path, m[1]);
|
|
438
|
+
for (const part of text.replace(/[()]/g, "").split(",")) {
|
|
439
|
+
const a = part.trim().match(/^([A-Za-z_]\w*)(?:\s+as\s+([A-Za-z_]\w*))?$/);
|
|
440
|
+
if (part.trim() === "*" && target)
|
|
441
|
+
imp.star.push(target);
|
|
442
|
+
if (!a)
|
|
443
|
+
continue;
|
|
444
|
+
const local = a[2] ?? a[1];
|
|
445
|
+
// from pkg import submodule
|
|
446
|
+
const sub = pyResolve(ix, f.path, m[1].endsWith(".") ? m[1] + a[1] : `${m[1]}.${a[1]}`);
|
|
447
|
+
if (sub)
|
|
448
|
+
imp.qualifiers.set(local, [sub]);
|
|
449
|
+
else if (target)
|
|
450
|
+
imp.names.set(local, { file: target, name: a[1] });
|
|
451
|
+
}
|
|
452
|
+
continue;
|
|
453
|
+
}
|
|
454
|
+
m = l.match(/^\s*import\s+(.+)$/);
|
|
455
|
+
if (m) {
|
|
456
|
+
for (const part of m[1].split(",")) {
|
|
457
|
+
const a = part.trim().match(/^([\w.]+)(?:\s+as\s+([A-Za-z_]\w*))?$/);
|
|
458
|
+
if (!a)
|
|
459
|
+
continue;
|
|
460
|
+
const target = pyResolve(ix, f.path, a[1]);
|
|
461
|
+
if (target)
|
|
462
|
+
imp.qualifiers.set(a[2] ?? a[1], [target]);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
else if (fam === "js") {
|
|
468
|
+
for (let i = 0; i < code.length; i++) {
|
|
469
|
+
const l = code[i];
|
|
470
|
+
if (/^\s*import\s/.test(l) && !/^\s*import\s+type\s/.test(l)) {
|
|
471
|
+
const t = joinFrom(i, /\bfrom\s*["']|^\s*import\s*["']/);
|
|
472
|
+
const spec = t.match(/\bfrom\s*["']([^"']+)["']/)?.[1];
|
|
473
|
+
const target = spec ? jsResolve(ix, f.path, spec) : null;
|
|
474
|
+
if (!target)
|
|
475
|
+
continue;
|
|
476
|
+
const ns = t.match(/\*\s+as\s+([A-Za-z_$][\w$]*)/);
|
|
477
|
+
if (ns)
|
|
478
|
+
imp.qualifiers.set(ns[1], [target]);
|
|
479
|
+
const named = t.match(/\{([^}]*)\}/);
|
|
480
|
+
if (named) {
|
|
481
|
+
for (const part of named[1].split(",")) {
|
|
482
|
+
const a = part.trim().replace(/^type\s+/, "").match(/^([A-Za-z_$][\w$]*)(?:\s+as\s+([A-Za-z_$][\w$]*))?$/);
|
|
483
|
+
if (a)
|
|
484
|
+
imp.names.set(a[2] ?? a[1], { file: target, name: a[1] });
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
continue;
|
|
488
|
+
}
|
|
489
|
+
const r = l.match(/^\s*(?:const|let|var)\s+(\{[^}]*\}|[A-Za-z_$][\w$]*)\s*=\s*require\(\s*["']([^"']+)["']\s*\)/);
|
|
490
|
+
if (r) {
|
|
491
|
+
const target = jsResolve(ix, f.path, r[2]);
|
|
492
|
+
if (!target)
|
|
493
|
+
continue;
|
|
494
|
+
if (r[1].startsWith("{")) {
|
|
495
|
+
for (const part of r[1].slice(1, -1).split(",")) {
|
|
496
|
+
const a = part.trim().match(/^([A-Za-z_$][\w$]*)(?:\s*:\s*([A-Za-z_$][\w$]*))?$/);
|
|
497
|
+
if (a)
|
|
498
|
+
imp.names.set(a[2] ?? a[1], { file: target, name: a[1] });
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
else
|
|
502
|
+
imp.qualifiers.set(r[1], [target]);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
else if (fam === "go") {
|
|
507
|
+
const dir = dirOf(f.path);
|
|
508
|
+
// The same package: the other files in the folder.
|
|
509
|
+
imp.star.push(...goPackageFiles(ix, dir).filter((p) => p !== f.path));
|
|
510
|
+
const paths = [];
|
|
511
|
+
for (let i = 0; i < code.length && i < 400; i++) {
|
|
512
|
+
const one = code[i].match(/^\s*import\s+(?:([A-Za-z_]\w*|\.)\s+)?"([^"]+)"/);
|
|
513
|
+
if (one)
|
|
514
|
+
paths.push({ alias: one[1], path: one[2] });
|
|
515
|
+
if (/^\s*import\s*\(\s*$/.test(code[i])) {
|
|
516
|
+
for (let j = i + 1; j < code.length && !/^\s*\)/.test(code[j]); j++) {
|
|
517
|
+
const m = code[j].match(/^\s*(?:([A-Za-z_]\w*|\.)\s+)?"([^"]+)"/);
|
|
518
|
+
if (m)
|
|
519
|
+
paths.push({ alias: m[1], path: m[2] });
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
for (const p of paths) {
|
|
524
|
+
const mod = ix.goMods.filter((g) => p.path === g.module || p.path.startsWith(g.module + "/")).sort((a, b) => b.module.length - a.module.length)[0];
|
|
525
|
+
if (!mod)
|
|
526
|
+
continue;
|
|
527
|
+
const target = joinPath(mod.dir, p.path.slice(mod.module.length).replace(/^\//, ""));
|
|
528
|
+
const pkgFiles = goPackageFiles(ix, target);
|
|
529
|
+
if (!pkgFiles.length)
|
|
530
|
+
continue;
|
|
531
|
+
if (p.alias === ".")
|
|
532
|
+
imp.star.push(...pkgFiles);
|
|
533
|
+
else if (p.alias !== "_")
|
|
534
|
+
imp.qualifiers.set(p.alias ?? ix.goPackages.get(target) ?? p.path.split("/").pop(), pkgFiles);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
else if (fam === "jvm" || fam === "cs") {
|
|
538
|
+
for (const l of code.slice(0, 400)) {
|
|
539
|
+
const s = fam === "jvm" ? l.match(/^\s*import\s+static\s+([\w.]+)\.([A-Za-z_]\w*|\*)\s*;?\s*$/) : l.match(/^\s*using\s+static\s+([\w.]+)\s*;/);
|
|
540
|
+
if (!s)
|
|
541
|
+
continue;
|
|
542
|
+
const cls = fam === "jvm" ? s[1] : s[1];
|
|
543
|
+
const simple = cls.split(".").pop();
|
|
544
|
+
const byPath = [...ix.files.keys()].find((p) => p.endsWith(`/${cls.split(".").join("/")}.java`) || p.endsWith(`/${cls.split(".").join("/")}.kt`));
|
|
545
|
+
const hits = byPath ? [byPath] : (ix.classes.get(`${fam}:${simple}`) ?? []);
|
|
546
|
+
if (hits.length !== 1)
|
|
547
|
+
continue;
|
|
548
|
+
if (fam === "cs" || s[2] === "*")
|
|
549
|
+
imp.star.push(hits[0]);
|
|
550
|
+
else
|
|
551
|
+
imp.names.set(s[2], { file: hits[0], name: s[2] });
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
else if (fam === "c") {
|
|
555
|
+
for (const l of code.slice(0, 400)) {
|
|
556
|
+
const m = l.match(/^\s*#\s*include\s+"([^"]+)"/);
|
|
557
|
+
if (!m)
|
|
558
|
+
continue;
|
|
559
|
+
const near = joinPath(dirOf(f.path), m[1]);
|
|
560
|
+
if (ix.files.has(near))
|
|
561
|
+
imp.star.push(near);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
return imp;
|
|
565
|
+
}
|
|
566
|
+
// ---------- constants other files can see ----------
|
|
567
|
+
function braceDepths(code) {
|
|
568
|
+
const out = [];
|
|
569
|
+
let d = 0;
|
|
570
|
+
for (const l of code) {
|
|
571
|
+
out.push(d);
|
|
572
|
+
let quote = null;
|
|
573
|
+
for (let i = 0; i < l.length; i++) {
|
|
574
|
+
const c = l[i];
|
|
575
|
+
if (quote) {
|
|
576
|
+
if (c === "\\")
|
|
577
|
+
i++;
|
|
578
|
+
else if (c === quote)
|
|
579
|
+
quote = null;
|
|
580
|
+
continue;
|
|
581
|
+
}
|
|
582
|
+
if (c === '"' || c === "'" || c === "`")
|
|
583
|
+
quote = c;
|
|
584
|
+
else if (c === "{")
|
|
585
|
+
d++;
|
|
586
|
+
else if (c === "}")
|
|
587
|
+
d = Math.max(0, d - 1);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
return out;
|
|
591
|
+
}
|
|
592
|
+
/** The constants a file offers to the others: module level, exported, public static final. */
|
|
593
|
+
function exportsOf(f, cache) {
|
|
594
|
+
const hit = cache.get(f.path);
|
|
595
|
+
if (hit)
|
|
596
|
+
return hit;
|
|
597
|
+
const out = new Map();
|
|
598
|
+
cache.set(f.path, out);
|
|
599
|
+
if (f.sideline)
|
|
600
|
+
return out;
|
|
601
|
+
const fam = familyOf(f.path);
|
|
602
|
+
const depths = fam === "go" || fam === "c" ? braceDepths(f.code) : [];
|
|
603
|
+
const exportList = new Set();
|
|
604
|
+
if (fam === "js") {
|
|
605
|
+
// export { A, B as C }; module.exports = { A, B }; exports.A = A; module.exports.A = A
|
|
606
|
+
for (const l of f.code) {
|
|
607
|
+
const m = l.match(/^\s*(?:export|module\.exports\s*=)\s*\{([^}]*)\}\s*;?\s*$/);
|
|
608
|
+
if (m)
|
|
609
|
+
for (const n of m[1].split(","))
|
|
610
|
+
exportList.add(n.trim().split(/\s+as\s+|\s*:/)[0]);
|
|
611
|
+
const e = l.match(/^\s*(?:module\.)?exports\.([A-Za-z_$][\w$]*)\s*=\s*([A-Za-z_$][\w$]*)\s*;?\s*$/);
|
|
612
|
+
if (e && e[1] === e[2])
|
|
613
|
+
exportList.add(e[1]);
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
for (const [name, k] of f.consts) {
|
|
617
|
+
const text = f.code[k.line - 1] ?? "";
|
|
618
|
+
let ok = false;
|
|
619
|
+
if (fam === "python")
|
|
620
|
+
ok = /^\S/.test(text) && !docstringLines(f.path, f.code).has(k.line - 1);
|
|
621
|
+
else if (fam === "js")
|
|
622
|
+
ok = /^export\s/.test(text) || (/^(?:const|let|var)\s/.test(text) && exportList.has(name));
|
|
623
|
+
else if (fam === "go")
|
|
624
|
+
ok = depths[k.line - 1] === 0 && !/:=/.test(text);
|
|
625
|
+
else if (fam === "jvm")
|
|
626
|
+
ok = !/\bprivate\b/.test(text) && ((/\bstatic\b/.test(text) && /\bfinal\b/.test(text)) || /\bconst\s+val\b/.test(text) || (/^\S/.test(text) && /^\s*(?:val|const)\b/.test(text)));
|
|
627
|
+
else if (fam === "cs")
|
|
628
|
+
ok = !/\bprivate\b/.test(text) && (/\bconst\b/.test(text) || (/\bstatic\b/.test(text) && /\breadonly\b/.test(text))) && /\b(public|internal)\b/.test(text);
|
|
629
|
+
else if (fam === "c")
|
|
630
|
+
ok = depths[k.line - 1] === 0;
|
|
631
|
+
if (ok)
|
|
632
|
+
out.set(name, { ...k, name, file: f.path, via: [{ file: f.path, line: k.line, note: `${name} = ${k.value}` }] });
|
|
633
|
+
}
|
|
634
|
+
return out;
|
|
635
|
+
}
|
|
636
|
+
function argValue(arg, f, ctx) {
|
|
637
|
+
const v = arg.trim();
|
|
638
|
+
if (isLiteral(v))
|
|
639
|
+
return { value: v, line: 0 };
|
|
640
|
+
const k = f.consts.get(v) ?? ctx.imported.get(v);
|
|
641
|
+
if (k)
|
|
642
|
+
return k;
|
|
643
|
+
return /^[A-Za-z_$][\w$]*$/.test(v) ? "param" : null;
|
|
644
|
+
}
|
|
645
|
+
/** Each argument bound to a parameter: positional, then Python and Kotlin keyword, C# named. */
|
|
646
|
+
function bindArgs(def, args) {
|
|
647
|
+
const bound = new Map();
|
|
648
|
+
let pos = 0;
|
|
649
|
+
for (const a of args) {
|
|
650
|
+
if (/^\*|^\.\.\./.test(a))
|
|
651
|
+
return { bound, ok: false }; // *args, ...spread
|
|
652
|
+
const kw = def.family === "cs" ? a.match(/^([A-Za-z_]\w*)\s*:(?!:)\s*(.+)$/) : def.family === "python" || def.family === "jvm" ? a.match(/^([A-Za-z_]\w*)\s*=(?!=)\s*(.+)$/) : null;
|
|
653
|
+
if (kw && def.params.some((p) => p.name === kw[1])) {
|
|
654
|
+
bound.set(kw[1], kw[2]);
|
|
655
|
+
continue;
|
|
656
|
+
}
|
|
657
|
+
const p = def.params[pos++];
|
|
658
|
+
if (!p || p.kwOnly)
|
|
659
|
+
return { bound, ok: bound.size > 0 };
|
|
660
|
+
bound.set(p.name, a);
|
|
661
|
+
}
|
|
662
|
+
return { bound, ok: true };
|
|
663
|
+
}
|
|
664
|
+
export function projectContext(files, goMods = []) {
|
|
665
|
+
const ix = buildIndex(files, goMods);
|
|
666
|
+
const exportCache = new Map();
|
|
667
|
+
const out = new Map();
|
|
668
|
+
const importsCache = new Map();
|
|
669
|
+
// Constants each file can see from the others.
|
|
670
|
+
for (const f of files) {
|
|
671
|
+
const fam = familyOf(f.path);
|
|
672
|
+
const ctx = { imported: new Map(), params: [] };
|
|
673
|
+
out.set(f.path, ctx);
|
|
674
|
+
if (!fam)
|
|
675
|
+
continue;
|
|
676
|
+
const imp = importsOf(ix, f);
|
|
677
|
+
importsCache.set(f.path, imp);
|
|
678
|
+
const set = (name, k) => {
|
|
679
|
+
if (!f.consts.has(name) && !ctx.imported.has(name))
|
|
680
|
+
ctx.imported.set(name, k);
|
|
681
|
+
};
|
|
682
|
+
for (const [local, from] of imp.names) {
|
|
683
|
+
const k = exportsOf(ix.files.get(from.file), exportCache).get(from.name);
|
|
684
|
+
if (k)
|
|
685
|
+
set(local, k);
|
|
686
|
+
}
|
|
687
|
+
for (const file of imp.star)
|
|
688
|
+
for (const [n, k] of exportsOf(ix.files.get(file), exportCache))
|
|
689
|
+
set(n, k);
|
|
690
|
+
for (const [q, targets] of imp.qualifiers)
|
|
691
|
+
for (const t of targets)
|
|
692
|
+
for (const [n, k] of exportsOf(ix.files.get(t), exportCache))
|
|
693
|
+
set(`${q}.${n}`, k);
|
|
694
|
+
if (fam === "jvm" || fam === "cs") {
|
|
695
|
+
// Class.CONST: only the names this file uses.
|
|
696
|
+
for (const l of f.code) {
|
|
697
|
+
if (!l.includes("."))
|
|
698
|
+
continue;
|
|
699
|
+
for (const m of l.matchAll(/(?<![\w$.])([A-Z][\w]*)\.([A-Za-z_]\w*)\b(?!\s*\()/g)) {
|
|
700
|
+
const key = `${m[1]}.${m[2]}`;
|
|
701
|
+
if (ctx.imported.has(key))
|
|
702
|
+
continue;
|
|
703
|
+
const hits = ix.classes.get(`${fam}:${m[1]}`) ?? [];
|
|
704
|
+
if (hits.length !== 1 || hits[0] === f.path)
|
|
705
|
+
continue;
|
|
706
|
+
const k = exportsOf(ix.files.get(hits[0]), exportCache).get(m[2]);
|
|
707
|
+
if (k)
|
|
708
|
+
set(key, k);
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
// Functions whose body does something with encryption using a parameter.
|
|
714
|
+
const defsCache = new Map();
|
|
715
|
+
const defsOf = (path) => {
|
|
716
|
+
let d = defsCache.get(path);
|
|
717
|
+
if (!d)
|
|
718
|
+
defsCache.set(path, (d = functionDefs(ix.files.get(path))));
|
|
719
|
+
return d;
|
|
720
|
+
};
|
|
721
|
+
const uses = new Map(); // the parameters used on encryption lines
|
|
722
|
+
for (const f of files) {
|
|
723
|
+
if (f.sideline || !familyOf(f.path) || familyOf(f.path) === "c")
|
|
724
|
+
continue;
|
|
725
|
+
if (!f.code.some((l) => HINT_FILE.test(l)))
|
|
726
|
+
continue;
|
|
727
|
+
for (const def of defsOf(f.path)) {
|
|
728
|
+
const names = new RegExp(`(?<![\\w$.])(${def.params.map((p) => p.name.replace(/\$/g, "\\$")).join("|")})(?![\\w$])`, "g");
|
|
729
|
+
for (let i = def.start; i <= def.end; i++) {
|
|
730
|
+
const l = f.code[i];
|
|
731
|
+
if (!l || !HINT.test(l))
|
|
732
|
+
continue;
|
|
733
|
+
for (const m of l.matchAll(names))
|
|
734
|
+
(uses.get(def) ?? uses.set(def, new Set()).get(def)).add(m[1]);
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
if (!uses.size)
|
|
739
|
+
return out;
|
|
740
|
+
const byName = new Map(); // family:name → defs
|
|
741
|
+
const register = (d) => {
|
|
742
|
+
const k = `${d.family}:${d.name}`;
|
|
743
|
+
const l = byName.get(k);
|
|
744
|
+
if (!l)
|
|
745
|
+
byName.set(k, [d]);
|
|
746
|
+
else if (!l.includes(d))
|
|
747
|
+
l.push(d);
|
|
748
|
+
};
|
|
749
|
+
for (const d of uses.keys())
|
|
750
|
+
register(d);
|
|
751
|
+
/** The one function a call names, or null. */
|
|
752
|
+
const resolve = (f, fam, qual, name) => {
|
|
753
|
+
const imp = importsCache.get(f.path);
|
|
754
|
+
const inFile = (file, owner) => {
|
|
755
|
+
const ds = defsOf(file).filter((d) => d.name === name && (owner === undefined || d.owner === owner));
|
|
756
|
+
return ds.length === 1 ? ds[0] : null;
|
|
757
|
+
};
|
|
758
|
+
if (!qual) {
|
|
759
|
+
const from = imp?.names.get(name);
|
|
760
|
+
if (from)
|
|
761
|
+
return inFile(from.file);
|
|
762
|
+
const local = defsOf(f.path).filter((d) => d.name === name && !d.self);
|
|
763
|
+
if (local.length)
|
|
764
|
+
return local.length === 1 ? local[0] : null;
|
|
765
|
+
const all = (byName.get(`${fam}:${name}`) ?? []).filter((d) => !d.self);
|
|
766
|
+
if (all.length !== 1)
|
|
767
|
+
return null;
|
|
768
|
+
// Go: a plain call reaches only the same package.
|
|
769
|
+
if (fam === "go" && dirOf(all[0].file) !== dirOf(f.path))
|
|
770
|
+
return null;
|
|
771
|
+
// Python and JS: a function from another file needs an import (checked above).
|
|
772
|
+
if ((fam === "python" || fam === "js") && all[0].file !== f.path)
|
|
773
|
+
return null;
|
|
774
|
+
return all[0];
|
|
775
|
+
}
|
|
776
|
+
if (qual === "self" || qual === "this" || qual === "cls") {
|
|
777
|
+
const d = inFile(f.path);
|
|
778
|
+
return d && (fam !== "python" || d.self) ? d : null;
|
|
779
|
+
}
|
|
780
|
+
const targets = imp?.qualifiers.get(qual);
|
|
781
|
+
if (targets) {
|
|
782
|
+
const ds = targets.flatMap((t) => defsOf(t).filter((d) => d.name === name && !d.self));
|
|
783
|
+
return ds.length === 1 ? ds[0] : null;
|
|
784
|
+
}
|
|
785
|
+
if (fam === "jvm" || fam === "cs" || fam === "python") {
|
|
786
|
+
// Class.method: a static method on a class the project defines once. In Python the class
|
|
787
|
+
// has to be defined in this file or imported by name, since another package may have a
|
|
788
|
+
// class of the same name.
|
|
789
|
+
const pyFrom = fam === "python" ? imp?.names.get(qual) : undefined;
|
|
790
|
+
const files2 = fam === "python" ? (pyFrom ? [pyFrom.file] : [f.path]) : (ix.classes.get(`${fam}:${qual}`) ?? []);
|
|
791
|
+
const ds = files2.flatMap((t) => defsOf(t).filter((d) => d.name === name && d.owner === qual));
|
|
792
|
+
return ds.length === 1 ? ds[0] : null;
|
|
793
|
+
}
|
|
794
|
+
return null;
|
|
795
|
+
};
|
|
796
|
+
// Call sites, in rounds: a function that passes its own parameter on is followed back to its callers.
|
|
797
|
+
const calls = [];
|
|
798
|
+
let wanted = new Set(byName.keys());
|
|
799
|
+
const scannedFor = new Set();
|
|
800
|
+
const forwards = new Map();
|
|
801
|
+
for (let round = 0; round < ROUNDS && wanted.size; round++) {
|
|
802
|
+
const next = new Set();
|
|
803
|
+
const names = new Set([...wanted].map((k) => k.slice(k.indexOf(":") + 1)));
|
|
804
|
+
for (const k of wanted)
|
|
805
|
+
scannedFor.add(k);
|
|
806
|
+
for (const f of files) {
|
|
807
|
+
const fam = familyOf(f.path);
|
|
808
|
+
if (!fam || fam === "c" || f.sideline)
|
|
809
|
+
continue;
|
|
810
|
+
const code = f.code;
|
|
811
|
+
const docs = docstringLines(f.path, code);
|
|
812
|
+
for (let idx = 0; idx < code.length; idx++) {
|
|
813
|
+
const l = code[idx];
|
|
814
|
+
if (l.length > 1000 || !l.includes("(") || docs.has(idx))
|
|
815
|
+
continue;
|
|
816
|
+
for (const m of l.matchAll(/(?<![\w$.])(?:([A-Za-z_$][\w$]*)\s*\.\s*)?([A-Za-z_$][\w$]*)\s*\(/g)) {
|
|
817
|
+
const name = m[2];
|
|
818
|
+
if (!names.has(name) || !wanted.has(`${fam}:${name}`))
|
|
819
|
+
continue;
|
|
820
|
+
const def = resolve(f, fam, m[1], name);
|
|
821
|
+
if (!def || (def.file === f.path && def.defIdx === idx))
|
|
822
|
+
continue;
|
|
823
|
+
if (def.self && !m[1])
|
|
824
|
+
continue;
|
|
825
|
+
const args = bracketText(code, idx, (m.index ?? 0) + m[0].length - 1);
|
|
826
|
+
if (!args)
|
|
827
|
+
continue;
|
|
828
|
+
calls.push({ file: f.path, idx, def, args: splitArgs(args.text) });
|
|
829
|
+
// An argument that is the calling function's own parameter: follow that function too.
|
|
830
|
+
const enclosing = defsOf(f.path).filter((d) => d.start <= idx && idx <= d.end).sort((a, b) => a.end - a.start - (b.end - b.start))[0];
|
|
831
|
+
if (enclosing && splitArgs(args.text).some((a) => enclosing.params.some((p) => a.replace(/^\w+\s*[=:]\s*/, "") === p.name))) {
|
|
832
|
+
(forwards.get(enclosing) ?? forwards.set(enclosing, new Set()).get(enclosing)).add(def);
|
|
833
|
+
const k = `${enclosing.family}:${enclosing.name}`;
|
|
834
|
+
register(enclosing);
|
|
835
|
+
if (!scannedFor.has(k))
|
|
836
|
+
next.add(k);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
wanted = next;
|
|
842
|
+
}
|
|
843
|
+
// Values for each parameter, starting from literals and constants, then passed along.
|
|
844
|
+
const values = new Map();
|
|
845
|
+
const addValue = (def, param, k) => {
|
|
846
|
+
let m = values.get(def);
|
|
847
|
+
if (!m)
|
|
848
|
+
values.set(def, (m = new Map()));
|
|
849
|
+
const list = m.get(param) ?? [];
|
|
850
|
+
if (list.length >= MAX_VALUES || list.some((x) => x.value === k.value))
|
|
851
|
+
return false;
|
|
852
|
+
list.push(k);
|
|
853
|
+
m.set(param, list);
|
|
854
|
+
return true;
|
|
855
|
+
};
|
|
856
|
+
const callsTo = new Map();
|
|
857
|
+
for (const c of calls)
|
|
858
|
+
(callsTo.get(c.def) ?? callsTo.set(c.def, []).get(c.def)).push(c);
|
|
859
|
+
const fnLabel = (d) => `${d.owner && d.family !== "go" ? `${d.owner}.` : ""}${d.name}()`;
|
|
860
|
+
// Defaults: when a call leaves the argument out, or no call to the function was found.
|
|
861
|
+
for (const [def, cs] of callsTo) {
|
|
862
|
+
for (const p of def.params) {
|
|
863
|
+
if (!p.default)
|
|
864
|
+
continue;
|
|
865
|
+
if (cs.some((c) => !bindArgs(def, c.args).bound.has(p.name)))
|
|
866
|
+
addValue(def, p.name, { value: p.default, line: def.defIdx + 1, file: def.file, via: [{ file: def.file, line: def.defIdx + 1, note: `${p.name} defaults to ${p.default}` }] });
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
for (const def of new Set([...byName.values()].flat())) {
|
|
870
|
+
if (callsTo.has(def))
|
|
871
|
+
continue;
|
|
872
|
+
for (const p of def.params)
|
|
873
|
+
if (p.default)
|
|
874
|
+
addValue(def, p.name, { value: p.default, line: def.defIdx + 1, file: def.file, via: [{ file: def.file, line: def.defIdx + 1, note: `${p.name} defaults to ${p.default}` }] });
|
|
875
|
+
}
|
|
876
|
+
for (let pass = 0; pass <= ROUNDS; pass++) {
|
|
877
|
+
let changed = false;
|
|
878
|
+
for (const c of calls) {
|
|
879
|
+
const f = ix.files.get(c.file);
|
|
880
|
+
const ctx = out.get(c.file);
|
|
881
|
+
const { bound, ok } = bindArgs(c.def, c.args);
|
|
882
|
+
if (!ok)
|
|
883
|
+
continue;
|
|
884
|
+
const enclosing = defsOf(c.file).filter((d) => d.start <= c.idx && c.idx <= d.end).sort((a, b) => a.end - a.start - (b.end - b.start))[0];
|
|
885
|
+
for (const [param, arg] of bound) {
|
|
886
|
+
const v = argValue(arg, f, ctx);
|
|
887
|
+
if (!v)
|
|
888
|
+
continue;
|
|
889
|
+
const callVia = { file: c.file, line: c.idx + 1, note: `${fnLabel(c.def)} is called with ${param} = ${v === "param" ? arg.trim() : v.value}` };
|
|
890
|
+
if (v === "param") {
|
|
891
|
+
const from = enclosing && values.get(enclosing)?.get(arg.trim());
|
|
892
|
+
if (!from)
|
|
893
|
+
continue;
|
|
894
|
+
for (const k of from) {
|
|
895
|
+
const via = [...(k.via ?? []), { ...callVia, note: `${fnLabel(c.def)} is called with ${param} = ${arg.trim()} (${k.value})` }];
|
|
896
|
+
if (via.length <= ROUNDS + 2 && addValue(c.def, param, { value: k.value, line: c.idx + 1, file: c.file, via }))
|
|
897
|
+
changed = true;
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
else if (addValue(c.def, param, { value: v.value, line: c.idx + 1, file: c.file, via: [...(v.via ?? (v.line && v.name ? [{ file: c.file, line: v.line, note: `${v.name} = ${v.value}` }] : [])), callVia] }))
|
|
901
|
+
changed = true;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
if (!changed)
|
|
905
|
+
break;
|
|
906
|
+
}
|
|
907
|
+
for (const [def, m] of values) {
|
|
908
|
+
if (!uses.has(def))
|
|
909
|
+
continue;
|
|
910
|
+
const used = uses.get(def);
|
|
911
|
+
const vals = new Map([...m].filter(([p]) => used.has(p)));
|
|
912
|
+
if (vals.size)
|
|
913
|
+
out.get(def.file)?.params.push({ start: def.start, end: def.end, values: vals });
|
|
914
|
+
}
|
|
915
|
+
return out;
|
|
916
|
+
}
|
|
917
|
+
/** The module path of each go.mod in the project, and the folder it covers. */
|
|
918
|
+
export function goModules(files) {
|
|
919
|
+
const out = [];
|
|
920
|
+
for (const f of files) {
|
|
921
|
+
if (!/(^|\/)go\.mod$/.test(f.path))
|
|
922
|
+
continue;
|
|
923
|
+
const m = f.content.match(/^\s*module\s+(\S+)/m);
|
|
924
|
+
if (m)
|
|
925
|
+
out.push({ module: m[1].replace(/^"|"$/g, ""), dir: dirOf(f.path) });
|
|
926
|
+
}
|
|
927
|
+
return out;
|
|
928
|
+
}
|