@telorun/templating 0.106.0 → 0.108.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 (66) hide show
  1. package/dist/builtins.d.ts +9 -0
  2. package/dist/builtins.d.ts.map +1 -1
  3. package/dist/builtins.js +10 -0
  4. package/dist/cel/analyze.d.ts +6 -6
  5. package/dist/cel/analyze.d.ts.map +1 -1
  6. package/dist/cel/analyze.js +138 -183
  7. package/dist/cel/compile.d.ts +50 -12
  8. package/dist/cel/compile.d.ts.map +1 -1
  9. package/dist/cel/compile.js +140 -48
  10. package/dist/cel/diagnose.d.ts +52 -56
  11. package/dist/cel/diagnose.d.ts.map +1 -1
  12. package/dist/cel/diagnose.js +69 -291
  13. package/dist/cel/environment.d.ts +33 -33
  14. package/dist/cel/environment.d.ts.map +1 -1
  15. package/dist/cel/environment.js +54 -80
  16. package/dist/cel/module-call.d.ts +68 -79
  17. package/dist/cel/module-call.d.ts.map +1 -1
  18. package/dist/cel/module-call.js +118 -194
  19. package/dist/cel/value-brands.d.ts +18 -0
  20. package/dist/cel/value-brands.d.ts.map +1 -0
  21. package/dist/cel/value-brands.js +33 -0
  22. package/dist/cel/verdict-codes.d.ts +40 -0
  23. package/dist/cel/verdict-codes.d.ts.map +1 -0
  24. package/dist/cel/verdict-codes.js +43 -0
  25. package/dist/engine.d.ts +18 -3
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engines/cel.d.ts +6 -5
  28. package/dist/engines/cel.d.ts.map +1 -1
  29. package/dist/engines/cel.js +57 -82
  30. package/dist/engines/hole-analysis.d.ts +7 -3
  31. package/dist/engines/hole-analysis.d.ts.map +1 -1
  32. package/dist/engines/hole-analysis.js +7 -1
  33. package/dist/engines/include.js +1 -1
  34. package/dist/engines/interpolate.d.ts.map +1 -1
  35. package/dist/engines/interpolate.js +17 -11
  36. package/dist/engines/module-path.js +1 -1
  37. package/dist/engines/sql.d.ts.map +1 -1
  38. package/dist/engines/sql.js +4 -1
  39. package/dist/index.d.ts +6 -6
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +10 -6
  42. package/package.json +6 -6
  43. package/src/builtins.ts +12 -0
  44. package/src/cel/analyze.ts +152 -193
  45. package/src/cel/compile.ts +166 -47
  46. package/src/cel/diagnose.ts +116 -370
  47. package/src/cel/environment.ts +66 -82
  48. package/src/cel/module-call.ts +129 -292
  49. package/src/cel/value-brands.ts +34 -0
  50. package/src/cel/verdict-codes.ts +66 -0
  51. package/src/engine.ts +19 -3
  52. package/src/engines/cel.ts +62 -95
  53. package/src/engines/hole-analysis.ts +16 -16
  54. package/src/engines/include.ts +1 -1
  55. package/src/engines/interpolate.ts +33 -19
  56. package/src/engines/module-path.ts +1 -1
  57. package/src/engines/sql.ts +11 -1
  58. package/src/index.ts +11 -11
  59. package/dist/cel/catalog.d.ts +0 -67
  60. package/dist/cel/catalog.d.ts.map +0 -1
  61. package/dist/cel/catalog.js +0 -1253
  62. package/dist/cel/value-identity.d.ts +0 -18
  63. package/dist/cel/value-identity.d.ts.map +0 -1
  64. package/dist/cel/value-identity.js +0 -25
  65. package/src/cel/catalog.ts +0 -1379
  66. package/src/cel/value-identity.ts +0 -34
@@ -1,3 +1,4 @@
1
+ import type { CelEnvironment } from "@telorun/cel";
1
2
  import { TemplatingEngineRegistry } from "./registry.js";
2
3
  import type { TemplatingEngine } from "./engine.js";
3
4
  /** Single source of truth for the built-in templating engines. Every host
@@ -27,6 +28,14 @@ export declare function producedTypeOf(engineName: string): Record<string, unkno
27
28
  * asking "what CEL is written here" never recognises a tag by name.
28
29
  */
29
30
  export declare function celExpressionsOf(engineName: string, source: string): string[];
31
+ /**
32
+ * Whether a tagged scalar's text, written twice in one value, means one value
33
+ * both times — false for a tag whose engine gives no such verdict.
34
+ *
35
+ * The single reader of `TemplatingEngine.repeatable`, so a consumer comparing
36
+ * two tagged scalars never recognises a tag by name.
37
+ */
38
+ export declare function repeatableSource(engineName: string, source: string, celEnv: CelEnvironment): boolean;
30
39
  /** Memoized singleton: returns the default registry. Hosts that don't need
31
40
  * per-instance isolation (precompile, the analyzer's tagged-value walker)
32
41
  * should use this so they share the same registry instance the YAML tag
@@ -1 +1 @@
1
- {"version":3,"file":"builtins.d.ts","sourceRoot":"","sources":["../src/builtins.ts"],"names":[],"mappings":"AAOA,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;;gEAKgE;AAChE,eAAO,MAAM,cAAc,EAAE,SAAS,gBAAgB,EASrD,CAAC;AAEF,wBAAgB,qBAAqB,IAAI,wBAAwB,CAMhE;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAEtF;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAG7E;AAID;;;oBAGoB;AACpB,wBAAgB,eAAe,IAAI,wBAAwB,CAK1D"}
1
+ {"version":3,"file":"builtins.d.ts","sourceRoot":"","sources":["../src/builtins.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAQnD,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;;;;gEAKgE;AAChE,eAAO,MAAM,cAAc,EAAE,SAAS,gBAAgB,EASrD,CAAC;AAEF,wBAAgB,qBAAqB,IAAI,wBAAwB,CAMhE;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,UAAU,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAEtF;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,EAAE,CAG7E;AAED;;;;;;GAMG;AACH,wBAAgB,gBAAgB,CAAC,UAAU,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO,CAEpG;AAID;;;oBAGoB;AACpB,wBAAgB,eAAe,IAAI,wBAAwB,CAK1D"}
package/dist/builtins.js CHANGED
@@ -53,6 +53,16 @@ export function celExpressionsOf(engineName, source) {
53
53
  const regions = defaultRegistry().get(engineName)?.expressionRegions?.(source) ?? [];
54
54
  return regions.map((r) => source.slice(r.start, r.end));
55
55
  }
56
+ /**
57
+ * Whether a tagged scalar's text, written twice in one value, means one value
58
+ * both times — false for a tag whose engine gives no such verdict.
59
+ *
60
+ * The single reader of `TemplatingEngine.repeatable`, so a consumer comparing
61
+ * two tagged scalars never recognises a tag by name.
62
+ */
63
+ export function repeatableSource(engineName, source, celEnv) {
64
+ return defaultRegistry().get(engineName)?.repeatable?.(source, celEnv) ?? false;
65
+ }
56
66
  let defaultRegistryCache;
57
67
  /** Memoized singleton: returns the default registry. Hosts that don't need
58
68
  * per-instance isolation (precompile, the analyzer's tagged-value walker)
@@ -1,4 +1,4 @@
1
- import type { ASTNode } from "@marcbachmann/cel-js";
1
+ import { type CelNode } from "@telorun/cel";
2
2
  /**
3
3
  * Extract all member-access chains from a CEL AST.
4
4
  * Returns arrays like ["request", "query", "name"] for `request.query.name`.
@@ -12,14 +12,14 @@ import type { ASTNode } from "@marcbachmann/cel-js";
12
12
  * tree, before this walk — an unresolved tree still reports the receiver, which
13
13
  * is the right answer for a caller that supplied no module names.
14
14
  */
15
- export declare function extractAccessChains(node: ASTNode): string[][];
15
+ export declare function extractAccessChains(node: CelNode): string[][];
16
16
  /**
17
17
  * Each resolved module call's arguments as plain member chains, keyed by the
18
18
  * call's node — `null` for an argument that is not one, or that is rooted at a
19
19
  * name the expression itself binds (`xs.map(item, Billing.total(item))`), which
20
20
  * no context schema describes.
21
21
  */
22
- export declare function moduleCallArgumentChains(node: ASTNode): Map<ASTNode, Array<string[] | null>>;
22
+ export declare function moduleCallArgumentChains(node: CelNode): Map<CelNode, Array<string[] | null>>;
23
23
  /** Sentinel chain segment emitted for index access (`obj[expr]`) — a dynamic
24
24
  * member that can't be resolved to a static name. Consumers that attribute
25
25
  * chains to declared names treat this as "unknown member". */
@@ -35,7 +35,7 @@ export interface CallResultAccess {
35
35
  * {@link extractAccessChains} deliberately skips, since their root is a value
36
36
  * no context schema describes. The caller supplies the call's result schema.
37
37
  */
38
- export declare function extractCallResultAccesses(node: ASTNode): CallResultAccess[];
38
+ export declare function extractCallResultAccesses(node: CelNode): CallResultAccess[];
39
39
  interface NullableIssue {
40
40
  /** Dotted path of the nullable value being dereferenced (e.g. "error"). */
41
41
  path: string;
@@ -45,7 +45,7 @@ interface NullableIssue {
45
45
  /** The dotted chain an expression IS, when it is a plain chain whose schema
46
46
  * admits null — a value that may itself be null, rather than one dereferenced
47
47
  * through a null. */
48
- export declare function nullableValueChain(node: ASTNode, contextSchema: Record<string, any>): string | null;
48
+ export declare function nullableValueChain(node: CelNode, contextSchema: Record<string, any>): string | null;
49
49
  /**
50
50
  * Find member accesses on a nullable value that are not null-guarded in the
51
51
  * surrounding expression. A context field whose schema admits `null` (e.g. the
@@ -54,7 +54,7 @@ export declare function nullableValueChain(node: ASTNode, contextSchema: Record<
54
54
  * `x != null` flowing through `?:` ternaries and `&&` / `||` short-circuits.
55
55
  * Returns one issue per unguarded dereference.
56
56
  */
57
- export declare function findNullableAccessIssues(node: ASTNode, contextSchema: Record<string, any>): NullableIssue[];
57
+ export declare function findNullableAccessIssues(node: CelNode, contextSchema: Record<string, any>): NullableIssue[];
58
58
  /**
59
59
  * Check whether a member-access chain accesses only fields declared in a JSON Schema.
60
60
  * Returns an error string if a field is unknown in a schema that declares explicit
@@ -1 +1 @@
1
- {"version":3,"file":"analyze.d.ts","sourceRoot":"","sources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import { childNodes, namespaceMacroBinding, receiverMacroBinding, } from "@telorun/cel";
1
2
  import { isLiveSlot } from "@telorun/sdk";
2
3
  import { moduleCallOf } from "./module-call.js";
3
4
  /**
@@ -31,30 +32,54 @@ export function moduleCallArgumentChains(node) {
31
32
  });
32
33
  return out;
33
34
  }
34
- const COMPREHENSION_METHODS = new Set(["filter", "map", "exists", "all", "exists_one"]);
35
- /** `cel.bind(name, init, body)` — CEL's only binding form, and the one the
36
- * parser expands rather than dispatching, so it appears as a receiver call on a
37
- * bare `cel` identifier that is in no scope and never will be. Both walks below
38
- * need the same three facts out of it, so the shape is read once here.
35
+ /**
36
+ * `cel.bind(name, init, body)`.
37
+ *
38
+ * `cel` is a RESERVED namespace — one a host can never claim, because the standard macros
39
+ * are written on it — so this is **not** a qualified call: it reads as a receiver call on
40
+ * the ident `cel`, which is in no scope and never will be. The engine's own binding table
41
+ * says which argument is the name and which are scoped.
39
42
  *
40
- * The receiver is deliberately not returned: it contributes no chain, and
41
- * descending into it is what produced a `CEL_UNKNOWN_FIELD` for `cel` itself. */
43
+ * The receiver is deliberately not returned: it contributes no chain, and descending into it
44
+ * is what produced a `CEL_UNKNOWN_FIELD` for `cel` itself.
45
+ */
42
46
  function bindCall(node) {
43
- if (node.op !== "rcall" || !Array.isArray(node.args))
47
+ if (node.kind !== "receiverCall")
48
+ return null;
49
+ if (node.receiver.kind !== "ident" || node.receiver.name !== "cel")
50
+ return null;
51
+ const binding = namespaceMacroBinding("cel", node.name, node.args.length);
52
+ if (!binding)
44
53
  return null;
45
- const [method, receiver, callArgs] = node.args;
46
- if (method !== "bind")
54
+ const nameNode = node.args[binding.variableArgument];
55
+ if (nameNode?.kind !== "ident")
47
56
  return null;
48
- if (!isASTNode(receiver) || receiver.op !== "id" || receiver.args !== "cel")
57
+ const scoped = binding.scopedArguments;
58
+ // `cel.bind(name, init, body)`: the body is scoped, everything else before it is not.
59
+ const body = node.args[scoped[scoped.length - 1]];
60
+ const init = node.args.find((unused, at) => at !== binding.variableArgument && !scoped.includes(at));
61
+ if (!init || !body)
49
62
  return null;
50
- if (!Array.isArray(callArgs) || callArgs.length !== 3)
63
+ return { name: nameNode.name, init, body };
64
+ }
65
+ /** A comprehension macro's iteration variable and the arguments it scopes, from the
66
+ * engine's own binding table rather than a list of method names kept in step with it. */
67
+ function comprehension(node) {
68
+ if (node.kind !== "receiverCall")
69
+ return null;
70
+ if (node.receiver.kind === "ident" && node.receiver.name === "cel")
51
71
  return null;
52
- const [nameNode, init, body] = callArgs;
53
- if (!isASTNode(nameNode) || nameNode.op !== "id")
72
+ const binding = receiverMacroBinding(node.name, node.args.length);
73
+ if (!binding)
54
74
  return null;
55
- if (!isASTNode(init) || !isASTNode(body))
75
+ const nameNode = node.args[binding.variableArgument];
76
+ if (nameNode?.kind !== "ident")
56
77
  return null;
57
- return { name: nameNode.args, init, body };
78
+ return {
79
+ receiver: node.receiver,
80
+ bound: nameNode.name,
81
+ scoped: binding.scopedArguments.flatMap((at) => (node.args[at] ? [node.args[at]] : [])),
82
+ };
58
83
  }
59
84
  function visitNode(node, chains, boundVars, onModuleCall) {
60
85
  const chain = extractChain(node, boundVars);
@@ -77,71 +102,48 @@ function visitNode(node, chains, boundVars, onModuleCall) {
77
102
  visitNode(bind.body, chains, new Set(boundVars).add(bind.name), onModuleCall);
78
103
  return;
79
104
  }
80
- if (node.op === "rcall" &&
81
- Array.isArray(node.args) &&
82
- typeof node.args[0] === "string" &&
83
- COMPREHENSION_METHODS.has(node.args[0])) {
84
- const receiver = node.args[1];
85
- const comprehensionArgs = node.args[2];
86
- if (isASTNode(receiver))
87
- visitNode(receiver, chains, boundVars, onModuleCall);
88
- if (Array.isArray(comprehensionArgs) &&
89
- comprehensionArgs.length >= 2 &&
90
- isASTNode(comprehensionArgs[0]) &&
91
- comprehensionArgs[0].op === "id") {
92
- const newBoundVars = new Set(boundVars);
93
- newBoundVars.add(comprehensionArgs[0].args);
94
- for (let i = 1; i < comprehensionArgs.length; i++) {
95
- const arg = comprehensionArgs[i];
96
- if (isASTNode(arg))
97
- visitNode(arg, chains, newBoundVars, onModuleCall);
98
- }
99
- }
105
+ const macro = comprehension(node);
106
+ if (macro) {
107
+ // The receiver is evaluated outside the binding; only the scoped arguments see it.
108
+ visitNode(macro.receiver, chains, boundVars, onModuleCall);
109
+ const inner = new Set(boundVars).add(macro.bound);
110
+ for (const arg of macro.scoped)
111
+ visitNode(arg, chains, inner, onModuleCall);
100
112
  return;
101
113
  }
102
- const args = node.args;
103
- if (Array.isArray(args)) {
104
- for (const arg of args) {
105
- if (isASTNode(arg)) {
106
- visitNode(arg, chains, boundVars, onModuleCall);
107
- }
108
- else if (Array.isArray(arg)) {
109
- for (const item of arg) {
110
- if (isASTNode(item))
111
- visitNode(item, chains, boundVars, onModuleCall);
112
- }
113
- }
114
- }
115
- }
116
- else if (isASTNode(args)) {
117
- // Unary operators (`!_`, `-_`) carry their operand as a single node
118
- // rather than a one-element array, so descend into it directly.
119
- visitNode(args, chains, boundVars, onModuleCall);
120
- }
121
- }
122
- function isASTNode(v) {
123
- return v !== null && typeof v === "object" && "op" in v;
114
+ for (const child of childNodes(node))
115
+ visitNode(child, chains, boundVars, onModuleCall);
124
116
  }
125
117
  /** Sentinel chain segment emitted for index access (`obj[expr]`) — a dynamic
126
118
  * member that can't be resolved to a static name. Consumers that attribute
127
119
  * chains to declared names treat this as "unknown member". */
128
120
  export const INDEX_SEGMENT = "[*]";
129
121
  function extractChain(node, boundVars) {
130
- if (node.op === "id") {
131
- const name = node.args;
132
- if (boundVars.has(name))
122
+ if (node.kind === "ident") {
123
+ // An ABSOLUTE name (`.y`) resolves against the environment's declarations and never
124
+ // against a name the expression bound, so a binding of the same spelling cannot hide it.
125
+ if (!node.absolute && boundVars.has(node.name))
133
126
  return null;
134
- return [name];
127
+ return [node.name];
135
128
  }
136
- if (node.op === ".") {
137
- const [obj, field] = node.args;
138
- const parent = extractChain(obj, boundVars);
129
+ // **An OPTIONAL read ends the chain.** `a.?b` and `a[?0]` answer an `optional<T>`, so what
130
+ // follows is a method on that optional (`.orValue('')`) rather than a member of the
131
+ // context — and the author has said outright that the member may be absent, which is not a
132
+ // claim a context schema has to declare. The engine makes both a real `select`/`index`
133
+ // with `optional: true`, where the replaced engine had no node this walk could chain, so
134
+ // continuing would both invent a read and hand `validateChainAgainstSchema` a path past
135
+ // the point the expression stopped asserting anything.
136
+ if (node.kind === "select") {
137
+ if (node.optional)
138
+ return null;
139
+ const parent = extractChain(node.operand, boundVars);
139
140
  if (parent !== null)
140
- return [...parent, field];
141
+ return [...parent, node.field];
141
142
  }
142
- if (node.op === "[]") {
143
- const [obj] = node.args;
144
- const parent = extractChain(obj, boundVars);
143
+ if (node.kind === "index") {
144
+ if (node.optional)
145
+ return null;
146
+ const parent = extractChain(node.operand, boundVars);
145
147
  if (parent !== null)
146
148
  return [...parent, INDEX_SEGMENT];
147
149
  }
@@ -158,15 +160,19 @@ export function extractCallResultAccesses(node) {
158
160
  return out;
159
161
  }
160
162
  function visitAccess(node, out) {
161
- if (node.op === "." || node.op === "[]") {
163
+ if (node.kind === "select" || node.kind === "index") {
162
164
  const members = [];
163
165
  let base = node;
164
- while (base.op === "." || base.op === "[]") {
165
- const [obj, field] = base.args;
166
- members.unshift(base.op === "." ? String(field) : INDEX_SEGMENT);
167
- if (base.op === "[]" && isASTNode(field))
168
- visitAccess(field, out);
169
- base = obj;
166
+ while (base.kind === "select" || base.kind === "index") {
167
+ if (base.kind === "select") {
168
+ members.unshift(base.field);
169
+ }
170
+ else {
171
+ members.unshift(INDEX_SEGMENT);
172
+ // The index is its own expression and may itself read off a call's result.
173
+ visitAccess(base.index, out);
174
+ }
175
+ base = base.operand;
170
176
  }
171
177
  const call = moduleCallOf(base);
172
178
  if (call) {
@@ -178,26 +184,8 @@ function visitAccess(node, out) {
178
184
  visitAccess(base, out);
179
185
  return;
180
186
  }
181
- const moduleCall = moduleCallOf(node);
182
- if (moduleCall) {
183
- for (const arg of moduleCall.args)
184
- visitAccess(arg, out);
185
- return;
186
- }
187
- const args = node.args;
188
- if (Array.isArray(args)) {
189
- for (const arg of args) {
190
- if (isASTNode(arg))
191
- visitAccess(arg, out);
192
- else if (Array.isArray(arg))
193
- for (const item of arg)
194
- if (isASTNode(item))
195
- visitAccess(item, out);
196
- }
197
- }
198
- else if (isASTNode(args)) {
199
- visitAccess(args, out);
200
- }
187
+ for (const child of childNodes(node))
188
+ visitAccess(child, out);
201
189
  }
202
190
  /** True when a JSON Schema admits `null` — `type: "null"` or a union that
203
191
  * includes it (e.g. `["object", "null"]`). */
@@ -231,7 +219,7 @@ export function nullableValueChain(node, contextSchema) {
231
219
  return schemaIsNullable(schemaAtChain(chain.split("."), contextSchema)) ? chain : null;
232
220
  }
233
221
  function isNullLiteral(node) {
234
- return node.op === "value" && node.args === null;
222
+ return node.kind === "literal" && node.literal.type === "null";
235
223
  }
236
224
  /** Dotted form of a static member chain rooted at a free identifier, or null
237
225
  * when the node isn't such a chain (call result, bound var, index, …). */
@@ -245,35 +233,34 @@ const EMPTY_NARROWING = { whenTrue: new Set(), whenFalse: new Set() };
245
233
  /** Derive which chains a boolean condition proves non-null in its true / false
246
234
  * branches. Handles `x == null` / `x != null`, negation, and `&&` / `||`. */
247
235
  function deriveNarrowing(node, boundVars) {
248
- if (node.op === "==" || node.op === "!=") {
249
- const [l, r] = node.args;
250
- const chain = isNullLiteral(r)
251
- ? dottedChain(l, boundVars)
252
- : isNullLiteral(l)
253
- ? dottedChain(r, boundVars)
236
+ if (node.kind === "unary" && node.operator === "!") {
237
+ const inner = deriveNarrowing(node.operand, boundVars);
238
+ return { whenTrue: inner.whenFalse, whenFalse: inner.whenTrue };
239
+ }
240
+ if (node.kind !== "binary")
241
+ return EMPTY_NARROWING;
242
+ if (node.operator === "==" || node.operator === "!=") {
243
+ const chain = isNullLiteral(node.right)
244
+ ? dottedChain(node.left, boundVars)
245
+ : isNullLiteral(node.left)
246
+ ? dottedChain(node.right, boundVars)
254
247
  : null;
255
248
  if (chain === null)
256
249
  return EMPTY_NARROWING;
257
250
  const proven = new Set([chain]);
258
- return node.op === "!="
251
+ return node.operator === "!="
259
252
  ? { whenTrue: proven, whenFalse: new Set() }
260
253
  : { whenTrue: new Set(), whenFalse: proven };
261
254
  }
262
- if (node.op === "!_") {
263
- const inner = deriveNarrowing(node.args, boundVars);
264
- return { whenTrue: inner.whenFalse, whenFalse: inner.whenTrue };
265
- }
266
- if (node.op === "&&") {
267
- const [a, b] = node.args;
268
- const na = deriveNarrowing(a, boundVars);
269
- const nb = deriveNarrowing(b, boundVars);
270
- return { whenTrue: union(na.whenTrue, nb.whenTrue), whenFalse: new Set() };
255
+ if (node.operator === "&&") {
256
+ const left = deriveNarrowing(node.left, boundVars);
257
+ const right = deriveNarrowing(node.right, boundVars);
258
+ return { whenTrue: union(left.whenTrue, right.whenTrue), whenFalse: new Set() };
271
259
  }
272
- if (node.op === "||") {
273
- const [a, b] = node.args;
274
- const na = deriveNarrowing(a, boundVars);
275
- const nb = deriveNarrowing(b, boundVars);
276
- return { whenTrue: new Set(), whenFalse: union(na.whenFalse, nb.whenFalse) };
260
+ if (node.operator === "||") {
261
+ const left = deriveNarrowing(node.left, boundVars);
262
+ const right = deriveNarrowing(node.right, boundVars);
263
+ return { whenTrue: new Set(), whenFalse: union(left.whenFalse, right.whenFalse) };
277
264
  }
278
265
  return EMPTY_NARROWING;
279
266
  }
@@ -295,97 +282,65 @@ export function findNullableAccessIssues(node, contextSchema) {
295
282
  }
296
283
  function walkNullable(node, nonNull, boundVars, issues, schema) {
297
284
  // Dereference of an object/array member — check the receiver's nullability.
298
- if (node.op === "." || node.op === "[]") {
299
- const [obj, field] = node.args;
300
- const objChain = dottedChain(obj, boundVars);
285
+ if (node.kind === "select" || node.kind === "index") {
286
+ const objChain = dottedChain(node.operand, boundVars);
301
287
  if (objChain !== null && !nonNull.has(objChain)) {
302
- const objSchema = schemaAtChain(objChain.split("."), schema);
303
- if (schemaIsNullable(objSchema)) {
288
+ if (schemaIsNullable(schemaAtChain(objChain.split("."), schema))) {
304
289
  issues.push({
305
290
  path: objChain,
306
- member: node.op === "." ? String(field) : "[index]",
291
+ member: node.kind === "select" ? node.field : "[index]",
307
292
  });
308
293
  }
309
294
  }
310
- walkNullable(obj, nonNull, boundVars, issues, schema);
311
- // The index expression (`obj[expr]`) is itself evaluated — descend so a
312
- // nullable deref used as an index (e.g. `items[error.code]`) is caught.
313
- if (node.op === "[]" && isASTNode(field)) {
314
- walkNullable(field, nonNull, boundVars, issues, schema);
315
- }
295
+ walkNullable(node.operand, nonNull, boundVars, issues, schema);
296
+ // The index expression (`obj[expr]`) is itself evaluated — descend so a nullable
297
+ // deref used as an index (`items[error.code]`) is caught.
298
+ if (node.kind === "index")
299
+ walkNullable(node.index, nonNull, boundVars, issues, schema);
316
300
  return;
317
301
  }
318
- if (node.op === "?:") {
319
- const [cond, thenB, elseB] = node.args;
320
- walkNullable(cond, nonNull, boundVars, issues, schema);
321
- const n = deriveNarrowing(cond, boundVars);
322
- walkNullable(thenB, union(nonNull, n.whenTrue), boundVars, issues, schema);
323
- walkNullable(elseB, union(nonNull, n.whenFalse), boundVars, issues, schema);
302
+ if (node.kind === "conditional") {
303
+ walkNullable(node.condition, nonNull, boundVars, issues, schema);
304
+ const narrowed = deriveNarrowing(node.condition, boundVars);
305
+ walkNullable(node.whenTrue, union(nonNull, narrowed.whenTrue), boundVars, issues, schema);
306
+ walkNullable(node.whenFalse, union(nonNull, narrowed.whenFalse), boundVars, issues, schema);
324
307
  return;
325
308
  }
326
- if (node.op === "&&" || node.op === "||") {
327
- const [a, b] = node.args;
328
- walkNullable(a, nonNull, boundVars, issues, schema);
329
- const n = deriveNarrowing(a, boundVars);
330
- const carried = node.op === "&&" ? n.whenTrue : n.whenFalse;
331
- walkNullable(b, union(nonNull, carried), boundVars, issues, schema);
309
+ if (node.kind === "binary" && (node.operator === "&&" || node.operator === "||")) {
310
+ walkNullable(node.left, nonNull, boundVars, issues, schema);
311
+ const narrowed = deriveNarrowing(node.left, boundVars);
312
+ const carried = node.operator === "&&" ? narrowed.whenTrue : narrowed.whenFalse;
313
+ walkNullable(node.right, union(nonNull, carried), boundVars, issues, schema);
332
314
  return;
333
315
  }
334
- // A module call's receiver is a module name, not a value; mirror
335
- // extractAccessChains so it is never read as a nullable context field.
316
+ // A module call's receiver is a module name, not a value; mirror extractAccessChains so
317
+ // it is never read as a nullable context field.
336
318
  const moduleCall = moduleCallOf(node);
337
319
  if (moduleCall) {
338
320
  for (const arg of moduleCall.args)
339
321
  walkNullable(arg, nonNull, boundVars, issues, schema);
340
322
  return;
341
323
  }
342
- // `cel.bind` binds a name for its body; mirror extractAccessChains so a bound
343
- // name is never read as a nullable context field.
324
+ // `cel.bind` binds a name for its body; mirror extractAccessChains so a bound name is
325
+ // never read as a nullable context field.
344
326
  const bind = bindCall(node);
345
327
  if (bind) {
346
328
  walkNullable(bind.init, nonNull, boundVars, issues, schema);
347
329
  walkNullable(bind.body, nonNull, new Set(boundVars).add(bind.name), issues, schema);
348
330
  return;
349
331
  }
350
- // Comprehension macros bind a loop variable; mirror extractAccessChains so a
351
- // bound var is never mistaken for a nullable context field.
352
- if (node.op === "rcall" &&
353
- Array.isArray(node.args) &&
354
- typeof node.args[0] === "string" &&
355
- COMPREHENSION_METHODS.has(node.args[0])) {
356
- const receiver = node.args[1];
357
- const comprehensionArgs = node.args[2];
358
- if (isASTNode(receiver))
359
- walkNullable(receiver, nonNull, boundVars, issues, schema);
360
- if (Array.isArray(comprehensionArgs) &&
361
- comprehensionArgs.length >= 2 &&
362
- isASTNode(comprehensionArgs[0]) &&
363
- comprehensionArgs[0].op === "id") {
364
- const inner = new Set(boundVars);
365
- inner.add(comprehensionArgs[0].args);
366
- for (let i = 1; i < comprehensionArgs.length; i++) {
367
- const arg = comprehensionArgs[i];
368
- if (isASTNode(arg))
369
- walkNullable(arg, nonNull, inner, issues, schema);
370
- }
371
- }
332
+ // A comprehension binds a loop variable; mirror extractAccessChains so a bound var is
333
+ // never mistaken for a nullable context field.
334
+ const macro = comprehension(node);
335
+ if (macro) {
336
+ walkNullable(macro.receiver, nonNull, boundVars, issues, schema);
337
+ const inner = new Set(boundVars).add(macro.bound);
338
+ for (const arg of macro.scoped)
339
+ walkNullable(arg, nonNull, inner, issues, schema);
372
340
  return;
373
341
  }
374
- const args = node.args;
375
- if (Array.isArray(args)) {
376
- for (const arg of args) {
377
- if (isASTNode(arg))
378
- walkNullable(arg, nonNull, boundVars, issues, schema);
379
- else if (Array.isArray(arg)) {
380
- for (const item of arg) {
381
- if (isASTNode(item))
382
- walkNullable(item, nonNull, boundVars, issues, schema);
383
- }
384
- }
385
- }
386
- }
387
- else if (isASTNode(args)) {
388
- walkNullable(args, nonNull, boundVars, issues, schema);
342
+ for (const child of childNodes(node)) {
343
+ walkNullable(child, nonNull, boundVars, issues, schema);
389
344
  }
390
345
  }
391
346
  /**
@@ -1,13 +1,51 @@
1
- import type { CompiledValue } from "@telorun/sdk";
2
- import type { Environment } from "@marcbachmann/cel-js";
3
- /** Compile a single CEL expression into a CompiledValue. Throws on syntax
4
- * errors. The `!cel` engine compiles its whole scalar with it, and the tags
5
- * with holes compile each hole.
6
- *
7
- * `moduleNames` are the declaring module's own names; a call whose receiver is
8
- * one of them resolves to a late-bound module call HERE, on the parsed tree, so
9
- * every later reader — the root references below, the analyzer's passes, the
10
- * kernel's dependency walk — sees the resolved shape rather than re-deriving
11
- * it from the source text. */
12
- export declare function compileExpression(expr: string, env: Environment, moduleNames?: ReadonlySet<string>): CompiledValue;
1
+ /**
2
+ * Compiling one CEL expression into a `CompiledValue`, and the repeatability verdict on its
3
+ * text.
4
+ *
5
+ * **The module-call rewrite is gone from this package.** It used to happen here, on the
6
+ * parsed tree, through internals the previous engine did not expose — a `setMeta('macro')`
7
+ * redirect that made a qualified call check and evaluate as something else. `@telorun/cel`
8
+ * has `qcall` as a node of its own, produced by a total tree-to-tree pass over the name set
9
+ * (`parseExpression({ namespaces })`), and a `CelExpression` records the set it was resolved
10
+ * under, so an environment refuses a tree resolved for a different site rather than
11
+ * silently answering a different question. What is left here is reading the result.
12
+ */
13
+ import { type CompiledValue } from "@telorun/sdk";
14
+ import { type CelEnvironment, type CelExpression, type NamespaceImplementation } from "@telorun/cel";
15
+ import { type ModuleCallDispatch } from "./module-call.js";
16
+ /**
17
+ * Whether an expression's text, written twice in one value, means one value both times: it
18
+ * calls no catalog function whose result differs per call, and makes no call that may be a
19
+ * module's — a module function's determinism is its callee's, which the text does not say.
20
+ *
21
+ * Text that does not read whole means no value, so it is not repeatable; `analyze` is what
22
+ * reports why. Reading never throws here, which is the engine's own contract: the first
23
+ * unreadable thing is one ranged diagnostic and the tree keeps the longest prefix.
24
+ */
25
+ export declare function repeatableExpression(expr: string, environment: CelEnvironment): boolean;
26
+ /**
27
+ * Compile one CEL expression into a `CompiledValue`. The `!cel` engine compiles its whole
28
+ * scalar with it, and the tags with holes compile each hole.
29
+ *
30
+ * `moduleNames` are the declaring module's own names; a call whose receiver is one of them
31
+ * is resolved to a `qcall` as the expression is READ, so every later reader — the root
32
+ * references below, the analyzer's passes, the kernel's dependency walk — sees the resolved
33
+ * shape rather than re-deriving it from the source text.
34
+ */
35
+ export declare function compileExpression(expr: string, environment: CelEnvironment, moduleNames?: ReadonlySet<string>): CompiledValue;
36
+ /** A `CompiledValue` over an expression already read against its site's names. */
37
+ export declare function compiledValueOf(expr: string, parsed: CelExpression, environment: CelEnvironment): CompiledValue;
38
+ /**
39
+ * A scope's module functions as the engine's own per-evaluation dispatch — **the one
40
+ * adapter**, so every host that binds a table converts its arguments the same way.
41
+ *
42
+ * The table is keyed by qualified name (`Billing.total`), the form the kernel and the
43
+ * analyzer both hold it in; the engine asks by namespace and name, never learning that a
44
+ * qualified name is one string with a dot in it. A second adapter is what made `telo check`
45
+ * and the kernel disagree: the analyzer kept its own, called the bound function with the raw
46
+ * values, and handed a map literal written at a call site a `CelMap` where the kernel handed
47
+ * a plain object — a check/run divergence by construction, in the one direction nothing
48
+ * reports.
49
+ */
50
+ export declare function namespaceDispatchOf(table: ModuleCallDispatch | undefined): ((namespace: string, name: string) => NamespaceImplementation | undefined) | undefined;
13
51
  //# sourceMappingURL=compile.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/cel/compile.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAW,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAuCjE;;;;;;;;+BAQ+B;AAC/B,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,GAAG,EAAE,WAAW,EAChB,WAAW,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAChC,aAAa,CAYf"}
1
+ {"version":3,"file":"compile.d.ts","sourceRoot":"","sources":["../../src/cel/compile.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAe,KAAK,aAAa,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAKL,KAAK,cAAc,EACnB,KAAK,aAAa,EAElB,KAAK,uBAAuB,EAC7B,MAAM,cAAc,CAAC;AACtB,OAAO,EAGL,KAAK,kBAAkB,EACxB,MAAM,kBAAkB,CAAC;AAwC1B;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,cAAc,GAAG,OAAO,CAIvF;AAED;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,MAAM,EACZ,WAAW,EAAE,cAAc,EAC3B,WAAW,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,GAChC,aAAa,CAYf;AAED,kFAAkF;AAClF,wBAAgB,eAAe,CAC7B,IAAI,EAAE,MAAM,EACZ,MAAM,EAAE,aAAa,EACrB,WAAW,EAAE,cAAc,GAC1B,aAAa,CAiBf;AAiCD;;;;;;;;;;;GAWG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,kBAAkB,GAAG,SAAS,GACpC,CAAC,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,KAAK,uBAAuB,GAAG,SAAS,CAAC,GAAG,SAAS,CAMxF"}