@telorun/templating 0.107.0 → 0.109.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/dist/builtins.d.ts +2 -2
  2. package/dist/builtins.d.ts.map +1 -1
  3. package/dist/cel/analyze.d.ts +6 -6
  4. package/dist/cel/analyze.d.ts.map +1 -1
  5. package/dist/cel/analyze.js +138 -183
  6. package/dist/cel/compile.d.ts +47 -17
  7. package/dist/cel/compile.d.ts.map +1 -1
  8. package/dist/cel/compile.js +135 -83
  9. package/dist/cel/diagnose.d.ts +52 -57
  10. package/dist/cel/diagnose.d.ts.map +1 -1
  11. package/dist/cel/diagnose.js +69 -291
  12. package/dist/cel/environment.d.ts +33 -37
  13. package/dist/cel/environment.d.ts.map +1 -1
  14. package/dist/cel/environment.js +54 -86
  15. package/dist/cel/module-call.d.ts +68 -79
  16. package/dist/cel/module-call.d.ts.map +1 -1
  17. package/dist/cel/module-call.js +118 -194
  18. package/dist/cel/value-brands.d.ts +14 -12
  19. package/dist/cel/value-brands.d.ts.map +1 -1
  20. package/dist/cel/value-brands.js +26 -24
  21. package/dist/cel/verdict-codes.d.ts +13 -2
  22. package/dist/cel/verdict-codes.d.ts.map +1 -1
  23. package/dist/cel/verdict-codes.js +20 -1
  24. package/dist/engine.d.ts +4 -4
  25. package/dist/engine.d.ts.map +1 -1
  26. package/dist/engines/cel.d.ts +2 -2
  27. package/dist/engines/cel.d.ts.map +1 -1
  28. package/dist/engines/cel.js +53 -81
  29. package/dist/engines/hole-analysis.d.ts +3 -3
  30. package/dist/engines/hole-analysis.d.ts.map +1 -1
  31. package/dist/engines/interpolate.js +11 -8
  32. package/dist/index.d.ts +4 -5
  33. package/dist/index.d.ts.map +1 -1
  34. package/dist/index.js +8 -5
  35. package/package.json +4 -4
  36. package/src/builtins.ts +2 -2
  37. package/src/cel/analyze.ts +152 -193
  38. package/src/cel/compile.ts +160 -77
  39. package/src/cel/diagnose.ts +115 -370
  40. package/src/cel/environment.ts +64 -87
  41. package/src/cel/module-call.ts +129 -292
  42. package/src/cel/value-brands.ts +26 -25
  43. package/src/cel/verdict-codes.ts +20 -1
  44. package/src/engine.ts +4 -4
  45. package/src/engines/cel.ts +52 -84
  46. package/src/engines/hole-analysis.ts +3 -3
  47. package/src/engines/interpolate.ts +16 -13
  48. package/src/index.ts +8 -11
  49. package/dist/cel/catalog.d.ts +0 -70
  50. package/dist/cel/catalog.d.ts.map +0 -1
  51. package/dist/cel/catalog.js +0 -1320
  52. package/dist/cel/json-prefix-scan.d.ts +0 -17
  53. package/dist/cel/json-prefix-scan.d.ts.map +0 -1
  54. package/dist/cel/json-prefix-scan.js +0 -160
  55. package/dist/cel/value-identity.d.ts +0 -18
  56. package/dist/cel/value-identity.d.ts.map +0 -1
  57. package/dist/cel/value-identity.js +0 -25
  58. package/src/cel/catalog.ts +0 -1455
  59. package/src/cel/json-prefix-scan.ts +0 -152
  60. package/src/cel/value-identity.ts +0 -34
@@ -1,386 +1,131 @@
1
- import type { ASTNode, Environment } from "@marcbachmann/cel-js";
2
- import { CEL_FUNCTIONS, type CelFunctionDoc } from "./catalog.js";
3
- import { moduleCallOf } from "./module-call.js";
4
- import type { CallSite, DiagnosticFix, EngineDiagnostic } from "../engine.js";
5
- import type { CelCallAudit, CelDiagnostic } from "./verdict-codes.js";
6
-
7
- /** Classifies every function call in a CEL expression against the environment's
8
- * own function registry.
1
+ /**
2
+ * Every function call in a CEL expression, reported for the caller that judges it.
9
3
  *
10
- * This exists because cel-js reports one sentence for three unrelated mistakes
11
- * — a name that does not exist, a name called in the wrong form, and a genuine
12
- * type mismatch all surface as `found no matching overload for 'f(...)'`. Two
13
- * of those readings actively mislead: the message names argument types, so the
14
- * repair for `startsWith(key, 'x')` looks like a cast, when the real fix is
15
- * `key.startsWith('x')` and no cast helps.
4
+ * **The classifier is gone, and so is its reason for existing.** This file used to decide
5
+ * `CEL_UNKNOWN_FUNCTION`, `CEL_WRONG_CALL_FORM` and `CEL_INVALID_ARGUMENT` itself, by
6
+ * looking every call up in the environment's registry — because the engine it drove reported
7
+ * one sentence for three unrelated mistakes (`found no matching overload for 'f(...)'`), two
8
+ * of whose readings actively misled: the message named argument types, so the repair for
9
+ * `startsWith(key, 'x')` looked like a cast when the fix was `key.startsWith('x')`.
16
10
  *
17
- * Nothing here reads cel-js's message text. `Environment.getDefinitions()`
18
- * reports every registered signature — cel-js builtins and Telo's catalog
19
- * alike — with its call form and parameters, and the AST distinguishes `f(x)`
20
- * from `x.f()` structurally. Name existence, call form and arity are therefore
21
- * decidable by lookup, which is what keeps a cel-js version bump from silently
22
- * degrading this back into the passthrough it replaced. */
23
-
24
- /** One registered signature, reduced to what classification needs. */
25
- interface FnEntry {
26
- readonly signature: string;
27
- readonly form: "global" | "receiver";
28
- /** Parameter count, excluding the receiver for a receiver form. */
29
- readonly arity: number;
30
- }
31
-
32
- export interface FunctionIndex {
33
- readonly byName: ReadonlyMap<string, readonly FnEntry[]>;
34
- }
35
-
36
- /** Determinism is Telo catalog metadata; cel-js builtins carry none. Absent
37
- * means "no signal", never "deterministic" — consumers of
38
- * `CallSite.deterministic` must not read undefined as a guarantee. */
39
- const DETERMINISM: ReadonlyMap<string, boolean> = new Map(
40
- CEL_FUNCTIONS.map((f) => [f.name, f.deterministic]),
41
- );
42
- const HOST_BACKED: ReadonlyMap<string, boolean> = new Map(
43
- CEL_FUNCTIONS.map((f) => [f.name, f.hostBacked === true]),
44
- );
45
-
46
- /** The flags a module function carries, as the host that resolves it derives
47
- * them — or undefined where the name reaches no function. */
48
- export type ModuleCallFlags = (
49
- qualified: string,
50
- ) => { readonly deterministic: boolean; readonly hostBacked: boolean } | undefined;
51
-
52
- const INDEX_CACHE = new WeakMap<Environment, FunctionIndex>();
53
-
54
- /** Registry view of an environment, memoized: environments are rebuilt per
55
- * analysis path, but each is immutable once built. */
56
- export function functionIndex(env: Environment): FunctionIndex {
57
- const cached = INDEX_CACHE.get(env);
58
- if (cached) return cached;
59
-
60
- const byName = new Map<string, FnEntry[]>();
61
-
62
- for (const fn of env.getDefinitions().functions) {
63
- const form = fn.receiverType === null ? "global" : "receiver";
64
- const entry: FnEntry = { signature: fn.signature, form, arity: fn.params?.length ?? 0 };
65
- const entries = byName.get(fn.name);
66
- if (entries) entries.push(entry);
67
- else byName.set(fn.name, [entry]);
68
- }
69
-
70
- const index: FunctionIndex = { byName };
71
- INDEX_CACHE.set(env, index);
72
- return index;
73
- }
74
-
75
- /** A call plus the nodes needed to rewrite it. Internal — `CallSite` is the
76
- * shape that crosses the engine seam. */
77
- interface RawCall extends CallSite {
78
- readonly receiver?: ASTNode;
79
- readonly args: readonly ASTNode[];
80
- }
81
-
82
- /** Macros the parser expands rather than dispatching through the registry, so
83
- * they never appear in `getDefinitions()` and would otherwise classify as
84
- * unknown names.
11
+ * `@telorun/cel` decides each of them where the cause is known, with a range and a whole-
12
+ * source fix — an unknown name, a name called in the other form, a type no overload takes,
13
+ * and a refusal over a literal argument (`checkLiteralArguments`, which runs the very code
14
+ * the evaluation runs). So nothing here classifies, and nothing reads a message.
85
15
  *
86
- * Deliberately short: most macros ARE registered, and the caller only reports
87
- * this audit when the type-checker already rejected the expression, so a macro
88
- * missing from here degrades to "no extra explanation" rather than to a false
89
- * error on valid CEL. That is what keeps a cel-js upgrade from turning a new
90
- * macro into a manifest this analyzer refuses. */
91
- const MACROS = new Set(["optMap", "optFlatMap", "bind"]);
92
-
93
- function isNode(v: unknown): v is ASTNode {
94
- return typeof v === "object" && v !== null && "op" in (v as Record<string, unknown>);
95
- }
96
-
97
- /** Every non-macro call in the expression, in source order. */
98
- function collectCalls(
99
- root: ASTNode,
100
- index: FunctionIndex,
101
- moduleCallFlags: ModuleCallFlags | undefined,
102
- ): RawCall[] {
103
- const out: RawCall[] = [];
104
- visit(root);
105
- return out.sort((a, b) => a.start - b.start);
106
-
107
- function visit(node: ASTNode): void {
108
- const args = node.args as unknown;
109
- // A RESOLVED module call is reported under its QUALIFIED name and is never
110
- // looked up in the catalog: the two namespaces are disjoint by construction
111
- // (a bare name is always the catalog), so classifying it here would report
112
- // `there is no function 'format'` for a call that names a module's own, and
113
- // attaching the catalog's determinism to it would be a flag about a
114
- // different function entirely. What a module call's flags are is the
115
- // resolving host's answer, carried when it gives one.
116
- const moduleCall = moduleCallOf(node);
117
- if (moduleCall) {
118
- const flags = moduleCallFlags?.(moduleCall.qualified);
119
- out.push({
120
- name: moduleCall.qualified,
121
- form: "receiver",
122
- moduleCall: true,
123
- arity: moduleCall.args.length,
124
- start: node.start,
125
- end: node.end,
126
- ...(flags ? { deterministic: flags.deterministic, hostBacked: flags.hostBacked } : {}),
127
- args: moduleCall.args,
128
- });
129
- for (const arg of moduleCall.args) visit(arg);
130
- return;
131
- }
132
- if (node.op === "call" || node.op === "rcall") {
133
- const tuple = args as unknown[];
134
- const name = tuple[0];
135
- if (typeof name === "string" && !MACROS.has(name)) {
136
- const receiver = node.op === "rcall" ? tuple[1] : undefined;
137
- const rawArgs = node.op === "rcall" ? tuple[2] : tuple[1];
138
- const callArgs = (Array.isArray(rawArgs) ? rawArgs : []).filter(isNode);
139
- out.push({
140
- name,
141
- form: node.op === "rcall" ? "receiver" : "global",
142
- arity: callArgs.length,
143
- start: node.start,
144
- end: node.end,
145
- ...(index.byName.has(name)
146
- ? { deterministic: DETERMINISM.get(name), hostBacked: HOST_BACKED.get(name) }
147
- : {}),
148
- ...(isNode(receiver) ? { receiver } : {}),
149
- args: callArgs,
150
- });
151
- }
152
- }
153
- for (const arg of Array.isArray(args) ? args : [args]) {
154
- if (isNode(arg)) visit(arg);
155
- else if (Array.isArray(arg)) for (const item of arg) if (isNode(item)) visit(item);
156
- }
157
- }
158
- }
159
-
160
- /** Node shapes that can carry a `.` on their right without reparsing
161
- * differently. Everything else — an operator expression, a literal that would
162
- * sit against the dot — is parenthesized when moved into receiver position. */
163
- const SELF_DELIMITING = new Set<string>(["id", ".", ".?", "call", "rcall", "[]", "[?]"]);
164
-
165
- /** Source text of a node. When it is about to become a receiver it may need
166
- * parentheses: an identifier, member chain, index or call is self-delimiting,
167
- * but an operator expression or a bare literal against a `.` is not. */
168
- function nodeText(source: string, node: ASTNode, asReceiver = false): string {
169
- const text = source.slice(node.start, node.end);
170
- if (!asReceiver) return text;
171
- return SELF_DELIMITING.has(node.op) ? text : `(${text})`;
172
- }
173
-
174
- /** The same call written in the other form, or undefined when the shape does
175
- * not allow it (a global call with no arguments has no receiver to move). */
176
- function transpose(source: string, call: RawCall): string | undefined {
177
- if (call.form === "global") {
178
- const [first, ...rest] = call.args;
179
- if (!first) return undefined;
180
- const argText = rest.map((a) => nodeText(source, a));
181
- return `${nodeText(source, first, true)}.${call.name}(${argText.join(", ")})`;
182
- }
183
- if (!call.receiver) return undefined;
184
- const argText = [nodeText(source, call.receiver), ...call.args.map((a) => nodeText(source, a))];
185
- return `${call.name}(${argText.join(", ")})`;
186
- }
187
-
188
- /** Splice a rewritten call back into the full source. The fix always carries
189
- * the whole corrected source, so a consumer applies it by replacing the
190
- * scalar. */
191
- function spliceFix(source: string, call: RawCall, rewritten: string): DiagnosticFix {
192
- return { replacement: source.slice(0, call.start) + rewritten + source.slice(call.end) };
193
- }
194
-
195
- /** Replace only the called name, leaving arguments untouched. The offset is
196
- * derived rather than searched: a receiver whose own text contains the name
197
- * (`slice.slice(1)`) would defeat a first-occurrence replace. */
198
- function renameFix(source: string, call: RawCall, to: string): DiagnosticFix | undefined {
199
- const searchFrom = call.receiver ? call.receiver.end : call.start;
200
- const at = source.indexOf(call.name, searchFrom);
201
- if (at === -1 || at >= call.end) return undefined;
202
- return { replacement: source.slice(0, at) + to + source.slice(at + call.name.length) };
203
- }
204
-
205
- /** Arity the call would need in the other form: moving a receiver in adds an
206
- * argument, moving it out removes one. */
207
- function transposedArity(call: RawCall): number {
208
- return call.form === "global" ? call.arity - 1 : call.arity + 1;
209
- }
210
-
211
- function accepts(entries: readonly FnEntry[], form: CallSite["form"], arity: number): boolean {
212
- return entries.some((e) => e.form === form && e.arity === arity);
213
- }
214
-
215
- const listOf = (names: readonly string[]): string => [...new Set(names)].sort().join(", ");
216
-
217
- /** Names registered in exactly the form and arity the author wrote — the only
218
- * ones that could replace this call with no further edits. Ranked by shared
219
- * prefix, which is what reaches `nowIso` from `no`; edit distance never
220
- * would (4 characters against a 2-character name). Returns [] when nothing
221
- * shares a prefix, so the caller lists what is legal here instead of
222
- * guessing. */
223
- function candidates(call: RawCall, index: FunctionIndex): string[] {
224
- const written = call.name.toLowerCase();
225
- return callableHere(call, index)
226
- .filter((name) => {
227
- const lower = name.toLowerCase();
228
- return lower.startsWith(written) || written.startsWith(lower);
229
- })
230
- .sort((a, b) => a.length - b.length || a.localeCompare(b));
231
- }
232
-
233
- /** Every name callable in exactly the position written — same form, same
234
- * argument count. Arity is what makes the list usable: a receiver's type is
235
- * `dyn` at analysis time so every method is nominally reachable, and printing
236
- * all forty says nothing. */
237
- function callableHere(call: RawCall, index: FunctionIndex): string[] {
238
- const names: string[] = [];
239
- for (const [name, entries] of index.byName) {
240
- if (name !== call.name && accepts(entries, call.form, call.arity)) names.push(name);
241
- }
242
- return names;
243
- }
244
-
245
- function signaturesOf(name: string, index: FunctionIndex): string[] {
246
- return (index.byName.get(name) ?? []).map((e) => e.signature);
247
- }
248
-
249
- /** Spread-friendly optional `fix`, so an undecidable rewrite simply omits the
250
- * field rather than carrying `undefined` into the diagnostic. */
251
- const withFix = (fix: DiagnosticFix | undefined): { fix?: DiagnosticFix } => (fix ? { fix } : {});
252
-
253
- /** The literal value of an argument, or `undefined` when it is an expression
254
- * whose value is not statically known. A negated numeric literal is one node
255
- * out (`-1` parses as unary minus over a value), and reading it is what lets a
256
- * bound like "0–10" catch the below-range case as well as the above. */
257
- function literalOf(node: ASTNode): unknown {
258
- if (node.op === "value") return node.args;
259
- if (node.op === "-_") {
260
- const inner = node.args;
261
- if (isNode(inner) && inner.op === "value") {
262
- const v = inner.args as unknown;
263
- if (typeof v === "number") return -v;
264
- if (typeof v === "bigint") return -v;
265
- }
266
- }
267
- return undefined;
268
- }
269
-
270
- const ARG_CHECKS: ReadonlyMap<string, NonNullable<CelFunctionDoc["checkArgs"]>> = new Map(
271
- CEL_FUNCTIONS.flatMap((f) => (f.checkArgs ? [[f.name, f.checkArgs] as const] : [])),
272
- );
273
-
16
+ * What is left is Telo's half: the call LIST, which the analyzer needs to judge whether a
17
+ * module call reaches a function at all (`FUNCTION_UNRESOLVED` / `_NOT_EXPORTED` /
18
+ * `_NOT_CALLABLE` / `_ARITY_MISMATCH` / `_ARGUMENT_MISMATCH`) — a verdict that is manifest
19
+ * context and never this engine's.
20
+ */
21
+ import type { CelEnvironment, CelNode, CheckResult } from "@telorun/cel";
22
+ import { moduleCallArgumentChains } from "./analyze.js";
23
+ import type { CallSite, EngineDiagnostic } from "../engine.js";
24
+ import type { CelCallAudit } from "./verdict-codes.js";
25
+
26
+ /**
27
+ * What one audit establishes. The diagnostic lists stay on the shape because the ANALYZER
28
+ * reads them: they are now always empty from here — the engine's checker decides every
29
+ * verdict about a call — and a caller that merged two lists keeps doing so unchanged.
30
+ */
274
31
  export interface CallAudit {
275
32
  readonly diagnostics: readonly EngineDiagnostic[];
33
+ /** Every function call in the source, in source order. */
276
34
  readonly calls: readonly CallSite[];
277
- /** Refusals decided from arguments written as literals. Reported whatever the
278
- * type-checker said, unlike {@link CallAudit.diagnostics}: a call whose types
279
- * are all correct and whose specifier is `.2q` type-checks perfectly, and is
280
- * exactly the defect this catches. */
281
- readonly argumentIssues: readonly EngineDiagnostic[];
282
- /** Names that resolve, but that no registered signature accepts as written.
283
- * The caller appends their signatures to a type-check failure it could not
284
- * otherwise explain. */
35
+ /** Names a type-check failure mentions that resolved to nothing. */
285
36
  readonly unresolved: readonly string[];
37
+ /** Refusals over a literal argument. The engine decides these at check now. */
38
+ readonly argumentIssues: readonly EngineDiagnostic[];
286
39
  }
287
40
 
288
- /** Classify every call in `ast`. Runs unconditionally rather than only after a
289
- * failed type-check, because a type-check reports its first error and stops:
290
- * an expression with two bad calls would otherwise fix one, re-run, and
291
- * discover the next. */
41
+ /**
42
+ * The flags a module function carries, as the host that resolves it derives them — or
43
+ * undefined where the name reaches no function. Absent means "no signal", never
44
+ * "deterministic".
45
+ */
46
+ export type ModuleCallFlags = (
47
+ qualified: string,
48
+ ) => { readonly deterministic: boolean; readonly hostBacked: boolean } | undefined;
49
+
50
+ /**
51
+ * Every call the expression makes, with what the checker resolved about it.
52
+ *
53
+ * A module call carries no catalog flag: what a module function's determinism and
54
+ * host-backedness are follows from the callable it resolves to, so they are carried only as
55
+ * the host's `moduleCallFlags` reports them, and stay absent where it gives nothing.
56
+ */
292
57
  export function auditCalls(
293
58
  source: string,
294
- ast: ASTNode,
295
- env: Environment,
296
- /** The flags of the module functions the expression calls, from whoever
297
- * resolves them. */
59
+ root: CelNode,
60
+ environment: CelEnvironment,
298
61
  moduleCallFlags?: ModuleCallFlags,
62
+ checked?: CheckResult,
299
63
  ): CelCallAudit {
300
- const index = functionIndex(env);
301
- const diagnostics: CelDiagnostic[] = [];
302
- const unresolved: string[] = [];
303
- const calls = collectCalls(ast, index, moduleCallFlags);
304
-
305
- for (const call of calls) {
306
- // Resolution already decided what a module call names. The catalog has
307
- // nothing to say about it, and saying it anyway would be a refusal of valid
308
- // CEL.
309
- if (call.moduleCall) continue;
310
- const entries = index.byName.get(call.name);
311
- const noun = call.form === "receiver" ? "method" : "function";
312
-
313
- if (!entries) {
314
- const near = candidates(call, index);
315
- const hint =
316
- near.length > 0
317
- ? `Closest taking ${call.arity} argument${call.arity === 1 ? "" : "s"}: ${near
318
- .slice(0, 5)
319
- .map((n) => `\`${n}\``)
320
- .join(", ")}.`
321
- : `Every ${noun} taking ${call.arity} argument${call.arity === 1 ? "" : "s"}: ${listOf(callableHere(call, index))}.`;
322
- diagnostics.push({
323
- code: "CEL_UNKNOWN_FUNCTION",
324
- message: `there is no ${noun} \`${call.name}\`. ${hint} Full list: \`telo cel functions\`.`,
325
- // A single candidate is an unambiguous rename; several are a menu, and
326
- // applying an arbitrary one would be a guess wearing a fix's clothes.
327
- ...(near.length === 1 ? withFix(renameFix(source, call, near[0]!)) : {}),
328
- });
329
- continue;
330
- }
331
-
332
- if (accepts(entries, call.form, call.arity)) continue;
333
-
334
- const otherForm = call.form === "global" ? "receiver" : "global";
335
- if (accepts(entries, otherForm, transposedArity(call))) {
336
- const rewritten = transpose(source, call);
337
- const written = source.slice(call.start, call.end);
338
- diagnostics.push({
339
- code: "CEL_WRONG_CALL_FORM",
340
- message:
341
- `\`${call.name}\` is ${
342
- call.form === "global"
343
- ? "a method, not a global function — call it on the value"
344
- : "a global function, not a method — pass the value to it"
345
- }:` +
346
- (rewritten ? `\n write: ${rewritten}\n not: ${written}` : "") +
347
- `\nRegistered: ${listOf(signaturesOf(call.name, index))}.`,
348
- ...withFix(rewritten ? spliceFix(source, call, rewritten) : undefined),
349
- });
350
- continue;
351
- }
352
-
353
- unresolved.push(call.name);
64
+ const result = checked ?? environment.check({ source, root, namespaces: environment.namespaces(), diagnostics: [] });
65
+ const argumentChains = moduleCallArgumentChains(root);
66
+ const byNode = new Map(
67
+ [...argumentChains].map(([node, chains]) => [`${node.range[0]}:${node.range[1]}`, chains]),
68
+ );
69
+
70
+ // **One entry per call.** The checker already lists a qualified call — with `namespace`
71
+ // set and `form: "receiver"`, which is how it was written — so appending a second entry
72
+ // per `qualifiedCalls` showed every module call twice, with two contradictory forms, to
73
+ // every consumer that iterates this list.
74
+ const calls: CallSite[] = [];
75
+ for (const call of result.calls) {
76
+ const qualified = call.namespace !== undefined;
77
+ const flags = qualified ? moduleCallFlags?.(call.name) : undefined;
78
+ const chains = qualified ? byNode.get(`${call.range[0]}:${call.range[1]}`) : undefined;
79
+ calls.push({
80
+ name: call.name,
81
+ form: call.form,
82
+ arity: call.arity,
83
+ start: call.range[0],
84
+ end: call.range[1],
85
+ ...(qualified ? { moduleCall: true as const } : {}),
86
+ // A module call's arguments as the analysis saw them: the type the checker gives each
87
+ // one, and the chain where the argument IS one — so a caller holding the callee's
88
+ // signature can compare a declared shape rather than a CEL type alone.
89
+ ...(qualified && call.argumentTypes
90
+ ? {
91
+ arguments: call.argumentTypes.map((type, at) => {
92
+ const chain = chains?.[at] ?? null;
93
+ return {
94
+ ...(type === "dyn" ? {} : { type }),
95
+ ...(chain ? { chain } : {}),
96
+ };
97
+ }),
98
+ }
99
+ : {}),
100
+ // A module call carries no catalog flag: its determinism and host-backedness follow
101
+ // from the callable the name resolves to, which only the host that resolves it knows.
102
+ ...(qualified
103
+ ? flags
104
+ ? { deterministic: flags.deterministic, hostBacked: flags.hostBacked }
105
+ : {}
106
+ : {
107
+ ...(call.deterministic === undefined ? {} : { deterministic: call.deterministic }),
108
+ ...(call.hostBacked === undefined ? {} : { hostBacked: call.hostBacked }),
109
+ }),
110
+ });
354
111
  }
355
112
 
356
- const argumentIssues: CelDiagnostic[] = [];
357
- for (const call of calls) {
358
- const check = ARG_CHECKS.get(call.name);
359
- if (!check) continue;
360
- const message = check(call.args.map(literalOf));
361
- // No span on an EngineDiagnostic, so the written call goes in the message —
362
- // an expression with two calls to the same function is otherwise ambiguous.
363
- if (message) {
364
- argumentIssues.push({
365
- code: "CEL_INVALID_ARGUMENT",
366
- message: `${message} (in \`${source.slice(call.start, call.end)}\`)`,
367
- });
368
- }
369
- }
370
-
371
- return {
372
- diagnostics,
373
- calls: calls.map(({ receiver: _receiver, args: _args, ...site }) => site),
374
- unresolved,
375
- argumentIssues,
376
- };
377
- }
378
-
379
- /** Registered signatures for names a type-check failure mentions, so the
380
- * residual says what the function actually accepts rather than only echoing
381
- * what the author wrote. */
382
- export function explainUnresolved(names: readonly string[], env: Environment): string {
383
- const index = functionIndex(env);
384
- const signatures = [...new Set(names)].flatMap((n) => signaturesOf(n, index));
385
- return signatures.length > 0 ? ` Registered: ${listOf(signatures)}.` : "";
113
+ calls.sort((left, right) => left.start - right.start || left.end - right.end);
114
+ return { diagnostics: [], calls, unresolved: [], argumentIssues: [] };
115
+ }
116
+
117
+ /**
118
+ * Registered signatures for names a type-check failure mentions, so a residual message says
119
+ * what the function actually accepts rather than only echoing what the author wrote.
120
+ */
121
+ export function explainUnresolved(names: readonly string[], environment: CelEnvironment): string {
122
+ const wanted = new Set(names);
123
+ const signatures = environment
124
+ .definitions()
125
+ .functions.filter((entry) => wanted.has(entry.name))
126
+ .map((entry) => entry.signature);
127
+ if (signatures.length === 0) return "";
128
+ const listed = [...new Set(signatures)].map((signature) => `\`${signature}\``);
129
+ const last = listed.pop()!;
130
+ return ` Registered: ${listed.length > 0 ? `${listed.join(", ")} and ${last}` : last}.`;
386
131
  }