@telorun/templating 0.24.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/dist/engine.d.ts +16 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.d.ts.map +1 -1
- package/dist/engines/cel.js +49 -2
- package/package.json +1 -1
- package/src/engine.ts +17 -0
- package/src/engines/cel.ts +49 -2
package/dist/engine.d.ts
CHANGED
|
@@ -72,6 +72,15 @@ export interface AnalyzeEnv {
|
|
|
72
72
|
* unqualified, which is what it says with no host rule to consult.
|
|
73
73
|
*/
|
|
74
74
|
readonly couldNameModule?: (name: string) => boolean;
|
|
75
|
+
/**
|
|
76
|
+
* The names this site reads, as a schema — consulted ONLY to explain a
|
|
77
|
+
* rejection `check()` already made. A chain naming a field it does not declare
|
|
78
|
+
* is then reported as `CEL_UNKNOWN_FIELD` with the fields it does, rather than
|
|
79
|
+
* the checker's own wording. Never a verdict of its own: an expression the
|
|
80
|
+
* checker accepts is not re-judged against it. Lazy, since only a failing
|
|
81
|
+
* expression asks.
|
|
82
|
+
*/
|
|
83
|
+
readonly explainSchema?: () => Record<string, unknown> | null;
|
|
75
84
|
}
|
|
76
85
|
/** A mechanically applicable repair for a diagnostic. `replacement` is the
|
|
77
86
|
* **whole analyzed source**, corrected — never a fragment — so a consumer can
|
|
@@ -152,6 +161,13 @@ export interface AnalyzeResult {
|
|
|
152
161
|
readonly type?: string;
|
|
153
162
|
/** Every function call in the source, in source order. */
|
|
154
163
|
readonly calls: readonly CallSite[];
|
|
164
|
+
/** The string the whole expression is a literal of, read off the parsed tree
|
|
165
|
+
* (so `(':memory:')` is one), when it is one. */
|
|
166
|
+
readonly stringLiteral?: string;
|
|
167
|
+
/** When the checker rejected the expression, the CEL type of each plain chain
|
|
168
|
+
* it reads (`variables.db` → `Telo.HostPath`), so a host can say how to use a
|
|
169
|
+
* value whose type is what the rejection turned on. */
|
|
170
|
+
readonly readTypes?: readonly string[];
|
|
155
171
|
}
|
|
156
172
|
/** One module-relative file a tagged node embeds, reported by the engine that
|
|
157
173
|
* owns the tag.
|
package/dist/engine.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AACxD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACzD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAEnE;;wEAEwE;AACxE,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CAC5C;AAED;;;;;;;;;eASe;AACf,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC;IAC7B,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,IAAI,CAAC;IACvD;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;IAEjC;;;2DAGuD;IACvD,QAAQ,CAAC,WAAW,CAAC,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAE3C;;qEAEiE;IACjE,QAAQ,CAAC,cAAc,CAAC,EAAE,sBAAsB,CAAC;IAEjD;;gEAE4D;IAC5D,QAAQ,CAAC,gBAAgB,CAAC,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC;IAEvF;;4EAEwE;IACxE,QAAQ,CAAC,eAAe,CAAC,EAAE,eAAe,CAAC;IAE3C;;;;;;;;O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,MAAM,EAAE,OAAO,CAAC,CAAC;IAEzC;;;;;;qDAMiD;IACjD,iBAAiB,CAAC,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,gBAAgB,EAAE,CAAC;CACjE"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cel.d.ts","sourceRoot":"","sources":["../../src/engines/cel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,OAAO,KAAK,EACV,UAAU,EACV,aAAa,EAGb,gBAAgB,EACjB,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;2BAU2B;AAC3B,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,aAAa,CAEnF;AAED;8EAC8E;AAC9E,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,UAAU,GACd;IAAE,MAAM,EAAE,aAAa,CAAC;IAAC,GAAG,CAAC,EAAE,OAAO,CAAA;CAAE,
|
|
1
|
+
{"version":3,"file":"cel.d.ts","sourceRoot":"","sources":["../../src/engines/cel.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAkBpD,OAAO,KAAK,EACV,UAAU,EACV,aAAa,EAGb,gBAAgB,EACjB,MAAM,cAAc,CAAC;AAEtB;;;;;;;;;;2BAU2B;AAC3B,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,UAAU,GAAG,aAAa,CAEnF;AAED;8EAC8E;AAC9E,wBAAgB,kBAAkB,CAChC,MAAM,EAAE,MAAM,EACd,GAAG,EAAE,UAAU,GACd;IAAE,MAAM,EAAE,aAAa,CAAC;IAAC,GAAG,CAAC,EAAE,OAAO,CAAA;CAAE,CAoL1C;AA4ED;yCACyC;AACzC,eAAO,MAAM,SAAS,EAAE,gBAevB,CAAC"}
|
package/dist/engines/cel.js
CHANGED
|
@@ -95,11 +95,18 @@ export function analyzeCelWithTree(source, env) {
|
|
|
95
95
|
// not become a hard error on valid CEL. Reporting nothing where cel-js is
|
|
96
96
|
// happy makes an unknown future macro a silent no-op rather than a manifest
|
|
97
97
|
// this analyzer refuses and the kernel would run fine.
|
|
98
|
+
// What explaining a rejection already reported, so the context's own chain
|
|
99
|
+
// check below does not report it twice.
|
|
100
|
+
const explained = new Set();
|
|
98
101
|
if (checkError !== undefined) {
|
|
99
102
|
// `check()` stops at its first problem; the audit enumerates every bad call,
|
|
100
103
|
// which is the whole reason it exists as more than a message rewriter.
|
|
101
104
|
out.push(...audit.diagnostics);
|
|
102
|
-
|
|
105
|
+
const unknownFields = audit.diagnostics.length === 0 ? explainUnknownFields(parsed.ast, env) : [];
|
|
106
|
+
out.push(...unknownFields);
|
|
107
|
+
for (const d of unknownFields)
|
|
108
|
+
explained.add(d.message);
|
|
109
|
+
if (audit.diagnostics.length === 0 && unknownFields.length === 0) {
|
|
103
110
|
out.push({
|
|
104
111
|
code: "CEL_TYPE_ERROR",
|
|
105
112
|
message: checkError + explainUnresolved(audit.unresolved, env.celEnv) + DYN_HINT(checkError),
|
|
@@ -159,7 +166,7 @@ export function analyzeCelWithTree(source, env) {
|
|
|
159
166
|
const contextSchema = env.contextSchema;
|
|
160
167
|
for (const chain of extractAccessChains(parsed.ast)) {
|
|
161
168
|
const err = validateChainAgainstSchema(chain, contextSchema);
|
|
162
|
-
if (err)
|
|
169
|
+
if (err && !explained.has(err))
|
|
163
170
|
out.push({ code: "CEL_UNKNOWN_FIELD", message: err });
|
|
164
171
|
}
|
|
165
172
|
for (const issue of findNullableAccessIssues(parsed.ast, contextSchema)) {
|
|
@@ -177,10 +184,50 @@ export function analyzeCelWithTree(source, env) {
|
|
|
177
184
|
diagnostics: out,
|
|
178
185
|
calls: resolvedCalls ? withCallArguments(audit.calls, parsed.ast) : audit.calls,
|
|
179
186
|
...(type === undefined ? {} : { type }),
|
|
187
|
+
...(ast.op === "value" && typeof ast.args === "string" ? { stringLiteral: ast.args } : {}),
|
|
188
|
+
...(checkError === undefined ? {} : { readTypes: chainTypes(parsed.ast, env) }),
|
|
180
189
|
},
|
|
181
190
|
ast,
|
|
182
191
|
};
|
|
183
192
|
}
|
|
193
|
+
/** Every chain naming a field the site's names do not declare, as the host's
|
|
194
|
+
* explain schema reads them — the readable half of a rejection the checker
|
|
195
|
+
* reported in its own words. The explain schema is not the checker's
|
|
196
|
+
* environment, so a chain is reported only when the checker rejects it too;
|
|
197
|
+
* otherwise the rejection was about something else and keeps its own words. */
|
|
198
|
+
function explainUnknownFields(ast, env) {
|
|
199
|
+
const schema = env.explainSchema?.();
|
|
200
|
+
if (!schema)
|
|
201
|
+
return [];
|
|
202
|
+
const out = [];
|
|
203
|
+
const reported = new Set();
|
|
204
|
+
for (const chain of extractAccessChains(ast)) {
|
|
205
|
+
const message = validateChainAgainstSchema(chain, schema);
|
|
206
|
+
if (message && !reported.has(message) && checkerRejects(chain, env)) {
|
|
207
|
+
reported.add(message);
|
|
208
|
+
out.push({ code: "CEL_UNKNOWN_FIELD", message });
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return out;
|
|
212
|
+
}
|
|
213
|
+
/** The checked type of each distinct plain chain the expression reads. */
|
|
214
|
+
function chainTypes(ast, env) {
|
|
215
|
+
const types = new Set();
|
|
216
|
+
for (const chain of extractAccessChains(ast)) {
|
|
217
|
+
if (chain.includes(INDEX_SEGMENT))
|
|
218
|
+
continue;
|
|
219
|
+
const result = env.celEnv.check(chain.join("."));
|
|
220
|
+
if (result.valid && result.type !== undefined)
|
|
221
|
+
types.add(result.type);
|
|
222
|
+
}
|
|
223
|
+
return [...types];
|
|
224
|
+
}
|
|
225
|
+
/** Does the checker reject the chain's named prefix (up to its first index)? */
|
|
226
|
+
function checkerRejects(chain, env) {
|
|
227
|
+
const end = chain.indexOf(INDEX_SEGMENT);
|
|
228
|
+
const named = end === -1 ? chain : chain.slice(0, end);
|
|
229
|
+
return !env.celEnv.check(named.join(".")).valid;
|
|
230
|
+
}
|
|
184
231
|
/** The audited call sites, each module call carrying its arguments as the type
|
|
185
232
|
* check saw them. Matched by position, which is what identifies one call among
|
|
186
233
|
* several of the same name. */
|
package/package.json
CHANGED
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
|
package/src/engines/cel.ts
CHANGED
|
@@ -128,11 +128,18 @@ export function analyzeCelWithTree(
|
|
|
128
128
|
// not become a hard error on valid CEL. Reporting nothing where cel-js is
|
|
129
129
|
// happy makes an unknown future macro a silent no-op rather than a manifest
|
|
130
130
|
// this analyzer refuses and the kernel would run fine.
|
|
131
|
+
// What explaining a rejection already reported, so the context's own chain
|
|
132
|
+
// check below does not report it twice.
|
|
133
|
+
const explained = new Set<string>();
|
|
131
134
|
if (checkError !== undefined) {
|
|
132
135
|
// `check()` stops at its first problem; the audit enumerates every bad call,
|
|
133
136
|
// which is the whole reason it exists as more than a message rewriter.
|
|
134
137
|
out.push(...audit.diagnostics);
|
|
135
|
-
|
|
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) {
|
|
136
143
|
out.push({
|
|
137
144
|
code: "CEL_TYPE_ERROR",
|
|
138
145
|
message: checkError + explainUnresolved(audit.unresolved, env.celEnv) + DYN_HINT(checkError),
|
|
@@ -193,7 +200,7 @@ export function analyzeCelWithTree(
|
|
|
193
200
|
const contextSchema = env.contextSchema as Record<string, any>;
|
|
194
201
|
for (const chain of extractAccessChains(parsed.ast)) {
|
|
195
202
|
const err = validateChainAgainstSchema(chain, contextSchema);
|
|
196
|
-
if (err) out.push({ code: "CEL_UNKNOWN_FIELD", message: err });
|
|
203
|
+
if (err && !explained.has(err)) out.push({ code: "CEL_UNKNOWN_FIELD", message: err });
|
|
197
204
|
}
|
|
198
205
|
|
|
199
206
|
for (const issue of findNullableAccessIssues(parsed.ast, contextSchema)) {
|
|
@@ -212,11 +219,51 @@ export function analyzeCelWithTree(
|
|
|
212
219
|
diagnostics: out,
|
|
213
220
|
calls: resolvedCalls ? withCallArguments(audit.calls, parsed.ast) : audit.calls,
|
|
214
221
|
...(type === undefined ? {} : { type }),
|
|
222
|
+
...(ast.op === "value" && typeof ast.args === "string" ? { stringLiteral: ast.args } : {}),
|
|
223
|
+
...(checkError === undefined ? {} : { readTypes: chainTypes(parsed.ast, env) }),
|
|
215
224
|
},
|
|
216
225
|
ast,
|
|
217
226
|
};
|
|
218
227
|
}
|
|
219
228
|
|
|
229
|
+
/** Every chain naming a field the site's names do not declare, as the host's
|
|
230
|
+
* explain schema reads them — the readable half of a rejection the checker
|
|
231
|
+
* reported in its own words. The explain schema is not the checker's
|
|
232
|
+
* environment, so a chain is reported only when the checker rejects it too;
|
|
233
|
+
* otherwise the rejection was about something else and keeps its own words. */
|
|
234
|
+
function explainUnknownFields(ast: ASTNode, env: AnalyzeEnv): EngineDiagnostic[] {
|
|
235
|
+
const schema = env.explainSchema?.();
|
|
236
|
+
if (!schema) return [];
|
|
237
|
+
const out: EngineDiagnostic[] = [];
|
|
238
|
+
const reported = new Set<string>();
|
|
239
|
+
for (const chain of extractAccessChains(ast)) {
|
|
240
|
+
const message = validateChainAgainstSchema(chain, schema as Record<string, any>);
|
|
241
|
+
if (message && !reported.has(message) && checkerRejects(chain, env)) {
|
|
242
|
+
reported.add(message);
|
|
243
|
+
out.push({ code: "CEL_UNKNOWN_FIELD", message });
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
return out;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** The checked type of each distinct plain chain the expression reads. */
|
|
250
|
+
function chainTypes(ast: ASTNode, env: AnalyzeEnv): string[] {
|
|
251
|
+
const types = new Set<string>();
|
|
252
|
+
for (const chain of extractAccessChains(ast)) {
|
|
253
|
+
if (chain.includes(INDEX_SEGMENT)) continue;
|
|
254
|
+
const result = env.celEnv.check(chain.join("."));
|
|
255
|
+
if (result.valid && result.type !== undefined) types.add(result.type);
|
|
256
|
+
}
|
|
257
|
+
return [...types];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Does the checker reject the chain's named prefix (up to its first index)? */
|
|
261
|
+
function checkerRejects(chain: readonly string[], env: AnalyzeEnv): boolean {
|
|
262
|
+
const end = chain.indexOf(INDEX_SEGMENT);
|
|
263
|
+
const named = end === -1 ? chain : chain.slice(0, end);
|
|
264
|
+
return !env.celEnv.check(named.join(".")).valid;
|
|
265
|
+
}
|
|
266
|
+
|
|
220
267
|
/** The audited call sites, each module call carrying its arguments as the type
|
|
221
268
|
* check saw them. Matched by position, which is what identifies one call among
|
|
222
269
|
* several of the same name. */
|