@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 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EAChB,KAAK,WAAW,GACjB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EACL,kBAAkB,EAClB,aAAa,EACb,KAAK,eAAe,EACpB,KAAK,cAAc,EACnB,KAAK,mBAAmB,GACzB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AACrD,OAAO,EACL,kBAAkB,EAClB,gBAAgB,EAChB,sBAAsB,EACtB,KAAK,WAAW,EAChB,KAAK,iBAAiB,EACtB,KAAK,kBAAkB,GACxB,MAAM,8BAA8B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0BAA0B,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,SAAS,EAAE,kBAAkB,EAAE,KAAK,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAExF,OAAO,EAAE,wBAAwB,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,qBAAqB,EACrB,eAAe,EACf,cAAc,EACd,gBAAgB,GACjB,MAAM,eAAe,CAAC;AACvB,YAAY,EACV,UAAU,EACV,aAAa,EACb,YAAY,EACZ,QAAQ,EACR,UAAU,EACV,aAAa,EACb,gBAAgB,EAChB,eAAe,EACf,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAErB,OAAO,EACL,UAAU,EACV,oBAAoB,EACpB,oBAAoB,EACpB,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,aAAa,EACb,gBAAgB,EAChB,kBAAkB,EAClB,wBAAwB,EACxB,kBAAkB,EAClB,YAAY,EACZ,KAAK,cAAc,GACpB,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,eAAe,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -1,15 +1,19 @@
1
- export { buildCelEnvironment, celBuiltinFunctions, deriveSignatures, } from "./cel/environment.js";
2
- export { celFunctionCatalog, CEL_FUNCTIONS, } from "./cel/catalog.js";
3
- export { compileExpression } from "./cel/compile.js";
1
+ export { buildCelEnvironment, buildCelLanguageEnvironment, celBuiltinFunctions, celFunctionCatalog, } from "./cel/environment.js";
2
+ // `namespaceDispatchOf` is the ONE adapter a host's dispatch table crosses to
3
+ // become the engine's namespace seam — exported because the analyzer evaluates
4
+ // rule conditions and function bodies through the same table, and a second
5
+ // adapter is what let it convert arguments differently from the kernel.
6
+ export { compileExpression, namespaceDispatchOf } from "./cel/compile.js";
7
+ export { registerValueBrands } from "./cel/value-brands.js";
4
8
  export { interpolationShape, literalFragments, readInterpolationHoles, } from "./cel/interpolation-holes.js";
5
9
  export { extractAccessChains, findNullableAccessIssues, INDEX_SEGMENT, validateChainAgainstSchema, } from "./cel/analyze.js";
6
- export { auditCalls, explainUnresolved, functionIndex, } from "./cel/diagnose.js";
10
+ export { auditCalls, explainUnresolved, } from "./cel/diagnose.js";
7
11
  // The rewrite, its reader, and the activation key the kernel binds a scope's
8
12
  // dispatch table under. The unbound message and the receiver and bound-name
9
13
  // walks are read inside this package only.
10
14
  // `ModuleCallTypeResolver` is here because `AnalyzeEnv` names it: an exported
11
15
  // interface whose field type cannot be named is not usable from outside.
12
- export { MODULE_CALL_DISPATCH_KEY, moduleCallOf, resolveModuleCalls, } from "./cel/module-call.js";
16
+ export { celNamespaceNames, MODULE_CALL_DISPATCH_KEY, moduleCallNames, moduleCallOf, } from "./cel/module-call.js";
13
17
  export { walkCelExpressions } from "./cel/walk.js";
14
18
  export { celEngine } from "./engines/cel.js";
15
19
  export { includeBytesEngine, includeTextEngine, normalizeIncludePath, normalizeModulePath, } from "./engines/include.js";
@@ -19,6 +23,6 @@ export { modulePathEngine } from "./engines/module-path.js";
19
23
  export { refEngine } from "./engines/ref.js";
20
24
  export { sqlEngine, isParameterizedSql } from "./engines/sql.js";
21
25
  export { TemplatingEngineRegistry } from "./registry.js";
22
- export { builtinEngines, celExpressionsOf, createDefaultRegistry, defaultRegistry, producedTypeOf, } from "./builtins.js";
26
+ export { builtinEngines, celExpressionsOf, createDefaultRegistry, defaultRegistry, producedTypeOf, repeatableSource, } from "./builtins.js";
23
27
  export { CEL_ENGINE, INCLUDE_BYTES_ENGINE, INCLUDE_ENGINE_NAMES, INCLUDE_TEXT_ENGINE, isIncludeSentinel, isModulePathSentinel, isRefSentinel, isTaggedSentinel, makeTaggedSentinel, MODULE_FILE_ENGINE_NAMES, MODULE_PATH_ENGINE, plainChainOf, } from "./sentinel.js";
24
28
  export { buildCustomTags, defaultCustomTags } from "./yaml-tags.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@telorun/templating",
3
- "version": "0.106.0",
3
+ "version": "0.108.0",
4
4
  "description": "Telo Templating - Engine registry and shared CEL core for Telo manifests.",
5
5
  "keywords": [
6
6
  "telo",
@@ -35,18 +35,18 @@
35
35
  "src/**"
36
36
  ],
37
37
  "dependencies": {
38
- "@marcbachmann/cel-js": "7.6.1",
39
38
  "d3-format": "3.1.2",
40
- "re2js": "^2.8.3",
41
- "uuid": "^14.0.1",
42
- "yaml": "^2.8.3"
39
+ "re2js": "2.8.3",
40
+ "uuid": "14.0.1",
41
+ "yaml": "^2.8.3",
42
+ "@telorun/cel": "0.108.0"
43
43
  },
44
44
  "devDependencies": {
45
45
  "@types/d3-format": "^3.0.4",
46
46
  "@types/node": "^20.0.0",
47
47
  "typescript": "^5.0.0",
48
48
  "vitest": "^2.1.8",
49
- "@telorun/sdk": "0.106.0"
49
+ "@telorun/sdk": "0.108.0"
50
50
  },
51
51
  "peerDependencies": {
52
52
  "@telorun/sdk": "*"
package/src/builtins.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import type { CelEnvironment } from "@telorun/cel";
1
2
  import { celEngine } from "./engines/cel.js";
2
3
  import { includeBytesEngine, includeTextEngine } from "./engines/include.js";
3
4
  import { interpolateEngine } from "./engines/interpolate.js";
@@ -59,6 +60,17 @@ export function celExpressionsOf(engineName: string, source: string): string[] {
59
60
  return regions.map((r) => source.slice(r.start, r.end));
60
61
  }
61
62
 
63
+ /**
64
+ * Whether a tagged scalar's text, written twice in one value, means one value
65
+ * both times — false for a tag whose engine gives no such verdict.
66
+ *
67
+ * The single reader of `TemplatingEngine.repeatable`, so a consumer comparing
68
+ * two tagged scalars never recognises a tag by name.
69
+ */
70
+ export function repeatableSource(engineName: string, source: string, celEnv: CelEnvironment): boolean {
71
+ return defaultRegistry().get(engineName)?.repeatable?.(source, celEnv) ?? false;
72
+ }
73
+
62
74
  let defaultRegistryCache: TemplatingEngineRegistry | undefined;
63
75
 
64
76
  /** Memoized singleton: returns the default registry. Hosts that don't need
@@ -1,4 +1,9 @@
1
- import type { ASTNode } from "@marcbachmann/cel-js";
1
+ import {
2
+ childNodes,
3
+ namespaceMacroBinding,
4
+ receiverMacroBinding,
5
+ type CelNode,
6
+ } from "@telorun/cel";
2
7
  import { isLiveSlot } from "@telorun/sdk";
3
8
  import { moduleCallOf } from "./module-call.js";
4
9
 
@@ -15,7 +20,7 @@ import { moduleCallOf } from "./module-call.js";
15
20
  * tree, before this walk — an unresolved tree still reports the receiver, which
16
21
  * is the right answer for a caller that supplied no module names.
17
22
  */
18
- export function extractAccessChains(node: ASTNode): string[][] {
23
+ export function extractAccessChains(node: CelNode): string[][] {
19
24
  const chains: string[][] = [];
20
25
  visitNode(node, chains, new Set());
21
26
  return chains;
@@ -27,8 +32,8 @@ export function extractAccessChains(node: ASTNode): string[][] {
27
32
  * name the expression itself binds (`xs.map(item, Billing.total(item))`), which
28
33
  * no context schema describes.
29
34
  */
30
- export function moduleCallArgumentChains(node: ASTNode): Map<ASTNode, Array<string[] | null>> {
31
- const out = new Map<ASTNode, Array<string[] | null>>();
35
+ export function moduleCallArgumentChains(node: CelNode): Map<CelNode, Array<string[] | null>> {
36
+ const out = new Map<CelNode, Array<string[] | null>>();
32
37
  visitNode(node, [], new Set(), (call, callNode, boundVars) => {
33
38
  out.set(
34
39
  callNode,
@@ -40,33 +45,58 @@ export function moduleCallArgumentChains(node: ASTNode): Map<ASTNode, Array<stri
40
45
 
41
46
  type ModuleCallVisitor = (
42
47
  call: NonNullable<ReturnType<typeof moduleCallOf>>,
43
- node: ASTNode,
48
+ node: CelNode,
44
49
  boundVars: ReadonlySet<string>,
45
50
  ) => void;
46
51
 
47
- const COMPREHENSION_METHODS = new Set(["filter", "map", "exists", "all", "exists_one"]);
48
-
49
- /** `cel.bind(name, init, body)` — CEL's only binding form, and the one the
50
- * parser expands rather than dispatching, so it appears as a receiver call on a
51
- * bare `cel` identifier that is in no scope and never will be. Both walks below
52
- * need the same three facts out of it, so the shape is read once here.
52
+ /**
53
+ * `cel.bind(name, init, body)`.
53
54
  *
54
- * The receiver is deliberately not returned: it contributes no chain, and
55
- * descending into it is what produced a `CEL_UNKNOWN_FIELD` for `cel` itself. */
56
- function bindCall(node: ASTNode): { name: string; init: ASTNode; body: ASTNode } | null {
57
- if (node.op !== "rcall" || !Array.isArray(node.args)) return null;
58
- const [method, receiver, callArgs] = node.args as [unknown, unknown, unknown];
59
- if (method !== "bind") return null;
60
- if (!isASTNode(receiver) || receiver.op !== "id" || receiver.args !== "cel") return null;
61
- if (!Array.isArray(callArgs) || callArgs.length !== 3) return null;
62
- const [nameNode, init, body] = callArgs as [unknown, unknown, unknown];
63
- if (!isASTNode(nameNode) || nameNode.op !== "id") return null;
64
- if (!isASTNode(init) || !isASTNode(body)) return null;
65
- return { name: nameNode.args as string, init, body };
55
+ * `cel` is a RESERVED namespace — one a host can never claim, because the standard macros
56
+ * are written on it — so this is **not** a qualified call: it reads as a receiver call on
57
+ * the ident `cel`, which is in no scope and never will be. The engine's own binding table
58
+ * says which argument is the name and which are scoped.
59
+ *
60
+ * The receiver is deliberately not returned: it contributes no chain, and descending into it
61
+ * is what produced a `CEL_UNKNOWN_FIELD` for `cel` itself.
62
+ */
63
+ function bindCall(node: CelNode): { name: string; init: CelNode; body: CelNode } | null {
64
+ if (node.kind !== "receiverCall") return null;
65
+ if (node.receiver.kind !== "ident" || node.receiver.name !== "cel") return null;
66
+ const binding = namespaceMacroBinding("cel", node.name, node.args.length);
67
+ if (!binding) return null;
68
+ const nameNode = node.args[binding.variableArgument];
69
+ if (nameNode?.kind !== "ident") return null;
70
+ const scoped = binding.scopedArguments;
71
+ // `cel.bind(name, init, body)`: the body is scoped, everything else before it is not.
72
+ const body = node.args[scoped[scoped.length - 1]!];
73
+ const init = node.args.find(
74
+ (unused, at) => at !== binding.variableArgument && !scoped.includes(at),
75
+ );
76
+ if (!init || !body) return null;
77
+ return { name: nameNode.name, init, body };
78
+ }
79
+
80
+ /** A comprehension macro's iteration variable and the arguments it scopes, from the
81
+ * engine's own binding table rather than a list of method names kept in step with it. */
82
+ function comprehension(
83
+ node: CelNode,
84
+ ): { receiver: CelNode; bound: string; scoped: readonly CelNode[] } | null {
85
+ if (node.kind !== "receiverCall") return null;
86
+ if (node.receiver.kind === "ident" && node.receiver.name === "cel") return null;
87
+ const binding = receiverMacroBinding(node.name, node.args.length);
88
+ if (!binding) return null;
89
+ const nameNode = node.args[binding.variableArgument];
90
+ if (nameNode?.kind !== "ident") return null;
91
+ return {
92
+ receiver: node.receiver,
93
+ bound: nameNode.name,
94
+ scoped: binding.scopedArguments.flatMap((at) => (node.args[at] ? [node.args[at]!] : [])),
95
+ };
66
96
  }
67
97
 
68
98
  function visitNode(
69
- node: ASTNode,
99
+ node: CelNode,
70
100
  chains: string[][],
71
101
  boundVars: Set<string>,
72
102
  onModuleCall?: ModuleCallVisitor,
@@ -93,51 +123,16 @@ function visitNode(
93
123
  return;
94
124
  }
95
125
 
96
- if (
97
- node.op === "rcall" &&
98
- Array.isArray(node.args) &&
99
- typeof node.args[0] === "string" &&
100
- COMPREHENSION_METHODS.has(node.args[0])
101
- ) {
102
- const receiver = node.args[1];
103
- const comprehensionArgs = node.args[2];
104
- if (isASTNode(receiver)) visitNode(receiver, chains, boundVars, onModuleCall);
105
- if (
106
- Array.isArray(comprehensionArgs) &&
107
- comprehensionArgs.length >= 2 &&
108
- isASTNode(comprehensionArgs[0]) &&
109
- (comprehensionArgs[0] as ASTNode).op === "id"
110
- ) {
111
- const newBoundVars = new Set(boundVars);
112
- newBoundVars.add((comprehensionArgs[0] as ASTNode).args as string);
113
- for (let i = 1; i < comprehensionArgs.length; i++) {
114
- const arg = comprehensionArgs[i];
115
- if (isASTNode(arg)) visitNode(arg as ASTNode, chains, newBoundVars, onModuleCall);
116
- }
117
- }
126
+ const macro = comprehension(node);
127
+ if (macro) {
128
+ // The receiver is evaluated outside the binding; only the scoped arguments see it.
129
+ visitNode(macro.receiver, chains, boundVars, onModuleCall);
130
+ const inner = new Set(boundVars).add(macro.bound);
131
+ for (const arg of macro.scoped) visitNode(arg, chains, inner, onModuleCall);
118
132
  return;
119
133
  }
120
134
 
121
- const args = node.args;
122
- if (Array.isArray(args)) {
123
- for (const arg of args) {
124
- if (isASTNode(arg)) {
125
- visitNode(arg, chains, boundVars, onModuleCall);
126
- } else if (Array.isArray(arg)) {
127
- for (const item of arg) {
128
- if (isASTNode(item)) visitNode(item, chains, boundVars, onModuleCall);
129
- }
130
- }
131
- }
132
- } else if (isASTNode(args)) {
133
- // Unary operators (`!_`, `-_`) carry their operand as a single node
134
- // rather than a one-element array, so descend into it directly.
135
- visitNode(args, chains, boundVars, onModuleCall);
136
- }
137
- }
138
-
139
- function isASTNode(v: unknown): v is ASTNode {
140
- return v !== null && typeof v === "object" && "op" in (v as object);
135
+ for (const child of childNodes(node)) visitNode(child, chains, boundVars, onModuleCall);
141
136
  }
142
137
 
143
138
  /** Sentinel chain segment emitted for index access (`obj[expr]`) — a dynamic
@@ -145,20 +140,28 @@ function isASTNode(v: unknown): v is ASTNode {
145
140
  * chains to declared names treat this as "unknown member". */
146
141
  export const INDEX_SEGMENT = "[*]";
147
142
 
148
- function extractChain(node: ASTNode, boundVars: ReadonlySet<string>): string[] | null {
149
- if (node.op === "id") {
150
- const name = node.args as string;
151
- if (boundVars.has(name)) return null;
152
- return [name];
143
+ function extractChain(node: CelNode, boundVars: ReadonlySet<string>): string[] | null {
144
+ if (node.kind === "ident") {
145
+ // An ABSOLUTE name (`.y`) resolves against the environment's declarations and never
146
+ // against a name the expression bound, so a binding of the same spelling cannot hide it.
147
+ if (!node.absolute && boundVars.has(node.name)) return null;
148
+ return [node.name];
153
149
  }
154
- if (node.op === ".") {
155
- const [obj, field] = node.args as [ASTNode, string];
156
- const parent = extractChain(obj, boundVars);
157
- if (parent !== null) return [...parent, field];
150
+ // **An OPTIONAL read ends the chain.** `a.?b` and `a[?0]` answer an `optional<T>`, so what
151
+ // follows is a method on that optional (`.orValue('')`) rather than a member of the
152
+ // context — and the author has said outright that the member may be absent, which is not a
153
+ // claim a context schema has to declare. The engine makes both a real `select`/`index`
154
+ // with `optional: true`, where the replaced engine had no node this walk could chain, so
155
+ // continuing would both invent a read and hand `validateChainAgainstSchema` a path past
156
+ // the point the expression stopped asserting anything.
157
+ if (node.kind === "select") {
158
+ if (node.optional) return null;
159
+ const parent = extractChain(node.operand, boundVars);
160
+ if (parent !== null) return [...parent, node.field];
158
161
  }
159
- if (node.op === "[]") {
160
- const [obj] = node.args as [ASTNode, ASTNode];
161
- const parent = extractChain(obj, boundVars);
162
+ if (node.kind === "index") {
163
+ if (node.optional) return null;
164
+ const parent = extractChain(node.operand, boundVars);
162
165
  if (parent !== null) return [...parent, INDEX_SEGMENT];
163
166
  }
164
167
  return null;
@@ -176,21 +179,25 @@ export interface CallResultAccess {
176
179
  * {@link extractAccessChains} deliberately skips, since their root is a value
177
180
  * no context schema describes. The caller supplies the call's result schema.
178
181
  */
179
- export function extractCallResultAccesses(node: ASTNode): CallResultAccess[] {
182
+ export function extractCallResultAccesses(node: CelNode): CallResultAccess[] {
180
183
  const out: CallResultAccess[] = [];
181
184
  visitAccess(node, out);
182
185
  return out;
183
186
  }
184
187
 
185
- function visitAccess(node: ASTNode, out: CallResultAccess[]): void {
186
- if (node.op === "." || node.op === "[]") {
188
+ function visitAccess(node: CelNode, out: CallResultAccess[]): void {
189
+ if (node.kind === "select" || node.kind === "index") {
187
190
  const members: string[] = [];
188
- let base: ASTNode = node;
189
- while (base.op === "." || base.op === "[]") {
190
- const [obj, field] = base.args as [ASTNode, unknown];
191
- members.unshift(base.op === "." ? String(field) : INDEX_SEGMENT);
192
- if (base.op === "[]" && isASTNode(field)) visitAccess(field, out);
193
- base = obj;
191
+ let base: CelNode = node;
192
+ while (base.kind === "select" || base.kind === "index") {
193
+ if (base.kind === "select") {
194
+ members.unshift(base.field);
195
+ } else {
196
+ members.unshift(INDEX_SEGMENT);
197
+ // The index is its own expression and may itself read off a call's result.
198
+ visitAccess(base.index, out);
199
+ }
200
+ base = base.operand;
194
201
  }
195
202
  const call = moduleCallOf(base);
196
203
  if (call) {
@@ -201,20 +208,7 @@ function visitAccess(node: ASTNode, out: CallResultAccess[]): void {
201
208
  visitAccess(base, out);
202
209
  return;
203
210
  }
204
- const moduleCall = moduleCallOf(node);
205
- if (moduleCall) {
206
- for (const arg of moduleCall.args) visitAccess(arg, out);
207
- return;
208
- }
209
- const args = node.args;
210
- if (Array.isArray(args)) {
211
- for (const arg of args) {
212
- if (isASTNode(arg)) visitAccess(arg, out);
213
- else if (Array.isArray(arg)) for (const item of arg) if (isASTNode(item)) visitAccess(item, out);
214
- }
215
- } else if (isASTNode(args)) {
216
- visitAccess(args, out);
217
- }
211
+ for (const child of childNodes(node)) visitAccess(child, out);
218
212
  }
219
213
 
220
214
  interface NullableIssue {
@@ -252,7 +246,7 @@ function schemaAtChain(
252
246
  * admits null — a value that may itself be null, rather than one dereferenced
253
247
  * through a null. */
254
248
  export function nullableValueChain(
255
- node: ASTNode,
249
+ node: CelNode,
256
250
  contextSchema: Record<string, any>,
257
251
  ): string | null {
258
252
  const chain = dottedChain(node, new Set());
@@ -260,13 +254,13 @@ export function nullableValueChain(
260
254
  return schemaIsNullable(schemaAtChain(chain.split("."), contextSchema)) ? chain : null;
261
255
  }
262
256
 
263
- function isNullLiteral(node: ASTNode): boolean {
264
- return node.op === "value" && (node.args as unknown) === null;
257
+ function isNullLiteral(node: CelNode): boolean {
258
+ return node.kind === "literal" && node.literal.type === "null";
265
259
  }
266
260
 
267
261
  /** Dotted form of a static member chain rooted at a free identifier, or null
268
262
  * when the node isn't such a chain (call result, bound var, index, …). */
269
- function dottedChain(node: ASTNode, boundVars: Set<string>): string | null {
263
+ function dottedChain(node: CelNode, boundVars: Set<string>): string | null {
270
264
  const chain = extractChain(node, boundVars);
271
265
  if (chain === null || chain.includes(INDEX_SEGMENT)) return null;
272
266
  return chain.join(".");
@@ -281,35 +275,33 @@ const EMPTY_NARROWING: Narrowing = { whenTrue: new Set(), whenFalse: new Set() }
281
275
 
282
276
  /** Derive which chains a boolean condition proves non-null in its true / false
283
277
  * branches. Handles `x == null` / `x != null`, negation, and `&&` / `||`. */
284
- function deriveNarrowing(node: ASTNode, boundVars: Set<string>): Narrowing {
285
- if (node.op === "==" || node.op === "!=") {
286
- const [l, r] = node.args as [ASTNode, ASTNode];
287
- const chain = isNullLiteral(r)
288
- ? dottedChain(l, boundVars)
289
- : isNullLiteral(l)
290
- ? dottedChain(r, boundVars)
278
+ function deriveNarrowing(node: CelNode, boundVars: Set<string>): Narrowing {
279
+ if (node.kind === "unary" && node.operator === "!") {
280
+ const inner = deriveNarrowing(node.operand, boundVars);
281
+ return { whenTrue: inner.whenFalse, whenFalse: inner.whenTrue };
282
+ }
283
+ if (node.kind !== "binary") return EMPTY_NARROWING;
284
+ if (node.operator === "==" || node.operator === "!=") {
285
+ const chain = isNullLiteral(node.right)
286
+ ? dottedChain(node.left, boundVars)
287
+ : isNullLiteral(node.left)
288
+ ? dottedChain(node.right, boundVars)
291
289
  : null;
292
290
  if (chain === null) return EMPTY_NARROWING;
293
291
  const proven = new Set([chain]);
294
- return node.op === "!="
292
+ return node.operator === "!="
295
293
  ? { whenTrue: proven, whenFalse: new Set() }
296
294
  : { whenTrue: new Set(), whenFalse: proven };
297
295
  }
298
- if (node.op === "!_") {
299
- const inner = deriveNarrowing(node.args as ASTNode, boundVars);
300
- return { whenTrue: inner.whenFalse, whenFalse: inner.whenTrue };
301
- }
302
- if (node.op === "&&") {
303
- const [a, b] = node.args as [ASTNode, ASTNode];
304
- const na = deriveNarrowing(a, boundVars);
305
- const nb = deriveNarrowing(b, boundVars);
306
- return { whenTrue: union(na.whenTrue, nb.whenTrue), whenFalse: new Set() };
296
+ if (node.operator === "&&") {
297
+ const left = deriveNarrowing(node.left, boundVars);
298
+ const right = deriveNarrowing(node.right, boundVars);
299
+ return { whenTrue: union(left.whenTrue, right.whenTrue), whenFalse: new Set() };
307
300
  }
308
- if (node.op === "||") {
309
- const [a, b] = node.args as [ASTNode, ASTNode];
310
- const na = deriveNarrowing(a, boundVars);
311
- const nb = deriveNarrowing(b, boundVars);
312
- return { whenTrue: new Set(), whenFalse: union(na.whenFalse, nb.whenFalse) };
301
+ if (node.operator === "||") {
302
+ const left = deriveNarrowing(node.left, boundVars);
303
+ const right = deriveNarrowing(node.right, boundVars);
304
+ return { whenTrue: new Set(), whenFalse: union(left.whenFalse, right.whenFalse) };
313
305
  }
314
306
  return EMPTY_NARROWING;
315
307
  }
@@ -327,7 +319,7 @@ function union(a: Set<string>, b: Set<string>): Set<string> {
327
319
  * Returns one issue per unguarded dereference.
328
320
  */
329
321
  export function findNullableAccessIssues(
330
- node: ASTNode,
322
+ node: CelNode,
331
323
  contextSchema: Record<string, any>,
332
324
  ): NullableIssue[] {
333
325
  const issues: NullableIssue[] = [];
@@ -336,62 +328,56 @@ export function findNullableAccessIssues(
336
328
  }
337
329
 
338
330
  function walkNullable(
339
- node: ASTNode,
331
+ node: CelNode,
340
332
  nonNull: Set<string>,
341
333
  boundVars: Set<string>,
342
334
  issues: NullableIssue[],
343
335
  schema: Record<string, any>,
344
336
  ): void {
345
337
  // Dereference of an object/array member — check the receiver's nullability.
346
- if (node.op === "." || node.op === "[]") {
347
- const [obj, field] = node.args as [ASTNode, unknown];
348
- const objChain = dottedChain(obj, boundVars);
338
+ if (node.kind === "select" || node.kind === "index") {
339
+ const objChain = dottedChain(node.operand, boundVars);
349
340
  if (objChain !== null && !nonNull.has(objChain)) {
350
- const objSchema = schemaAtChain(objChain.split("."), schema);
351
- if (schemaIsNullable(objSchema)) {
341
+ if (schemaIsNullable(schemaAtChain(objChain.split("."), schema))) {
352
342
  issues.push({
353
343
  path: objChain,
354
- member: node.op === "." ? String(field) : "[index]",
344
+ member: node.kind === "select" ? node.field : "[index]",
355
345
  });
356
346
  }
357
347
  }
358
- walkNullable(obj, nonNull, boundVars, issues, schema);
359
- // The index expression (`obj[expr]`) is itself evaluated — descend so a
360
- // nullable deref used as an index (e.g. `items[error.code]`) is caught.
361
- if (node.op === "[]" && isASTNode(field)) {
362
- walkNullable(field as ASTNode, nonNull, boundVars, issues, schema);
363
- }
348
+ walkNullable(node.operand, nonNull, boundVars, issues, schema);
349
+ // The index expression (`obj[expr]`) is itself evaluated — descend so a nullable
350
+ // deref used as an index (`items[error.code]`) is caught.
351
+ if (node.kind === "index") walkNullable(node.index, nonNull, boundVars, issues, schema);
364
352
  return;
365
353
  }
366
354
 
367
- if (node.op === "?:") {
368
- const [cond, thenB, elseB] = node.args as [ASTNode, ASTNode, ASTNode];
369
- walkNullable(cond, nonNull, boundVars, issues, schema);
370
- const n = deriveNarrowing(cond, boundVars);
371
- walkNullable(thenB, union(nonNull, n.whenTrue), boundVars, issues, schema);
372
- walkNullable(elseB, union(nonNull, n.whenFalse), boundVars, issues, schema);
355
+ if (node.kind === "conditional") {
356
+ walkNullable(node.condition, nonNull, boundVars, issues, schema);
357
+ const narrowed = deriveNarrowing(node.condition, boundVars);
358
+ walkNullable(node.whenTrue, union(nonNull, narrowed.whenTrue), boundVars, issues, schema);
359
+ walkNullable(node.whenFalse, union(nonNull, narrowed.whenFalse), boundVars, issues, schema);
373
360
  return;
374
361
  }
375
362
 
376
- if (node.op === "&&" || node.op === "||") {
377
- const [a, b] = node.args as [ASTNode, ASTNode];
378
- walkNullable(a, nonNull, boundVars, issues, schema);
379
- const n = deriveNarrowing(a, boundVars);
380
- const carried = node.op === "&&" ? n.whenTrue : n.whenFalse;
381
- walkNullable(b, union(nonNull, carried), boundVars, issues, schema);
363
+ if (node.kind === "binary" && (node.operator === "&&" || node.operator === "||")) {
364
+ walkNullable(node.left, nonNull, boundVars, issues, schema);
365
+ const narrowed = deriveNarrowing(node.left, boundVars);
366
+ const carried = node.operator === "&&" ? narrowed.whenTrue : narrowed.whenFalse;
367
+ walkNullable(node.right, union(nonNull, carried), boundVars, issues, schema);
382
368
  return;
383
369
  }
384
370
 
385
- // A module call's receiver is a module name, not a value; mirror
386
- // extractAccessChains so it is never read as a nullable context field.
371
+ // A module call's receiver is a module name, not a value; mirror extractAccessChains so
372
+ // it is never read as a nullable context field.
387
373
  const moduleCall = moduleCallOf(node);
388
374
  if (moduleCall) {
389
375
  for (const arg of moduleCall.args) walkNullable(arg, nonNull, boundVars, issues, schema);
390
376
  return;
391
377
  }
392
378
 
393
- // `cel.bind` binds a name for its body; mirror extractAccessChains so a bound
394
- // name is never read as a nullable context field.
379
+ // `cel.bind` binds a name for its body; mirror extractAccessChains so a bound name is
380
+ // never read as a nullable context field.
395
381
  const bind = bindCall(node);
396
382
  if (bind) {
397
383
  walkNullable(bind.init, nonNull, boundVars, issues, schema);
@@ -399,45 +385,18 @@ function walkNullable(
399
385
  return;
400
386
  }
401
387
 
402
- // Comprehension macros bind a loop variable; mirror extractAccessChains so a
403
- // bound var is never mistaken for a nullable context field.
404
- if (
405
- node.op === "rcall" &&
406
- Array.isArray(node.args) &&
407
- typeof node.args[0] === "string" &&
408
- COMPREHENSION_METHODS.has(node.args[0])
409
- ) {
410
- const receiver = node.args[1];
411
- const comprehensionArgs = node.args[2];
412
- if (isASTNode(receiver)) walkNullable(receiver, nonNull, boundVars, issues, schema);
413
- if (
414
- Array.isArray(comprehensionArgs) &&
415
- comprehensionArgs.length >= 2 &&
416
- isASTNode(comprehensionArgs[0]) &&
417
- (comprehensionArgs[0] as ASTNode).op === "id"
418
- ) {
419
- const inner = new Set(boundVars);
420
- inner.add((comprehensionArgs[0] as ASTNode).args as string);
421
- for (let i = 1; i < comprehensionArgs.length; i++) {
422
- const arg = comprehensionArgs[i];
423
- if (isASTNode(arg)) walkNullable(arg as ASTNode, nonNull, inner, issues, schema);
424
- }
425
- }
388
+ // A comprehension binds a loop variable; mirror extractAccessChains so a bound var is
389
+ // never mistaken for a nullable context field.
390
+ const macro = comprehension(node);
391
+ if (macro) {
392
+ walkNullable(macro.receiver, nonNull, boundVars, issues, schema);
393
+ const inner = new Set(boundVars).add(macro.bound);
394
+ for (const arg of macro.scoped) walkNullable(arg, nonNull, inner, issues, schema);
426
395
  return;
427
396
  }
428
397
 
429
- const args = node.args;
430
- if (Array.isArray(args)) {
431
- for (const arg of args) {
432
- if (isASTNode(arg)) walkNullable(arg, nonNull, boundVars, issues, schema);
433
- else if (Array.isArray(arg)) {
434
- for (const item of arg) {
435
- if (isASTNode(item)) walkNullable(item, nonNull, boundVars, issues, schema);
436
- }
437
- }
438
- }
439
- } else if (isASTNode(args)) {
440
- walkNullable(args, nonNull, boundVars, issues, schema);
398
+ for (const child of childNodes(node)) {
399
+ walkNullable(child, nonNull, boundVars, issues, schema);
441
400
  }
442
401
  }
443
402