@telorun/templating 0.20.0 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cel/analyze.d.ts +26 -0
- package/dist/cel/analyze.d.ts.map +1 -1
- package/dist/cel/analyze.js +114 -8
- package/dist/cel/catalog.d.ts.map +1 -1
- package/dist/cel/catalog.js +10 -0
- package/dist/cel/compile.d.ts +9 -3
- package/dist/cel/compile.d.ts.map +1 -1
- package/dist/cel/compile.js +25 -11
- package/dist/cel/diagnose.d.ts +10 -1
- package/dist/cel/diagnose.d.ts.map +1 -1
- package/dist/cel/diagnose.js +42 -6
- package/dist/cel/environment.d.ts.map +1 -1
- package/dist/cel/environment.js +2 -0
- package/dist/cel/module-call.d.ts +108 -0
- package/dist/cel/module-call.d.ts.map +1 -0
- package/dist/cel/module-call.js +245 -0
- package/dist/cel/value-identity.d.ts +18 -0
- package/dist/cel/value-identity.d.ts.map +1 -0
- package/dist/cel/value-identity.js +25 -0
- package/dist/engine.d.ts +66 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.d.ts.map +1 -1
- package/dist/engines/cel.js +87 -6
- package/dist/engines/sql.d.ts.map +1 -1
- package/dist/engines/sql.js +5 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/package.json +3 -3
- package/src/cel/analyze.ts +131 -9
- package/src/cel/catalog.ts +10 -0
- package/src/cel/compile.ts +32 -12
- package/src/cel/diagnose.ts +56 -6
- package/src/cel/environment.ts +3 -0
- package/src/cel/module-call.ts +369 -0
- package/src/cel/value-identity.ts +34 -0
- package/src/engine.ts +72 -0
- package/src/engines/cel.ts +104 -6
- package/src/engines/sql.ts +5 -1
- package/src/index.ts +20 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/templating",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.21.0",
|
|
4
4
|
"description": "Telo Templating - Engine registry and shared CEL core for Telo manifests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"telo",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"src/**"
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@marcbachmann/cel-js": "
|
|
38
|
+
"@marcbachmann/cel-js": "7.6.1",
|
|
39
39
|
"d3-format": "3.1.2",
|
|
40
40
|
"re2js": "^2.8.3",
|
|
41
41
|
"uuid": "^14.0.1",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
"@types/node": "^20.0.0",
|
|
47
47
|
"typescript": "^5.0.0",
|
|
48
48
|
"vitest": "^2.1.8",
|
|
49
|
-
"@telorun/sdk": "0.
|
|
49
|
+
"@telorun/sdk": "0.92.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@telorun/sdk": "*"
|
package/src/cel/analyze.ts
CHANGED
|
@@ -1,11 +1,19 @@
|
|
|
1
1
|
import type { ASTNode } from "@marcbachmann/cel-js";
|
|
2
2
|
import { isLiveSlot } from "@telorun/sdk";
|
|
3
|
+
import { moduleCallOf } from "./module-call.js";
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Extract all member-access chains from a CEL AST.
|
|
6
7
|
* Returns arrays like ["request", "query", "name"] for `request.query.name`.
|
|
7
8
|
* Chains that start with a call or non-identifier root are ignored.
|
|
8
9
|
* Bound variables in comprehension macros (filter, map, exists, all, exists_one) are excluded.
|
|
10
|
+
*
|
|
11
|
+
* A RESOLVED module call contributes no chain for its receiver: `Billing` in
|
|
12
|
+
* `Billing.format(x)` names a module, not a value, so reading it as a root
|
|
13
|
+
* would invent a dependency, an undeclared identifier and a `resources` read
|
|
14
|
+
* that the expression never states. Resolution is `resolveModuleCalls`, on the
|
|
15
|
+
* tree, before this walk — an unresolved tree still reports the receiver, which
|
|
16
|
+
* is the right answer for a caller that supplied no module names.
|
|
9
17
|
*/
|
|
10
18
|
export function extractAccessChains(node: ASTNode): string[][] {
|
|
11
19
|
const chains: string[][] = [];
|
|
@@ -13,6 +21,29 @@ export function extractAccessChains(node: ASTNode): string[][] {
|
|
|
13
21
|
return chains;
|
|
14
22
|
}
|
|
15
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Each resolved module call's arguments as plain member chains, keyed by the
|
|
26
|
+
* call's node — `null` for an argument that is not one, or that is rooted at a
|
|
27
|
+
* name the expression itself binds (`xs.map(item, Billing.total(item))`), which
|
|
28
|
+
* no context schema describes.
|
|
29
|
+
*/
|
|
30
|
+
export function moduleCallArgumentChains(node: ASTNode): Map<ASTNode, Array<string[] | null>> {
|
|
31
|
+
const out = new Map<ASTNode, Array<string[] | null>>();
|
|
32
|
+
visitNode(node, [], new Set(), (call, callNode, boundVars) => {
|
|
33
|
+
out.set(
|
|
34
|
+
callNode,
|
|
35
|
+
call.args.map((arg) => extractChain(arg, boundVars)),
|
|
36
|
+
);
|
|
37
|
+
});
|
|
38
|
+
return out;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type ModuleCallVisitor = (
|
|
42
|
+
call: NonNullable<ReturnType<typeof moduleCallOf>>,
|
|
43
|
+
node: ASTNode,
|
|
44
|
+
boundVars: ReadonlySet<string>,
|
|
45
|
+
) => void;
|
|
46
|
+
|
|
16
47
|
const COMPREHENSION_METHODS = new Set(["filter", "map", "exists", "all", "exists_one"]);
|
|
17
48
|
|
|
18
49
|
/** `cel.bind(name, init, body)` — CEL's only binding form, and the one the
|
|
@@ -34,19 +65,31 @@ function bindCall(node: ASTNode): { name: string; init: ASTNode; body: ASTNode }
|
|
|
34
65
|
return { name: nameNode.args as string, init, body };
|
|
35
66
|
}
|
|
36
67
|
|
|
37
|
-
function visitNode(
|
|
68
|
+
function visitNode(
|
|
69
|
+
node: ASTNode,
|
|
70
|
+
chains: string[][],
|
|
71
|
+
boundVars: Set<string>,
|
|
72
|
+
onModuleCall?: ModuleCallVisitor,
|
|
73
|
+
): void {
|
|
38
74
|
const chain = extractChain(node, boundVars);
|
|
39
75
|
if (chain !== null) {
|
|
40
76
|
chains.push(chain);
|
|
41
77
|
return;
|
|
42
78
|
}
|
|
43
79
|
|
|
80
|
+
const moduleCall = moduleCallOf(node);
|
|
81
|
+
if (moduleCall) {
|
|
82
|
+
onModuleCall?.(moduleCall, node, boundVars);
|
|
83
|
+
for (const arg of moduleCall.args) visitNode(arg, chains, boundVars, onModuleCall);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
44
87
|
// The bound name is in scope for the body ONLY; `init` is evaluated in the
|
|
45
88
|
// enclosing scope, so a name used there still has to resolve there.
|
|
46
89
|
const bind = bindCall(node);
|
|
47
90
|
if (bind) {
|
|
48
|
-
visitNode(bind.init, chains, boundVars);
|
|
49
|
-
visitNode(bind.body, chains, new Set(boundVars).add(bind.name));
|
|
91
|
+
visitNode(bind.init, chains, boundVars, onModuleCall);
|
|
92
|
+
visitNode(bind.body, chains, new Set(boundVars).add(bind.name), onModuleCall);
|
|
50
93
|
return;
|
|
51
94
|
}
|
|
52
95
|
|
|
@@ -58,7 +101,7 @@ function visitNode(node: ASTNode, chains: string[][], boundVars: Set<string>): v
|
|
|
58
101
|
) {
|
|
59
102
|
const receiver = node.args[1];
|
|
60
103
|
const comprehensionArgs = node.args[2];
|
|
61
|
-
if (isASTNode(receiver)) visitNode(receiver, chains, boundVars);
|
|
104
|
+
if (isASTNode(receiver)) visitNode(receiver, chains, boundVars, onModuleCall);
|
|
62
105
|
if (
|
|
63
106
|
Array.isArray(comprehensionArgs) &&
|
|
64
107
|
comprehensionArgs.length >= 2 &&
|
|
@@ -69,7 +112,7 @@ function visitNode(node: ASTNode, chains: string[][], boundVars: Set<string>): v
|
|
|
69
112
|
newBoundVars.add((comprehensionArgs[0] as ASTNode).args as string);
|
|
70
113
|
for (let i = 1; i < comprehensionArgs.length; i++) {
|
|
71
114
|
const arg = comprehensionArgs[i];
|
|
72
|
-
if (isASTNode(arg)) visitNode(arg as ASTNode, chains, newBoundVars);
|
|
115
|
+
if (isASTNode(arg)) visitNode(arg as ASTNode, chains, newBoundVars, onModuleCall);
|
|
73
116
|
}
|
|
74
117
|
}
|
|
75
118
|
return;
|
|
@@ -79,17 +122,17 @@ function visitNode(node: ASTNode, chains: string[][], boundVars: Set<string>): v
|
|
|
79
122
|
if (Array.isArray(args)) {
|
|
80
123
|
for (const arg of args) {
|
|
81
124
|
if (isASTNode(arg)) {
|
|
82
|
-
visitNode(arg, chains, boundVars);
|
|
125
|
+
visitNode(arg, chains, boundVars, onModuleCall);
|
|
83
126
|
} else if (Array.isArray(arg)) {
|
|
84
127
|
for (const item of arg) {
|
|
85
|
-
if (isASTNode(item)) visitNode(item, chains, boundVars);
|
|
128
|
+
if (isASTNode(item)) visitNode(item, chains, boundVars, onModuleCall);
|
|
86
129
|
}
|
|
87
130
|
}
|
|
88
131
|
}
|
|
89
132
|
} else if (isASTNode(args)) {
|
|
90
133
|
// Unary operators (`!_`, `-_`) carry their operand as a single node
|
|
91
134
|
// rather than a one-element array, so descend into it directly.
|
|
92
|
-
visitNode(args, chains, boundVars);
|
|
135
|
+
visitNode(args, chains, boundVars, onModuleCall);
|
|
93
136
|
}
|
|
94
137
|
}
|
|
95
138
|
|
|
@@ -102,7 +145,7 @@ function isASTNode(v: unknown): v is ASTNode {
|
|
|
102
145
|
* chains to declared names treat this as "unknown member". */
|
|
103
146
|
export const INDEX_SEGMENT = "[*]";
|
|
104
147
|
|
|
105
|
-
function extractChain(node: ASTNode, boundVars:
|
|
148
|
+
function extractChain(node: ASTNode, boundVars: ReadonlySet<string>): string[] | null {
|
|
106
149
|
if (node.op === "id") {
|
|
107
150
|
const name = node.args as string;
|
|
108
151
|
if (boundVars.has(name)) return null;
|
|
@@ -121,6 +164,59 @@ function extractChain(node: ASTNode, boundVars: Set<string>): string[] | null {
|
|
|
121
164
|
return null;
|
|
122
165
|
}
|
|
123
166
|
|
|
167
|
+
/** A member access on a module call's result: the call, and the members read
|
|
168
|
+
* off it in order (`Billing.total(xs).amount` → `amount`). */
|
|
169
|
+
export interface CallResultAccess {
|
|
170
|
+
readonly qualified: string;
|
|
171
|
+
readonly members: readonly string[];
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Every member access whose base is a module call, longest first — the chains
|
|
176
|
+
* {@link extractAccessChains} deliberately skips, since their root is a value
|
|
177
|
+
* no context schema describes. The caller supplies the call's result schema.
|
|
178
|
+
*/
|
|
179
|
+
export function extractCallResultAccesses(node: ASTNode): CallResultAccess[] {
|
|
180
|
+
const out: CallResultAccess[] = [];
|
|
181
|
+
visitAccess(node, out);
|
|
182
|
+
return out;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function visitAccess(node: ASTNode, out: CallResultAccess[]): void {
|
|
186
|
+
if (node.op === "." || node.op === "[]") {
|
|
187
|
+
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;
|
|
194
|
+
}
|
|
195
|
+
const call = moduleCallOf(base);
|
|
196
|
+
if (call) {
|
|
197
|
+
out.push({ qualified: call.qualified, members });
|
|
198
|
+
for (const arg of call.args) visitAccess(arg, out);
|
|
199
|
+
return;
|
|
200
|
+
}
|
|
201
|
+
visitAccess(base, out);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
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
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
124
220
|
interface NullableIssue {
|
|
125
221
|
/** Dotted path of the nullable value being dereferenced (e.g. "error"). */
|
|
126
222
|
path: string;
|
|
@@ -274,6 +370,14 @@ function walkNullable(
|
|
|
274
370
|
return;
|
|
275
371
|
}
|
|
276
372
|
|
|
373
|
+
// A module call's receiver is a module name, not a value; mirror
|
|
374
|
+
// extractAccessChains so it is never read as a nullable context field.
|
|
375
|
+
const moduleCall = moduleCallOf(node);
|
|
376
|
+
if (moduleCall) {
|
|
377
|
+
for (const arg of moduleCall.args) walkNullable(arg, nonNull, boundVars, issues, schema);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
|
|
277
381
|
// `cel.bind` binds a name for its body; mirror extractAccessChains so a bound
|
|
278
382
|
// name is never read as a nullable context field.
|
|
279
383
|
const bind = bindCall(node);
|
|
@@ -341,6 +445,24 @@ export function validateChainAgainstSchema(
|
|
|
341
445
|
for (let i = 0; i < chain.length; i++) {
|
|
342
446
|
const key = chain[i]!;
|
|
343
447
|
if (!current || typeof current !== "object") return null;
|
|
448
|
+
// An index reads an ELEMENT: an array's `items`, or a map's value schema. What
|
|
449
|
+
// is below it is checked like any member access, which is what reaches a
|
|
450
|
+
// typo inside a list of records (`items[0].amont`). An object that also
|
|
451
|
+
// declares `properties` may be indexed by one of those names, which the
|
|
452
|
+
// chain does not record, so nothing below it is judged.
|
|
453
|
+
if (key === INDEX_SEGMENT) {
|
|
454
|
+
const element =
|
|
455
|
+
current.items && typeof current.items === "object" && !Array.isArray(current.items)
|
|
456
|
+
? current.items
|
|
457
|
+
: !current.properties &&
|
|
458
|
+
current.additionalProperties &&
|
|
459
|
+
typeof current.additionalProperties === "object"
|
|
460
|
+
? current.additionalProperties
|
|
461
|
+
: undefined;
|
|
462
|
+
if (!element) return null;
|
|
463
|
+
current = element;
|
|
464
|
+
continue;
|
|
465
|
+
}
|
|
344
466
|
const props: Record<string, any> | undefined = current.properties;
|
|
345
467
|
if (!props) return null;
|
|
346
468
|
if (key in props) {
|
package/src/cel/catalog.ts
CHANGED
|
@@ -1030,6 +1030,16 @@ export const CEL_FUNCTIONS: readonly CelFunctionDoc[] = [
|
|
|
1030
1030
|
},
|
|
1031
1031
|
// Time (non-deterministic). `nowIso`/`today` take an optional IANA timezone
|
|
1032
1032
|
// (default "UTC"); epoch values are absolute and take none.
|
|
1033
|
+
{
|
|
1034
|
+
name: "now",
|
|
1035
|
+
signature: "now(): timestamp",
|
|
1036
|
+
register: ["now(): google.protobuf.Timestamp"],
|
|
1037
|
+
category: "time",
|
|
1038
|
+
summary: "Current instant as a native timestamp, for comparison and duration arithmetic.",
|
|
1039
|
+
deterministic: false,
|
|
1040
|
+
hostBacked: false,
|
|
1041
|
+
build: () => () => new Date(),
|
|
1042
|
+
},
|
|
1033
1043
|
{
|
|
1034
1044
|
name: "nowIso",
|
|
1035
1045
|
signature: "nowIso(string?): string",
|
package/src/cel/compile.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { isCompiledValue, type CompiledValue } from "@telorun/sdk";
|
|
2
|
-
import type { Environment } from "@marcbachmann/cel-js";
|
|
2
|
+
import type { ASTNode, Environment } from "@marcbachmann/cel-js";
|
|
3
3
|
import { extractAccessChains } from "./analyze.js";
|
|
4
|
+
import { resolveModuleCalls } from "./module-call.js";
|
|
4
5
|
|
|
5
6
|
export const TEMPLATE_REGEX = /\$\{\{\s*([^}]+?)\s*\}\}/g;
|
|
6
7
|
export const EXACT_TEMPLATE_REGEX = /^\s*\$\{\{\s*([^}]+?)\s*\}\}\s*$/;
|
|
@@ -9,11 +10,9 @@ export const EXACT_TEMPLATE_REGEX = /^\s*\$\{\{\s*([^}]+?)\s*\}\}\s*$/;
|
|
|
9
10
|
* first element of every member-access chain (`self.table` → `self`). Returns
|
|
10
11
|
* undefined when the parse result carries no AST so consumers can tell "no
|
|
11
12
|
* refs" from "unknown". */
|
|
12
|
-
function rootRefs(
|
|
13
|
-
const ast = (parsed as { ast?: unknown }).ast;
|
|
14
|
-
if (!ast) return undefined;
|
|
13
|
+
function rootRefs(ast: ASTNode): readonly string[] {
|
|
15
14
|
const roots = new Set<string>();
|
|
16
|
-
for (const chain of extractAccessChains(ast
|
|
15
|
+
for (const chain of extractAccessChains(ast)) {
|
|
17
16
|
if (chain.length > 0) roots.add(chain[0]!);
|
|
18
17
|
}
|
|
19
18
|
return [...roots];
|
|
@@ -21,13 +20,26 @@ function rootRefs(parsed: unknown): readonly string[] | undefined {
|
|
|
21
20
|
|
|
22
21
|
/** Compile a single CEL expression (no `${{ }}` wrapping) into a CompiledValue.
|
|
23
22
|
* Throws on syntax errors. Used by the `!cel` engine where the entire tagged
|
|
24
|
-
* scalar is treated as one expression.
|
|
25
|
-
|
|
23
|
+
* scalar is treated as one expression.
|
|
24
|
+
*
|
|
25
|
+
* `moduleNames` are the declaring module's own names; a call whose receiver is
|
|
26
|
+
* one of them resolves to a late-bound module call HERE, on the parsed tree, so
|
|
27
|
+
* every later reader — the root references below, the analyzer's passes, the
|
|
28
|
+
* kernel's dependency walk — sees the resolved shape rather than re-deriving
|
|
29
|
+
* it from the source text. */
|
|
30
|
+
export function compileExpression(
|
|
31
|
+
expr: string,
|
|
32
|
+
env: Environment,
|
|
33
|
+
moduleNames?: ReadonlySet<string>,
|
|
34
|
+
): CompiledValue {
|
|
26
35
|
const fn = env.parse(expr);
|
|
36
|
+
const ast: ASTNode = fn.ast;
|
|
37
|
+
const calls = resolveModuleCalls(ast, moduleNames);
|
|
27
38
|
return {
|
|
28
39
|
__compiled: true,
|
|
29
40
|
source: expr,
|
|
30
|
-
refs: rootRefs(
|
|
41
|
+
refs: rootRefs(ast),
|
|
42
|
+
calls,
|
|
31
43
|
call: (ctx: Record<string, unknown>) => fn(ctx),
|
|
32
44
|
};
|
|
33
45
|
}
|
|
@@ -37,26 +49,33 @@ export function compileExpression(expr: string, env: Environment): CompiledValue
|
|
|
37
49
|
* contains interpolations, returns a CompiledValue that joins literal parts
|
|
38
50
|
* with stringified expression results. If no expressions are present, returns
|
|
39
51
|
* the input string unchanged. Throws on CEL syntax errors. */
|
|
40
|
-
export function compileString(
|
|
52
|
+
export function compileString(
|
|
53
|
+
s: string,
|
|
54
|
+
env: Environment,
|
|
55
|
+
moduleNames?: ReadonlySet<string>,
|
|
56
|
+
): unknown {
|
|
41
57
|
if (!s.includes("${{")) return s;
|
|
42
58
|
|
|
43
59
|
const exact = s.match(EXACT_TEMPLATE_REGEX);
|
|
44
60
|
if (exact) {
|
|
45
|
-
return compileExpression(exact[1].trim(), env);
|
|
61
|
+
return compileExpression(exact[1].trim(), env, moduleNames);
|
|
46
62
|
}
|
|
47
63
|
|
|
48
64
|
const parts: Array<string | CompiledValue> = [];
|
|
49
65
|
let last = 0;
|
|
50
66
|
for (const m of s.matchAll(TEMPLATE_REGEX)) {
|
|
51
67
|
if (m.index! > last) parts.push(s.slice(last, m.index));
|
|
52
|
-
parts.push(compileExpression(m[1].trim(), env));
|
|
68
|
+
parts.push(compileExpression(m[1].trim(), env, moduleNames));
|
|
53
69
|
last = m.index! + m[0].length;
|
|
54
70
|
}
|
|
55
71
|
if (last < s.length) parts.push(s.slice(last));
|
|
56
72
|
|
|
57
73
|
const refs = new Set<string>();
|
|
74
|
+
const calls = new Set<string>();
|
|
58
75
|
for (const p of parts) {
|
|
59
|
-
if (typeof p
|
|
76
|
+
if (typeof p === "string") continue;
|
|
77
|
+
if (p.refs) for (const r of p.refs) refs.add(r);
|
|
78
|
+
if (p.calls) for (const c of p.calls) calls.add(c);
|
|
60
79
|
}
|
|
61
80
|
|
|
62
81
|
return {
|
|
@@ -64,6 +83,7 @@ export function compileString(s: string, env: Environment): unknown {
|
|
|
64
83
|
source: s,
|
|
65
84
|
parts,
|
|
66
85
|
refs: [...refs],
|
|
86
|
+
calls: [...calls],
|
|
67
87
|
call: (ctx: Record<string, unknown>) =>
|
|
68
88
|
parts.map((p) => (typeof p === "string" ? p : String(p.call(ctx) ?? ""))).join(""),
|
|
69
89
|
} satisfies CompiledValue;
|
package/src/cel/diagnose.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { ASTNode, Environment } from "@marcbachmann/cel-js";
|
|
2
2
|
import { CEL_FUNCTIONS, type CelFunctionDoc } from "./catalog.js";
|
|
3
|
+
import { moduleCallOf } from "./module-call.js";
|
|
3
4
|
import type { CallSite, DiagnosticFix, EngineDiagnostic } from "../engine.js";
|
|
4
5
|
|
|
5
6
|
/** Classifies every function call in a CEL expression against the environment's
|
|
@@ -37,6 +38,15 @@ export interface FunctionIndex {
|
|
|
37
38
|
const DETERMINISM: ReadonlyMap<string, boolean> = new Map(
|
|
38
39
|
CEL_FUNCTIONS.map((f) => [f.name, f.deterministic]),
|
|
39
40
|
);
|
|
41
|
+
const HOST_BACKED: ReadonlyMap<string, boolean> = new Map(
|
|
42
|
+
CEL_FUNCTIONS.map((f) => [f.name, f.hostBacked === true]),
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
/** The flags a module function carries, as the host that resolves it derives
|
|
46
|
+
* them — or undefined where the name reaches no function. */
|
|
47
|
+
export type ModuleCallFlags = (
|
|
48
|
+
qualified: string,
|
|
49
|
+
) => { readonly deterministic: boolean; readonly hostBacked: boolean } | undefined;
|
|
40
50
|
|
|
41
51
|
const INDEX_CACHE = new WeakMap<Environment, FunctionIndex>();
|
|
42
52
|
|
|
@@ -84,13 +94,40 @@ function isNode(v: unknown): v is ASTNode {
|
|
|
84
94
|
}
|
|
85
95
|
|
|
86
96
|
/** Every non-macro call in the expression, in source order. */
|
|
87
|
-
function collectCalls(
|
|
97
|
+
function collectCalls(
|
|
98
|
+
root: ASTNode,
|
|
99
|
+
index: FunctionIndex,
|
|
100
|
+
moduleCallFlags: ModuleCallFlags | undefined,
|
|
101
|
+
): RawCall[] {
|
|
88
102
|
const out: RawCall[] = [];
|
|
89
103
|
visit(root);
|
|
90
104
|
return out.sort((a, b) => a.start - b.start);
|
|
91
105
|
|
|
92
106
|
function visit(node: ASTNode): void {
|
|
93
107
|
const args = node.args as unknown;
|
|
108
|
+
// A RESOLVED module call is reported under its QUALIFIED name and is never
|
|
109
|
+
// looked up in the catalog: the two namespaces are disjoint by construction
|
|
110
|
+
// (a bare name is always the catalog), so classifying it here would report
|
|
111
|
+
// `there is no function 'format'` for a call that names a module's own, and
|
|
112
|
+
// attaching the catalog's determinism to it would be a flag about a
|
|
113
|
+
// different function entirely. What a module call's flags are is the
|
|
114
|
+
// resolving host's answer, carried when it gives one.
|
|
115
|
+
const moduleCall = moduleCallOf(node);
|
|
116
|
+
if (moduleCall) {
|
|
117
|
+
const flags = moduleCallFlags?.(moduleCall.qualified);
|
|
118
|
+
out.push({
|
|
119
|
+
name: moduleCall.qualified,
|
|
120
|
+
form: "receiver",
|
|
121
|
+
moduleCall: true,
|
|
122
|
+
arity: moduleCall.args.length,
|
|
123
|
+
start: node.start,
|
|
124
|
+
end: node.end,
|
|
125
|
+
...(flags ? { deterministic: flags.deterministic, hostBacked: flags.hostBacked } : {}),
|
|
126
|
+
args: moduleCall.args,
|
|
127
|
+
});
|
|
128
|
+
for (const arg of moduleCall.args) visit(arg);
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
94
131
|
if (node.op === "call" || node.op === "rcall") {
|
|
95
132
|
const tuple = args as unknown[];
|
|
96
133
|
const name = tuple[0];
|
|
@@ -104,7 +141,9 @@ function collectCalls(root: ASTNode, index: FunctionIndex): RawCall[] {
|
|
|
104
141
|
arity: callArgs.length,
|
|
105
142
|
start: node.start,
|
|
106
143
|
end: node.end,
|
|
107
|
-
...(index.byName.has(name)
|
|
144
|
+
...(index.byName.has(name)
|
|
145
|
+
? { deterministic: DETERMINISM.get(name), hostBacked: HOST_BACKED.get(name) }
|
|
146
|
+
: {}),
|
|
108
147
|
...(isNode(receiver) ? { receiver } : {}),
|
|
109
148
|
args: callArgs,
|
|
110
149
|
});
|
|
@@ -176,8 +215,8 @@ const listOf = (names: readonly string[]): string => [...new Set(names)].sort().
|
|
|
176
215
|
|
|
177
216
|
/** Names registered in exactly the form and arity the author wrote — the only
|
|
178
217
|
* ones that could replace this call with no further edits. Ranked by shared
|
|
179
|
-
* prefix, which is what reaches `nowIso` from `
|
|
180
|
-
* would (
|
|
218
|
+
* prefix, which is what reaches `nowIso` from `no`; edit distance never
|
|
219
|
+
* would (4 characters against a 2-character name). Returns [] when nothing
|
|
181
220
|
* shares a prefix, so the caller lists what is legal here instead of
|
|
182
221
|
* guessing. */
|
|
183
222
|
function candidates(call: RawCall, index: FunctionIndex): string[] {
|
|
@@ -249,13 +288,24 @@ export interface CallAudit {
|
|
|
249
288
|
* failed type-check, because a type-check reports its first error and stops:
|
|
250
289
|
* an expression with two bad calls would otherwise fix one, re-run, and
|
|
251
290
|
* discover the next. */
|
|
252
|
-
export function auditCalls(
|
|
291
|
+
export function auditCalls(
|
|
292
|
+
source: string,
|
|
293
|
+
ast: ASTNode,
|
|
294
|
+
env: Environment,
|
|
295
|
+
/** The flags of the module functions the expression calls, from whoever
|
|
296
|
+
* resolves them. */
|
|
297
|
+
moduleCallFlags?: ModuleCallFlags,
|
|
298
|
+
): CallAudit {
|
|
253
299
|
const index = functionIndex(env);
|
|
254
300
|
const diagnostics: EngineDiagnostic[] = [];
|
|
255
301
|
const unresolved: string[] = [];
|
|
256
|
-
const calls = collectCalls(ast, index);
|
|
302
|
+
const calls = collectCalls(ast, index, moduleCallFlags);
|
|
257
303
|
|
|
258
304
|
for (const call of calls) {
|
|
305
|
+
// Resolution already decided what a module call names. The catalog has
|
|
306
|
+
// nothing to say about it, and saying it anyway would be a refusal of valid
|
|
307
|
+
// CEL.
|
|
308
|
+
if (call.moduleCall) continue;
|
|
259
309
|
const entries = index.byName.get(call.name);
|
|
260
310
|
const noun = call.form === "receiver" ? "method" : "function";
|
|
261
311
|
|
package/src/cel/environment.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { Environment } from "@marcbachmann/cel-js";
|
|
2
2
|
import { Stream } from "@telorun/sdk";
|
|
3
3
|
import { CEL_FUNCTIONS, type CelHandlers } from "./catalog.js";
|
|
4
|
+
import { assertCelValueIdentity, SDK_CEL_VALUE_CLASSES } from "./value-identity.js";
|
|
4
5
|
|
|
5
6
|
export type { CelHandlers } from "./catalog.js";
|
|
6
7
|
|
|
8
|
+
assertCelValueIdentity(SDK_CEL_VALUE_CLASSES);
|
|
9
|
+
|
|
7
10
|
const stub = (name: string) => () => {
|
|
8
11
|
throw new Error(
|
|
9
12
|
`${name}() is not available in this environment. ` +
|