@telorun/templating 0.105.0 → 0.107.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/builtins.d.ts +9 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +10 -0
- package/dist/cel/catalog.d.ts +3 -0
- package/dist/cel/catalog.d.ts.map +1 -1
- package/dist/cel/catalog.js +82 -15
- package/dist/cel/compile.d.ts +9 -1
- package/dist/cel/compile.d.ts.map +1 -1
- package/dist/cel/compile.js +40 -0
- package/dist/cel/diagnose.d.ts +2 -1
- package/dist/cel/diagnose.d.ts.map +1 -1
- package/dist/cel/environment.d.ts +4 -0
- package/dist/cel/environment.d.ts.map +1 -1
- package/dist/cel/environment.js +8 -2
- package/dist/cel/json-prefix-scan.d.ts +17 -0
- package/dist/cel/json-prefix-scan.d.ts.map +1 -0
- package/dist/cel/json-prefix-scan.js +160 -0
- package/dist/cel/value-brands.d.ts +16 -0
- package/dist/cel/value-brands.d.ts.map +1 -0
- package/dist/cel/value-brands.js +31 -0
- package/dist/cel/verdict-codes.d.ts +29 -0
- package/dist/cel/verdict-codes.d.ts.map +1 -0
- package/dist/cel/verdict-codes.js +24 -0
- package/dist/engine.d.ts +15 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.d.ts +4 -3
- package/dist/engines/cel.d.ts.map +1 -1
- package/dist/engines/cel.js +4 -1
- package/dist/engines/hole-analysis.d.ts +7 -3
- package/dist/engines/hole-analysis.d.ts.map +1 -1
- package/dist/engines/hole-analysis.js +7 -1
- package/dist/engines/include.js +1 -1
- package/dist/engines/interpolate.d.ts.map +1 -1
- package/dist/engines/interpolate.js +6 -3
- package/dist/engines/module-path.js +1 -1
- package/dist/engines/sql.d.ts.map +1 -1
- package/dist/engines/sql.js +4 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/package.json +4 -4
- package/src/builtins.ts +12 -0
- package/src/cel/catalog.ts +93 -17
- package/src/cel/compile.ts +37 -1
- package/src/cel/diagnose.ts +4 -3
- package/src/cel/environment.ts +9 -2
- package/src/cel/json-prefix-scan.ts +152 -0
- package/src/cel/value-brands.ts +33 -0
- package/src/cel/verdict-codes.ts +47 -0
- package/src/engine.ts +16 -0
- package/src/engines/cel.ts +12 -13
- package/src/engines/hole-analysis.ts +15 -15
- package/src/engines/include.ts +1 -1
- package/src/engines/interpolate.ts +17 -6
- package/src/engines/module-path.ts +1 -1
- package/src/engines/sql.ts +11 -1
- package/src/index.ts +3 -0
package/src/cel/compile.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CompiledValue } from "@telorun/sdk";
|
|
2
|
-
import type
|
|
2
|
+
import { ParseError, type ASTNode, type Environment } from "@marcbachmann/cel-js";
|
|
3
3
|
import { extractAccessChains } from "./analyze.js";
|
|
4
4
|
import { CEL_FUNCTIONS } from "./catalog.js";
|
|
5
5
|
import { resolveModuleCalls } from "./module-call.js";
|
|
@@ -38,6 +38,42 @@ function callsNonDeterministic(root: ASTNode): boolean {
|
|
|
38
38
|
return visit(root);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
|
+
/** True when the expression calls a method on a bare identifier (`Billing.f(x)`).
|
|
42
|
+
* Whether that identifier names a module is the declaring module's to say, and
|
|
43
|
+
* the text alone does not, so every such call may be a module call. */
|
|
44
|
+
function callsOnBareReceiver(root: ASTNode): boolean {
|
|
45
|
+
const visit = (node: unknown): boolean => {
|
|
46
|
+
if (Array.isArray(node)) return node.some(visit);
|
|
47
|
+
if (!node || typeof node !== "object" || !("op" in node)) return false;
|
|
48
|
+
const n = node as ASTNode;
|
|
49
|
+
if (n.op === "rcall" && Array.isArray(n.args)) {
|
|
50
|
+
const receiver = (n.args as unknown[])[1] as { op?: unknown } | null;
|
|
51
|
+
if (receiver && typeof receiver === "object" && receiver.op === "id") return true;
|
|
52
|
+
}
|
|
53
|
+
const args = n.args as unknown;
|
|
54
|
+
return Array.isArray(args) ? args.some(visit) : visit(args);
|
|
55
|
+
};
|
|
56
|
+
return visit(root);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether an expression's text, written twice in one value, means one value
|
|
61
|
+
* both times: it calls no catalog function whose result differs per call, and
|
|
62
|
+
* makes no call that may be a module's — a module function's determinism is its
|
|
63
|
+
* callee's, which the text does not say. Text that does not parse means no
|
|
64
|
+
* value, so it is not repeatable; `analyze` is what reports why.
|
|
65
|
+
*/
|
|
66
|
+
export function repeatableExpression(expr: string, env: Environment): boolean {
|
|
67
|
+
let ast: ASTNode;
|
|
68
|
+
try {
|
|
69
|
+
ast = env.parse(expr).ast;
|
|
70
|
+
} catch (error) {
|
|
71
|
+
if (error instanceof ParseError) return false;
|
|
72
|
+
throw error;
|
|
73
|
+
}
|
|
74
|
+
return !callsNonDeterministic(ast) && !callsOnBareReceiver(ast);
|
|
75
|
+
}
|
|
76
|
+
|
|
41
77
|
/** Compile a single CEL expression into a CompiledValue. Throws on syntax
|
|
42
78
|
* errors. The `!cel` engine compiles its whole scalar with it, and the tags
|
|
43
79
|
* with holes compile each hole.
|
package/src/cel/diagnose.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { ASTNode, Environment } from "@marcbachmann/cel-js";
|
|
|
2
2
|
import { CEL_FUNCTIONS, type CelFunctionDoc } from "./catalog.js";
|
|
3
3
|
import { moduleCallOf } from "./module-call.js";
|
|
4
4
|
import type { CallSite, DiagnosticFix, EngineDiagnostic } from "../engine.js";
|
|
5
|
+
import type { CelCallAudit, CelDiagnostic } from "./verdict-codes.js";
|
|
5
6
|
|
|
6
7
|
/** Classifies every function call in a CEL expression against the environment's
|
|
7
8
|
* own function registry.
|
|
@@ -295,9 +296,9 @@ export function auditCalls(
|
|
|
295
296
|
/** The flags of the module functions the expression calls, from whoever
|
|
296
297
|
* resolves them. */
|
|
297
298
|
moduleCallFlags?: ModuleCallFlags,
|
|
298
|
-
):
|
|
299
|
+
): CelCallAudit {
|
|
299
300
|
const index = functionIndex(env);
|
|
300
|
-
const diagnostics:
|
|
301
|
+
const diagnostics: CelDiagnostic[] = [];
|
|
301
302
|
const unresolved: string[] = [];
|
|
302
303
|
const calls = collectCalls(ast, index, moduleCallFlags);
|
|
303
304
|
|
|
@@ -352,7 +353,7 @@ export function auditCalls(
|
|
|
352
353
|
unresolved.push(call.name);
|
|
353
354
|
}
|
|
354
355
|
|
|
355
|
-
const argumentIssues:
|
|
356
|
+
const argumentIssues: CelDiagnostic[] = [];
|
|
356
357
|
for (const call of calls) {
|
|
357
358
|
const check = ARG_CHECKS.get(call.name);
|
|
358
359
|
if (!check) continue;
|
package/src/cel/environment.ts
CHANGED
|
@@ -92,12 +92,19 @@ const ENVIRONMENT_OPTIONS = {
|
|
|
92
92
|
* registered one differ for a third of the entries. Asking for the base set
|
|
93
93
|
* directly needs no matching at all. */
|
|
94
94
|
export function celBuiltinFunctions(): ReturnType<Environment["getDefinitions"]>["functions"] {
|
|
95
|
-
return
|
|
95
|
+
return buildCelLanguageEnvironment().getDefinitions().functions;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The bare CEL language under Telo's options: cel-js's built-ins and nothing
|
|
99
|
+
* else — no catalog, no `Stream`. The dialect is built on it, and the
|
|
100
|
+
* language conformance vectors (`templating/cel-conformance/`) run against it. */
|
|
101
|
+
export function buildCelLanguageEnvironment(): Environment {
|
|
102
|
+
return new Environment(ENVIRONMENT_OPTIONS);
|
|
96
103
|
}
|
|
97
104
|
|
|
98
105
|
export function buildCelEnvironment(handlers: Partial<CelHandlers> = {}): Environment {
|
|
99
106
|
const h: CelHandlers = { ...STUB_HANDLERS, ...handlers };
|
|
100
|
-
let env =
|
|
107
|
+
let env = buildCelLanguageEnvironment();
|
|
101
108
|
for (const fn of CEL_FUNCTIONS) {
|
|
102
109
|
const impl = fn.build(h);
|
|
103
110
|
// `register` lists one cel-js signature per arity (overloaded functions).
|
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Where a text stops being a prefix of any RFC 8259 JSON text — the offset
|
|
3
|
+
* `parseJson`'s refusal names, computed here so the wording never comes from the
|
|
4
|
+
* host's JSON parser.
|
|
5
|
+
*/
|
|
6
|
+
export interface JsonRefusal {
|
|
7
|
+
/** UTF-16 offset of the first offending code unit; the text's length when
|
|
8
|
+
* every prefix is viable and the text is merely incomplete. */
|
|
9
|
+
readonly offset: number;
|
|
10
|
+
readonly endOfInput: boolean;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
type ScanState = "value" | "valueOrClose" | "key" | "keyOrClose" | "after";
|
|
14
|
+
|
|
15
|
+
const QUOTE = 0x22;
|
|
16
|
+
const BACKSLASH = 0x5c;
|
|
17
|
+
const SIMPLE_ESCAPES = '"\\/bfnrt';
|
|
18
|
+
|
|
19
|
+
const isWhitespace = (c: string | undefined): boolean => c === " " || c === "\t" || c === "\n" || c === "\r";
|
|
20
|
+
const isDigit = (c: string | undefined): boolean => c !== undefined && c >= "0" && c <= "9";
|
|
21
|
+
const isHexDigit = (c: string): boolean => isDigit(c) || (c >= "a" && c <= "f") || (c >= "A" && c <= "F");
|
|
22
|
+
|
|
23
|
+
/** The refusal of `text`, or `undefined` when it is a JSON text. Iterative, so
|
|
24
|
+
* nesting depth is bounded by memory rather than by the call stack. Never
|
|
25
|
+
* stricter or laxer than RFC 8259: over 400,000 fuzzed texts it agreed with
|
|
26
|
+
* V8's `JSON.parse` on which are JSON every time. */
|
|
27
|
+
export function scanJsonPrefix(text: string): JsonRefusal | undefined {
|
|
28
|
+
const length = text.length;
|
|
29
|
+
let at = 0;
|
|
30
|
+
|
|
31
|
+
// Each token reader returns the offset it refuses, or `undefined` once `at`
|
|
32
|
+
// is past the token.
|
|
33
|
+
const readString = (): number | undefined => {
|
|
34
|
+
at++;
|
|
35
|
+
for (;;) {
|
|
36
|
+
if (at >= length) return length;
|
|
37
|
+
const code = text.charCodeAt(at);
|
|
38
|
+
if (code === QUOTE) {
|
|
39
|
+
at++;
|
|
40
|
+
return undefined;
|
|
41
|
+
}
|
|
42
|
+
if (code < 0x20) return at;
|
|
43
|
+
at++;
|
|
44
|
+
if (code !== BACKSLASH) continue;
|
|
45
|
+
if (at >= length) return length;
|
|
46
|
+
const escape = text[at]!;
|
|
47
|
+
if (escape === "u") {
|
|
48
|
+
at++;
|
|
49
|
+
for (let digit = 0; digit < 4; digit++, at++) {
|
|
50
|
+
if (at >= length) return length;
|
|
51
|
+
if (!isHexDigit(text[at]!)) return at;
|
|
52
|
+
}
|
|
53
|
+
} else if (SIMPLE_ESCAPES.includes(escape)) at++;
|
|
54
|
+
else return at;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const readDigits = (): number | undefined => {
|
|
59
|
+
if (!isDigit(text[at])) return at;
|
|
60
|
+
while (isDigit(text[at])) at++;
|
|
61
|
+
return undefined;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const readNumber = (): number | undefined => {
|
|
65
|
+
if (text[at] === "-") at++;
|
|
66
|
+
if (text[at] === "0") at++;
|
|
67
|
+
else {
|
|
68
|
+
const refused = readDigits();
|
|
69
|
+
if (refused !== undefined) return refused;
|
|
70
|
+
}
|
|
71
|
+
if (text[at] === ".") {
|
|
72
|
+
at++;
|
|
73
|
+
const refused = readDigits();
|
|
74
|
+
if (refused !== undefined) return refused;
|
|
75
|
+
}
|
|
76
|
+
if (text[at] === "e" || text[at] === "E") {
|
|
77
|
+
at++;
|
|
78
|
+
if (text[at] === "+" || text[at] === "-") at++;
|
|
79
|
+
return readDigits();
|
|
80
|
+
}
|
|
81
|
+
return undefined;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const readLiteral = (literal: string): number | undefined => {
|
|
85
|
+
for (const expected of literal) {
|
|
86
|
+
if (text[at] !== expected) return at;
|
|
87
|
+
at++;
|
|
88
|
+
}
|
|
89
|
+
return undefined;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const refusedAt = (offset: number): JsonRefusal => ({ offset, endOfInput: offset >= length });
|
|
93
|
+
|
|
94
|
+
const open: ("{" | "[")[] = [];
|
|
95
|
+
let state: ScanState = "value";
|
|
96
|
+
for (;;) {
|
|
97
|
+
while (isWhitespace(text[at])) at++;
|
|
98
|
+
const c = text[at];
|
|
99
|
+
|
|
100
|
+
if (state === "after") {
|
|
101
|
+
const container = open[open.length - 1];
|
|
102
|
+
if (container === undefined) return at === length ? undefined : refusedAt(at);
|
|
103
|
+
if (c === ",") {
|
|
104
|
+
at++;
|
|
105
|
+
state = container === "{" ? "key" : "value";
|
|
106
|
+
} else if (c === (container === "{" ? "}" : "]")) {
|
|
107
|
+
at++;
|
|
108
|
+
open.pop();
|
|
109
|
+
} else return refusedAt(at);
|
|
110
|
+
continue;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (state === "key" || state === "keyOrClose") {
|
|
114
|
+
if (state === "keyOrClose" && c === "}") {
|
|
115
|
+
at++;
|
|
116
|
+
open.pop();
|
|
117
|
+
state = "after";
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (c !== '"') return refusedAt(at);
|
|
121
|
+
const refused = readString();
|
|
122
|
+
if (refused !== undefined) return refusedAt(refused);
|
|
123
|
+
while (isWhitespace(text[at])) at++;
|
|
124
|
+
if (text[at] !== ":") return refusedAt(at);
|
|
125
|
+
at++;
|
|
126
|
+
state = "value";
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (state === "valueOrClose" && c === "]") {
|
|
131
|
+
at++;
|
|
132
|
+
open.pop();
|
|
133
|
+
state = "after";
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (c === "{" || c === "[") {
|
|
137
|
+
at++;
|
|
138
|
+
open.push(c);
|
|
139
|
+
state = c === "{" ? "keyOrClose" : "valueOrClose";
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
let refused: number | undefined;
|
|
143
|
+
if (c === '"') refused = readString();
|
|
144
|
+
else if (c === "-" || isDigit(c)) refused = readNumber();
|
|
145
|
+
else if (c === "t") refused = readLiteral("true");
|
|
146
|
+
else if (c === "f") refused = readLiteral("false");
|
|
147
|
+
else if (c === "n") refused = readLiteral("null");
|
|
148
|
+
else refused = at;
|
|
149
|
+
if (refused !== undefined) return refusedAt(refused);
|
|
150
|
+
state = "after";
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { Environment } from "@marcbachmann/cel-js";
|
|
2
|
+
import { VALUE_TYPES, valueBrandBases } from "@telorun/sdk";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Register every nominal value brand on an analysis environment, with what a
|
|
6
|
+
* brand may do: convert to its base with the base's own conversion
|
|
7
|
+
* (`int(ports.http)`, `string(variables.dataDir)`), render as text
|
|
8
|
+
* (`string(ports.http)`), and — for a host path — be
|
|
9
|
+
* extended with `.joinPath(relative)` and stay one.
|
|
10
|
+
*
|
|
11
|
+
* Static only: at runtime a branded value IS its base (an int, a string), so the
|
|
12
|
+
* catalog's own overloads — `string.joinPath` among them — are what evaluate. The
|
|
13
|
+
* implementations below exist because registration requires one; a brand's
|
|
14
|
+
* values never reach them. `buildCelEnvironment` registers none of this: a
|
|
15
|
+
* caller typing a site registers the brands on its own clone.
|
|
16
|
+
*/
|
|
17
|
+
export function registerValueBrands(env: Environment): void {
|
|
18
|
+
for (const [brand, base] of Object.entries(valueBrandBases())) {
|
|
19
|
+
(env as any).registerType(brand, { fields: {} });
|
|
20
|
+
(env as any).registerFunction(`${base}(${brand}): ${base}`, (value: unknown) => value);
|
|
21
|
+
// Every base CEL can render as text renders its brand too — the runtime
|
|
22
|
+
// value IS the base — so `string(ports.http)` and a port in an
|
|
23
|
+
// `!interpolate` hole convert as the integer they are.
|
|
24
|
+
if (base !== "string") {
|
|
25
|
+
(env as any).registerFunction(`string(${brand}): string`, (value: unknown) => String(value));
|
|
26
|
+
}
|
|
27
|
+
if (VALUE_TYPES.get(brand)?.fromHost !== undefined) {
|
|
28
|
+
(env as any).registerFunction(`${brand}.joinPath(string): ${brand}`, () => {
|
|
29
|
+
throw new Error("joinPath() is evaluated by the runtime, not the analyzer.");
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { RuntimeError } from "@telorun/sdk";
|
|
2
|
+
import type { AnalyzeResult, EngineDiagnostic } from "../engine.js";
|
|
3
|
+
import type { CallAudit } from "./diagnose.js";
|
|
4
|
+
|
|
5
|
+
/** Every code the `!cel`, `!interpolate` and `!sql` engines emit: `diagnostics`
|
|
6
|
+
* from `analyze`, `errors` thrown by compiling or evaluating. Stated once here;
|
|
7
|
+
* the types below hold every emit site to it, and the conformance runner
|
|
8
|
+
* requires a `verdicts.json` row for each. */
|
|
9
|
+
export const CEL_VERDICT_CODES = {
|
|
10
|
+
diagnostics: [
|
|
11
|
+
"BINDING_NAME_RESERVED",
|
|
12
|
+
"CEL_INVALID_ARGUMENT",
|
|
13
|
+
"CEL_NULLABLE_ACCESS",
|
|
14
|
+
"CEL_SYNTAX_ERROR",
|
|
15
|
+
"CEL_TYPE_ERROR",
|
|
16
|
+
"CEL_UNKNOWN_FIELD",
|
|
17
|
+
"CEL_UNKNOWN_FUNCTION",
|
|
18
|
+
"CEL_UNKNOWN_IDENTIFIER",
|
|
19
|
+
"CEL_WRONG_CALL_FORM",
|
|
20
|
+
"INTERPOLATION_HOLE_NOT_CONVERTIBLE",
|
|
21
|
+
],
|
|
22
|
+
errors: ["ERR_INTERPOLATION_HOLE_NOT_CONVERTIBLE"],
|
|
23
|
+
} as const;
|
|
24
|
+
|
|
25
|
+
export type CelDiagnosticCode = (typeof CEL_VERDICT_CODES.diagnostics)[number];
|
|
26
|
+
export type CelErrorCode = (typeof CEL_VERDICT_CODES.errors)[number];
|
|
27
|
+
|
|
28
|
+
/** A diagnostic one of the three engines builds. */
|
|
29
|
+
export interface CelDiagnostic extends EngineDiagnostic {
|
|
30
|
+
readonly code: CelDiagnosticCode;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** What those engines' `analyze` answers. */
|
|
34
|
+
export interface CelAnalyzeResult extends AnalyzeResult {
|
|
35
|
+
readonly diagnostics: readonly CelDiagnostic[];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The call audit, as those engines read it. */
|
|
39
|
+
export interface CelCallAudit extends CallAudit {
|
|
40
|
+
readonly diagnostics: readonly CelDiagnostic[];
|
|
41
|
+
readonly argumentIssues: readonly CelDiagnostic[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The one constructor of a coded error those engines throw. */
|
|
45
|
+
export function celVerdictError(code: CelErrorCode, message: string): RuntimeError {
|
|
46
|
+
return new RuntimeError(code, message);
|
|
47
|
+
}
|
package/src/engine.ts
CHANGED
|
@@ -205,6 +205,10 @@ export interface EngineFileClaim {
|
|
|
205
205
|
/** The path may name a directory, which then claims every file beneath it.
|
|
206
206
|
* Whether it does is a question for whoever holds the directory. */
|
|
207
207
|
readonly directory?: boolean;
|
|
208
|
+
/** The diagnostic code reporting a claim that names nothing — the static twin
|
|
209
|
+
* of the runtime's refusal, declared by the engine so the existence check
|
|
210
|
+
* recognises no tag by name. */
|
|
211
|
+
readonly notFoundCode: string;
|
|
208
212
|
}
|
|
209
213
|
|
|
210
214
|
/** One CEL expression inside a tagged scalar, by offset into the scalar's
|
|
@@ -283,4 +287,16 @@ export interface TemplatingEngine {
|
|
|
283
287
|
* engine whose scalar holds no CEL. A source the engine cannot read yields
|
|
284
288
|
* no regions; `analyze` is what reports why. */
|
|
285
289
|
expressionRegions?(source: string): readonly ExpressionRegion[];
|
|
290
|
+
|
|
291
|
+
/** Whether the tagged text, written twice in one value, means one value both
|
|
292
|
+
* times — the engine's verdict on the TEXT ALONE, with no module name set and
|
|
293
|
+
* nothing evaluated: no expression in it calls a catalog function whose
|
|
294
|
+
* result differs per call, and none makes a call that may be a module's (a
|
|
295
|
+
* method on a bare identifier), whose determinism is its callee's.
|
|
296
|
+
*
|
|
297
|
+
* What lets a consumer holding two character-identical tagged scalars say
|
|
298
|
+
* they are equal without evaluating either. Absent on an engine that gives no
|
|
299
|
+
* such verdict, which reads as "not repeatable". A source the engine cannot
|
|
300
|
+
* read is not repeatable; `analyze` is what reports why. */
|
|
301
|
+
repeatable?(source: string, celEnv: Environment): boolean;
|
|
286
302
|
}
|
package/src/engines/cel.ts
CHANGED
|
@@ -7,7 +7,7 @@ import {
|
|
|
7
7
|
moduleCallArgumentChains,
|
|
8
8
|
validateChainAgainstSchema,
|
|
9
9
|
} from "../cel/analyze.js";
|
|
10
|
-
import { compileExpression } from "../cel/compile.js";
|
|
10
|
+
import { compileExpression, repeatableExpression } from "../cel/compile.js";
|
|
11
11
|
import { auditCalls, explainUnresolved } from "../cel/diagnose.js";
|
|
12
12
|
import {
|
|
13
13
|
moduleCallNodes,
|
|
@@ -16,13 +16,8 @@ import {
|
|
|
16
16
|
unresolvedCallReceivers,
|
|
17
17
|
type ModuleCall,
|
|
18
18
|
} from "../cel/module-call.js";
|
|
19
|
-
import type {
|
|
20
|
-
|
|
21
|
-
AnalyzeResult,
|
|
22
|
-
CallSite,
|
|
23
|
-
EngineDiagnostic,
|
|
24
|
-
TemplatingEngine,
|
|
25
|
-
} from "../engine.js";
|
|
19
|
+
import type { CelAnalyzeResult, CelDiagnostic } from "../cel/verdict-codes.js";
|
|
20
|
+
import type { AnalyzeEnv, CallSite, TemplatingEngine } from "../engine.js";
|
|
26
21
|
|
|
27
22
|
/** Statically analyze one CEL expression against the effective context schema:
|
|
28
23
|
* parse → classify every call → type-check → validate member-access chains →
|
|
@@ -35,7 +30,7 @@ import type {
|
|
|
35
30
|
* "no matching overload" survive next to the diagnostic that actually
|
|
36
31
|
* explained it, and left `${{ }}` interpolations chain-validated but never
|
|
37
32
|
* type-checked at all. */
|
|
38
|
-
export function analyzeCelExpression(source: string, env: AnalyzeEnv):
|
|
33
|
+
export function analyzeCelExpression(source: string, env: AnalyzeEnv): CelAnalyzeResult {
|
|
39
34
|
return analyzeCelWithTree(source, env).result;
|
|
40
35
|
}
|
|
41
36
|
|
|
@@ -44,8 +39,8 @@ export function analyzeCelExpression(source: string, env: AnalyzeEnv): AnalyzeRe
|
|
|
44
39
|
export function analyzeCelWithTree(
|
|
45
40
|
source: string,
|
|
46
41
|
env: AnalyzeEnv,
|
|
47
|
-
): { result:
|
|
48
|
-
const out:
|
|
42
|
+
): { result: CelAnalyzeResult; ast?: ASTNode } {
|
|
43
|
+
const out: CelDiagnostic[] = [];
|
|
49
44
|
|
|
50
45
|
let parsed: ReturnType<typeof env.celEnv.parse>;
|
|
51
46
|
try {
|
|
@@ -231,10 +226,10 @@ export function analyzeCelWithTree(
|
|
|
231
226
|
* reported in its own words. The explain schema is not the checker's
|
|
232
227
|
* environment, so a chain is reported only when the checker rejects it too;
|
|
233
228
|
* otherwise the rejection was about something else and keeps its own words. */
|
|
234
|
-
function explainUnknownFields(ast: ASTNode, env: AnalyzeEnv):
|
|
229
|
+
function explainUnknownFields(ast: ASTNode, env: AnalyzeEnv): CelDiagnostic[] {
|
|
235
230
|
const schema = env.explainSchema?.();
|
|
236
231
|
if (!schema) return [];
|
|
237
|
-
const out:
|
|
232
|
+
const out: CelDiagnostic[] = [];
|
|
238
233
|
const reported = new Set<string>();
|
|
239
234
|
for (const chain of extractAccessChains(ast)) {
|
|
240
235
|
const message = validateChainAgainstSchema(chain, schema as Record<string, any>);
|
|
@@ -317,4 +312,8 @@ export const celEngine: TemplatingEngine = {
|
|
|
317
312
|
expressionRegions(source) {
|
|
318
313
|
return [{ start: 0, end: source.length }];
|
|
319
314
|
},
|
|
315
|
+
|
|
316
|
+
repeatable(source, celEnv) {
|
|
317
|
+
return repeatableExpression(source, celEnv);
|
|
318
|
+
},
|
|
320
319
|
};
|
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
import type { ASTNode } from "@marcbachmann/cel-js";
|
|
1
|
+
import type { ASTNode, Environment } from "@marcbachmann/cel-js";
|
|
2
2
|
import type { CompiledValue } from "@telorun/sdk";
|
|
3
|
-
import { compileExpression } from "../cel/compile.js";
|
|
3
|
+
import { compileExpression, repeatableExpression } from "../cel/compile.js";
|
|
4
4
|
import {
|
|
5
5
|
readInterpolationHoles,
|
|
6
6
|
type InterpolationHole,
|
|
7
7
|
} from "../cel/interpolation-holes.js";
|
|
8
|
-
import type {
|
|
9
|
-
|
|
10
|
-
AnalyzeResult,
|
|
11
|
-
CallSite,
|
|
12
|
-
CompileEnv,
|
|
13
|
-
DiagnosticFix,
|
|
14
|
-
EngineDiagnostic,
|
|
15
|
-
ExpressionRegion,
|
|
16
|
-
} from "../engine.js";
|
|
8
|
+
import type { CelAnalyzeResult, CelDiagnostic } from "../cel/verdict-codes.js";
|
|
9
|
+
import type { AnalyzeEnv, CallSite, CompileEnv, DiagnosticFix, ExpressionRegion } from "../engine.js";
|
|
17
10
|
import { analyzeCelWithTree } from "./cel.js";
|
|
18
11
|
|
|
19
12
|
/** The holes of a scalar a hole-bearing tag compiles, or a thrown error naming
|
|
@@ -56,15 +49,15 @@ export function analyzeHoles(
|
|
|
56
49
|
env: AnalyzeEnv,
|
|
57
50
|
perHole?: (
|
|
58
51
|
hole: InterpolationHole,
|
|
59
|
-
result:
|
|
52
|
+
result: CelAnalyzeResult,
|
|
60
53
|
ast: ASTNode | undefined,
|
|
61
|
-
) => readonly
|
|
62
|
-
):
|
|
54
|
+
) => readonly CelDiagnostic[],
|
|
55
|
+
): CelAnalyzeResult {
|
|
63
56
|
const reading = readInterpolationHoles(source);
|
|
64
57
|
if (!reading.ok) {
|
|
65
58
|
return { diagnostics: [{ code: "CEL_SYNTAX_ERROR", message: reading.message }], calls: [] };
|
|
66
59
|
}
|
|
67
|
-
const diagnostics:
|
|
60
|
+
const diagnostics: CelDiagnostic[] = [];
|
|
68
61
|
const calls: CallSite[] = [];
|
|
69
62
|
for (const hole of reading.holes) {
|
|
70
63
|
const { result, ast } = analyzeCelWithTree(hole.expr, env);
|
|
@@ -86,6 +79,13 @@ export function holeRegions(source: string): readonly ExpressionRegion[] {
|
|
|
86
79
|
return reading.holes.map((h) => ({ start: h.exprStart, end: h.exprStart + h.expr.length }));
|
|
87
80
|
}
|
|
88
81
|
|
|
82
|
+
/** Whether every hole of a hole-bearing scalar is repeatable; the literal text
|
|
83
|
+
* between them always is. */
|
|
84
|
+
export function holesRepeatable(source: string, celEnv: Environment): boolean {
|
|
85
|
+
const reading = readInterpolationHoles(source);
|
|
86
|
+
return reading.ok && reading.holes.every((h) => repeatableExpression(h.expr, celEnv));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
89
|
function reanchor(source: string, hole: InterpolationHole, fix: DiagnosticFix): DiagnosticFix {
|
|
90
90
|
return {
|
|
91
91
|
replacement:
|
package/src/engines/include.ts
CHANGED
|
@@ -187,7 +187,7 @@ function includeEngine(name: string, produced: Record<string, unknown>): Templat
|
|
|
187
187
|
// A malformed path claims nothing. `analyze` is what says why, so
|
|
188
188
|
// claiming a half-understood path here would produce a second, worse
|
|
189
189
|
// report from publish about the same mistake.
|
|
190
|
-
return path ? [{ path }] : [];
|
|
190
|
+
return path ? [{ path, notFoundCode: "INCLUDE_FILE_NOT_FOUND" }] : [];
|
|
191
191
|
},
|
|
192
192
|
};
|
|
193
193
|
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { Environment, EvaluationError, type ASTNode } from "@marcbachmann/cel-js";
|
|
2
|
-
import {
|
|
2
|
+
import type { CompiledValue } from "@telorun/sdk";
|
|
3
3
|
import { nullableValueChain } from "../cel/analyze.js";
|
|
4
4
|
import { literalFragments, type InterpolationHole } from "../cel/interpolation-holes.js";
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
5
|
+
import { celVerdictError, type CelAnalyzeResult, type CelDiagnostic } from "../cel/verdict-codes.js";
|
|
6
|
+
import type { AnalyzeEnv, TemplatingEngine } from "../engine.js";
|
|
7
|
+
import {
|
|
8
|
+
analyzeHoles,
|
|
9
|
+
compileHoles,
|
|
10
|
+
holeRegions,
|
|
11
|
+
holesRepeatable,
|
|
12
|
+
requireHoles,
|
|
13
|
+
} from "./hole-analysis.js";
|
|
7
14
|
|
|
8
15
|
const INTERPOLATE_ENGINE = "interpolate";
|
|
9
16
|
|
|
@@ -53,16 +60,20 @@ export const interpolateEngine: TemplatingEngine = {
|
|
|
53
60
|
expressionRegions(source) {
|
|
54
61
|
return holeRegions(source);
|
|
55
62
|
},
|
|
63
|
+
|
|
64
|
+
repeatable(source, celEnv) {
|
|
65
|
+
return holesRepeatable(source, celEnv);
|
|
66
|
+
},
|
|
56
67
|
};
|
|
57
68
|
|
|
58
69
|
/** A clean hole's own checks, over the tree its analysis already built:
|
|
59
70
|
* whether it may be null, and whether CEL can turn its type into text at all. */
|
|
60
71
|
function holeVerdict(
|
|
61
72
|
hole: InterpolationHole,
|
|
62
|
-
result:
|
|
73
|
+
result: CelAnalyzeResult,
|
|
63
74
|
ast: ASTNode | undefined,
|
|
64
75
|
env: AnalyzeEnv,
|
|
65
|
-
): readonly
|
|
76
|
+
): readonly CelDiagnostic[] {
|
|
66
77
|
if (ast && env.contextSchema) {
|
|
67
78
|
const chain = nullableValueChain(ast, env.contextSchema as Record<string, any>);
|
|
68
79
|
if (chain !== null) {
|
|
@@ -119,7 +130,7 @@ function stringConversion(env: Environment): Conversion {
|
|
|
119
130
|
return program({ value }) as string;
|
|
120
131
|
} catch (error) {
|
|
121
132
|
if (!(error instanceof EvaluationError)) throw error;
|
|
122
|
-
throw
|
|
133
|
+
throw celVerdictError(
|
|
123
134
|
"ERR_INTERPOLATION_HOLE_NOT_CONVERTIBLE",
|
|
124
135
|
`the hole '\${{ ${hole.expr} }}' at offset ${hole.start} of !interpolate ${JSON.stringify(source)} ` +
|
|
125
136
|
`evaluated to ${runtimeTypeOf(value)}, which CEL's string() cannot convert to text. ` +
|
|
@@ -34,6 +34,6 @@ export const modulePathEngine: TemplatingEngine = {
|
|
|
34
34
|
|
|
35
35
|
fileClaims(source): readonly EngineFileClaim[] {
|
|
36
36
|
const { path } = normalizeModulePath(source);
|
|
37
|
-
return path ? [{ path, directory: true }] : [];
|
|
37
|
+
return path ? [{ path, directory: true, notFoundCode: "MODULE_PATH_NOT_FOUND" }] : [];
|
|
38
38
|
},
|
|
39
39
|
};
|
package/src/engines/sql.ts
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
import { isParameterizedSql, type CompiledValue, type ParameterizedSql } from "@telorun/sdk";
|
|
2
2
|
import { literalFragments } from "../cel/interpolation-holes.js";
|
|
3
3
|
import type { TemplatingEngine } from "../engine.js";
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
analyzeHoles,
|
|
6
|
+
compileHoles,
|
|
7
|
+
holeRegions,
|
|
8
|
+
holesRepeatable,
|
|
9
|
+
requireHoles,
|
|
10
|
+
} from "./hole-analysis.js";
|
|
5
11
|
|
|
6
12
|
export { isParameterizedSql, type ParameterizedSql };
|
|
7
13
|
|
|
@@ -45,4 +51,8 @@ export const sqlEngine: TemplatingEngine = {
|
|
|
45
51
|
expressionRegions(source) {
|
|
46
52
|
return holeRegions(source);
|
|
47
53
|
},
|
|
54
|
+
|
|
55
|
+
repeatable(source, celEnv) {
|
|
56
|
+
return holesRepeatable(source, celEnv);
|
|
57
|
+
},
|
|
48
58
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export {
|
|
2
2
|
buildCelEnvironment,
|
|
3
|
+
buildCelLanguageEnvironment,
|
|
3
4
|
celBuiltinFunctions,
|
|
4
5
|
deriveSignatures,
|
|
5
6
|
type CelHandlers,
|
|
@@ -12,6 +13,7 @@ export {
|
|
|
12
13
|
type CelFunctionCategory,
|
|
13
14
|
} from "./cel/catalog.js";
|
|
14
15
|
export { compileExpression } from "./cel/compile.js";
|
|
16
|
+
export { registerValueBrands } from "./cel/value-brands.js";
|
|
15
17
|
export {
|
|
16
18
|
interpolationShape,
|
|
17
19
|
literalFragments,
|
|
@@ -68,6 +70,7 @@ export {
|
|
|
68
70
|
createDefaultRegistry,
|
|
69
71
|
defaultRegistry,
|
|
70
72
|
producedTypeOf,
|
|
73
|
+
repeatableSource,
|
|
71
74
|
} from "./builtins.js";
|
|
72
75
|
export type {
|
|
73
76
|
AnalyzeEnv,
|