changeguard 0.0.0-stage → 0.7.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 +440 -0
- package/LICENSE +202 -0
- package/LICENSE-PROPRIETARY +94 -0
- package/README.md +317 -2
- package/SECURITY.md +40 -0
- package/data/changes/openai.seed.json +87 -0
- package/data/changes/shopify.seed.json +42 -0
- package/data/changes/stripe.seed.json +60 -0
- package/data/changes/supabase.seed.json +56 -0
- package/data/changes/twilio.seed.json +63 -0
- package/data/openapi/openai/meta.json +9 -0
- package/data/openapi/openai/new.json +24 -0
- package/data/openapi/openai/old.json +68 -0
- package/data/providers/openai.json +60 -0
- package/data/providers/shopify.json +52 -0
- package/data/providers/stripe.json +57 -0
- package/data/providers/supabase.json +56 -0
- package/data/providers/twilio.json +57 -0
- package/dist/analyze/go-analyzer.js +343 -0
- package/dist/analyze/index.js +77 -0
- package/dist/analyze/js-analyzer.js +380 -0
- package/dist/analyze/parser.js +78 -0
- package/dist/analyze/py-analyzer.js +313 -0
- package/dist/analyze/rules.js +1 -0
- package/dist/analyze/ts-utils.js +62 -0
- package/dist/cli.js +335 -0
- package/dist/codemod/apply.js +159 -0
- package/dist/config.js +121 -0
- package/dist/deps/manifests.js +145 -0
- package/dist/github/api.js +136 -0
- package/dist/github/auth.js +54 -0
- package/dist/impact/engine.js +384 -0
- package/dist/ingest/canonical.js +52 -0
- package/dist/ingest/changelog-html.js +63 -0
- package/dist/ingest/feed-client.js +99 -0
- package/dist/ingest/feed-keys.js +198 -0
- package/dist/ingest/feed.js +178 -0
- package/dist/ingest/github-releases.js +59 -0
- package/dist/ingest/note-parser.js +274 -0
- package/dist/ingest/openapi-diff.js +217 -0
- package/dist/ingest/store.js +181 -0
- package/dist/llm/explanation.js +273 -0
- package/dist/mcp/format.js +116 -0
- package/dist/mcp/index.js +29 -0
- package/dist/mcp/protocol.js +36 -0
- package/dist/mcp/server.js +110 -0
- package/dist/mcp/standalone.js +12 -0
- package/dist/mcp/tools.js +189 -0
- package/dist/notify/index.js +174 -0
- package/dist/providers/registry.js +215 -0
- package/dist/report/html.js +194 -0
- package/dist/report/markdown.js +90 -0
- package/dist/report/pr-draft.js +37 -0
- package/dist/scan.js +168 -0
- package/dist/source/repo-source.js +100 -0
- package/dist/types.js +6 -0
- package/dist/util/http.js +40 -0
- package/dist/util/logger.js +61 -0
- package/dist/util/metrics.js +99 -0
- package/dist/util/request-context.js +22 -0
- package/dist/util/task-queue.js +69 -0
- package/dist/util/versions.js +52 -0
- package/fixtures/sample-repo/cmd/billing/main.go +47 -0
- package/fixtures/sample-repo/go.mod +12 -0
- package/fixtures/sample-repo/package.json +17 -0
- package/fixtures/sample-repo/requirements.txt +7 -0
- package/fixtures/sample-repo/services/ai.py +65 -0
- package/fixtures/sample-repo/src/billing.ts +68 -0
- package/package.json +84 -4
|
@@ -0,0 +1,343 @@
|
|
|
1
|
+
import { providerForPackage } from '../providers/registry.js';
|
|
2
|
+
import { chainSegments, isStringLiteral, sameNode, siteId, snippet, stringValue, walk, } from './ts-utils.js';
|
|
3
|
+
function defaultLocalName(path) {
|
|
4
|
+
const segs = path.split('/');
|
|
5
|
+
let last = segs[segs.length - 1];
|
|
6
|
+
if (/^v\d+$/.test(last))
|
|
7
|
+
last = segs[segs.length - 2];
|
|
8
|
+
return last.replace(/^go-/, '').replace(/-go$/, '');
|
|
9
|
+
}
|
|
10
|
+
function goArgs(call) {
|
|
11
|
+
const list = call.childForFieldName('arguments');
|
|
12
|
+
if (!list)
|
|
13
|
+
return [];
|
|
14
|
+
return list.children.filter((c) => !['(', ')', ',', 'comment'].includes(c.type));
|
|
15
|
+
}
|
|
16
|
+
function compositeOf(arg) {
|
|
17
|
+
if (!arg)
|
|
18
|
+
return undefined;
|
|
19
|
+
if (arg.type === 'composite_literal')
|
|
20
|
+
return arg;
|
|
21
|
+
if (arg.type === 'unary_expression') {
|
|
22
|
+
const op = arg.children[0];
|
|
23
|
+
const operand = arg.childForFieldName('operand');
|
|
24
|
+
if (op?.type === '&' || op?.text === '&')
|
|
25
|
+
return compositeOf(operand ?? undefined);
|
|
26
|
+
}
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
function structKeys(callNodes) {
|
|
30
|
+
const keys = [];
|
|
31
|
+
const lit = new Map();
|
|
32
|
+
for (const call of callNodes) {
|
|
33
|
+
const args = goArgs(call);
|
|
34
|
+
const comp = args.map((a) => compositeOf(a)).find((c) => !!c);
|
|
35
|
+
const body = comp?.childForFieldName('body');
|
|
36
|
+
if (!body)
|
|
37
|
+
continue;
|
|
38
|
+
for (const el of body.children.filter((c) => c.type === 'keyed_element')) {
|
|
39
|
+
const keyNode = el.childForFieldName('key');
|
|
40
|
+
const valNode = el.childForFieldName('value');
|
|
41
|
+
const key = keyNode?.type === 'field_identifier' ? keyNode.text : keyNode?.text;
|
|
42
|
+
if (!key)
|
|
43
|
+
continue;
|
|
44
|
+
keys.push(key);
|
|
45
|
+
if (valNode && isStringLiteral(valNode))
|
|
46
|
+
lit.set(key, [...(lit.get(key) ?? []), stringValue(valNode)]);
|
|
47
|
+
}
|
|
48
|
+
args
|
|
49
|
+
.filter((a) => a.type !== 'composite_literal' && a.type !== 'unary_expression')
|
|
50
|
+
.forEach((a, i) => {
|
|
51
|
+
if (isStringLiteral(a))
|
|
52
|
+
lit.set(`$${i}`, [...(lit.get(`$${i}`) ?? []), stringValue(a)]);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return { keys: [...new Set(keys)], literals: Object.fromEntries(lit) };
|
|
56
|
+
}
|
|
57
|
+
export function analyzeGo(root, code, file, index) {
|
|
58
|
+
const rules = index.rules.go;
|
|
59
|
+
const active = new Set();
|
|
60
|
+
const bindings = new Map();
|
|
61
|
+
const instances = new Map();
|
|
62
|
+
const sites = [];
|
|
63
|
+
const seen = new Set();
|
|
64
|
+
const matchModule = (path) => {
|
|
65
|
+
const pid = providerForPackage('go', path);
|
|
66
|
+
if (!pid)
|
|
67
|
+
return undefined;
|
|
68
|
+
const rule = rules.find((r) => r.providerId === pid);
|
|
69
|
+
active.add(pid);
|
|
70
|
+
// Normalize major-version suffixes: .../stripe-go/v78 == .../stripe-go
|
|
71
|
+
const norm = (p) => p.replace(/\/v\d+(\/|$)/, (_m, tail) => tail ?? '');
|
|
72
|
+
const normalized = norm(path);
|
|
73
|
+
let bestPrefix;
|
|
74
|
+
for (const prefix of rule.goModulePrefixes ?? []) {
|
|
75
|
+
const npre = norm(prefix);
|
|
76
|
+
if (normalized === npre || normalized.startsWith(`${npre}/`)) {
|
|
77
|
+
if (!bestPrefix || npre.length > bestPrefix.length)
|
|
78
|
+
bestPrefix = npre;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
let subpath;
|
|
82
|
+
if (bestPrefix !== undefined) {
|
|
83
|
+
const rest = normalized.slice(bestPrefix.length).replace(/^\/+/, '');
|
|
84
|
+
if (rest)
|
|
85
|
+
subpath = rest.split('/').join('.');
|
|
86
|
+
}
|
|
87
|
+
return { rule, subpath };
|
|
88
|
+
};
|
|
89
|
+
// --- pass 1: imports ---
|
|
90
|
+
for (const node of walk(root)) {
|
|
91
|
+
if (node.type !== 'import_spec')
|
|
92
|
+
continue;
|
|
93
|
+
const pathNode = node.childForFieldName('path');
|
|
94
|
+
if (!pathNode || !isStringLiteral(pathNode))
|
|
95
|
+
continue;
|
|
96
|
+
const path = stringValue(pathNode);
|
|
97
|
+
const match = matchModule(path);
|
|
98
|
+
if (!match)
|
|
99
|
+
continue;
|
|
100
|
+
const alias = node.childForFieldName('name');
|
|
101
|
+
const local = alias?.text ?? defaultLocalName(path);
|
|
102
|
+
bindings.set(local, { providerId: match.rule.providerId, subpath: match.subpath });
|
|
103
|
+
}
|
|
104
|
+
// --- pass 2: factory propagation ---
|
|
105
|
+
const calleeBinding = (fn) => {
|
|
106
|
+
if (fn.type !== 'selector_expression')
|
|
107
|
+
return undefined;
|
|
108
|
+
const operand = fn.childForFieldName('operand');
|
|
109
|
+
const field = fn.childForFieldName('field');
|
|
110
|
+
if (!operand || operand.type !== 'identifier' || !field)
|
|
111
|
+
return undefined;
|
|
112
|
+
const b = bindings.get(operand.text);
|
|
113
|
+
if (!b || b.subpath)
|
|
114
|
+
return undefined;
|
|
115
|
+
const rule = rules.find((r) => r.providerId === b.providerId);
|
|
116
|
+
if (!rule?.factories.includes(field.text))
|
|
117
|
+
return undefined;
|
|
118
|
+
return b;
|
|
119
|
+
};
|
|
120
|
+
/** Resolve the package of a selector type, e.g. &stripe.ChargeParams{} -> stripe binding */
|
|
121
|
+
const compositePackage = (n) => {
|
|
122
|
+
let lit = n;
|
|
123
|
+
if (lit?.type === 'unary_expression') {
|
|
124
|
+
const op = lit.children[0];
|
|
125
|
+
if (op?.text !== '&')
|
|
126
|
+
return undefined;
|
|
127
|
+
lit = lit.childForFieldName('operand');
|
|
128
|
+
}
|
|
129
|
+
if (!lit || lit.type !== 'composite_literal')
|
|
130
|
+
return undefined;
|
|
131
|
+
const typeNode = lit.childForFieldName('type');
|
|
132
|
+
if (!typeNode || typeNode.type !== 'qualified_type')
|
|
133
|
+
return undefined;
|
|
134
|
+
const pkg = typeNode.children.find((c) => c.type === 'package_identifier');
|
|
135
|
+
if (!pkg)
|
|
136
|
+
return undefined;
|
|
137
|
+
return bindings.get(pkg.text);
|
|
138
|
+
};
|
|
139
|
+
for (const node of walk(root)) {
|
|
140
|
+
const isDecl = node.type === 'short_var_declaration' ||
|
|
141
|
+
node.type === 'assignment_statement' ||
|
|
142
|
+
node.type === 'var_spec';
|
|
143
|
+
if (!isDecl)
|
|
144
|
+
continue;
|
|
145
|
+
const leftNode = node.childForFieldName('left') ?? node.childForFieldName('name');
|
|
146
|
+
const lefts = (leftNode?.type === 'identifier' ? [leftNode] : (leftNode?.children ?? [])).filter((c) => c.type === 'identifier');
|
|
147
|
+
const rhs = node.childForFieldName('right') ?? node.childForFieldName('value');
|
|
148
|
+
if (!rhs || lefts.length === 0)
|
|
149
|
+
continue;
|
|
150
|
+
const exprs = rhs.type === 'expression_list' ? rhs.children : [rhs];
|
|
151
|
+
exprs.forEach((expr, i) => {
|
|
152
|
+
const target = lefts[i] ?? lefts[0];
|
|
153
|
+
if (!target || target.text === '_')
|
|
154
|
+
return;
|
|
155
|
+
if (expr.type === 'call_expression') {
|
|
156
|
+
const fn = expr.childForFieldName('function');
|
|
157
|
+
const b = fn ? calleeBinding(fn) : undefined;
|
|
158
|
+
if (b)
|
|
159
|
+
instances.set(target.text, b);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
const b = compositePackage(expr);
|
|
163
|
+
if (b)
|
|
164
|
+
instances.set(target.text, b);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
const rootOf = (id) => instances.get(id) ?? bindings.get(id);
|
|
169
|
+
const emit = (s) => {
|
|
170
|
+
const key = `${s.file}:${s.line}:${s.path}:${s.receiver}`;
|
|
171
|
+
if (seen.has(key))
|
|
172
|
+
return;
|
|
173
|
+
seen.add(key);
|
|
174
|
+
sites.push({ id: siteId(s.file, s.line - 1, s.path), ...s });
|
|
175
|
+
};
|
|
176
|
+
const pkgOf = (pid) => rules.find((r) => r.providerId === pid)?.packageName ?? pid;
|
|
177
|
+
// --- pass 3: selector-call chains ---
|
|
178
|
+
for (const node of walk(root)) {
|
|
179
|
+
if (node.type !== 'call_expression')
|
|
180
|
+
continue;
|
|
181
|
+
if (node.parent?.type === 'selector_expression' &&
|
|
182
|
+
sameNode(node.parent.childForFieldName('operand'), node) &&
|
|
183
|
+
node.parent.parent?.type === 'call_expression') {
|
|
184
|
+
continue;
|
|
185
|
+
}
|
|
186
|
+
const chain = [];
|
|
187
|
+
const callNodes = [];
|
|
188
|
+
let cur = node;
|
|
189
|
+
while (cur && cur.type === 'call_expression') {
|
|
190
|
+
const fn = cur.childForFieldName('function');
|
|
191
|
+
if (!fn || fn.type !== 'selector_expression')
|
|
192
|
+
break;
|
|
193
|
+
const field = fn.childForFieldName('field');
|
|
194
|
+
if (field)
|
|
195
|
+
chain.unshift(field.text);
|
|
196
|
+
callNodes.unshift(cur);
|
|
197
|
+
let operand = fn.childForFieldName('operand');
|
|
198
|
+
// descend pure selector chain: pkg.Api.CreateMessage() -> [Api, CreateMessage]
|
|
199
|
+
while (operand && operand.type === 'selector_expression') {
|
|
200
|
+
const f2 = operand.childForFieldName('field');
|
|
201
|
+
if (f2)
|
|
202
|
+
chain.unshift(f2.text);
|
|
203
|
+
operand = operand.childForFieldName('operand');
|
|
204
|
+
}
|
|
205
|
+
cur = operand;
|
|
206
|
+
}
|
|
207
|
+
if (cur && cur.type === 'identifier' && chain.length > 0) {
|
|
208
|
+
const resolved = rootOf(cur.text);
|
|
209
|
+
if (resolved && active.has(resolved.providerId)) {
|
|
210
|
+
// suppress bare factory calls: stripe.NewClient() / twilio.NewRestClient()
|
|
211
|
+
const isFactoryNoise = !instances.has(cur.text) &&
|
|
212
|
+
chain.length === 1 &&
|
|
213
|
+
rules.find((r) => r.providerId === resolved.providerId)?.factories.includes(chain[0]);
|
|
214
|
+
if (!isFactoryNoise) {
|
|
215
|
+
const args = structKeys(callNodes);
|
|
216
|
+
const pathParts = resolved.subpath ? [resolved.subpath, ...chain] : chain;
|
|
217
|
+
emit({
|
|
218
|
+
file,
|
|
219
|
+
language: 'go',
|
|
220
|
+
line: node.startPosition.row + 1,
|
|
221
|
+
endLine: node.endPosition.row + 1,
|
|
222
|
+
package: pkgOf(resolved.providerId),
|
|
223
|
+
providerId: resolved.providerId,
|
|
224
|
+
path: pathParts.join('.'),
|
|
225
|
+
receiver: [cur.text, ...pathParts].join('.'),
|
|
226
|
+
callText: snippet(code, node.startPosition.row),
|
|
227
|
+
argumentKeys: args.keys,
|
|
228
|
+
argumentLiterals: args.literals,
|
|
229
|
+
memberRefs: [],
|
|
230
|
+
});
|
|
231
|
+
continue;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// net/http raw calls
|
|
236
|
+
if (cur && cur.type === 'identifier' && cur.text === 'http' && chain.length === 1) {
|
|
237
|
+
const fnName = chain[0];
|
|
238
|
+
const args = goArgs(callNodes[0] ?? node);
|
|
239
|
+
if (fnName === 'NewRequest' || fnName === 'NewRequestWithContext') {
|
|
240
|
+
const offset = fnName === 'NewRequestWithContext' ? 1 : 0;
|
|
241
|
+
const methodArg = args[offset];
|
|
242
|
+
const urlArg = args[offset + 1];
|
|
243
|
+
if (methodArg && urlArg && isStringLiteral(methodArg) && isStringLiteral(urlArg)) {
|
|
244
|
+
emitHttp(sites, emit, file, node, stringValue(methodArg).toUpperCase(), stringValue(urlArg), index, code);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else if (['Get', 'Post', 'PostForm', 'Head'].includes(fnName) &&
|
|
248
|
+
isStringLiteral(args[0])) {
|
|
249
|
+
const method = fnName === 'PostForm' ? 'POST' : fnName.toUpperCase();
|
|
250
|
+
emitHttp(sites, emit, file, node, method, stringValue(args[0]), index, code);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
// --- pass 4: enum / const selector references + qualified struct types ---
|
|
255
|
+
for (const node of walk(root)) {
|
|
256
|
+
if (node.type === 'qualified_type') {
|
|
257
|
+
const pkg = node.children.find((c) => c.type === 'package_identifier');
|
|
258
|
+
const binding = pkg ? bindings.get(pkg.text) : undefined;
|
|
259
|
+
if (!binding || !active.has(binding.providerId))
|
|
260
|
+
continue;
|
|
261
|
+
const typeName = node.children.find((c) => c.type === 'field_identifier')?.text ??
|
|
262
|
+
node.children.find((c) => c.type === 'type_identifier')?.text;
|
|
263
|
+
if (!typeName)
|
|
264
|
+
continue;
|
|
265
|
+
const fullRef = binding.subpath ? `${binding.subpath}.${typeName}` : typeName;
|
|
266
|
+
emit({
|
|
267
|
+
file,
|
|
268
|
+
language: 'go',
|
|
269
|
+
line: node.startPosition.row + 1,
|
|
270
|
+
package: pkgOf(binding.providerId),
|
|
271
|
+
providerId: binding.providerId,
|
|
272
|
+
path: fullRef,
|
|
273
|
+
receiver: node.text,
|
|
274
|
+
callText: snippet(code, node.startPosition.row),
|
|
275
|
+
argumentKeys: [],
|
|
276
|
+
argumentLiterals: {},
|
|
277
|
+
memberRefs: [fullRef],
|
|
278
|
+
});
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
if (node.type !== 'selector_expression')
|
|
282
|
+
continue;
|
|
283
|
+
const parent = node.parent;
|
|
284
|
+
if (parent?.type === 'selector_expression')
|
|
285
|
+
continue;
|
|
286
|
+
if (parent?.type === 'call_expression' &&
|
|
287
|
+
sameNode(parent.childForFieldName('function'), node)) {
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const segs = chainSegments(node);
|
|
291
|
+
if (!segs)
|
|
292
|
+
continue;
|
|
293
|
+
const [r, ...rest] = segs;
|
|
294
|
+
if (!r || rest.length === 0)
|
|
295
|
+
continue;
|
|
296
|
+
const binding = rootOf(r);
|
|
297
|
+
const pid = binding?.providerId;
|
|
298
|
+
if (!pid || !active.has(pid))
|
|
299
|
+
continue;
|
|
300
|
+
const ref = rest.join('.');
|
|
301
|
+
const fullRef = binding.subpath ? `${binding.subpath}.${ref}` : ref;
|
|
302
|
+
emit({
|
|
303
|
+
file,
|
|
304
|
+
language: 'go',
|
|
305
|
+
line: node.startPosition.row + 1,
|
|
306
|
+
package: pkgOf(pid),
|
|
307
|
+
providerId: pid,
|
|
308
|
+
path: fullRef,
|
|
309
|
+
receiver: segs.join('.'),
|
|
310
|
+
callText: snippet(code, node.startPosition.row),
|
|
311
|
+
argumentKeys: [],
|
|
312
|
+
argumentLiterals: {},
|
|
313
|
+
memberRefs: [fullRef],
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
return sites;
|
|
317
|
+
}
|
|
318
|
+
function emitHttp(_sites, emit, file, node, method, raw, index, code) {
|
|
319
|
+
let host;
|
|
320
|
+
try {
|
|
321
|
+
host = new URL(raw).hostname.toLowerCase();
|
|
322
|
+
}
|
|
323
|
+
catch {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
const hit = index.hosts.find((h) => host === h.suffix || host.endsWith(`.${h.suffix}`));
|
|
327
|
+
if (!hit)
|
|
328
|
+
return;
|
|
329
|
+
emit({
|
|
330
|
+
file,
|
|
331
|
+
language: 'go',
|
|
332
|
+
line: node.startPosition.row + 1,
|
|
333
|
+
package: '(http)',
|
|
334
|
+
providerId: hit.providerId,
|
|
335
|
+
path: new URL(raw).pathname,
|
|
336
|
+
receiver: raw,
|
|
337
|
+
callText: snippet(code, node.startPosition.row),
|
|
338
|
+
argumentKeys: [],
|
|
339
|
+
argumentLiterals: { $0: [raw] },
|
|
340
|
+
memberRefs: [],
|
|
341
|
+
http: { method, url: raw },
|
|
342
|
+
});
|
|
343
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { AstParser, EXTENSION_LANGUAGE } from './parser.js';
|
|
2
|
+
import { buildAnalyzerIndex } from '../providers/registry.js';
|
|
3
|
+
import { analyzeJavaScript } from './js-analyzer.js';
|
|
4
|
+
import { analyzePython } from './py-analyzer.js';
|
|
5
|
+
import { analyzeGo } from './go-analyzer.js';
|
|
6
|
+
import { logger } from '../util/logger.js';
|
|
7
|
+
const MAX_SOURCE_BYTES = 400_000;
|
|
8
|
+
/**
|
|
9
|
+
* Parse every source file in the repository and extract third-party
|
|
10
|
+
* API/SDK call sites. Files are parsed independently; a syntax error in one
|
|
11
|
+
* file never aborts the scan.
|
|
12
|
+
*/
|
|
13
|
+
export async function analyzeRepository(source, analyzerIndex = buildAnalyzerIndex()) {
|
|
14
|
+
const parser = new AstParser();
|
|
15
|
+
const files = await source.listFiles();
|
|
16
|
+
const sites = [];
|
|
17
|
+
const errors = [];
|
|
18
|
+
let filesParsed = 0;
|
|
19
|
+
let filesSkipped = 0;
|
|
20
|
+
// Warm all three grammars up front so failures surface once.
|
|
21
|
+
const languages = new Set();
|
|
22
|
+
for (const file of files) {
|
|
23
|
+
const ext = file.split('.').pop()?.toLowerCase();
|
|
24
|
+
const lang = ext ? EXTENSION_LANGUAGE[ext] : undefined;
|
|
25
|
+
if (lang)
|
|
26
|
+
languages.add(lang);
|
|
27
|
+
}
|
|
28
|
+
for (const lang of languages) {
|
|
29
|
+
try {
|
|
30
|
+
await parser.forLanguage(lang);
|
|
31
|
+
}
|
|
32
|
+
catch (err) {
|
|
33
|
+
errors.push(`grammar:${lang}: ${err instanceof Error ? err.message : String(err)}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
for (const file of files) {
|
|
37
|
+
const ext = file.split('.').pop()?.toLowerCase();
|
|
38
|
+
const lang = ext ? EXTENSION_LANGUAGE[ext] : undefined;
|
|
39
|
+
if (!lang)
|
|
40
|
+
continue;
|
|
41
|
+
if (errors.some((e) => e.startsWith(`grammar:${lang}:`))) {
|
|
42
|
+
filesSkipped++;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const code = await source.readText(file);
|
|
46
|
+
if (typeof code !== 'string' || code.length > MAX_SOURCE_BYTES) {
|
|
47
|
+
filesSkipped++;
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
// skip obvious generated/minified blobs
|
|
51
|
+
if (code.includes('// !') || /\n[^\n]{4000,}/.test(code)) {
|
|
52
|
+
filesSkipped++;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const root = await parser.parse(lang, code);
|
|
57
|
+
const found = lang === 'javascript'
|
|
58
|
+
? analyzeJavaScript(root, code, file, lang, analyzerIndex)
|
|
59
|
+
: lang === 'python'
|
|
60
|
+
? analyzePython(root, code, file, analyzerIndex)
|
|
61
|
+
: analyzeGo(root, code, file, analyzerIndex);
|
|
62
|
+
sites.push(...found);
|
|
63
|
+
filesParsed++;
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
logger.debug(`parse failed: ${file}: ${err instanceof Error ? err.message : String(err)}`);
|
|
67
|
+
errors.push(`${file}: ${err instanceof Error ? err.message : String(err)}`);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// deterministic order: file, line, path
|
|
71
|
+
sites.sort((a, b) => a.file === b.file
|
|
72
|
+
? a.line === b.line
|
|
73
|
+
? a.path.localeCompare(b.path)
|
|
74
|
+
: a.line - b.line
|
|
75
|
+
: a.file.localeCompare(b.file));
|
|
76
|
+
return { sites, errors, filesParsed, filesSkipped };
|
|
77
|
+
}
|