@telorun/templating 0.20.1 → 0.21.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cel/analyze.d.ts +26 -0
- package/dist/cel/analyze.d.ts.map +1 -1
- package/dist/cel/analyze.js +114 -8
- package/dist/cel/catalog.d.ts.map +1 -1
- package/dist/cel/catalog.js +10 -0
- package/dist/cel/compile.d.ts +9 -3
- package/dist/cel/compile.d.ts.map +1 -1
- package/dist/cel/compile.js +52 -11
- package/dist/cel/diagnose.d.ts +10 -1
- package/dist/cel/diagnose.d.ts.map +1 -1
- package/dist/cel/diagnose.js +42 -6
- package/dist/cel/environment.d.ts.map +1 -1
- package/dist/cel/environment.js +2 -0
- package/dist/cel/module-call.d.ts +108 -0
- package/dist/cel/module-call.d.ts.map +1 -0
- package/dist/cel/module-call.js +245 -0
- package/dist/cel/value-identity.d.ts +18 -0
- package/dist/cel/value-identity.d.ts.map +1 -0
- package/dist/cel/value-identity.js +25 -0
- package/dist/engine.d.ts +66 -0
- package/dist/engine.d.ts.map +1 -1
- package/dist/engines/cel.d.ts.map +1 -1
- package/dist/engines/cel.js +87 -6
- package/dist/engines/sql.d.ts.map +1 -1
- package/dist/engines/sql.js +5 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/package.json +3 -3
- package/src/cel/analyze.ts +131 -9
- package/src/cel/catalog.ts +10 -0
- package/src/cel/compile.ts +59 -12
- package/src/cel/diagnose.ts +56 -6
- package/src/cel/environment.ts +3 -0
- package/src/cel/module-call.ts +369 -0
- package/src/cel/value-identity.ts +34 -0
- package/src/engine.ts +72 -0
- package/src/engines/cel.ts +104 -6
- package/src/engines/sql.ts +5 -1
- package/src/index.ts +20 -1
package/src/engines/cel.ts
CHANGED
|
@@ -1,11 +1,28 @@
|
|
|
1
|
+
import type { ASTNode } from "@marcbachmann/cel-js";
|
|
1
2
|
import {
|
|
2
3
|
extractAccessChains,
|
|
4
|
+
extractCallResultAccesses,
|
|
3
5
|
findNullableAccessIssues,
|
|
6
|
+
INDEX_SEGMENT,
|
|
7
|
+
moduleCallArgumentChains,
|
|
4
8
|
validateChainAgainstSchema,
|
|
5
9
|
} from "../cel/analyze.js";
|
|
6
10
|
import { compileExpression } from "../cel/compile.js";
|
|
7
11
|
import { auditCalls, explainUnresolved } from "../cel/diagnose.js";
|
|
8
|
-
import
|
|
12
|
+
import {
|
|
13
|
+
moduleCallNodes,
|
|
14
|
+
moduleNameBindings,
|
|
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";
|
|
9
26
|
|
|
10
27
|
/** Statically analyze one CEL expression against the effective context schema:
|
|
11
28
|
* parse → classify every call → type-check → validate member-access chains →
|
|
@@ -31,7 +48,27 @@ export function analyzeCelExpression(source: string, env: AnalyzeEnv): AnalyzeRe
|
|
|
31
48
|
};
|
|
32
49
|
}
|
|
33
50
|
|
|
34
|
-
|
|
51
|
+
// Resolution, before anything reads the tree — the audit, the type check and
|
|
52
|
+
// every chain walk below all see the resolved shape, so none of them has to
|
|
53
|
+
// know a module name from a variable.
|
|
54
|
+
// The walks that read resolved calls below are skipped when there are none.
|
|
55
|
+
const resolvedCalls = resolveModuleCalls(parsed.ast, env.moduleNames, env.moduleCallType).length > 0;
|
|
56
|
+
|
|
57
|
+
// A name bound inside the expression can never be read once a module owns it:
|
|
58
|
+
// `Billing.f(x)` under a comprehension variable named `Billing` resolves to
|
|
59
|
+
// the module. Reported rather than silently shadowed, the rule every other
|
|
60
|
+
// name in CEL scope follows.
|
|
61
|
+
for (const name of moduleNameBindings(parsed.ast, env.moduleNames)) {
|
|
62
|
+
out.push({
|
|
63
|
+
code: "BINDING_NAME_RESERVED",
|
|
64
|
+
message:
|
|
65
|
+
`'${name}' names a module here (an imports: alias, Self, or this module's own name), ` +
|
|
66
|
+
`so a call written '${name}.f(…)' resolves to that module and this binding could never ` +
|
|
67
|
+
`be read. Rename the bound variable.`,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const audit = auditCalls(source, parsed.ast, env.celEnv, env.moduleCallFlags);
|
|
35
72
|
|
|
36
73
|
// Reported whatever the type-checker concludes. A literal argument a guard
|
|
37
74
|
// will refuse is a defect the manifest states outright, and leaving it to the
|
|
@@ -42,7 +79,10 @@ export function analyzeCelExpression(source: string, env: AnalyzeEnv): AnalyzeRe
|
|
|
42
79
|
let type: string | undefined;
|
|
43
80
|
let checkError: string | undefined;
|
|
44
81
|
try {
|
|
45
|
-
|
|
82
|
+
// `parsed.check()`, never `celEnv.check(source)`: the latter RE-PARSES, so
|
|
83
|
+
// it would type a tree in which no module call was ever resolved — every
|
|
84
|
+
// one of them reported as a missing method on an unknown receiver.
|
|
85
|
+
const result = parsed.check();
|
|
46
86
|
if (result.valid) type = result.type;
|
|
47
87
|
else if (result.error) {
|
|
48
88
|
checkError = String((result.error as { message?: string }).message ?? result.error)
|
|
@@ -94,18 +134,46 @@ export function analyzeCelExpression(source: string, env: AnalyzeEnv): AnalyzeRe
|
|
|
94
134
|
// Comprehension variables are not roots: `extractAccessChains` drops the
|
|
95
135
|
// names a `.all(x, …)` binds, so the check never sees them.
|
|
96
136
|
if (env.rootsDeclared) {
|
|
137
|
+
// A receiver that resolved to no module name AND could be one — the host
|
|
138
|
+
// decides the second half, since a name's level is its naming rule, not
|
|
139
|
+
// this engine's. `Foo.bar()` is an unknown identifier like any other, but
|
|
140
|
+
// its repair is an `imports:` alias rather than a different spelling, so it
|
|
141
|
+
// says so; `dbb.query(1)` is a misspelled value and gets no such advice.
|
|
142
|
+
// Walked only once a root is unknown, which a correct expression never has.
|
|
143
|
+
let callReceivers: Set<string> | undefined;
|
|
97
144
|
const reported = new Set<string>();
|
|
98
145
|
for (const chain of extractAccessChains(parsed.ast)) {
|
|
99
146
|
const root = chain[0];
|
|
100
147
|
if (!root || reported.has(root) || env.celEnv.hasVariable(root)) continue;
|
|
101
148
|
reported.add(root);
|
|
149
|
+
callReceivers ??= unresolvedCallReceivers(parsed.ast, env.moduleNames);
|
|
102
150
|
out.push({
|
|
103
151
|
code: "CEL_UNKNOWN_IDENTIFIER",
|
|
104
|
-
message:
|
|
152
|
+
message:
|
|
153
|
+
`unknown identifier '${root}' — nothing by that name is in scope here.` +
|
|
154
|
+
(callReceivers.has(root) && env.couldNameModule?.(root) === true
|
|
155
|
+
? ` To call a function another module declares, '${root}' must be one of this module's names — an 'imports:' alias, 'Self', or the module's own metadata.name.`
|
|
156
|
+
: ""),
|
|
105
157
|
});
|
|
106
158
|
}
|
|
107
159
|
}
|
|
108
160
|
|
|
161
|
+
// A module call's result is typed by the callee's declared `returns`, which the
|
|
162
|
+
// caller resolves: member access on it is checked exactly as a read off the
|
|
163
|
+
// context would be, under the call as written.
|
|
164
|
+
if (resolvedCalls && env.moduleCallResult) {
|
|
165
|
+
for (const access of extractCallResultAccesses(parsed.ast)) {
|
|
166
|
+
const schema = env.moduleCallResult(access.qualified);
|
|
167
|
+
if (!schema) continue;
|
|
168
|
+
const label = `${access.qualified}(…)`;
|
|
169
|
+
const err = validateChainAgainstSchema([label, ...access.members], {
|
|
170
|
+
type: "object",
|
|
171
|
+
properties: { [label]: schema },
|
|
172
|
+
});
|
|
173
|
+
if (err) out.push({ code: "CEL_UNKNOWN_FIELD", message: err });
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
109
177
|
if (env.contextSchema) {
|
|
110
178
|
const contextSchema = env.contextSchema as Record<string, any>;
|
|
111
179
|
for (const chain of extractAccessChains(parsed.ast)) {
|
|
@@ -124,7 +192,37 @@ export function analyzeCelExpression(source: string, env: AnalyzeEnv): AnalyzeRe
|
|
|
124
192
|
}
|
|
125
193
|
}
|
|
126
194
|
|
|
127
|
-
return {
|
|
195
|
+
return {
|
|
196
|
+
diagnostics: out,
|
|
197
|
+
calls: resolvedCalls ? withCallArguments(audit.calls, parsed.ast) : audit.calls,
|
|
198
|
+
...(type === undefined ? {} : { type }),
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** The audited call sites, each module call carrying its arguments as the type
|
|
203
|
+
* check saw them. Matched by position, which is what identifies one call among
|
|
204
|
+
* several of the same name. */
|
|
205
|
+
function withCallArguments(calls: readonly CallSite[], ast: ASTNode): CallSite[] {
|
|
206
|
+
const byStart = new Map<number, ModuleCall>();
|
|
207
|
+
for (const { node, call } of moduleCallNodes(ast)) byStart.set(node.start, call);
|
|
208
|
+
const chainsByStart = new Map<number, Array<string[] | null>>();
|
|
209
|
+
for (const [node, chains] of moduleCallArgumentChains(ast)) chainsByStart.set(node.start, chains);
|
|
210
|
+
return calls.map((site) => {
|
|
211
|
+
const call = site.moduleCall ? byStart.get(site.start) : undefined;
|
|
212
|
+
if (!call) return site;
|
|
213
|
+
const chains = chainsByStart.get(site.start);
|
|
214
|
+
return {
|
|
215
|
+
...site,
|
|
216
|
+
arguments: call.args.map((arg, index) => {
|
|
217
|
+
const type = call.argumentTypes?.[index];
|
|
218
|
+
const chain = chains?.[index] ?? null;
|
|
219
|
+
return {
|
|
220
|
+
...(type !== undefined ? { type } : {}),
|
|
221
|
+
...(chain && !chain.includes(INDEX_SEGMENT) ? { chain } : {}),
|
|
222
|
+
};
|
|
223
|
+
}),
|
|
224
|
+
};
|
|
225
|
+
});
|
|
128
226
|
}
|
|
129
227
|
|
|
130
228
|
/** `dyn` in a checker message means an operand whose type is unknown here —
|
|
@@ -144,7 +242,7 @@ export const celEngine: TemplatingEngine = {
|
|
|
144
242
|
language: "cel",
|
|
145
243
|
|
|
146
244
|
compile(source, env) {
|
|
147
|
-
return compileExpression(source, env.celEnv);
|
|
245
|
+
return compileExpression(source, env.celEnv, env.moduleNames);
|
|
148
246
|
},
|
|
149
247
|
|
|
150
248
|
analyze(source, env) {
|
package/src/engines/sql.ts
CHANGED
|
@@ -21,10 +21,14 @@ export const sqlEngine: TemplatingEngine = {
|
|
|
21
21
|
language: "sql",
|
|
22
22
|
|
|
23
23
|
compile(source, env) {
|
|
24
|
-
const inner = compileString(source, env.celEnv);
|
|
24
|
+
const inner = compileString(source, env.celEnv, env.moduleNames);
|
|
25
25
|
return {
|
|
26
26
|
__compiled: true,
|
|
27
27
|
source,
|
|
28
|
+
// The qualified module calls of every interpolation, for the reason
|
|
29
|
+
// `refs` are carried: a consumer asking what an expression CALLS cannot
|
|
30
|
+
// re-derive it from the text without the declaring module's names.
|
|
31
|
+
calls: typeof inner === "string" ? [] : ((inner as CompiledValue).calls ?? []),
|
|
28
32
|
// The AST-derived root identifiers of every interpolation, carried through
|
|
29
33
|
// rather than dropped. `compileString` has already computed them one line
|
|
30
34
|
// above, and a consumer that asks a compiled value what it READS —
|
package/src/index.ts
CHANGED
|
@@ -24,7 +24,25 @@ export {
|
|
|
24
24
|
INDEX_SEGMENT,
|
|
25
25
|
validateChainAgainstSchema,
|
|
26
26
|
} from "./cel/analyze.js";
|
|
27
|
-
export {
|
|
27
|
+
export {
|
|
28
|
+
auditCalls,
|
|
29
|
+
explainUnresolved,
|
|
30
|
+
functionIndex,
|
|
31
|
+
type CallAudit,
|
|
32
|
+
type ModuleCallFlags,
|
|
33
|
+
} from "./cel/diagnose.js";
|
|
34
|
+
// The rewrite, its reader, and the activation key the kernel binds a scope's
|
|
35
|
+
// dispatch table under. The unbound message and the receiver and bound-name
|
|
36
|
+
// walks are read inside this package only.
|
|
37
|
+
// `ModuleCallTypeResolver` is here because `AnalyzeEnv` names it: an exported
|
|
38
|
+
// interface whose field type cannot be named is not usable from outside.
|
|
39
|
+
export {
|
|
40
|
+
MODULE_CALL_DISPATCH_KEY,
|
|
41
|
+
moduleCallOf,
|
|
42
|
+
resolveModuleCalls,
|
|
43
|
+
type ModuleCallDispatch,
|
|
44
|
+
type ModuleCallTypeResolver,
|
|
45
|
+
} from "./cel/module-call.js";
|
|
28
46
|
export { walkCelExpressions, type CelSurface } from "./cel/walk.js";
|
|
29
47
|
|
|
30
48
|
export { celEngine } from "./engines/cel.js";
|
|
@@ -48,6 +66,7 @@ export {
|
|
|
48
66
|
export type {
|
|
49
67
|
AnalyzeEnv,
|
|
50
68
|
AnalyzeResult,
|
|
69
|
+
CallArgument,
|
|
51
70
|
CallSite,
|
|
52
71
|
CompileEnv,
|
|
53
72
|
DiagnosticFix,
|