@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.
- package/dist/builtins.d.ts +9 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +10 -0
- package/dist/cel/analyze.d.ts +6 -6
- package/dist/cel/analyze.d.ts.map +1 -1
- package/dist/cel/analyze.js +138 -183
- package/dist/cel/compile.d.ts +50 -12
- package/dist/cel/compile.d.ts.map +1 -1
- package/dist/cel/compile.js +140 -48
- package/dist/cel/diagnose.d.ts +52 -56
- package/dist/cel/diagnose.d.ts.map +1 -1
- package/dist/cel/diagnose.js +69 -291
- package/dist/cel/environment.d.ts +33 -33
- package/dist/cel/environment.d.ts.map +1 -1
- package/dist/cel/environment.js +54 -80
- package/dist/cel/module-call.d.ts +68 -79
- package/dist/cel/module-call.d.ts.map +1 -1
- package/dist/cel/module-call.js +118 -194
- package/dist/cel/value-brands.d.ts +18 -0
- package/dist/cel/value-brands.d.ts.map +1 -0
- package/dist/cel/value-brands.js +33 -0
- package/dist/cel/verdict-codes.d.ts +40 -0
- package/dist/cel/verdict-codes.d.ts.map +1 -0
- package/dist/cel/verdict-codes.js +43 -0
- package/dist/engine.d.ts +18 -3
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.d.ts +6 -5
- package/dist/engines/cel.d.ts.map +1 -1
- package/dist/engines/cel.js +57 -82
- 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 +17 -11
- 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 +6 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -6
- package/package.json +6 -6
- package/src/builtins.ts +12 -0
- package/src/cel/analyze.ts +152 -193
- package/src/cel/compile.ts +166 -47
- package/src/cel/diagnose.ts +116 -370
- package/src/cel/environment.ts +66 -82
- package/src/cel/module-call.ts +129 -292
- package/src/cel/value-brands.ts +34 -0
- package/src/cel/verdict-codes.ts +66 -0
- package/src/engine.ts +19 -3
- package/src/engines/cel.ts +62 -95
- package/src/engines/hole-analysis.ts +16 -16
- package/src/engines/include.ts +1 -1
- package/src/engines/interpolate.ts +33 -19
- package/src/engines/module-path.ts +1 -1
- package/src/engines/sql.ts +11 -1
- package/src/index.ts +11 -11
- package/dist/cel/catalog.d.ts +0 -67
- package/dist/cel/catalog.d.ts.map +0 -1
- package/dist/cel/catalog.js +0 -1253
- package/dist/cel/value-identity.d.ts +0 -18
- package/dist/cel/value-identity.d.ts.map +0 -1
- package/dist/cel/value-identity.js +0 -25
- package/src/cel/catalog.ts +0 -1379
- package/src/cel/value-identity.ts +0 -34
package/src/engine.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CelEnvironment } from "@telorun/cel";
|
|
2
2
|
import type { CompiledValue } from "@telorun/sdk";
|
|
3
3
|
import type { ModuleCallFlags } from "./cel/diagnose.js";
|
|
4
4
|
import type { ModuleCallTypeResolver } from "./cel/module-call.js";
|
|
@@ -7,7 +7,7 @@ import type { ModuleCallTypeResolver } from "./cel/module-call.js";
|
|
|
7
7
|
* parse against a CEL environment (the `cel` engine) read it from `celEnv`;
|
|
8
8
|
* engines that resolve fully at compile time (`literal`) ignore it. */
|
|
9
9
|
export interface CompileEnv {
|
|
10
|
-
readonly celEnv:
|
|
10
|
+
readonly celEnv: CelEnvironment;
|
|
11
11
|
/**
|
|
12
12
|
* The names a call's receiver may be for the call to resolve as a MODULE
|
|
13
13
|
* call — the declaring module's `imports:` keys, `Self`, its `metadata.name`
|
|
@@ -33,7 +33,7 @@ export interface CompileEnv {
|
|
|
33
33
|
* expression again against a different environment. One expression, one
|
|
34
34
|
* verdict. */
|
|
35
35
|
export interface AnalyzeEnv {
|
|
36
|
-
readonly celEnv:
|
|
36
|
+
readonly celEnv: CelEnvironment;
|
|
37
37
|
readonly contextSchema: Record<string, unknown> | null;
|
|
38
38
|
/**
|
|
39
39
|
* The caller vouches that `celEnv` declares EVERY name legal at this site, so
|
|
@@ -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: CelEnvironment): boolean;
|
|
286
302
|
}
|
package/src/engines/cel.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { formatType, qualifiedCalls, type CelNode } from "@telorun/cel";
|
|
2
2
|
import {
|
|
3
3
|
extractAccessChains,
|
|
4
4
|
extractCallResultAccesses,
|
|
@@ -7,22 +7,11 @@ 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
|
-
import {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
resolveModuleCalls,
|
|
16
|
-
unresolvedCallReceivers,
|
|
17
|
-
type ModuleCall,
|
|
18
|
-
} from "../cel/module-call.js";
|
|
19
|
-
import type {
|
|
20
|
-
AnalyzeEnv,
|
|
21
|
-
AnalyzeResult,
|
|
22
|
-
CallSite,
|
|
23
|
-
EngineDiagnostic,
|
|
24
|
-
TemplatingEngine,
|
|
25
|
-
} from "../engine.js";
|
|
12
|
+
import { moduleNameBindings, unresolvedCallReceivers } from "../cel/module-call.js";
|
|
13
|
+
import type { CelAnalyzeResult, CelDiagnostic } from "../cel/verdict-codes.js";
|
|
14
|
+
import type { AnalyzeEnv, CallSite, TemplatingEngine } from "../engine.js";
|
|
26
15
|
|
|
27
16
|
/** Statically analyze one CEL expression against the effective context schema:
|
|
28
17
|
* parse → classify every call → type-check → validate member-access chains →
|
|
@@ -35,7 +24,7 @@ import type {
|
|
|
35
24
|
* "no matching overload" survive next to the diagnostic that actually
|
|
36
25
|
* explained it, and left `${{ }}` interpolations chain-validated but never
|
|
37
26
|
* type-checked at all. */
|
|
38
|
-
export function analyzeCelExpression(source: string, env: AnalyzeEnv):
|
|
27
|
+
export function analyzeCelExpression(source: string, env: AnalyzeEnv): CelAnalyzeResult {
|
|
39
28
|
return analyzeCelWithTree(source, env).result;
|
|
40
29
|
}
|
|
41
30
|
|
|
@@ -44,33 +33,34 @@ export function analyzeCelExpression(source: string, env: AnalyzeEnv): AnalyzeRe
|
|
|
44
33
|
export function analyzeCelWithTree(
|
|
45
34
|
source: string,
|
|
46
35
|
env: AnalyzeEnv,
|
|
47
|
-
): { result:
|
|
48
|
-
const out:
|
|
36
|
+
): { result: CelAnalyzeResult; ast?: CelNode } {
|
|
37
|
+
const out: CelDiagnostic[] = [];
|
|
49
38
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
39
|
+
// **Reading never throws and never discards what it read**: the first unreadable thing is
|
|
40
|
+
// one ranged diagnostic and the tree keeps the longest prefix, so a scalar an author is
|
|
41
|
+
// mid-way through typing still reports its chains and its calls.
|
|
42
|
+
const parsed = env.celEnv.parse(source);
|
|
43
|
+
if (parsed.diagnostics.length > 0) {
|
|
44
|
+
const first = parsed.diagnostics[0]!;
|
|
54
45
|
return {
|
|
55
46
|
result: {
|
|
56
|
-
diagnostics: [{ code: "CEL_SYNTAX_ERROR", message:
|
|
47
|
+
diagnostics: [{ code: "CEL_SYNTAX_ERROR", message: first.message }],
|
|
57
48
|
calls: [],
|
|
58
49
|
},
|
|
59
50
|
};
|
|
60
51
|
}
|
|
61
|
-
const ast:
|
|
52
|
+
const ast: CelNode = parsed.root;
|
|
62
53
|
|
|
63
|
-
// Resolution
|
|
64
|
-
//
|
|
65
|
-
// know a module name from a variable.
|
|
66
|
-
|
|
67
|
-
const resolvedCalls = resolveModuleCalls(parsed.ast, env.moduleNames, env.moduleCallType).length > 0;
|
|
54
|
+
// Resolution happened as the expression was READ, over the environment's own namespaces —
|
|
55
|
+
// `qcall` is a node of its own rather than a rewrite — so every walk below already sees
|
|
56
|
+
// the resolved shape and none of them has to know a module name from a variable.
|
|
57
|
+
const resolvedCalls = qualifiedCalls(ast).length > 0;
|
|
68
58
|
|
|
69
59
|
// A name bound inside the expression can never be read once a module owns it:
|
|
70
60
|
// `Billing.f(x)` under a comprehension variable named `Billing` resolves to
|
|
71
61
|
// the module. Reported rather than silently shadowed, the rule every other
|
|
72
62
|
// name in CEL scope follows.
|
|
73
|
-
for (const name of moduleNameBindings(
|
|
63
|
+
for (const name of moduleNameBindings(ast, env.moduleNames)) {
|
|
74
64
|
out.push({
|
|
75
65
|
code: "BINDING_NAME_RESERVED",
|
|
76
66
|
message:
|
|
@@ -80,7 +70,8 @@ export function analyzeCelWithTree(
|
|
|
80
70
|
});
|
|
81
71
|
}
|
|
82
72
|
|
|
83
|
-
const
|
|
73
|
+
const checked = env.celEnv.check(parsed);
|
|
74
|
+
const audit = auditCalls(source, ast, env.celEnv, env.moduleCallFlags, checked);
|
|
84
75
|
|
|
85
76
|
// Reported whatever the type-checker concludes. A literal argument a guard
|
|
86
77
|
// will refuse is a defect the manifest states outright, and leaving it to the
|
|
@@ -91,15 +82,24 @@ export function analyzeCelWithTree(
|
|
|
91
82
|
let type: string | undefined;
|
|
92
83
|
let checkError: string | undefined;
|
|
93
84
|
try {
|
|
94
|
-
//
|
|
95
|
-
//
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
85
|
+
// The TREE is checked, never the source again: `check(source)` would re-read it, and a
|
|
86
|
+
// tree resolved under another site's names is refused rather than answered about.
|
|
87
|
+
if (checked.valid) type = checked.typeName;
|
|
88
|
+
else {
|
|
89
|
+
// **Every verdict is the engine's, coded and ranged.** This used to be one opaque
|
|
90
|
+
// sentence — `found no matching overload for 'f(...)'` for three unrelated mistakes —
|
|
91
|
+
// which is why a classifier sat beside it rewriting the message. The engine decides an
|
|
92
|
+
// unknown name, a name called in the other form, an unknown field, a type no overload
|
|
93
|
+
// takes and a refusal over a literal argument each where its cause is known, so they
|
|
94
|
+
// come through as they are.
|
|
95
|
+
for (const diagnostic of checked.diagnostics) {
|
|
96
|
+
out.push({
|
|
97
|
+
code: diagnostic.code,
|
|
98
|
+
message: diagnostic.message,
|
|
99
|
+
...(diagnostic.fix ? { fix: diagnostic.fix } : {}),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
checkError = checked.diagnostics[0]?.message;
|
|
103
103
|
}
|
|
104
104
|
} catch (e) {
|
|
105
105
|
// The checker is now the ONLY type verdict for every CEL expression, so a
|
|
@@ -130,22 +130,9 @@ export function analyzeCelWithTree(
|
|
|
130
130
|
// this analyzer refuses and the kernel would run fine.
|
|
131
131
|
// What explaining a rejection already reported, so the context's own chain
|
|
132
132
|
// check below does not report it twice.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
// which is the whole reason it exists as more than a message rewriter.
|
|
137
|
-
out.push(...audit.diagnostics);
|
|
138
|
-
const unknownFields =
|
|
139
|
-
audit.diagnostics.length === 0 ? explainUnknownFields(parsed.ast, env) : [];
|
|
140
|
-
out.push(...unknownFields);
|
|
141
|
-
for (const d of unknownFields) explained.add(d.message);
|
|
142
|
-
if (audit.diagnostics.length === 0 && unknownFields.length === 0) {
|
|
143
|
-
out.push({
|
|
144
|
-
code: "CEL_TYPE_ERROR",
|
|
145
|
-
message: checkError + explainUnresolved(audit.unresolved, env.celEnv) + DYN_HINT(checkError),
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
}
|
|
133
|
+
// What the engine already said, so the host's own schema reading below does not say it
|
|
134
|
+
// twice in different words.
|
|
135
|
+
const explained = new Set(out.map((diagnostic) => diagnostic.message));
|
|
149
136
|
|
|
150
137
|
// An undeclared ROOT identifier. cel-js types an unknown name as `dyn` and
|
|
151
138
|
// accepts it, so `!cel "fff"` type-checked, reached the runtime and resolved
|
|
@@ -164,11 +151,11 @@ export function analyzeCelWithTree(
|
|
|
164
151
|
// Walked only once a root is unknown, which a correct expression never has.
|
|
165
152
|
let callReceivers: Set<string> | undefined;
|
|
166
153
|
const reported = new Set<string>();
|
|
167
|
-
for (const chain of extractAccessChains(
|
|
154
|
+
for (const chain of extractAccessChains(ast)) {
|
|
168
155
|
const root = chain[0];
|
|
169
156
|
if (!root || reported.has(root) || env.celEnv.hasVariable(root)) continue;
|
|
170
157
|
reported.add(root);
|
|
171
|
-
callReceivers ??= unresolvedCallReceivers(
|
|
158
|
+
callReceivers ??= unresolvedCallReceivers(ast, env.moduleNames);
|
|
172
159
|
out.push({
|
|
173
160
|
code: "CEL_UNKNOWN_IDENTIFIER",
|
|
174
161
|
message:
|
|
@@ -184,7 +171,7 @@ export function analyzeCelWithTree(
|
|
|
184
171
|
// caller resolves: member access on it is checked exactly as a read off the
|
|
185
172
|
// context would be, under the call as written.
|
|
186
173
|
if (resolvedCalls && env.moduleCallResult) {
|
|
187
|
-
for (const access of extractCallResultAccesses(
|
|
174
|
+
for (const access of extractCallResultAccesses(ast)) {
|
|
188
175
|
const schema = env.moduleCallResult(access.qualified);
|
|
189
176
|
if (!schema) continue;
|
|
190
177
|
const label = `${access.qualified}(…)`;
|
|
@@ -198,12 +185,12 @@ export function analyzeCelWithTree(
|
|
|
198
185
|
|
|
199
186
|
if (env.contextSchema) {
|
|
200
187
|
const contextSchema = env.contextSchema as Record<string, any>;
|
|
201
|
-
for (const chain of extractAccessChains(
|
|
188
|
+
for (const chain of extractAccessChains(ast)) {
|
|
202
189
|
const err = validateChainAgainstSchema(chain, contextSchema);
|
|
203
190
|
if (err && !explained.has(err)) out.push({ code: "CEL_UNKNOWN_FIELD", message: err });
|
|
204
191
|
}
|
|
205
192
|
|
|
206
|
-
for (const issue of findNullableAccessIssues(
|
|
193
|
+
for (const issue of findNullableAccessIssues(ast, contextSchema)) {
|
|
207
194
|
// Index access (member "[index]") attaches without a dot; a named field
|
|
208
195
|
// attaches with one — so the suggested CEL stays valid either way.
|
|
209
196
|
const access = issue.member === "[index]" ? issue.member : `.${issue.member}`;
|
|
@@ -217,10 +204,12 @@ export function analyzeCelWithTree(
|
|
|
217
204
|
return {
|
|
218
205
|
result: {
|
|
219
206
|
diagnostics: out,
|
|
220
|
-
calls:
|
|
207
|
+
calls: audit.calls,
|
|
221
208
|
...(type === undefined ? {} : { type }),
|
|
222
|
-
...(ast.
|
|
223
|
-
|
|
209
|
+
...(ast.kind === "literal" && ast.literal.type === "string"
|
|
210
|
+
? { stringLiteral: ast.literal.value }
|
|
211
|
+
: {}),
|
|
212
|
+
...(checkError === undefined ? {} : { readTypes: chainTypes(ast, env) }),
|
|
224
213
|
},
|
|
225
214
|
ast,
|
|
226
215
|
};
|
|
@@ -231,10 +220,10 @@ export function analyzeCelWithTree(
|
|
|
231
220
|
* reported in its own words. The explain schema is not the checker's
|
|
232
221
|
* environment, so a chain is reported only when the checker rejects it too;
|
|
233
222
|
* otherwise the rejection was about something else and keeps its own words. */
|
|
234
|
-
function explainUnknownFields(ast:
|
|
223
|
+
function explainUnknownFields(ast: CelNode, env: AnalyzeEnv): CelDiagnostic[] {
|
|
235
224
|
const schema = env.explainSchema?.();
|
|
236
225
|
if (!schema) return [];
|
|
237
|
-
const out:
|
|
226
|
+
const out: CelDiagnostic[] = [];
|
|
238
227
|
const reported = new Set<string>();
|
|
239
228
|
for (const chain of extractAccessChains(ast)) {
|
|
240
229
|
const message = validateChainAgainstSchema(chain, schema as Record<string, any>);
|
|
@@ -247,12 +236,12 @@ function explainUnknownFields(ast: ASTNode, env: AnalyzeEnv): EngineDiagnostic[]
|
|
|
247
236
|
}
|
|
248
237
|
|
|
249
238
|
/** The checked type of each distinct plain chain the expression reads. */
|
|
250
|
-
function chainTypes(ast:
|
|
239
|
+
function chainTypes(ast: CelNode, env: AnalyzeEnv): string[] {
|
|
251
240
|
const types = new Set<string>();
|
|
252
241
|
for (const chain of extractAccessChains(ast)) {
|
|
253
242
|
if (chain.includes(INDEX_SEGMENT)) continue;
|
|
254
243
|
const result = env.celEnv.check(chain.join("."));
|
|
255
|
-
if (result.valid
|
|
244
|
+
if (result.valid) types.add(result.typeName);
|
|
256
245
|
}
|
|
257
246
|
return [...types];
|
|
258
247
|
}
|
|
@@ -264,32 +253,6 @@ function checkerRejects(chain: readonly string[], env: AnalyzeEnv): boolean {
|
|
|
264
253
|
return !env.celEnv.check(named.join(".")).valid;
|
|
265
254
|
}
|
|
266
255
|
|
|
267
|
-
/** The audited call sites, each module call carrying its arguments as the type
|
|
268
|
-
* check saw them. Matched by position, which is what identifies one call among
|
|
269
|
-
* several of the same name. */
|
|
270
|
-
function withCallArguments(calls: readonly CallSite[], ast: ASTNode): CallSite[] {
|
|
271
|
-
const byStart = new Map<number, ModuleCall>();
|
|
272
|
-
for (const { node, call } of moduleCallNodes(ast)) byStart.set(node.start, call);
|
|
273
|
-
const chainsByStart = new Map<number, Array<string[] | null>>();
|
|
274
|
-
for (const [node, chains] of moduleCallArgumentChains(ast)) chainsByStart.set(node.start, chains);
|
|
275
|
-
return calls.map((site) => {
|
|
276
|
-
const call = site.moduleCall ? byStart.get(site.start) : undefined;
|
|
277
|
-
if (!call) return site;
|
|
278
|
-
const chains = chainsByStart.get(site.start);
|
|
279
|
-
return {
|
|
280
|
-
...site,
|
|
281
|
-
arguments: call.args.map((arg, index) => {
|
|
282
|
-
const type = call.argumentTypes?.[index];
|
|
283
|
-
const chain = chains?.[index] ?? null;
|
|
284
|
-
return {
|
|
285
|
-
...(type !== undefined ? { type } : {}),
|
|
286
|
-
...(chain && !chain.includes(INDEX_SEGMENT) ? { chain } : {}),
|
|
287
|
-
};
|
|
288
|
-
}),
|
|
289
|
-
};
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
|
|
293
256
|
/** `dyn` in a checker message means an operand whose type is unknown here —
|
|
294
257
|
* almost always a step result whose invoked resource declares no
|
|
295
258
|
* `outputType:`. Without this, the reader takes `dyn` for a cast problem. */
|
|
@@ -317,4 +280,8 @@ export const celEngine: TemplatingEngine = {
|
|
|
317
280
|
expressionRegions(source) {
|
|
318
281
|
return [{ start: 0, end: source.length }];
|
|
319
282
|
},
|
|
283
|
+
|
|
284
|
+
repeatable(source, celEnv) {
|
|
285
|
+
return repeatableExpression(source, celEnv);
|
|
286
|
+
},
|
|
320
287
|
};
|
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CelEnvironment, CelNode } from "@telorun/cel";
|
|
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:
|
|
60
|
-
ast:
|
|
61
|
-
) => readonly
|
|
62
|
-
):
|
|
52
|
+
result: CelAnalyzeResult,
|
|
53
|
+
ast: CelNode | undefined,
|
|
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: CelEnvironment): 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
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { CelEnvironment, CelEvaluationError, isCelMap, isCelRecord, type CelNode } from "@telorun/cel";
|
|
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:
|
|
63
|
-
ast:
|
|
73
|
+
result: CelAnalyzeResult,
|
|
74
|
+
ast: CelNode | 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) {
|
|
@@ -87,17 +98,17 @@ function holeVerdict(
|
|
|
87
98
|
];
|
|
88
99
|
}
|
|
89
100
|
|
|
90
|
-
const CONVERTIBLE = new WeakMap<
|
|
101
|
+
const CONVERTIBLE = new WeakMap<CelEnvironment, ReadonlySet<string>>();
|
|
91
102
|
|
|
92
103
|
/** The types a `string()` overload of `env` accepts, `dyn` among them — read
|
|
93
104
|
* off the registered overloads, so the check is the one evaluation applies. */
|
|
94
|
-
function convertibleTypes(env:
|
|
105
|
+
function convertibleTypes(env: CelEnvironment): ReadonlySet<string> {
|
|
95
106
|
const cached = CONVERTIBLE.get(env);
|
|
96
107
|
if (cached) return cached;
|
|
97
108
|
const types = new Set<string>(["dyn"]);
|
|
98
|
-
for (const fn of env.
|
|
99
|
-
if (fn.name === "string" && fn.receiverType === null && fn.
|
|
100
|
-
types.add(fn.
|
|
109
|
+
for (const fn of env.definitions().functions) {
|
|
110
|
+
if (fn.name === "string" && fn.receiverType === null && fn.parameters.length === 1) {
|
|
111
|
+
types.add(fn.parameters[0]!);
|
|
101
112
|
}
|
|
102
113
|
}
|
|
103
114
|
CONVERTIBLE.set(env, types);
|
|
@@ -106,20 +117,20 @@ function convertibleTypes(env: Environment): ReadonlySet<string> {
|
|
|
106
117
|
|
|
107
118
|
type Conversion = (value: unknown, hole: InterpolationHole, source: string) => string;
|
|
108
119
|
|
|
109
|
-
const CONVERSIONS = new WeakMap<
|
|
120
|
+
const CONVERSIONS = new WeakMap<CelEnvironment, Conversion>();
|
|
110
121
|
|
|
111
122
|
/** CEL's `string(v)` in `env`, with every overload the environment registers. */
|
|
112
|
-
function stringConversion(env:
|
|
123
|
+
function stringConversion(env: CelEnvironment): Conversion {
|
|
113
124
|
const cached = CONVERSIONS.get(env);
|
|
114
125
|
if (cached) return cached;
|
|
115
|
-
const program = env.
|
|
126
|
+
const program = env.compile("string(value)");
|
|
116
127
|
const conversion: Conversion = (value, hole, source) => {
|
|
117
128
|
if (typeof value === "string") return value;
|
|
118
129
|
try {
|
|
119
|
-
return program({ value }) as string;
|
|
130
|
+
return program.evaluate({ value }) as string;
|
|
120
131
|
} catch (error) {
|
|
121
|
-
if (!(error instanceof
|
|
122
|
-
throw
|
|
132
|
+
if (!(error instanceof CelEvaluationError)) throw error;
|
|
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. ` +
|
|
@@ -134,6 +145,9 @@ function stringConversion(env: Environment): Conversion {
|
|
|
134
145
|
function runtimeTypeOf(value: unknown): string {
|
|
135
146
|
if (value === null || value === undefined) return "null";
|
|
136
147
|
if (Array.isArray(value)) return "a list";
|
|
137
|
-
|
|
148
|
+
// Both of the value domain's map representations, asked of the domain rather than of the
|
|
149
|
+
// prototype: a branded value is a plain object, so a prototype test named an optional, a
|
|
150
|
+
// type and every host named type "a map".
|
|
151
|
+
if (isCelMap(value) || isCelRecord(value)) return "a map";
|
|
138
152
|
return `a value of type ${(value as object).constructor?.name ?? typeof value}`;
|
|
139
153
|
}
|
|
@@ -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,17 +1,16 @@
|
|
|
1
1
|
export {
|
|
2
2
|
buildCelEnvironment,
|
|
3
|
+
buildCelLanguageEnvironment,
|
|
3
4
|
celBuiltinFunctions,
|
|
4
|
-
|
|
5
|
+
celFunctionCatalog,
|
|
5
6
|
type CelHandlers,
|
|
6
7
|
} from "./cel/environment.js";
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
} from "./cel/catalog.js";
|
|
14
|
-
export { compileExpression } from "./cel/compile.js";
|
|
8
|
+
// `namespaceDispatchOf` is the ONE adapter a host's dispatch table crosses to
|
|
9
|
+
// become the engine's namespace seam — exported because the analyzer evaluates
|
|
10
|
+
// rule conditions and function bodies through the same table, and a second
|
|
11
|
+
// adapter is what let it convert arguments differently from the kernel.
|
|
12
|
+
export { compileExpression, namespaceDispatchOf } from "./cel/compile.js";
|
|
13
|
+
export { registerValueBrands } from "./cel/value-brands.js";
|
|
15
14
|
export {
|
|
16
15
|
interpolationShape,
|
|
17
16
|
literalFragments,
|
|
@@ -29,7 +28,6 @@ export {
|
|
|
29
28
|
export {
|
|
30
29
|
auditCalls,
|
|
31
30
|
explainUnresolved,
|
|
32
|
-
functionIndex,
|
|
33
31
|
type CallAudit,
|
|
34
32
|
type ModuleCallFlags,
|
|
35
33
|
} from "./cel/diagnose.js";
|
|
@@ -39,9 +37,10 @@ export {
|
|
|
39
37
|
// `ModuleCallTypeResolver` is here because `AnalyzeEnv` names it: an exported
|
|
40
38
|
// interface whose field type cannot be named is not usable from outside.
|
|
41
39
|
export {
|
|
40
|
+
celNamespaceNames,
|
|
42
41
|
MODULE_CALL_DISPATCH_KEY,
|
|
42
|
+
moduleCallNames,
|
|
43
43
|
moduleCallOf,
|
|
44
|
-
resolveModuleCalls,
|
|
45
44
|
type ModuleCallDispatch,
|
|
46
45
|
type ModuleCallTypeResolver,
|
|
47
46
|
} from "./cel/module-call.js";
|
|
@@ -68,6 +67,7 @@ export {
|
|
|
68
67
|
createDefaultRegistry,
|
|
69
68
|
defaultRegistry,
|
|
70
69
|
producedTypeOf,
|
|
70
|
+
repeatableSource,
|
|
71
71
|
} from "./builtins.js";
|
|
72
72
|
export type {
|
|
73
73
|
AnalyzeEnv,
|