@telorun/templating 0.23.0 → 0.25.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/README.md +1 -1
- package/dist/builtins.d.ts +8 -0
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +13 -0
- package/dist/cel/analyze.d.ts +4 -0
- package/dist/cel/analyze.d.ts.map +1 -1
- package/dist/cel/analyze.js +9 -0
- package/dist/cel/catalog.d.ts.map +1 -1
- package/dist/cel/catalog.js +12 -4
- package/dist/cel/compile.d.ts +4 -26
- package/dist/cel/compile.d.ts.map +1 -1
- package/dist/cel/compile.js +3 -88
- package/dist/cel/interpolation-holes.d.ts +43 -0
- package/dist/cel/interpolation-holes.d.ts.map +1 -0
- package/dist/cel/interpolation-holes.js +121 -0
- package/dist/cel/walk.d.ts +7 -28
- package/dist/cel/walk.d.ts.map +1 -1
- package/dist/cel/walk.js +7 -39
- package/dist/engine.d.ts +30 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.d.ts +7 -0
- package/dist/engines/cel.d.ts.map +1 -1
- package/dist/engines/cel.js +78 -14
- package/dist/engines/hole-analysis.d.ts +23 -0
- package/dist/engines/hole-analysis.d.ts.map +1 -0
- package/dist/engines/hole-analysis.js +66 -0
- package/dist/engines/interpolate.d.ts +13 -0
- package/dist/engines/interpolate.d.ts.map +1 -0
- package/dist/engines/interpolate.js +123 -0
- package/dist/engines/sql.d.ts +3 -3
- package/dist/engines/sql.d.ts.map +1 -1
- package/dist/engines/sql.js +24 -58
- package/dist/index.d.ts +6 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -2
- package/dist/sentinel.d.ts +4 -5
- package/dist/sentinel.d.ts.map +1 -1
- package/dist/sentinel.js +6 -14
- package/package.json +2 -2
- package/src/builtins.ts +14 -0
- package/src/cel/analyze.ts +12 -0
- package/src/cel/catalog.ts +14 -4
- package/src/cel/compile.ts +4 -92
- package/src/cel/interpolation-holes.ts +138 -0
- package/src/cel/walk.ts +8 -54
- package/src/engine.ts +33 -0
- package/src/engines/cel.ts +85 -16
- package/src/engines/hole-analysis.ts +96 -0
- package/src/engines/interpolate.ts +139 -0
- package/src/engines/sql.ts +26 -67
- package/src/index.ts +12 -7
- package/src/sentinel.ts +6 -14
package/dist/sentinel.js
CHANGED
|
@@ -31,27 +31,19 @@ export const MODULE_PATH_ENGINE = "module-path";
|
|
|
31
31
|
/**
|
|
32
32
|
* The dotted chain a value names, or undefined.
|
|
33
33
|
*
|
|
34
|
-
* Only a PLAIN CHAIN — `steps.encode.result.output
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* the analyzer knows least is the conservative direction.
|
|
34
|
+
* Only a PLAIN CHAIN written as a `!cel` value — `steps.encode.result.output`.
|
|
35
|
+
* An expression that COMPUTES rather than names has no schema to read off a
|
|
36
|
+
* context, so a caller that navigates one gets nothing and reports nothing:
|
|
37
|
+
* silence where the analyzer knows least is the conservative direction.
|
|
39
38
|
*
|
|
40
39
|
* Here rather than in each caller because "is this expression a plain chain" is
|
|
41
40
|
* one question, and two copies of the answer would eventually disagree about a
|
|
42
41
|
* shape like `a.b[0]`.
|
|
43
42
|
*/
|
|
44
43
|
export function plainChainOf(value) {
|
|
45
|
-
|
|
46
|
-
? value.engine === CEL_ENGINE
|
|
47
|
-
? value.source
|
|
48
|
-
: undefined
|
|
49
|
-
: typeof value === "string"
|
|
50
|
-
? /^\s*\$\{\{(.+)\}\}\s*$/.exec(value)?.[1]
|
|
51
|
-
: undefined;
|
|
52
|
-
if (typeof source !== "string")
|
|
44
|
+
if (!isTaggedSentinel(value) || value.engine !== CEL_ENGINE)
|
|
53
45
|
return undefined;
|
|
54
|
-
const trimmed = source.trim();
|
|
46
|
+
const trimmed = value.source.trim();
|
|
55
47
|
return /^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/.test(trimmed)
|
|
56
48
|
? trimmed
|
|
57
49
|
: undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telorun/templating",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "Telo Templating - Engine registry and shared CEL core for Telo manifests.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"telo",
|
|
@@ -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.101.0"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
52
|
"@telorun/sdk": "*"
|
package/src/builtins.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { celEngine } from "./engines/cel.js";
|
|
2
2
|
import { includeBytesEngine, includeTextEngine } from "./engines/include.js";
|
|
3
|
+
import { interpolateEngine } from "./engines/interpolate.js";
|
|
3
4
|
import { literalEngine } from "./engines/literal.js";
|
|
4
5
|
import { modulePathEngine } from "./engines/module-path.js";
|
|
5
6
|
import { refEngine } from "./engines/ref.js";
|
|
@@ -17,6 +18,7 @@ export const builtinEngines: readonly TemplatingEngine[] = [
|
|
|
17
18
|
celEngine,
|
|
18
19
|
includeBytesEngine,
|
|
19
20
|
includeTextEngine,
|
|
21
|
+
interpolateEngine,
|
|
20
22
|
literalEngine,
|
|
21
23
|
modulePathEngine,
|
|
22
24
|
refEngine,
|
|
@@ -45,6 +47,18 @@ export function producedTypeOf(engineName: string): Record<string, unknown> | un
|
|
|
45
47
|
return defaultRegistry().get(engineName)?.producedType?.();
|
|
46
48
|
}
|
|
47
49
|
|
|
50
|
+
/**
|
|
51
|
+
* The CEL expressions a tagged scalar holds — its whole source under `!cel`,
|
|
52
|
+
* each hole under a tag with holes, none under a tag holding no CEL.
|
|
53
|
+
*
|
|
54
|
+
* The single reader of `TemplatingEngine.expressionRegions`, so an analysis pass
|
|
55
|
+
* asking "what CEL is written here" never recognises a tag by name.
|
|
56
|
+
*/
|
|
57
|
+
export function celExpressionsOf(engineName: string, source: string): string[] {
|
|
58
|
+
const regions = defaultRegistry().get(engineName)?.expressionRegions?.(source) ?? [];
|
|
59
|
+
return regions.map((r) => source.slice(r.start, r.end));
|
|
60
|
+
}
|
|
61
|
+
|
|
48
62
|
let defaultRegistryCache: TemplatingEngineRegistry | undefined;
|
|
49
63
|
|
|
50
64
|
/** Memoized singleton: returns the default registry. Hosts that don't need
|
package/src/cel/analyze.ts
CHANGED
|
@@ -248,6 +248,18 @@ function schemaAtChain(
|
|
|
248
248
|
return current;
|
|
249
249
|
}
|
|
250
250
|
|
|
251
|
+
/** The dotted chain an expression IS, when it is a plain chain whose schema
|
|
252
|
+
* admits null — a value that may itself be null, rather than one dereferenced
|
|
253
|
+
* through a null. */
|
|
254
|
+
export function nullableValueChain(
|
|
255
|
+
node: ASTNode,
|
|
256
|
+
contextSchema: Record<string, any>,
|
|
257
|
+
): string | null {
|
|
258
|
+
const chain = dottedChain(node, new Set());
|
|
259
|
+
if (chain === null) return null;
|
|
260
|
+
return schemaIsNullable(schemaAtChain(chain.split("."), contextSchema)) ? chain : null;
|
|
261
|
+
}
|
|
262
|
+
|
|
251
263
|
function isNullLiteral(node: ASTNode): boolean {
|
|
252
264
|
return node.op === "value" && (node.args as unknown) === null;
|
|
253
265
|
}
|
package/src/cel/catalog.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { formatLocale } from "d3-format";
|
|
2
2
|
import { RE2JS } from "re2js";
|
|
3
3
|
import { v1, v3, v4, v5, v6, v7, validate as uuidValidate, version as uuidVersion } from "uuid";
|
|
4
|
+
import { PLAIN_ENCODINGS } from "@telorun/sdk";
|
|
4
5
|
|
|
5
6
|
/** Host-injected functions that need platform APIs the templating package must
|
|
6
7
|
* not import directly (Node `crypto` / `Buffer`), keeping it browser-safe. The
|
|
@@ -1100,15 +1101,24 @@ export const CEL_FUNCTIONS: readonly CelFunctionDoc[] = [
|
|
|
1100
1101
|
// so an expiry could be computed and not stored. Semantics follow cel-go
|
|
1101
1102
|
// exactly — RFC 3339 and epoch SECONDS — so `int(timestamp)` and
|
|
1102
1103
|
// `timestamp(int)` round-trip in one unit.
|
|
1104
|
+
//
|
|
1105
|
+
// `string(duration)` is cel-spec's too, and cel-js omits it: seconds with a
|
|
1106
|
+
// trimmed fraction, the `Telo.Duration` plain encoding, so a duration renders
|
|
1107
|
+
// one way whether it is interpolated or serialized.
|
|
1103
1108
|
{
|
|
1104
1109
|
name: "string",
|
|
1105
|
-
signature: "string(timestamp): string",
|
|
1106
|
-
register: [
|
|
1110
|
+
signature: "string(timestamp | duration): string",
|
|
1111
|
+
register: [
|
|
1112
|
+
"string(google.protobuf.Timestamp): string",
|
|
1113
|
+
"string(google.protobuf.Duration): string",
|
|
1114
|
+
],
|
|
1107
1115
|
category: "conversion",
|
|
1108
|
-
summary:
|
|
1116
|
+
summary:
|
|
1117
|
+
"Format an instant as RFC 3339 (ISO-8601, UTC), or a duration as seconds with a trimmed fraction (`5400s`, `1.5s`).",
|
|
1109
1118
|
deterministic: true,
|
|
1110
1119
|
hostBacked: false,
|
|
1111
|
-
build: () => (
|
|
1120
|
+
build: () => (value: unknown) =>
|
|
1121
|
+
value instanceof Date ? value.toISOString() : PLAIN_ENCODINGS["cel-duration"]!.encode(value),
|
|
1112
1122
|
},
|
|
1113
1123
|
{
|
|
1114
1124
|
name: "int",
|
package/src/cel/compile.ts
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { CompiledValue } from "@telorun/sdk";
|
|
2
2
|
import type { ASTNode, 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";
|
|
6
6
|
|
|
7
|
-
export const TEMPLATE_REGEX = /\$\{\{\s*([^}]+?)\s*\}\}/g;
|
|
8
|
-
export const EXACT_TEMPLATE_REGEX = /^\s*\$\{\{\s*([^}]+?)\s*\}\}\s*$/;
|
|
9
|
-
|
|
10
7
|
/** Root variable identifiers an expression reads, from its parsed AST — the
|
|
11
8
|
* first element of every member-access chain (`self.table` → `self`). Returns
|
|
12
9
|
* undefined when the parse result carries no AST so consumers can tell "no
|
|
@@ -41,9 +38,9 @@ function callsNonDeterministic(root: ASTNode): boolean {
|
|
|
41
38
|
return visit(root);
|
|
42
39
|
}
|
|
43
40
|
|
|
44
|
-
/** Compile a single CEL expression
|
|
45
|
-
*
|
|
46
|
-
*
|
|
41
|
+
/** Compile a single CEL expression into a CompiledValue. Throws on syntax
|
|
42
|
+
* errors. The `!cel` engine compiles its whole scalar with it, and the tags
|
|
43
|
+
* with holes compile each hole.
|
|
47
44
|
*
|
|
48
45
|
* `moduleNames` are the declaring module's own names; a call whose receiver is
|
|
49
46
|
* one of them resolves to a late-bound module call HERE, on the parsed tree, so
|
|
@@ -67,88 +64,3 @@ export function compileExpression(
|
|
|
67
64
|
call: (ctx: Record<string, unknown>) => fn(ctx),
|
|
68
65
|
};
|
|
69
66
|
}
|
|
70
|
-
|
|
71
|
-
/** Compile a string that may contain `${{ }}`-delimited CEL segments. If the
|
|
72
|
-
* string is exactly one expression, returns a single CompiledValue. If it
|
|
73
|
-
* contains interpolations, returns a CompiledValue that joins literal parts
|
|
74
|
-
* with stringified expression results. If no expressions are present, returns
|
|
75
|
-
* the input string unchanged. Throws on CEL syntax errors. */
|
|
76
|
-
export function compileString(
|
|
77
|
-
s: string,
|
|
78
|
-
env: Environment,
|
|
79
|
-
moduleNames?: ReadonlySet<string>,
|
|
80
|
-
): unknown {
|
|
81
|
-
if (!s.includes("${{")) return s;
|
|
82
|
-
|
|
83
|
-
const exact = s.match(EXACT_TEMPLATE_REGEX);
|
|
84
|
-
if (exact) {
|
|
85
|
-
return compileExpression(exact[1].trim(), env, moduleNames);
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const parts: Array<string | CompiledValue> = [];
|
|
89
|
-
let last = 0;
|
|
90
|
-
for (const m of s.matchAll(TEMPLATE_REGEX)) {
|
|
91
|
-
if (m.index! > last) parts.push(s.slice(last, m.index));
|
|
92
|
-
parts.push(compileExpression(m[1].trim(), env, moduleNames));
|
|
93
|
-
last = m.index! + m[0].length;
|
|
94
|
-
}
|
|
95
|
-
if (last < s.length) parts.push(s.slice(last));
|
|
96
|
-
|
|
97
|
-
const refs = new Set<string>();
|
|
98
|
-
const calls = new Set<string>();
|
|
99
|
-
let volatile = false;
|
|
100
|
-
for (const p of parts) {
|
|
101
|
-
if (typeof p === "string") continue;
|
|
102
|
-
if (p.refs) for (const r of p.refs) refs.add(r);
|
|
103
|
-
if (p.calls) for (const c of p.calls) calls.add(c);
|
|
104
|
-
if (p.volatile) volatile = true;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
return {
|
|
108
|
-
__compiled: true,
|
|
109
|
-
source: s,
|
|
110
|
-
parts,
|
|
111
|
-
refs: [...refs],
|
|
112
|
-
calls: [...calls],
|
|
113
|
-
...(volatile ? { volatile: true as const } : {}),
|
|
114
|
-
call: (ctx: Record<string, unknown>) =>
|
|
115
|
-
parts.map((p) => (typeof p === "string" ? p : String(p.call(ctx) ?? ""))).join(""),
|
|
116
|
-
} satisfies CompiledValue;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/** Split an interpolated value into literal fragments and the evaluated values
|
|
120
|
-
* of its embedded expressions, instead of joining them into one string. Lets a
|
|
121
|
-
* consumer emit its own placeholders between fragments and bind the values
|
|
122
|
-
* separately (e.g. parameterized SQL). The invariant
|
|
123
|
-
* `fragments.length === values.length + 1` always holds.
|
|
124
|
-
*
|
|
125
|
-
* - plain string (no `${{ }}`) → `{ fragments: [s], values: [] }`
|
|
126
|
-
* - bare single expression `${{ x }}` → `{ fragments: ["", ""], values: [x] }`
|
|
127
|
-
* - interpolated `"a ${{ x }} b"` → `{ fragments: ["a ", " b"], values: [x] }`
|
|
128
|
-
*/
|
|
129
|
-
export function toParameterized(
|
|
130
|
-
value: unknown,
|
|
131
|
-
ctx: Record<string, unknown>,
|
|
132
|
-
): { fragments: string[]; values: unknown[] } {
|
|
133
|
-
if (typeof value === "string") return { fragments: [value], values: [] };
|
|
134
|
-
if (!isCompiledValue(value)) {
|
|
135
|
-
throw new Error("toParameterized expects a string or CompiledValue");
|
|
136
|
-
}
|
|
137
|
-
if (!value.parts) {
|
|
138
|
-
return { fragments: ["", ""], values: [value.call(ctx)] };
|
|
139
|
-
}
|
|
140
|
-
const fragments: string[] = [];
|
|
141
|
-
const values: unknown[] = [];
|
|
142
|
-
let current = "";
|
|
143
|
-
for (const p of value.parts) {
|
|
144
|
-
if (typeof p === "string") {
|
|
145
|
-
current += p;
|
|
146
|
-
} else {
|
|
147
|
-
fragments.push(current);
|
|
148
|
-
current = "";
|
|
149
|
-
values.push(p.call(ctx));
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
fragments.push(current);
|
|
153
|
-
return { fragments, values };
|
|
154
|
-
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/** One `${{ expr }}` hole in a scalar. Offsets index the scalar's source. */
|
|
2
|
+
export interface InterpolationHole {
|
|
3
|
+
/** Offset of the opening `${{`. */
|
|
4
|
+
readonly start: number;
|
|
5
|
+
/** Offset just past the closing `}}`. */
|
|
6
|
+
readonly end: number;
|
|
7
|
+
/** The expression, trimmed of the whitespace inside the delimiters. */
|
|
8
|
+
readonly expr: string;
|
|
9
|
+
/** Offset of {@link expr} within the source. */
|
|
10
|
+
readonly exprStart: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type HoleReading =
|
|
14
|
+
| { readonly ok: true; readonly holes: readonly InterpolationHole[] }
|
|
15
|
+
| { readonly ok: false; readonly offset: number; readonly message: string };
|
|
16
|
+
|
|
17
|
+
/** How a scalar's text relates to holes: none at all, exactly one hole with only
|
|
18
|
+
* whitespace around it, holes among text, or a `${{` that opens no readable
|
|
19
|
+
* hole. */
|
|
20
|
+
export type InterpolationShape = "none" | "lone-hole" | "interpolated" | "malformed";
|
|
21
|
+
|
|
22
|
+
const OPEN = "${{";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Every `${{ expr }}` hole in `source` — the one hole grammar every tag with
|
|
26
|
+
* holes (`!interpolate`, `!sql`) and the untagged-interpolation migration read.
|
|
27
|
+
*
|
|
28
|
+
* A hole opens at `${{` and closes at the first `}}` that is outside a CEL
|
|
29
|
+
* string literal and outside any `{` the expression itself opened, so a map
|
|
30
|
+
* literal or a string holding braces is read whole. String literals follow CEL's
|
|
31
|
+
* lexical grammar: `'`, `"`, their triple-quoted forms, and the `r`/`b` prefixes,
|
|
32
|
+
* where a raw string takes no escapes. Outside a hole nothing is special but
|
|
33
|
+
* `${{`; a literal `${{` is written as a hole yielding it (`${{ '${{' }}`).
|
|
34
|
+
*
|
|
35
|
+
* A hole that never closes is a reading failure at its opening offset, never a
|
|
36
|
+
* guess at where it meant to end.
|
|
37
|
+
*/
|
|
38
|
+
export function readInterpolationHoles(source: string): HoleReading {
|
|
39
|
+
const holes: InterpolationHole[] = [];
|
|
40
|
+
let from = 0;
|
|
41
|
+
for (;;) {
|
|
42
|
+
const start = source.indexOf(OPEN, from);
|
|
43
|
+
if (start === -1) return { ok: true, holes };
|
|
44
|
+
const close = closingOffset(source, start + OPEN.length);
|
|
45
|
+
if (close === undefined) {
|
|
46
|
+
return {
|
|
47
|
+
ok: false,
|
|
48
|
+
offset: start,
|
|
49
|
+
message: `the hole opened at offset ${start} never closes — a hole ends at the first '}}' outside a string literal and outside any braces it opened`,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
const body = source.slice(start + OPEN.length, close);
|
|
53
|
+
const lead = body.length - body.trimStart().length;
|
|
54
|
+
holes.push({
|
|
55
|
+
start,
|
|
56
|
+
end: close + 2,
|
|
57
|
+
expr: body.trim(),
|
|
58
|
+
exprStart: start + OPEN.length + lead,
|
|
59
|
+
});
|
|
60
|
+
from = close + 2;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The shape of `source` under {@link readInterpolationHoles}. */
|
|
65
|
+
export function interpolationShape(source: string): InterpolationShape {
|
|
66
|
+
if (!source.includes(OPEN)) return "none";
|
|
67
|
+
const reading = readInterpolationHoles(source);
|
|
68
|
+
if (!reading.ok) return "malformed";
|
|
69
|
+
const [only, ...rest] = reading.holes;
|
|
70
|
+
if (!only) return "none";
|
|
71
|
+
const lone =
|
|
72
|
+
rest.length === 0 &&
|
|
73
|
+
source.slice(0, only.start).trim() === "" &&
|
|
74
|
+
source.slice(only.end).trim() === "";
|
|
75
|
+
return lone ? "lone-hole" : "interpolated";
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** The literal text between holes: always `holes.length + 1` fragments. */
|
|
79
|
+
export function literalFragments(source: string, holes: readonly InterpolationHole[]): string[] {
|
|
80
|
+
const out: string[] = [];
|
|
81
|
+
let last = 0;
|
|
82
|
+
for (const hole of holes) {
|
|
83
|
+
out.push(source.slice(last, hole.start));
|
|
84
|
+
last = hole.end;
|
|
85
|
+
}
|
|
86
|
+
out.push(source.slice(last));
|
|
87
|
+
return out;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** Offset of the `}}` closing a hole whose body starts at `from`. */
|
|
91
|
+
function closingOffset(source: string, from: number): number | undefined {
|
|
92
|
+
let depth = 0;
|
|
93
|
+
let i = from;
|
|
94
|
+
while (i < source.length) {
|
|
95
|
+
const c = source[i]!;
|
|
96
|
+
if (c === "'" || c === '"') {
|
|
97
|
+
const after = skipString(source, i);
|
|
98
|
+
if (after === undefined) return undefined;
|
|
99
|
+
i = after;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (c === "{") depth++;
|
|
103
|
+
else if (c === "}") {
|
|
104
|
+
if (depth === 0) return source[i + 1] === "}" ? i : undefined;
|
|
105
|
+
depth--;
|
|
106
|
+
}
|
|
107
|
+
i++;
|
|
108
|
+
}
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** Offset just past the string literal whose opening quote is at `quoteAt`. */
|
|
113
|
+
function skipString(source: string, quoteAt: number): number | undefined {
|
|
114
|
+
const quote = source[quoteAt]!;
|
|
115
|
+
const raw = /[rR]/.test(literalPrefix(source, quoteAt));
|
|
116
|
+
const triple = source.startsWith(quote.repeat(3), quoteAt);
|
|
117
|
+
const delimiter = triple ? quote.repeat(3) : quote;
|
|
118
|
+
let i = quoteAt + delimiter.length;
|
|
119
|
+
while (i < source.length) {
|
|
120
|
+
if (!raw && source[i] === "\\") {
|
|
121
|
+
i += 2;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (!triple && source[i] === "\n") return undefined;
|
|
125
|
+
if (source.startsWith(delimiter, i)) return i + delimiter.length;
|
|
126
|
+
i++;
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** The `r`/`b` prefix letters written immediately before a quote, when they are
|
|
132
|
+
* a prefix rather than the tail of an identifier. */
|
|
133
|
+
function literalPrefix(source: string, quoteAt: number): string {
|
|
134
|
+
let i = quoteAt;
|
|
135
|
+
while (i > 0 && quoteAt - i < 2 && /[rRbB]/.test(source[i - 1]!)) i--;
|
|
136
|
+
if (i > 0 && /[A-Za-z0-9_]/.test(source[i - 1]!)) return "";
|
|
137
|
+
return source.slice(i, quoteAt);
|
|
138
|
+
}
|
package/src/cel/walk.ts
CHANGED
|
@@ -1,64 +1,18 @@
|
|
|
1
1
|
import { isTaggedSentinel } from "../sentinel.js";
|
|
2
|
-
import { TEMPLATE_REGEX } from "./compile.js";
|
|
3
2
|
|
|
4
|
-
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* for the legacy interpolation form. Carrying it as data rather than letting
|
|
11
|
-
* each consumer re-derive it is what keeps `${{ … }}` a first-class fix site
|
|
12
|
-
* instead of an exclusion: the two surfaces differ only by these delimiters. */
|
|
13
|
-
export interface CelSurface {
|
|
14
|
-
readonly whole: boolean;
|
|
15
|
-
readonly wrapper?: { readonly prefix: string; readonly suffix: string };
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const OPEN = "${{";
|
|
19
|
-
|
|
20
|
-
/** Walks `value` and emits each templated source segment with its dotted
|
|
21
|
-
* path (e.g. `routes[0].handler.body`) and the engine that owns it.
|
|
22
|
-
*
|
|
23
|
-
* - Untagged strings: every `${{ ... }}` segment is emitted with
|
|
24
|
-
* `engineName = "cel"` (the implicit engine for the legacy interpolation
|
|
25
|
-
* syntax).
|
|
26
|
-
* - Tagged sentinels: emitted once with the sentinel's declared engine.
|
|
27
|
-
* This includes engines that may produce no diagnostics (`literal`) —
|
|
28
|
-
* routing through the registry stays generic so adding a third engine
|
|
29
|
-
* that wants real analysis doesn't require touching the walker.
|
|
30
|
-
* - Compiled values are skipped so a precompiled tree won't be re-walked. */
|
|
3
|
+
/** Walks `value` and emits each tagged scalar with its dotted path (e.g.
|
|
4
|
+
* `routes[0].handler.body`) and the engine that owns it — including engines
|
|
5
|
+
* that produce no diagnostics (`literal`), so routing through the registry
|
|
6
|
+
* stays generic and a new engine needs no change here. A plain string is
|
|
7
|
+
* never a value's expression, and compiled values are skipped so a
|
|
8
|
+
* precompiled tree is not re-walked. */
|
|
31
9
|
export function walkCelExpressions(
|
|
32
10
|
value: unknown,
|
|
33
11
|
path: string,
|
|
34
|
-
cb: (source: string, path: string, engineName: string
|
|
12
|
+
cb: (source: string, path: string, engineName: string) => void,
|
|
35
13
|
): void {
|
|
36
14
|
if (isTaggedSentinel(value)) {
|
|
37
|
-
cb(value.source, path, value.engine
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (typeof value === "string") {
|
|
41
|
-
if (!value.includes(OPEN)) return;
|
|
42
|
-
const matches = [...value.matchAll(TEMPLATE_REGEX)];
|
|
43
|
-
for (const m of matches) {
|
|
44
|
-
const expr = m[1]!.trim();
|
|
45
|
-
// A string that is nothing but one interpolation is as whole as a
|
|
46
|
-
// tagged scalar — the delimiters are the only difference, and they are
|
|
47
|
-
// handed back as the wrapper.
|
|
48
|
-
const only = matches.length === 1 && m.index === 0 && m[0]!.length === value.length;
|
|
49
|
-
const lead = /^\s*/.exec(m[0]!.slice(OPEN.length))![0]!.length;
|
|
50
|
-
cb(expr, path, "cel", {
|
|
51
|
-
whole: only,
|
|
52
|
-
...(only
|
|
53
|
-
? {
|
|
54
|
-
wrapper: {
|
|
55
|
-
prefix: value.slice(0, OPEN.length + lead),
|
|
56
|
-
suffix: value.slice(OPEN.length + lead + expr.length),
|
|
57
|
-
},
|
|
58
|
-
}
|
|
59
|
-
: {}),
|
|
60
|
-
});
|
|
61
|
-
}
|
|
15
|
+
cb(value.source, path, value.engine);
|
|
62
16
|
return;
|
|
63
17
|
}
|
|
64
18
|
if (Array.isArray(value)) {
|
package/src/engine.ts
CHANGED
|
@@ -79,6 +79,16 @@ export interface AnalyzeEnv {
|
|
|
79
79
|
* unqualified, which is what it says with no host rule to consult.
|
|
80
80
|
*/
|
|
81
81
|
readonly couldNameModule?: (name: string) => boolean;
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The names this site reads, as a schema — consulted ONLY to explain a
|
|
85
|
+
* rejection `check()` already made. A chain naming a field it does not declare
|
|
86
|
+
* is then reported as `CEL_UNKNOWN_FIELD` with the fields it does, rather than
|
|
87
|
+
* the checker's own wording. Never a verdict of its own: an expression the
|
|
88
|
+
* checker accepts is not re-judged against it. Lazy, since only a failing
|
|
89
|
+
* expression asks.
|
|
90
|
+
*/
|
|
91
|
+
readonly explainSchema?: () => Record<string, unknown> | null;
|
|
82
92
|
}
|
|
83
93
|
|
|
84
94
|
/** A mechanically applicable repair for a diagnostic. `replacement` is the
|
|
@@ -164,6 +174,13 @@ export interface AnalyzeResult {
|
|
|
164
174
|
readonly type?: string;
|
|
165
175
|
/** Every function call in the source, in source order. */
|
|
166
176
|
readonly calls: readonly CallSite[];
|
|
177
|
+
/** The string the whole expression is a literal of, read off the parsed tree
|
|
178
|
+
* (so `(':memory:')` is one), when it is one. */
|
|
179
|
+
readonly stringLiteral?: string;
|
|
180
|
+
/** When the checker rejected the expression, the CEL type of each plain chain
|
|
181
|
+
* it reads (`variables.db` → `Telo.HostPath`), so a host can say how to use a
|
|
182
|
+
* value whose type is what the rejection turned on. */
|
|
183
|
+
readonly readTypes?: readonly string[];
|
|
167
184
|
}
|
|
168
185
|
|
|
169
186
|
/** One module-relative file a tagged node embeds, reported by the engine that
|
|
@@ -190,6 +207,13 @@ export interface EngineFileClaim {
|
|
|
190
207
|
readonly directory?: boolean;
|
|
191
208
|
}
|
|
192
209
|
|
|
210
|
+
/** One CEL expression inside a tagged scalar, by offset into the scalar's
|
|
211
|
+
* source. */
|
|
212
|
+
export interface ExpressionRegion {
|
|
213
|
+
readonly start: number;
|
|
214
|
+
readonly end: number;
|
|
215
|
+
}
|
|
216
|
+
|
|
193
217
|
/** Per-property templating engine. Matches a YAML tag (`!<name>`); the kernel
|
|
194
218
|
* and analyzer dispatch through the registry rather than knowing about
|
|
195
219
|
* specific engines. */
|
|
@@ -250,4 +274,13 @@ export interface TemplatingEngine {
|
|
|
250
274
|
* byte slot both fail statically, through the ordinary schema check and with
|
|
251
275
|
* no diagnostic code of their own. */
|
|
252
276
|
producedType?(): Record<string, unknown>;
|
|
277
|
+
|
|
278
|
+
/** Where the CEL expressions sit inside the tagged scalar — the whole source
|
|
279
|
+
* for `!cel`, each hole's expression for a tag with holes.
|
|
280
|
+
*
|
|
281
|
+
* What an editor colours, completes, hovers and renames against, so it finds
|
|
282
|
+
* the expressions of any tag without recognising the tag. Absent on an
|
|
283
|
+
* engine whose scalar holds no CEL. A source the engine cannot read yields
|
|
284
|
+
* no regions; `analyze` is what reports why. */
|
|
285
|
+
expressionRegions?(source: string): readonly ExpressionRegion[];
|
|
253
286
|
}
|