@vantageos/mcp-doctor 0.18.0 → 0.20.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.
@@ -0,0 +1,1093 @@
1
+ // src/kit/source/ast.ts
2
+ import * as ts from "typescript";
3
+ function sourceOf(f) {
4
+ return f.lines.map((l) => l.raw).join("\n");
5
+ }
6
+ function parseFile(f) {
7
+ return {
8
+ rel: f.rel,
9
+ sf: ts.createSourceFile(f.rel, sourceOf(f), ts.ScriptTarget.Latest, true, ts.ScriptKind.TS)
10
+ };
11
+ }
12
+ function lineOf(n) {
13
+ const sf = n.getSourceFile();
14
+ return sf.getLineAndCharacterOfPosition(n.getStart(sf)).line + 1;
15
+ }
16
+ function unwrap(e) {
17
+ let x = e;
18
+ for (; ; ) {
19
+ if (ts.isAwaitExpression(x) || ts.isParenthesizedExpression(x) || ts.isNonNullExpression(x) || ts.isAsExpression(x) || ts.isSatisfiesExpression(x))
20
+ x = x.expression;
21
+ else return x;
22
+ }
23
+ }
24
+ function propRead(e) {
25
+ if (ts.isPropertyAccessExpression(e)) return { obj: e.expression, name: e.name.text };
26
+ if (ts.isElementAccessExpression(e) && ts.isStringLiteralLike(e.argumentExpression))
27
+ return { obj: e.expression, name: e.argumentExpression.text };
28
+ return void 0;
29
+ }
30
+ function constInit(id) {
31
+ for (let x = id.parent; x; x = x.parent) {
32
+ if (ts.isFunctionLike(x) && x.parameters.some((q) => ts.isIdentifier(q.name) && q.name.text === id.text))
33
+ return void 0;
34
+ const stmts = ts.isBlock(x) || ts.isSourceFile(x) || ts.isModuleBlock(x) ? x.statements : ts.isCaseClause(x) || ts.isDefaultClause(x) ? x.statements : void 0;
35
+ if (!stmts) continue;
36
+ for (const st of stmts) {
37
+ if (!ts.isVariableStatement(st)) continue;
38
+ for (const d of st.declarationList.declarations)
39
+ if (ts.isIdentifier(d.name) && d.name.text === id.text)
40
+ return st.declarationList.flags & ts.NodeFlags.Const ? d.initializer : void 0;
41
+ }
42
+ }
43
+ return void 0;
44
+ }
45
+ function propReadResolved(e) {
46
+ const u = unwrap(e);
47
+ const direct = propRead(u);
48
+ if (direct) return direct;
49
+ if (!ts.isIdentifier(u)) return void 0;
50
+ const init = constInit(u);
51
+ return init ? propRead(unwrap(init)) : void 0;
52
+ }
53
+ function walk(n, visit, ownOnly = false) {
54
+ const rec = (x) => {
55
+ visit(x);
56
+ if (ownOnly && x !== n && (ts.isFunctionDeclaration(x) || ts.isFunctionExpression(x) || ts.isArrowFunction(x)))
57
+ return;
58
+ ts.forEachChild(x, rec);
59
+ };
60
+ rec(n);
61
+ }
62
+ function dotted(e) {
63
+ const x = unwrap(e);
64
+ if (ts.isIdentifier(x)) return x.text;
65
+ if (ts.isPropertyAccessExpression(x)) {
66
+ const head = dotted(x.expression);
67
+ return head === void 0 ? void 0 : `${head}.${x.name.text}`;
68
+ }
69
+ return void 0;
70
+ }
71
+ function calleeName(c) {
72
+ const e = unwrap(c.expression);
73
+ if (ts.isIdentifier(e)) return e.text;
74
+ if (ts.isPropertyAccessExpression(e)) return e.name.text;
75
+ return void 0;
76
+ }
77
+ var BUILDERS = /* @__PURE__ */ new Set([
78
+ "mutation",
79
+ "action",
80
+ "query",
81
+ "internalMutation",
82
+ "internalAction",
83
+ "internalQuery"
84
+ ]);
85
+ function idTableOf(text) {
86
+ const m = /^\s*(?:v\.optional\(\s*)?v\.id\(\s*["'`]([A-Za-z0-9_]+)["'`]\s*\)/.exec(text);
87
+ return m?.[1];
88
+ }
89
+ function singleIdTable(text) {
90
+ const ts_ = new Set(
91
+ [...text.matchAll(/v\.id\(\s*["'`]([A-Za-z0-9_]+)["'`]\s*\)/g)].map((m) => m[1])
92
+ );
93
+ return ts_.size === 1 ? [...ts_][0] : void 0;
94
+ }
95
+ function argsObject(p, e0) {
96
+ const e = unwrap(e0);
97
+ if (ts.isObjectLiteralExpression(e)) return e;
98
+ if (ts.isCallExpression(e) && calleeName(e) === "object" && e.arguments[0] && ts.isObjectLiteralExpression(unwrap(e.arguments[0])))
99
+ return unwrap(e.arguments[0]);
100
+ if (ts.isIdentifier(e)) {
101
+ for (const st of p.sf.statements) {
102
+ if (!ts.isVariableStatement(st)) continue;
103
+ for (const d of st.declarationList.declarations)
104
+ if (ts.isIdentifier(d.name) && d.name.text === e.text && d.initializer)
105
+ return argsObject(p, d.initializer);
106
+ }
107
+ }
108
+ return void 0;
109
+ }
110
+ function registrations(p) {
111
+ const out = [];
112
+ for (const st of p.sf.statements) {
113
+ if (!ts.isVariableStatement(st)) continue;
114
+ for (const d of st.declarationList.declarations) {
115
+ if (!ts.isIdentifier(d.name) || !d.initializer) continue;
116
+ const init = unwrap(d.initializer);
117
+ if (!ts.isCallExpression(init)) continue;
118
+ const b = calleeName(init);
119
+ if (b === void 0 || !BUILDERS.has(b)) continue;
120
+ const cfg = init.arguments[0];
121
+ if (!cfg || !ts.isObjectLiteralExpression(cfg)) continue;
122
+ let handler;
123
+ const argIdTable = /* @__PURE__ */ new Map();
124
+ for (const prop of cfg.properties) {
125
+ const key = prop.name && ts.isIdentifier(prop.name) ? prop.name.text : void 0;
126
+ if (key === "handler") {
127
+ if (ts.isMethodDeclaration(prop)) handler = prop;
128
+ else if (ts.isPropertyAssignment(prop)) {
129
+ const h = unwrap(prop.initializer);
130
+ if (ts.isArrowFunction(h) || ts.isFunctionExpression(h)) handler = h;
131
+ }
132
+ }
133
+ if (key === "args" && ts.isPropertyAssignment(prop)) {
134
+ const obj = argsObject(p, prop.initializer);
135
+ for (const a of obj?.properties ?? []) {
136
+ if (ts.isPropertyAssignment(a) && ts.isIdentifier(a.name)) {
137
+ const t = singleIdTable(a.initializer.getText(p.sf));
138
+ if (t) argIdTable.set(a.name.text, t);
139
+ }
140
+ }
141
+ }
142
+ }
143
+ if (handler === void 0) continue;
144
+ out.push({
145
+ name: d.name.text,
146
+ builder: b,
147
+ rel: p.rel,
148
+ line: lineOf(d),
149
+ handler,
150
+ argIdTable
151
+ });
152
+ }
153
+ }
154
+ return out;
155
+ }
156
+ function indexFunctions(ps) {
157
+ const idx = /* @__PURE__ */ new Map();
158
+ const add = (d) => {
159
+ const xs = idx.get(d.name) ?? [];
160
+ xs.push(d);
161
+ idx.set(d.name, xs);
162
+ };
163
+ for (const p of ps) {
164
+ for (const st of p.sf.statements) {
165
+ if (ts.isFunctionDeclaration(st) && st.name && st.body)
166
+ add({ name: st.name.text, rel: p.rel, line: lineOf(st), fn: st });
167
+ else if (ts.isVariableStatement(st))
168
+ for (const d of st.declarationList.declarations) {
169
+ const init = d.initializer && unwrap(d.initializer);
170
+ if (ts.isIdentifier(d.name) && init && (ts.isArrowFunction(init) || ts.isFunctionExpression(init)))
171
+ add({ name: d.name.text, rel: p.rel, line: lineOf(d), fn: init });
172
+ }
173
+ }
174
+ }
175
+ return idx;
176
+ }
177
+ function calledNames(fn) {
178
+ const out = /* @__PURE__ */ new Set();
179
+ walk(fn, (x) => {
180
+ if (ts.isCallExpression(x)) {
181
+ const e = unwrap(x.expression);
182
+ if (ts.isIdentifier(e)) out.add(e.text);
183
+ }
184
+ });
185
+ return out;
186
+ }
187
+ function closeOverCalls(idx, own) {
188
+ const holds = /* @__PURE__ */ new Set();
189
+ for (const [n, ds] of idx) if (ds.some(own)) holds.add(n);
190
+ let grew = true;
191
+ while (grew) {
192
+ grew = false;
193
+ for (const [n, ds] of idx) {
194
+ if (holds.has(n)) continue;
195
+ if (ds.some((d) => [...calledNames(d.fn)].some((c) => holds.has(c)))) {
196
+ holds.add(n);
197
+ grew = true;
198
+ }
199
+ }
200
+ }
201
+ return holds;
202
+ }
203
+ var TENANT_COLUMNS = /* @__PURE__ */ new Set([
204
+ "orgId",
205
+ "tenantId",
206
+ "workspaceId",
207
+ "orgSlug"
208
+ ]);
209
+ var NAMESPACE_COLUMN = "namespace";
210
+ function readSchema(p) {
211
+ const out = /* @__PURE__ */ new Map();
212
+ walk(p.sf, (x) => {
213
+ if (!ts.isPropertyAssignment(x) || !ts.isIdentifier(x.name)) return;
214
+ let e = x.initializer;
215
+ for (; ; ) {
216
+ const u = unwrap(e);
217
+ if (ts.isCallExpression(u) && ts.isPropertyAccessExpression(u.expression)) {
218
+ e = u.expression.expression;
219
+ continue;
220
+ }
221
+ e = u;
222
+ break;
223
+ }
224
+ if (!ts.isCallExpression(e) || calleeName(e) !== "defineTable" || !e.arguments[0] || !ts.isObjectLiteralExpression(e.arguments[0]))
225
+ return;
226
+ const fields = [];
227
+ const idRef = /* @__PURE__ */ new Map();
228
+ let tenant = "none";
229
+ let namespace = "none";
230
+ for (const prop of e.arguments[0].properties) {
231
+ if (!ts.isPropertyAssignment(prop)) continue;
232
+ const k = ts.isIdentifier(prop.name) ? prop.name.text : ts.isStringLiteral(prop.name) ? prop.name.text : void 0;
233
+ if (k === void 0) continue;
234
+ fields.push(k);
235
+ const text = prop.initializer.getText(p.sf);
236
+ const t = idTableOf(text);
237
+ if (t) idRef.set(k, t);
238
+ const opt = /^\s*v\.optional\(/.test(text);
239
+ if (TENANT_COLUMNS.has(k)) {
240
+ if (!opt) tenant = "required";
241
+ else if (tenant === "none") tenant = "optional";
242
+ }
243
+ if (k === NAMESPACE_COLUMN) namespace = opt ? "optional" : "required";
244
+ }
245
+ out.set(x.name.text, {
246
+ name: x.name.text,
247
+ tenant,
248
+ namespace,
249
+ fields,
250
+ idRef
251
+ });
252
+ });
253
+ return out;
254
+ }
255
+
256
+ // src/kit/source/scan.ts
257
+ function emptyScan() {
258
+ return { conform: [], violate: [], cnj: [] };
259
+ }
260
+ function base(rel) {
261
+ return (rel.split("/").pop() ?? rel).toLowerCase();
262
+ }
263
+ function isJudgeable(f) {
264
+ const b = base(f.rel);
265
+ return !b.endsWith(".test.ts") && !f.rel.includes("__tests__") && !f.rel.includes("_generated/");
266
+ }
267
+
268
+ // src/kit/source/taint.ts
269
+ import * as ts2 from "typescript";
270
+ function declaredNames(fn) {
271
+ const out = /* @__PURE__ */ new Set();
272
+ const addBinding = (b) => {
273
+ if (ts2.isIdentifier(b)) out.add(b.text);
274
+ else for (const e of b.elements) if (!ts2.isOmittedExpression(e)) addBinding(e.name);
275
+ };
276
+ walk(fn, (x) => {
277
+ if (ts2.isParameter(x)) addBinding(x.name);
278
+ else if (ts2.isVariableDeclaration(x)) addBinding(x.name);
279
+ });
280
+ return out;
281
+ }
282
+ function readsTainted(node, tainted) {
283
+ const declared = declaredNames(node);
284
+ let hit = false;
285
+ walk(node, (x) => {
286
+ if (hit || !ts2.isIdentifier(x)) return;
287
+ const p = x.parent;
288
+ if (p && ts2.isPropertyAccessExpression(p) && p.name === x) return;
289
+ if (p && ts2.isPropertyAssignment(p) && p.name === x) return;
290
+ if (p && ts2.isTypeReferenceNode(p)) return;
291
+ if (declared.has(x.text)) return;
292
+ if (tainted.has(x.text)) hit = true;
293
+ });
294
+ return hit;
295
+ }
296
+
297
+ // src/kit/source/r4-filter-paths.ts
298
+ import * as ts3 from "typescript";
299
+ var R4_SOURCE_ID = "R-4 source: a list read does not filter in memory, on a caller argument, rows its index range does not bound";
300
+ var isConvexFile = (f) => /(^|\/)convex\//.test(f.rel);
301
+ var TERMINALS = /* @__PURE__ */ new Set(["take", "collect", "paginate"]);
302
+ var RANGE_METHODS = /* @__PURE__ */ new Set(["eq", "gt", "gte", "lt", "lte", "search"]);
303
+ var ROWS_METHODS = /* @__PURE__ */ new Set([
304
+ "flat",
305
+ "slice",
306
+ "sort",
307
+ "reverse",
308
+ "concat",
309
+ "filter",
310
+ "toSorted",
311
+ "toReversed"
312
+ ]);
313
+ var SCOPE_RESOLVER = /^(with|resolve|require|get)\w*(Scope|Caller|Principal|Identity)\w*$/;
314
+ var isUndefinedLike = (e) => {
315
+ const x = unwrap(e);
316
+ return ts3.isIdentifier(x) && x.text === "undefined" || x.kind === ts3.SyntaxKind.NullKeyword;
317
+ };
318
+ function definedTest(e) {
319
+ const op = e.operatorToken.kind;
320
+ const eq = op === ts3.SyntaxKind.EqualsEqualsEqualsToken || op === ts3.SyntaxKind.EqualsEqualsToken;
321
+ const ne = op === ts3.SyntaxKind.ExclamationEqualsEqualsToken || op === ts3.SyntaxKind.ExclamationEqualsToken;
322
+ if (!eq && !ne) return void 0;
323
+ const l = unwrap(e.left);
324
+ const r = unwrap(e.right);
325
+ let name;
326
+ if (isUndefinedLike(r)) name = dotted(l);
327
+ else if (isUndefinedLike(l)) name = dotted(r);
328
+ if (name === void 0 || name === "undefined") return void 0;
329
+ return { name, whenTrueDefined: ne };
330
+ }
331
+ function evalCond(e, f) {
332
+ const x = unwrap(e);
333
+ if (ts3.isBinaryExpression(x)) {
334
+ const op = x.operatorToken.kind;
335
+ if (op === ts3.SyntaxKind.AmpersandAmpersandToken || op === ts3.SyntaxKind.BarBarToken) {
336
+ const a = evalCond(x.left, f);
337
+ const b = evalCond(x.right, f);
338
+ if (op === ts3.SyntaxKind.AmpersandAmpersandToken) {
339
+ if (a === false || b === false) return false;
340
+ return a === true && b === true ? true : void 0;
341
+ }
342
+ if (a === true || b === true) return true;
343
+ return a === false && b === false ? false : void 0;
344
+ }
345
+ const t = definedTest(x);
346
+ if (t) {
347
+ if (f.defined.has(t.name)) return t.whenTrueDefined;
348
+ if (f.undef.has(t.name)) return !t.whenTrueDefined;
349
+ }
350
+ return void 0;
351
+ }
352
+ if (ts3.isPrefixUnaryExpression(x) && x.operator === ts3.SyntaxKind.ExclamationToken) {
353
+ const v = evalCond(x.operand, f);
354
+ return v === void 0 ? void 0 : !v;
355
+ }
356
+ if (ts3.isIdentifier(x)) {
357
+ if (f.undef.has(x.text)) return false;
358
+ const init = constInit(x);
359
+ return init === void 0 || !isCondition(init) ? void 0 : evalCond(init, f);
360
+ }
361
+ return void 0;
362
+ }
363
+ function isCondition(e) {
364
+ const x = unwrap(e);
365
+ return ts3.isBinaryExpression(x) && (x.operatorToken.kind === ts3.SyntaxKind.AmpersandAmpersandToken || x.operatorToken.kind === ts3.SyntaxKind.BarBarToken || definedTest(x) !== void 0) || ts3.isPrefixUnaryExpression(x) && x.operator === ts3.SyntaxKind.ExclamationToken;
366
+ }
367
+ function assume(e, truth, f) {
368
+ const x = unwrap(e);
369
+ const add = (set, n) => {
370
+ if (set.has(n)) return false;
371
+ set.add(n);
372
+ return true;
373
+ };
374
+ if (ts3.isPrefixUnaryExpression(x) && x.operator === ts3.SyntaxKind.ExclamationToken)
375
+ return assume(x.operand, !truth, f);
376
+ if (ts3.isIdentifier(x)) {
377
+ let grew = add(truth ? f.defined : f.undef, x.text);
378
+ const init = constInit(x);
379
+ if (init !== void 0 && isCondition(init)) grew = assume(init, truth, f) || grew;
380
+ return grew;
381
+ }
382
+ if (!ts3.isBinaryExpression(x)) return false;
383
+ const op = x.operatorToken.kind;
384
+ if (op === ts3.SyntaxKind.AmpersandAmpersandToken) {
385
+ if (truth) {
386
+ const l = assume(x.left, true, f);
387
+ return assume(x.right, true, f) || l;
388
+ }
389
+ let grew = false;
390
+ if (evalCond(x.left, f) === true) grew = assume(x.right, false, f) || grew;
391
+ if (evalCond(x.right, f) === true) grew = assume(x.left, false, f) || grew;
392
+ return grew;
393
+ }
394
+ if (op === ts3.SyntaxKind.BarBarToken) {
395
+ if (!truth) {
396
+ const l = assume(x.left, false, f);
397
+ return assume(x.right, false, f) || l;
398
+ }
399
+ let grew = false;
400
+ if (evalCond(x.left, f) === false) grew = assume(x.right, true, f) || grew;
401
+ if (evalCond(x.right, f) === false) grew = assume(x.left, true, f) || grew;
402
+ return grew;
403
+ }
404
+ const t = definedTest(x);
405
+ if (!t) return false;
406
+ return add(truth === t.whenTrueDefined ? f.defined : f.undef, t.name);
407
+ }
408
+ function terminates(s) {
409
+ if (!s) return false;
410
+ if (ts3.isThrowStatement(s) || ts3.isReturnStatement(s) || ts3.isContinueStatement(s) || ts3.isBreakStatement(s))
411
+ return true;
412
+ if (ts3.isBlock(s)) return s.statements.some((x) => terminates(x));
413
+ return false;
414
+ }
415
+ function factsAt(node, root) {
416
+ const intros = [];
417
+ const chain = [];
418
+ for (let x = node; x && x !== root; x = x.parent) chain.push(x);
419
+ chain.reverse();
420
+ for (let i = 0; i < chain.length; i++) {
421
+ const child = chain[i];
422
+ const parent = child.parent;
423
+ if (!parent) continue;
424
+ if (ts3.isIfStatement(parent)) {
425
+ if (child === parent.thenStatement) intros.push({ cond: parent.expression, truth: true });
426
+ else if (child === parent.elseStatement)
427
+ intros.push({ cond: parent.expression, truth: false });
428
+ } else if (ts3.isConditionalExpression(parent)) {
429
+ if (child === parent.whenTrue) intros.push({ cond: parent.condition, truth: true });
430
+ else if (child === parent.whenFalse) intros.push({ cond: parent.condition, truth: false });
431
+ } else if (ts3.isBinaryExpression(parent) && child === parent.right && (parent.operatorToken.kind === ts3.SyntaxKind.AmpersandAmpersandToken || parent.operatorToken.kind === ts3.SyntaxKind.BarBarToken)) {
432
+ intros.push({
433
+ cond: parent.left,
434
+ truth: parent.operatorToken.kind === ts3.SyntaxKind.AmpersandAmpersandToken
435
+ });
436
+ }
437
+ const siblings = ts3.isBlock(parent) ? parent.statements : ts3.isCaseClause(parent) || ts3.isDefaultClause(parent) ? parent.statements : void 0;
438
+ if (siblings) {
439
+ for (const s of siblings) {
440
+ if (s === child) break;
441
+ if (ts3.isIfStatement(s) && terminates(s.thenStatement))
442
+ intros.push({ cond: s.expression, truth: false });
443
+ }
444
+ }
445
+ }
446
+ const facts = { defined: /* @__PURE__ */ new Set(), undef: /* @__PURE__ */ new Set() };
447
+ for (let round = 0; round < 4; round++) {
448
+ let grew = false;
449
+ for (const i of intros) grew = assume(i.cond, i.truth, facts) || grew;
450
+ if (!grew) break;
451
+ }
452
+ return facts;
453
+ }
454
+ function bindingNames(b, out) {
455
+ if (ts3.isIdentifier(b)) out.add(b.text);
456
+ else for (const e of b.elements) if (!ts3.isOmittedExpression(e)) bindingNames(e.name, out);
457
+ }
458
+ function holdsDbRead(e) {
459
+ let hit = false;
460
+ walk(e, (x) => {
461
+ if (hit || !ts3.isCallExpression(x)) return;
462
+ const c = unwrap(x.expression);
463
+ if (!ts3.isPropertyAccessExpression(c)) return;
464
+ const m = c.name.text;
465
+ if (TERMINALS.has(m) || m === "first" || m === "unique") hit = true;
466
+ else if (m === "get") {
467
+ const r = unwrap(c.expression);
468
+ if (ts3.isPropertyAccessExpression(r) && r.name.text === "db") hit = true;
469
+ }
470
+ });
471
+ return hit;
472
+ }
473
+ function argTaint(fn, taintedParams, base2) {
474
+ const tainted = new Set(base2);
475
+ fn.parameters.forEach((p, i) => {
476
+ if (taintedParams.has(i)) bindingNames(p.name, tainted);
477
+ });
478
+ const mentions = (e) => {
479
+ let hit = false;
480
+ walk(e, (x) => {
481
+ if (hit) return;
482
+ if (ts3.isCallExpression(x)) {
483
+ const c = unwrap(x.expression);
484
+ const n = ts3.isIdentifier(c) ? c.text : ts3.isPropertyAccessExpression(c) ? c.name.text : void 0;
485
+ if (n !== void 0 && SCOPE_RESOLVER.test(n)) return;
486
+ }
487
+ if (ts3.isIdentifier(x) && tainted.has(x.text)) {
488
+ const p = x.parent;
489
+ if (!(p && ts3.isPropertyAccessExpression(p) && p.name === x)) {
490
+ for (let up = x.parent; up; up = up.parent) {
491
+ if (up === e) break;
492
+ if (ts3.isCallExpression(up)) {
493
+ const c = unwrap(up.expression);
494
+ const n = ts3.isIdentifier(c) ? c.text : ts3.isPropertyAccessExpression(c) ? c.name.text : void 0;
495
+ if (n !== void 0 && SCOPE_RESOLVER.test(n)) return;
496
+ }
497
+ }
498
+ hit = true;
499
+ }
500
+ }
501
+ });
502
+ return hit;
503
+ };
504
+ const body = fn.body;
505
+ if (!body) return tainted;
506
+ const data = /* @__PURE__ */ new Set();
507
+ walk(body, (x) => {
508
+ if (ts3.isVariableDeclaration(x) && x.initializer && holdsDbRead(x.initializer))
509
+ bindingNames(x.name, data);
510
+ else if (ts3.isBinaryExpression(x) && x.operatorToken.kind === ts3.SyntaxKind.EqualsToken && ts3.isIdentifier(x.left) && holdsDbRead(x.right))
511
+ data.add(x.left.text);
512
+ });
513
+ const declaredCount = /* @__PURE__ */ new Map();
514
+ walk(body, (x) => {
515
+ if (ts3.isVariableDeclaration(x) || ts3.isParameter(x)) {
516
+ const names = /* @__PURE__ */ new Set();
517
+ bindingNames(x.name, names);
518
+ for (const n of names) declaredCount.set(n, (declaredCount.get(n) ?? 0) + 1);
519
+ }
520
+ });
521
+ for (let pass = 0; pass < 6; pass++) {
522
+ let grew = false;
523
+ const add = (b, shared = false) => {
524
+ const names = /* @__PURE__ */ new Set();
525
+ bindingNames(b, names);
526
+ for (const n of names)
527
+ if (!tainted.has(n) && !data.has(n) && !(shared && (declaredCount.get(n) ?? 0) > 1)) {
528
+ tainted.add(n);
529
+ grew = true;
530
+ }
531
+ };
532
+ walk(body, (x) => {
533
+ if (ts3.isVariableDeclaration(x) && x.initializer && !holdsDbRead(x.initializer) && mentions(x.initializer))
534
+ add(x.name);
535
+ else if (ts3.isBinaryExpression(x) && x.operatorToken.kind === ts3.SyntaxKind.EqualsToken && ts3.isIdentifier(x.left) && !holdsDbRead(x.right) && mentions(x.right))
536
+ add(x.left);
537
+ else if (ts3.isCallExpression(x)) {
538
+ const c = unwrap(x.expression);
539
+ if (ts3.isPropertyAccessExpression(c) && ["map", "forEach", "flatMap", "some", "every", "find"].includes(c.name.text) && mentions(c.expression)) {
540
+ const cb = x.arguments[0] && unwrap(x.arguments[0]);
541
+ if (cb && (ts3.isArrowFunction(cb) || ts3.isFunctionExpression(cb)))
542
+ for (const p of cb.parameters) add(p.name, true);
543
+ }
544
+ }
545
+ });
546
+ if (!grew) break;
547
+ }
548
+ return tainted;
549
+ }
550
+ function taintReads(node, tainted) {
551
+ const declared = declaredNames(node);
552
+ const names = /* @__PURE__ */ new Set();
553
+ const shown = /* @__PURE__ */ new Set();
554
+ walk(node, (x) => {
555
+ if (!ts3.isIdentifier(x)) return;
556
+ const p = x.parent;
557
+ if (p && ts3.isPropertyAccessExpression(p) && p.name === x) return;
558
+ if (p && ts3.isPropertyAssignment(p) && p.name === x) return;
559
+ if (declared.has(x.text)) return;
560
+ if (!tainted.has(x.text)) return;
561
+ names.add(x.text);
562
+ shown.add(
563
+ x.text === "args" && p && ts3.isPropertyAccessExpression(p) && p.expression === x ? `args.${p.name.text}` : x.text
564
+ );
565
+ });
566
+ return { names, shown };
567
+ }
568
+ function isFnExpr(e) {
569
+ if (!e) return false;
570
+ const x = unwrap(e);
571
+ return ts3.isArrowFunction(x) || ts3.isFunctionExpression(x);
572
+ }
573
+ function gatingConds(node, stop) {
574
+ const out = [];
575
+ for (let child = node; child !== stop && child.parent; child = child.parent) {
576
+ const parent = child.parent;
577
+ if (ts3.isConditionalExpression(parent)) {
578
+ if (child === parent.whenTrue) out.push({ cond: parent.condition, truth: true });
579
+ else if (child === parent.whenFalse) out.push({ cond: parent.condition, truth: false });
580
+ } else if (ts3.isIfStatement(parent)) {
581
+ if (child === parent.thenStatement) out.push({ cond: parent.expression, truth: true });
582
+ else if (child === parent.elseStatement) out.push({ cond: parent.expression, truth: false });
583
+ } else if (ts3.isBinaryExpression(parent) && child === parent.right && (parent.operatorToken.kind === ts3.SyntaxKind.AmpersandAmpersandToken || parent.operatorToken.kind === ts3.SyntaxKind.BarBarToken)) {
584
+ out.push({
585
+ cond: parent.left,
586
+ truth: parent.operatorToken.kind === ts3.SyntaxKind.AmpersandAmpersandToken
587
+ });
588
+ }
589
+ }
590
+ return out;
591
+ }
592
+ function readsCallbackArgument(value, tainted) {
593
+ let hit = false;
594
+ walk(value, (x) => {
595
+ if (hit || !ts3.isIdentifier(x)) return;
596
+ const p = x.parent;
597
+ if (p && ts3.isPropertyAccessExpression(p) && p.name === x) return;
598
+ for (let up = x.parent; up; up = up.parent) {
599
+ if (!ts3.isArrowFunction(up) && !ts3.isFunctionExpression(up)) continue;
600
+ if (!up.parameters.some((q) => ts3.isIdentifier(q.name) && q.name.text === x.text)) continue;
601
+ const call = up.parent;
602
+ if (call && ts3.isCallExpression(call) && call.arguments[0] === up) {
603
+ const c = unwrap(call.expression);
604
+ if (ts3.isPropertyAccessExpression(c) && ["map", "forEach", "flatMap", "some", "every", "find"].includes(c.name.text) && readsTainted(c.expression, tainted))
605
+ hit = true;
606
+ }
607
+ break;
608
+ }
609
+ });
610
+ return hit;
611
+ }
612
+ function clausesOf(cb, tainted) {
613
+ const out = [];
614
+ walk(cb.body, (x) => {
615
+ if (!ts3.isCallExpression(x)) return;
616
+ const c = unwrap(x.expression);
617
+ if (!ts3.isPropertyAccessExpression(c) || !RANGE_METHODS.has(c.name.text)) return;
618
+ const a0 = x.arguments[0];
619
+ if (!a0 || !ts3.isStringLiteralLike(a0)) return;
620
+ const value = x.arguments[1];
621
+ out.push({
622
+ field: a0.text,
623
+ conds: gatingConds(x, cb),
624
+ viaArgument: value !== void 0 && (readsTainted(value, tainted) || readsCallbackArgument(value, tainted))
625
+ });
626
+ });
627
+ return out;
628
+ }
629
+ function queryInfoOf(recv, queryVars, tainted) {
630
+ const info = {
631
+ clauses: [],
632
+ opaque: false,
633
+ indexes: [],
634
+ reads: false
635
+ };
636
+ walk(recv, (x) => {
637
+ if (ts3.isCallExpression(x)) {
638
+ const c = unwrap(x.expression);
639
+ if (!ts3.isPropertyAccessExpression(c)) return;
640
+ const m = c.name.text;
641
+ if (m === "query") info.reads = true;
642
+ if (m === "withIndex" || m === "withSearchIndex") {
643
+ info.reads = true;
644
+ const name = x.arguments[0];
645
+ if (name && ts3.isStringLiteralLike(name)) info.indexes.push(name.text);
646
+ const cb = x.arguments[1];
647
+ if (cb === void 0) return;
648
+ if (isFnExpr(cb)) info.clauses.push(...clausesOf(unwrap(cb), tainted));
649
+ else info.opaque = true;
650
+ }
651
+ } else if (ts3.isIdentifier(x)) {
652
+ const p = x.parent;
653
+ if (p && ts3.isPropertyAccessExpression(p) && p.name === x) return;
654
+ const q = queryVars.get(x.text);
655
+ if (q) {
656
+ info.reads = true;
657
+ info.clauses.push(...q.clauses);
658
+ info.indexes.push(...q.indexes);
659
+ if (q.opaque) info.opaque = true;
660
+ }
661
+ }
662
+ });
663
+ return info.reads ? info : void 0;
664
+ }
665
+ function terminalReads(e, queryVars, tainted, cache) {
666
+ const out = [];
667
+ walk(e, (x) => {
668
+ if (!ts3.isCallExpression(x)) return;
669
+ const c = unwrap(x.expression);
670
+ if (!ts3.isPropertyAccessExpression(c) || !TERMINALS.has(c.name.text)) return;
671
+ const info = queryInfoOf(c.expression, queryVars, tainted);
672
+ if (!info) return;
673
+ const path = cache.get(x) ?? {
674
+ node: x,
675
+ line: lineOf(x),
676
+ clauses: [],
677
+ opaque: false,
678
+ indexes: []
679
+ };
680
+ path.clauses = info.clauses;
681
+ path.opaque = info.opaque;
682
+ path.indexes = info.indexes;
683
+ cache.set(x, path);
684
+ out.push(path);
685
+ });
686
+ return out;
687
+ }
688
+ function fieldsOfRow(body, name) {
689
+ const fields = /* @__PURE__ */ new Set();
690
+ let opaque = false;
691
+ walk(body, (x) => {
692
+ if (!ts3.isIdentifier(x) || x.text !== name) return;
693
+ const p = x.parent;
694
+ if (p && ts3.isPropertyAccessExpression(p) && p.expression === x) {
695
+ fields.add(p.name.text);
696
+ return;
697
+ }
698
+ if (p && ts3.isElementAccessExpression(p) && p.expression === x && ts3.isStringLiteralLike(p.argumentExpression)) {
699
+ fields.add(p.argumentExpression.text);
700
+ return;
701
+ }
702
+ if (p && ts3.isPropertyAccessExpression(p) && p.name === x) return;
703
+ if (p && ts3.isPropertyAssignment(p) && p.name === x) return;
704
+ opaque = true;
705
+ });
706
+ return { fields, opaque };
707
+ }
708
+ function predicateFields(cb) {
709
+ const p = cb.parameters[0];
710
+ if (!p) return void 0;
711
+ if (ts3.isIdentifier(p.name)) return fieldsOfRow(cb.body, p.name.text);
712
+ if (ts3.isObjectBindingPattern(p.name)) {
713
+ const fields = /* @__PURE__ */ new Set();
714
+ let opaque = false;
715
+ for (const el of p.name.elements) {
716
+ if (el.dotDotDotToken) opaque = true;
717
+ else if (ts3.isIdentifier(el.propertyName ?? el.name))
718
+ fields.add((el.propertyName ?? el.name).getText());
719
+ else opaque = true;
720
+ }
721
+ return { fields, opaque };
722
+ }
723
+ return { fields: /* @__PURE__ */ new Set(), opaque: true };
724
+ }
725
+ function analyse(ctx, run) {
726
+ const { fn } = run;
727
+ const body = fn.body;
728
+ if (!body) return;
729
+ const key = `${run.rel}:${fn.pos}:${[...run.tainted].sort().join(",")}:${[...run.rowsIn.keys()].sort().join(",")}`;
730
+ if (ctx.memo.has(key) && run.depth > 0) return;
731
+ ctx.memo.add(key);
732
+ const tainted = argTaint(fn, run.tainted, run.base);
733
+ const scanned = /* @__PURE__ */ new Map();
734
+ const queryVars = /* @__PURE__ */ new Map();
735
+ fn.parameters.forEach((p, i) => {
736
+ const rows = run.rowsIn.get(i);
737
+ if (rows && ts3.isIdentifier(p.name)) scanned.set(p.name.text, new Set(rows));
738
+ });
739
+ const pathsOfRows = (e) => {
740
+ if (!e) return [];
741
+ const x = unwrap(e);
742
+ if (ts3.isIdentifier(x)) return [...scanned.get(x.text) ?? []];
743
+ if (ts3.isPropertyAccessExpression(x))
744
+ return x.name.text === "page" ? pathsOfRows(x.expression) : [];
745
+ if (ts3.isArrayLiteralExpression(x))
746
+ return x.elements.flatMap((el) => ts3.isSpreadElement(el) ? pathsOfRows(el.expression) : []);
747
+ if (ts3.isConditionalExpression(x))
748
+ return [...pathsOfRows(x.whenTrue), ...pathsOfRows(x.whenFalse)];
749
+ if (ts3.isBinaryExpression(x)) {
750
+ const op = x.operatorToken.kind;
751
+ if (op === ts3.SyntaxKind.QuestionQuestionToken || op === ts3.SyntaxKind.BarBarToken)
752
+ return [...pathsOfRows(x.left), ...pathsOfRows(x.right)];
753
+ return [];
754
+ }
755
+ if (ts3.isCallExpression(x)) {
756
+ const c = unwrap(x.expression);
757
+ if (ts3.isPropertyAccessExpression(c))
758
+ return ROWS_METHODS.has(c.name.text) ? pathsOfRows(c.expression) : [];
759
+ if (ts3.isIdentifier(c)) return x.arguments.flatMap((a) => pathsOfRows(a));
760
+ }
761
+ return [];
762
+ };
763
+ const record = (node, kind, args, fields, opaque, paths) => {
764
+ if (paths.length === 0) return;
765
+ const a = ctx.acc.get(node) ?? {
766
+ node,
767
+ rel: run.rel,
768
+ line: lineOf(node),
769
+ unit: run.unit,
770
+ kind,
771
+ args: /* @__PURE__ */ new Set(),
772
+ shown: /* @__PURE__ */ new Set(),
773
+ fields: /* @__PURE__ */ new Set(),
774
+ opaque: false,
775
+ paths: /* @__PURE__ */ new Set()
776
+ };
777
+ for (const n of args.names) a.args.add(n);
778
+ for (const n of args.shown) {
779
+ a.shown.add(n);
780
+ a.args.add(n);
781
+ }
782
+ for (const f of fields) a.fields.add(f);
783
+ if (opaque) a.opaque = true;
784
+ for (const p of paths) a.paths.add(p);
785
+ ctx.acc.set(node, a);
786
+ };
787
+ const assign = (name, init) => {
788
+ const own = terminalReads(init, queryVars, tainted, ctx.cache);
789
+ for (const p of own) ctx.reads.add(p);
790
+ const inherited = pathsOfRows(init);
791
+ const all = [...own, ...inherited];
792
+ if (all.length > 0) {
793
+ const s = scanned.get(name) ?? /* @__PURE__ */ new Set();
794
+ for (const p of all) s.add(p);
795
+ scanned.set(name, s);
796
+ return;
797
+ }
798
+ const q = queryInfoOf(init, queryVars, tainted);
799
+ if (q) {
800
+ const prev = queryVars.get(name);
801
+ queryVars.set(name, {
802
+ clauses: [...prev?.clauses ?? [], ...q.clauses],
803
+ opaque: (prev?.opaque ?? false) || q.opaque,
804
+ indexes: [...prev?.indexes ?? [], ...q.indexes]
805
+ });
806
+ }
807
+ };
808
+ for (let pass = 0; pass < 2; pass++)
809
+ walk(body, (n) => {
810
+ if (ts3.isVariableDeclaration(n) && n.initializer) {
811
+ const names = /* @__PURE__ */ new Set();
812
+ bindingNames(n.name, names);
813
+ for (const nm of names) assign(nm, n.initializer);
814
+ } else if (ts3.isBinaryExpression(n) && n.operatorToken.kind === ts3.SyntaxKind.EqualsToken && ts3.isIdentifier(n.left))
815
+ assign(n.left.text, n.right);
816
+ else if (ts3.isReturnStatement(n) && n.expression)
817
+ for (const p of terminalReads(n.expression, queryVars, tainted, ctx.cache))
818
+ ctx.reads.add(p);
819
+ });
820
+ const closures = /* @__PURE__ */ new Map();
821
+ walk(body, (n) => {
822
+ if (ts3.isVariableDeclaration(n) && ts3.isIdentifier(n.name) && n.initializer && isFnExpr(n.initializer))
823
+ closures.set(n.name.text, unwrap(n.initializer));
824
+ });
825
+ walk(body, (n) => {
826
+ if (ts3.isCallExpression(n)) {
827
+ const c = unwrap(n.expression);
828
+ if (ts3.isPropertyAccessExpression(c) && c.name.text === "filter") {
829
+ const cb = n.arguments[0];
830
+ if (!cb || !isFnExpr(cb)) return;
831
+ const f = unwrap(cb);
832
+ let paths = pathsOfRows(c.expression);
833
+ if (paths.length === 0) paths = terminalReads(c.expression, queryVars, tainted, ctx.cache);
834
+ if (paths.length === 0) return;
835
+ const args = taintReads(f, tainted);
836
+ if (args.names.size === 0) return;
837
+ const pf = predicateFields(f);
838
+ if (!pf || pf.fields.size === 0 && !pf.opaque) return;
839
+ record(n, "filter", args, pf.fields, pf.opaque, paths);
840
+ return;
841
+ }
842
+ if (ts3.isIdentifier(c) && run.depth < 3) {
843
+ const handed = /* @__PURE__ */ new Map();
844
+ n.arguments.forEach((a, i) => {
845
+ const ps = pathsOfRows(a);
846
+ if (ps.length > 0) handed.set(i, ps);
847
+ });
848
+ if (handed.size === 0) return;
849
+ const taintedArgs = /* @__PURE__ */ new Set();
850
+ n.arguments.forEach((a, i) => {
851
+ if (readsTainted(a, tainted)) taintedArgs.add(i);
852
+ });
853
+ const local = closures.get(c.text);
854
+ if (local) {
855
+ analyse(ctx, {
856
+ fn: local,
857
+ rel: run.rel,
858
+ unit: run.unit,
859
+ tainted: taintedArgs,
860
+ base: tainted,
861
+ rowsIn: handed,
862
+ depth: run.depth + 1
863
+ });
864
+ return;
865
+ }
866
+ const defs = ctx.idx.get(c.text);
867
+ if (defs && defs.length === 1)
868
+ analyse(ctx, {
869
+ fn: defs[0].fn,
870
+ rel: defs[0].rel,
871
+ unit: c.text,
872
+ tainted: taintedArgs,
873
+ base: /* @__PURE__ */ new Set(),
874
+ rowsIn: handed,
875
+ depth: run.depth + 1
876
+ });
877
+ }
878
+ return;
879
+ }
880
+ if (ts3.isForOfStatement(n) && ts3.isVariableDeclarationList(n.initializer)) {
881
+ const d = n.initializer.declarations[0];
882
+ if (!d || !ts3.isIdentifier(d.name)) return;
883
+ let paths = pathsOfRows(n.expression);
884
+ if (paths.length === 0) paths = terminalReads(n.expression, queryVars, tainted, ctx.cache);
885
+ if (paths.length === 0) return;
886
+ const rowVar = d.name.text;
887
+ walk(n.statement, (x) => {
888
+ if (!ts3.isIfStatement(x)) return;
889
+ const skips = (s) => ts3.isContinueStatement(s) || ts3.isBlock(s) && s.statements.some((y) => ts3.isContinueStatement(y));
890
+ let keeps = false;
891
+ walk(x.thenStatement, (y) => {
892
+ if (!ts3.isCallExpression(y)) return;
893
+ const cc = unwrap(y.expression);
894
+ if (ts3.isPropertyAccessExpression(cc) && cc.name.text === "push" && y.arguments.some((a) => readsTainted(a, /* @__PURE__ */ new Set([rowVar]))))
895
+ keeps = true;
896
+ });
897
+ if (!skips(x.thenStatement) && !keeps) return;
898
+ const args = taintReads(x.expression, tainted);
899
+ if (args.names.size === 0) return;
900
+ const pf = fieldsOfRow(x.expression, rowVar);
901
+ if (pf.fields.size === 0 && !pf.opaque) return;
902
+ record(x, "loop", args, pf.fields, pf.opaque, paths);
903
+ });
904
+ }
905
+ });
906
+ }
907
+ function covers(path, acc, pathFacts) {
908
+ const facts = {
909
+ defined: /* @__PURE__ */ new Set([...pathFacts.defined, ...acc.args]),
910
+ undef: new Set([...pathFacts.undef].filter((n) => !acc.args.has(n)))
911
+ };
912
+ for (const f of acc.fields) {
913
+ const hit = path.clauses.some(
914
+ (cl) => cl.field === f && cl.viaArgument && cl.conds.every((c) => evalCond(c.cond, facts) === c.truth)
915
+ );
916
+ if (!hit) return false;
917
+ }
918
+ return true;
919
+ }
920
+ function unreadableRegistrations(p) {
921
+ const readable = new Set(
922
+ registrations(p).filter((r) => r.builder === "query").map((r) => r.name)
923
+ );
924
+ const out = [];
925
+ for (const st of p.sf.statements) {
926
+ if (!ts3.isVariableStatement(st)) continue;
927
+ for (const d of st.declarationList.declarations) {
928
+ if (!ts3.isIdentifier(d.name) || !d.initializer) continue;
929
+ const init = unwrap(d.initializer);
930
+ if (!ts3.isCallExpression(init)) continue;
931
+ const callee = unwrap(init.expression);
932
+ if (!ts3.isIdentifier(callee) || callee.text !== "query") continue;
933
+ if (!readable.has(d.name.text)) out.push(`${d.name.text} (line ${lineOf(d)})`);
934
+ }
935
+ }
936
+ return out;
937
+ }
938
+ function scanR4FilterPaths(files) {
939
+ const scan = emptyScan();
940
+ scan.reads = 0;
941
+ const judged = files.filter((f) => isJudgeable(f) && isConvexFile(f));
942
+ const ps = [];
943
+ for (const f of judged) {
944
+ const p = parseFile(f);
945
+ const diags = p.sf.parseDiagnostics;
946
+ if (diags && diags.length > 0) {
947
+ const d = diags[0];
948
+ const msg = ts3.flattenDiagnosticMessageText(d.messageText, " ");
949
+ scan.cnj.push({
950
+ rel: f.rel,
951
+ n: d.start === void 0 ? 1 : p.sf.getLineAndCharacterOfPosition(d.start).line + 1,
952
+ note: `${f.rel}: the file does not parse (${msg}) \u2014 a list read in it could not be read`
953
+ });
954
+ continue;
955
+ }
956
+ ps.push(p);
957
+ }
958
+ const idx = indexFunctions(ps);
959
+ const ctx = { idx, acc: /* @__PURE__ */ new Map(), reads: /* @__PURE__ */ new Set(), cache: /* @__PURE__ */ new Map(), memo: /* @__PURE__ */ new Set() };
960
+ for (const p of ps)
961
+ for (const name of unreadableRegistrations(p))
962
+ scan.cnj.push({
963
+ rel: p.rel,
964
+ n: Number(/line (\d+)/.exec(name)?.[1] ?? 1),
965
+ note: `${name.replace(/ \(line \d+\)$/, "")}: a public query whose handler is not an inline function \u2014 the reads it makes could not be read`
966
+ });
967
+ const doors = [];
968
+ for (const p of ps)
969
+ for (const r of registrations(p))
970
+ if (r.builder === "query") doors.push({ name: r.name, rel: r.rel, fn: r.handler });
971
+ const taintOf = /* @__PURE__ */ new Map();
972
+ const unitOf = /* @__PURE__ */ new Map();
973
+ const queue = [];
974
+ for (const d of doors) {
975
+ taintOf.set(d.fn, /* @__PURE__ */ new Set([1]));
976
+ unitOf.set(d.fn, { name: d.name, rel: d.rel });
977
+ queue.push(d.fn);
978
+ }
979
+ while (queue.length) {
980
+ const fn = queue.pop();
981
+ const t = argTaint(fn, taintOf.get(fn) ?? /* @__PURE__ */ new Set(), /* @__PURE__ */ new Set());
982
+ for (const name of calledNames(fn)) {
983
+ for (const def of idx.get(name) ?? []) {
984
+ const want = taintOf.get(def.fn) ?? /* @__PURE__ */ new Set();
985
+ const before = want.size;
986
+ walk(fn, (x) => {
987
+ if (!ts3.isCallExpression(x)) return;
988
+ const c = unwrap(x.expression);
989
+ if (!ts3.isIdentifier(c) || c.text !== name) return;
990
+ x.arguments.forEach((a, i) => {
991
+ if (readsTainted(a, t)) want.add(i);
992
+ });
993
+ });
994
+ const first = !taintOf.has(def.fn);
995
+ taintOf.set(def.fn, want);
996
+ if (first) unitOf.set(def.fn, { name, rel: def.rel });
997
+ if (first || want.size !== before) queue.push(def.fn);
998
+ }
999
+ }
1000
+ }
1001
+ for (const [fn, u] of unitOf)
1002
+ analyse(ctx, {
1003
+ fn,
1004
+ rel: u.rel,
1005
+ unit: u.name,
1006
+ tainted: taintOf.get(fn) ?? /* @__PURE__ */ new Set(),
1007
+ base: /* @__PURE__ */ new Set(),
1008
+ rowsIn: /* @__PURE__ */ new Map(),
1009
+ depth: 0
1010
+ });
1011
+ scan.reads = ctx.reads.size;
1012
+ const sites = [...ctx.acc.values()].sort((a, b) => a.rel.localeCompare(b.rel) || a.line - b.line);
1013
+ for (const a of sites) {
1014
+ const rootOf = (n) => {
1015
+ for (let x = n; x; x = x.parent)
1016
+ if (ts3.isFunctionLike(x) && unitOf.has(x)) return x;
1017
+ return n.getSourceFile();
1018
+ };
1019
+ const argList = [...a.shown].map((n) => `\`${n}\``).join(", ");
1020
+ const fieldList = [...a.fields].join(", ") || "the row";
1021
+ const what = a.kind === "loop" ? `a loop skips rows on ${argList} (row ${fieldList})` : `filters in memory on ${argList} (row ${fieldList})`;
1022
+ if (a.opaque) {
1023
+ scan.cnj.push({
1024
+ rel: a.rel,
1025
+ n: a.line,
1026
+ note: `${a.unit}: line ${a.line} ${what}, but the predicate takes the row as a whole, so the field it reads cannot be attributed \u2014 whether the index range bounds it is not decided`
1027
+ });
1028
+ continue;
1029
+ }
1030
+ const live = [];
1031
+ const uncovered = [];
1032
+ const unknown = [];
1033
+ for (const p of a.paths) {
1034
+ const pf = factsAt(p.node, rootOf(p.node));
1035
+ if ([...a.args].some((n) => pf.undef.has(n))) continue;
1036
+ live.push(p);
1037
+ if (covers(p, a, pf)) continue;
1038
+ if (p.opaque) unknown.push(p);
1039
+ else uncovered.push(p);
1040
+ }
1041
+ if (uncovered.length > 0) {
1042
+ const lines = [...new Set(uncovered.map((p) => p.line))].sort((x, y) => x - y);
1043
+ const idxNames = [...new Set(uncovered.flatMap((p) => p.indexes))];
1044
+ scan.violate.push({
1045
+ rel: a.rel,
1046
+ n: a.line,
1047
+ note: `${a.unit}: line ${a.line} ${what} after reading rows; ${uncovered.length} of ${live.length} read path(s) are not bounded by the index range on ${[...a.fields].join(", ")} (read at line ${lines.join(", ")}${idxNames.length ? `; index ${idxNames.join(", ")}` : "; no index"}) \u2014 a scan that filters after the read (R-4 unindexed-scan)`
1048
+ });
1049
+ } else if (unknown.length > 0) {
1050
+ scan.cnj.push({
1051
+ rel: a.rel,
1052
+ n: a.line,
1053
+ note: `${a.unit}: line ${a.line} ${what}; ${unknown.length} read path(s) use a .withIndex callback that is not inline, so their range could not be read`
1054
+ });
1055
+ } else {
1056
+ scan.conform.push({
1057
+ rel: a.rel,
1058
+ n: a.line,
1059
+ note: `${a.unit}: line ${a.line} ${what}; the index range already bounds ${[...a.fields].join(", ")} on ${live.length} live read path(s)`
1060
+ });
1061
+ }
1062
+ }
1063
+ return scan;
1064
+ }
1065
+
1066
+ export {
1067
+ sourceOf,
1068
+ parseFile,
1069
+ lineOf,
1070
+ unwrap,
1071
+ propRead,
1072
+ constInit,
1073
+ propReadResolved,
1074
+ walk,
1075
+ dotted,
1076
+ calleeName,
1077
+ idTableOf,
1078
+ singleIdTable,
1079
+ registrations,
1080
+ indexFunctions,
1081
+ calledNames,
1082
+ closeOverCalls,
1083
+ TENANT_COLUMNS,
1084
+ NAMESPACE_COLUMN,
1085
+ readSchema,
1086
+ emptyScan,
1087
+ base,
1088
+ isJudgeable,
1089
+ declaredNames,
1090
+ readsTainted,
1091
+ R4_SOURCE_ID,
1092
+ scanR4FilterPaths
1093
+ };