@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.
Files changed (66) hide show
  1. package/dist/builtins.d.ts +9 -0
  2. package/dist/builtins.d.ts.map +1 -1
  3. package/dist/builtins.js +10 -0
  4. package/dist/cel/analyze.d.ts +6 -6
  5. package/dist/cel/analyze.d.ts.map +1 -1
  6. package/dist/cel/analyze.js +138 -183
  7. package/dist/cel/compile.d.ts +50 -12
  8. package/dist/cel/compile.d.ts.map +1 -1
  9. package/dist/cel/compile.js +140 -48
  10. package/dist/cel/diagnose.d.ts +52 -56
  11. package/dist/cel/diagnose.d.ts.map +1 -1
  12. package/dist/cel/diagnose.js +69 -291
  13. package/dist/cel/environment.d.ts +33 -33
  14. package/dist/cel/environment.d.ts.map +1 -1
  15. package/dist/cel/environment.js +54 -80
  16. package/dist/cel/module-call.d.ts +68 -79
  17. package/dist/cel/module-call.d.ts.map +1 -1
  18. package/dist/cel/module-call.js +118 -194
  19. package/dist/cel/value-brands.d.ts +18 -0
  20. package/dist/cel/value-brands.d.ts.map +1 -0
  21. package/dist/cel/value-brands.js +33 -0
  22. package/dist/cel/verdict-codes.d.ts +40 -0
  23. package/dist/cel/verdict-codes.d.ts.map +1 -0
  24. package/dist/cel/verdict-codes.js +43 -0
  25. package/dist/engine.d.ts +18 -3
  26. package/dist/engine.d.ts.map +1 -1
  27. package/dist/engines/cel.d.ts +6 -5
  28. package/dist/engines/cel.d.ts.map +1 -1
  29. package/dist/engines/cel.js +57 -82
  30. package/dist/engines/hole-analysis.d.ts +7 -3
  31. package/dist/engines/hole-analysis.d.ts.map +1 -1
  32. package/dist/engines/hole-analysis.js +7 -1
  33. package/dist/engines/include.js +1 -1
  34. package/dist/engines/interpolate.d.ts.map +1 -1
  35. package/dist/engines/interpolate.js +17 -11
  36. package/dist/engines/module-path.js +1 -1
  37. package/dist/engines/sql.d.ts.map +1 -1
  38. package/dist/engines/sql.js +4 -1
  39. package/dist/index.d.ts +6 -6
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +10 -6
  42. package/package.json +6 -6
  43. package/src/builtins.ts +12 -0
  44. package/src/cel/analyze.ts +152 -193
  45. package/src/cel/compile.ts +166 -47
  46. package/src/cel/diagnose.ts +116 -370
  47. package/src/cel/environment.ts +66 -82
  48. package/src/cel/module-call.ts +129 -292
  49. package/src/cel/value-brands.ts +34 -0
  50. package/src/cel/verdict-codes.ts +66 -0
  51. package/src/engine.ts +19 -3
  52. package/src/engines/cel.ts +62 -95
  53. package/src/engines/hole-analysis.ts +16 -16
  54. package/src/engines/include.ts +1 -1
  55. package/src/engines/interpolate.ts +33 -19
  56. package/src/engines/module-path.ts +1 -1
  57. package/src/engines/sql.ts +11 -1
  58. package/src/index.ts +11 -11
  59. package/dist/cel/catalog.d.ts +0 -67
  60. package/dist/cel/catalog.d.ts.map +0 -1
  61. package/dist/cel/catalog.js +0 -1253
  62. package/dist/cel/value-identity.d.ts +0 -18
  63. package/dist/cel/value-identity.d.ts.map +0 -1
  64. package/dist/cel/value-identity.js +0 -25
  65. package/src/cel/catalog.ts +0 -1379
  66. package/src/cel/value-identity.ts +0 -34
@@ -1,108 +1,97 @@
1
1
  /**
2
- * **A call whose receiver is one of the declaring module's names.**
2
+ * Module calls: what a resolved one says, where the dispatch table hangs, and the two walks
3
+ * the analyzer asks of an expression's own names.
3
4
  *
4
- * `Billing.format(x)`, `Self.y(1)`, `<ModuleName>.y(1)` — CEL's own
5
- * qualified-function rule, the one `cel.bind` and cel-go's `math.greatest`
6
- * already follow. Resolution happens on the PARSED TREE, right after
7
- * `env.parse` and before any check: the author's text, spans and the expression
8
- * a trace shows stay exactly as written, which rewriting the source would move.
5
+ * **The rewrite is gone.** `Alias.fn(x)` and `obj.method(x)` are the same syntax and only a
6
+ * set of names distinguishes them, so this package used to rewrite the parsed tree itself,
7
+ * through internals the previous engine did not expose: a `setMeta('macro', …)` redirect
8
+ * that made a qualified call check and evaluate as something else, with a comprehension
9
+ * macro's `alternate` winning over `macro` unless cleared, and an rcall's asyncness derived
10
+ * from state the replaced checker filled in. Three facts about a dependency's internals,
11
+ * each pinned by a test, and the reason it was pinned to an exact version.
9
12
  *
10
- * A module's names are known from its own file before any import resolves — its
11
- * `imports:` keys, `Self`, its `metadata.name` and `Telo` — so the set is an
12
- * INPUT here rather than something this module derives. A call on a bare
13
- * identifier outside the set stays an ordinary method call, which is what keeps
14
- * the catalog's `format` and `Billing.format` from ever colliding, with one
15
- * catalog environment compiling both.
13
+ * `@telorun/cel` has **`qcall` as a node of its own**, produced by a total tree-to-tree pass
14
+ * over the name set — never by the parser, because the set is the host's — and a
15
+ * `CelExpression` records the set it was resolved under, so an environment refuses a tree
16
+ * resolved for a different site instead of answering a different question about it. So
17
+ * reading a module call is reading a node kind, and there is nothing to rewrite and nothing
18
+ * to pin.
16
19
  *
17
- * Two engine internals carry it, neither exposed by cel-js's typings — hence
18
- * the exact version pin and `tests/module-call.test.ts`, which fails the moment
19
- * an upgrade moves either:
20
- *
21
- * - `ASTNode.setMeta('macro', …)` redirects a node's check and evaluation. A
22
- * comprehension macro (`map`, `filter`, …) sets `alternate` instead, and
23
- * `alternate` WINS over `macro`, so a call the parser expanded as a macro
24
- * needs it cleared or the rewrite is silently ignored. The parser expands
25
- * those names BEFORE this rewrite and refuses any call whose arguments do not
26
- * fit the macro, so no function may take a macro's name
27
- * (`FUNCTION_NAME_RESERVED`); clearing keeps a call that did parse reported
28
- * as the module call it names.
29
- * - `async` must be set explicitly: cel-js derives it for an `rcall` from
30
- * `receiverWithArgs`, which only the rcall's own checker fills in — and ours
31
- * replaces it.
32
- *
33
- * The dispatch table is read from the activation under a key no CEL source can
34
- * spell, so nothing an author can write reaches the mechanism past the export
35
- * gate or the dependency edge. Binding the table is the kernel's, per scope;
36
- * until something binds one, evaluating a module call throws naming the
37
- * function.
20
+ * What is left here is Telo's half: the activation key the kernel binds a scope's table
21
+ * under, the unbound message, and the two questions the analyzer asks about a module's own
22
+ * names colliding with the expression's.
38
23
  */
39
- import type { ASTNode } from "@marcbachmann/cel-js";
24
+ import { type CelNode } from "@telorun/cel";
40
25
  /**
41
- * Activation key the per-scope dispatch table is read from.
26
+ * Where a scope's dispatch table hangs on the activation.
42
27
  *
43
- * `@` and `:` are outside CEL's identifier grammar and there is no other way to
44
- * reach the activation, so no expression can name this — a spellable dispatcher
45
- * would let an author call past the export gate and past the dependency edge
46
- * that orders the callee before its caller.
28
+ * `@` and `:` are outside CEL's identifier grammar and the activation is reachable no other
29
+ * way, so no author can call past the export gate or the dependency edge. Binding the table
30
+ * is the kernel's, per owning scope; with none, evaluating a module call is refused.
47
31
  */
48
32
  export declare const MODULE_CALL_DISPATCH_KEY = "@telo:module-functions";
49
- /** Per-scope binding table: qualified name → the callable's `call`. */
33
+ /** A scope's module functions, keyed by qualified name. */
50
34
  export type ModuleCallDispatch = ReadonlyMap<string, (args: readonly unknown[]) => unknown>;
51
35
  /**
52
- * The CEL type a qualified call yields, as a type name (`"string"`,
53
- * `"list<int>"`), or undefined for "not known here".
54
- *
55
- * A hook rather than a table because the answer belongs to the analysis, not to
56
- * the expression: a signature is declared on a resource in the module the call
57
- * resolves through. With no resolver — every compile, and every analysis until
58
- * signatures are typed — a module call is `dyn`, which types its arguments and
59
- * claims nothing about its result.
36
+ * What the host says a module call's RESULT is, as a CEL type expression. The engine types
37
+ * the call node from it, so an operator over the call checks; absent, the call is `dyn`.
60
38
  */
61
39
  export type ModuleCallTypeResolver = (qualified: string) => string | undefined;
62
- /** What a resolved module call says about itself: the qualified name as
63
- * written, the argument nodes, and — once the tree has been type-checked — the
64
- * type the checker gave each argument. The single reader — no other surface
65
- * pattern-matches the macro's shape. */
40
+ /** What a resolved module call says about itself. */
66
41
  export interface ModuleCall {
67
42
  readonly qualified: string;
68
- readonly args: readonly ASTNode[];
69
- readonly argumentTypes?: readonly string[];
43
+ readonly args: readonly CelNode[];
70
44
  }
71
- export declare function moduleCallOf(node: ASTNode): ModuleCall | undefined;
72
- /** Message for a call nothing has bound. Not a fallback value: a call that
73
- * cannot reach its function has no result, and inventing one would make a
74
- * missing export read as a null. */
45
+ /** The module call this node IS, or nothing. One node kind, no pattern-matching. */
46
+ export declare function moduleCallOf(node: CelNode): ModuleCall | undefined;
47
+ /**
48
+ * Message for a call nothing has bound. Not a fallback value: a call that cannot reach its
49
+ * function has no result, and inventing one would make a missing export read as a null.
50
+ */
75
51
  export declare function unboundModuleCallMessage(qualified: string): string;
76
52
  /**
77
- * Rewrite every module call in `ast` in place and report them in source order.
53
+ * Which of a module's own names CEL can read as a namespace.
54
+ *
55
+ * **A module name and an import alias are YAML scalars nothing lexes where they are
56
+ * written** — `my-module`, `test-run` and `cel` all reach a name set, and the naming
57
+ * diagnostics (`INVALID_NAME`, `INVALID_TYPE_NAME`) are what report them. The engine refuses
58
+ * such a name at registration and refuses the SET WHOLE, so handing one over unfiltered
59
+ * turns a reportable name into a crash that loses every other diagnostic in the file. A name
60
+ * CEL cannot read as a namespace resolves no call, which is exactly what the naming
61
+ * diagnostic says is wrong with it.
78
62
  *
79
- * Returns the qualified names as written (`["Billing.format", "Self.y"]`), which
80
- * is what a compiled value carries so no consumer has to re-parse to learn what
81
- * an expression calls. Idempotent: a node already resolved is rewritten once and
82
- * reported again, so two passes over one tree agree about what it calls.
63
+ * It is here rather than in a consumer because the module-name set is this package's concept
64
+ * — `CompileEnv.moduleNames` — and three callers need the same answer: compiling an
65
+ * expression, the analyzer's typed site environments, and the kernel's type-rule conditions.
83
66
  */
84
- export declare function resolveModuleCalls(ast: ASTNode | undefined, moduleNames?: ReadonlySet<string>, typeOf?: ModuleCallTypeResolver): string[];
85
- /** Every resolved module call in `ast` with its node, outermost first. */
86
- export declare function moduleCallNodes(ast: ASTNode | undefined): Array<{
87
- node: ASTNode;
67
+ export declare function celNamespaceNames(moduleNames: Iterable<string>): ReadonlySet<string>;
68
+ /** Every resolved module call in the tree with its node, outermost first. */
69
+ export declare function moduleCallNodes(root: CelNode | undefined): Array<{
70
+ node: CelNode;
88
71
  call: ModuleCall;
89
72
  }>;
73
+ /** Every qualified call's name, as written. */
74
+ export declare function moduleCallNames(root: CelNode | undefined): readonly string[];
90
75
  /**
91
- * Bare identifiers used as a call receiver that resolved to NO module name.
76
+ * Bare identifiers that are the RECEIVER of an unresolved call (`dbb.query(1)`), so the
77
+ * caller reporting the root can ask whether such a name could denote a module at all rather
78
+ * than telling an author to add an `imports:` alias for a typo.
92
79
  *
93
- * `Foo.bar()` with no `Foo` import is an unknown identifier like any other, but
94
- * the repair is not the usual one — it needs an `imports:` alias, not a
95
- * different spelling — so the caller that reports the root says so.
80
+ * A call the name set resolved is a `qcall` and is not here — it names a module, so reading
81
+ * its receiver as a root would invent a dependency the expression never states.
96
82
  */
97
- export declare function unresolvedCallReceivers(ast: ASTNode | undefined, moduleNames?: ReadonlySet<string>): Set<string>;
83
+ export declare function unresolvedCallReceivers(root: CelNode | undefined, moduleNames?: ReadonlySet<string>): Set<string>;
98
84
  /**
99
- * Names bound INSIDE the expression — a comprehension variable, a `cel.bind`
100
- * name — that collide with one of the module's names.
85
+ * Names bound INSIDE the expression — a comprehension variable, a `cel.bind` name — that
86
+ * collide with one of the module's names.
87
+ *
88
+ * Such a binding is unreachable through a call: `Billing.f(x)` inside it resolves to the
89
+ * module, never to the bound value. Reported as a reserved name rather than silently
90
+ * shadowed, which is the rule every other name in CEL scope already follows.
101
91
  *
102
- * Such a binding is unreachable through a call: `Billing.f(x)` inside it
103
- * resolves to the module, never to the bound value. Reported as a reserved name
104
- * rather than silently shadowed, which is the rule every other name in CEL
105
- * scope already follows.
92
+ * The binding forms come from the engine's own enumeration (`BINDING_FORMS`) rather than a
93
+ * list kept in step with it here — the previous spelling carried a hand-written set of
94
+ * comprehension method names, mirrored in a second file.
106
95
  */
107
- export declare function moduleNameBindings(ast: ASTNode | undefined, moduleNames?: ReadonlySet<string>): string[];
96
+ export declare function moduleNameBindings(root: CelNode | undefined, moduleNames?: ReadonlySet<string>): string[];
108
97
  //# sourceMappingURL=module-call.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"module-call.d.ts","sourceRoot":"","sources":["../../src/cel/module-call.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,sBAAsB,CAAC;AAEpD;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,2BAA2B,CAAC;AAEjE,uEAAuE;AACvE,MAAM,MAAM,kBAAkB,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC;AAE5F;;;;;;;;;GASG;AACH,MAAM,MAAM,sBAAsB,GAAG,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;AAwC/E;;;yCAGyC;AACzC,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,CAAC;IAClC,QAAQ,CAAC,aAAa,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;CAC5C;AAED,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CASlE;AAED;;qCAEqC;AACrC,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CASlE;AAID;;;;;;;GAOG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,OAAO,GAAG,SAAS,EACxB,WAAW,GAAE,WAAW,CAAC,MAAM,CAAY,EAC3C,MAAM,CAAC,EAAE,sBAAsB,GAC9B,MAAM,EAAE,CAIV;AAiGD,0EAA0E;AAC1E,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,GAAG,SAAS,GAAG,KAAK,CAAC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,CAcpG;AASD;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,GAAG,EAAE,OAAO,GAAG,SAAS,EACxB,WAAW,GAAE,WAAW,CAAC,MAAM,CAAY,GAC1C,GAAG,CAAC,MAAM,CAAC,CAkBb;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CAChC,GAAG,EAAE,OAAO,GAAG,SAAS,EACxB,WAAW,GAAE,WAAW,CAAC,MAAM,CAAY,GAC1C,MAAM,EAAE,CAUV"}
1
+ {"version":3,"file":"module-call.d.ts","sourceRoot":"","sources":["../../src/cel/module-call.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,OAAO,EAQL,KAAK,OAAO,EACb,MAAM,cAAc,CAAC;AAEtB;;;;;;GAMG;AACH,eAAO,MAAM,wBAAwB,2BAA2B,CAAC;AAEjE,2DAA2D;AAC3D,MAAM,MAAM,kBAAkB,GAAG,WAAW,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,KAAK,OAAO,CAAC,CAAC;AAE5F;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG,CAAC,SAAS,EAAE,MAAM,KAAK,MAAM,GAAG,SAAS,CAAC;AAE/E,qDAAqD;AACrD,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,IAAI,EAAE,SAAS,OAAO,EAAE,CAAC;CACnC;AAED,oFAAoF;AACpF,wBAAgB,YAAY,CAAC,IAAI,EAAE,OAAO,GAAG,UAAU,GAAG,SAAS,CAGlE;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,MAAM,GAAG,MAAM,CASlE;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,iBAAiB,CAAC,WAAW,EAAE,QAAQ,CAAC,MAAM,CAAC,GAAG,WAAW,CAAC,MAAM,CAAC,CAQpF;AAID,6EAA6E;AAC7E,wBAAgB,eAAe,CAC7B,IAAI,EAAE,OAAO,GAAG,SAAS,GACxB,KAAK,CAAC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC,CAU5C;AAED,+CAA+C;AAC/C,wBAAgB,eAAe,CAAC,IAAI,EAAE,OAAO,GAAG,SAAS,GAAG,SAAS,MAAM,EAAE,CAE5E;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,IAAI,EAAE,OAAO,GAAG,SAAS,EACzB,WAAW,GAAE,WAAW,CAAC,MAAM,CAAY,GAC1C,GAAG,CAAC,MAAM,CAAC,CAeb;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,kBAAkB,CAChC,IAAI,EAAE,OAAO,GAAG,SAAS,EACzB,WAAW,GAAE,WAAW,CAAC,MAAM,CAAY,GAC1C,MAAM,EAAE,CAUV"}
@@ -1,29 +1,45 @@
1
1
  /**
2
- * Activation key the per-scope dispatch table is read from.
2
+ * Module calls: what a resolved one says, where the dispatch table hangs, and the two walks
3
+ * the analyzer asks of an expression's own names.
3
4
  *
4
- * `@` and `:` are outside CEL's identifier grammar and there is no other way to
5
- * reach the activation, so no expression can name this — a spellable dispatcher
6
- * would let an author call past the export gate and past the dependency edge
7
- * that orders the callee before its caller.
5
+ * **The rewrite is gone.** `Alias.fn(x)` and `obj.method(x)` are the same syntax and only a
6
+ * set of names distinguishes them, so this package used to rewrite the parsed tree itself,
7
+ * through internals the previous engine did not expose: a `setMeta('macro', …)` redirect
8
+ * that made a qualified call check and evaluate as something else, with a comprehension
9
+ * macro's `alternate` winning over `macro` unless cleared, and an rcall's asyncness derived
10
+ * from state the replaced checker filled in. Three facts about a dependency's internals,
11
+ * each pinned by a test, and the reason it was pinned to an exact version.
12
+ *
13
+ * `@telorun/cel` has **`qcall` as a node of its own**, produced by a total tree-to-tree pass
14
+ * over the name set — never by the parser, because the set is the host's — and a
15
+ * `CelExpression` records the set it was resolved under, so an environment refuses a tree
16
+ * resolved for a different site instead of answering a different question about it. So
17
+ * reading a module call is reading a node kind, and there is nothing to rewrite and nothing
18
+ * to pin.
19
+ *
20
+ * What is left here is Telo's half: the activation key the kernel binds a scope's table
21
+ * under, the unbound message, and the two questions the analyzer asks about a module's own
22
+ * names colliding with the expression's.
23
+ */
24
+ import { childNodes, isIdentifierSpelling, isReservedWord, namespaceMacroBinding, qualifiedCalls, receiverMacroBinding, RESERVED_NAMESPACES, } from "@telorun/cel";
25
+ /**
26
+ * Where a scope's dispatch table hangs on the activation.
27
+ *
28
+ * `@` and `:` are outside CEL's identifier grammar and the activation is reachable no other
29
+ * way, so no author can call past the export gate or the dependency edge. Binding the table
30
+ * is the kernel's, per owning scope; with none, evaluating a module call is refused.
8
31
  */
9
32
  export const MODULE_CALL_DISPATCH_KEY = "@telo:module-functions";
10
- /** cel-js's `ASTNode` is a union over its operators, so it has no statically
11
- * known members to extend — the internals are read through this cast, in one
12
- * place. */
13
- const metaOf = (node) => node;
33
+ /** The module call this node IS, or nothing. One node kind, no pattern-matching. */
14
34
  export function moduleCallOf(node) {
15
- const macro = metaOf(node).meta?.macro;
16
- return macro?.teloModuleCall === true
17
- ? {
18
- qualified: macro.qualified,
19
- args: macro.args,
20
- ...(macro.argumentTypes ? { argumentTypes: macro.argumentTypes } : {}),
21
- }
22
- : undefined;
35
+ if (node.kind !== "qcall")
36
+ return undefined;
37
+ return { qualified: `${node.namespace}.${node.name}`, args: node.args };
23
38
  }
24
- /** Message for a call nothing has bound. Not a fallback value: a call that
25
- * cannot reach its function has no result, and inventing one would make a
26
- * missing export read as a null. */
39
+ /**
40
+ * Message for a call nothing has bound. Not a fallback value: a call that cannot reach its
41
+ * function has no result, and inventing one would make a missing export read as a null.
42
+ */
27
43
  export function unboundModuleCallMessage(qualified) {
28
44
  const dot = qualified.indexOf(".");
29
45
  const receiver = qualified.slice(0, dot);
@@ -32,214 +48,122 @@ export function unboundModuleCallMessage(qualified) {
32
48
  `'${receiver}' must be an imports: alias (or Self, or this module's own name) whose ` +
33
49
  `module declares a callable named '${fn}' and exports it.`);
34
50
  }
35
- const NO_NAMES = new Set();
36
51
  /**
37
- * Rewrite every module call in `ast` in place and report them in source order.
52
+ * Which of a module's own names CEL can read as a namespace.
53
+ *
54
+ * **A module name and an import alias are YAML scalars nothing lexes where they are
55
+ * written** — `my-module`, `test-run` and `cel` all reach a name set, and the naming
56
+ * diagnostics (`INVALID_NAME`, `INVALID_TYPE_NAME`) are what report them. The engine refuses
57
+ * such a name at registration and refuses the SET WHOLE, so handing one over unfiltered
58
+ * turns a reportable name into a crash that loses every other diagnostic in the file. A name
59
+ * CEL cannot read as a namespace resolves no call, which is exactly what the naming
60
+ * diagnostic says is wrong with it.
38
61
  *
39
- * Returns the qualified names as written (`["Billing.format", "Self.y"]`), which
40
- * is what a compiled value carries so no consumer has to re-parse to learn what
41
- * an expression calls. Idempotent: a node already resolved is rewritten once and
42
- * reported again, so two passes over one tree agree about what it calls.
62
+ * It is here rather than in a consumer because the module-name set is this package's concept
63
+ * — `CompileEnv.moduleNames` — and three callers need the same answer: compiling an
64
+ * expression, the analyzer's typed site environments, and the kernel's type-rule conditions.
43
65
  */
44
- export function resolveModuleCalls(ast, moduleNames = NO_NAMES, typeOf) {
45
- const calls = [];
46
- if (ast && moduleNames.size > 0)
47
- visit(ast, moduleNames, typeOf, calls);
48
- return calls;
49
- }
50
- function visit(node, moduleNames, typeOf, calls) {
51
- // Already resolved: rewritten once, reported every time, so the return value
52
- // is what the TREE calls rather than what this pass happened to change.
53
- const resolved = moduleCallOf(node);
54
- if (resolved) {
55
- calls.push(resolved.qualified);
56
- for (const arg of resolved.args)
57
- visit(arg, moduleNames, typeOf, calls);
58
- return;
59
- }
60
- const receiver = moduleCallReceiver(node, moduleNames);
61
- if (receiver) {
62
- const [method, , args] = node.args;
63
- const qualified = `${receiver}.${method}`;
64
- calls.push(qualified);
65
- rewrite(node, qualified, args, typeOf);
66
- // The receiver names a MODULE, not a value, so it is not descended into:
67
- // that is what keeps it out of every access chain, out of `refs`, and out
68
- // of the undeclared-root check.
69
- for (const arg of args)
70
- visit(arg, moduleNames, typeOf, calls);
71
- return;
66
+ export function celNamespaceNames(moduleNames) {
67
+ const usable = new Set();
68
+ for (const name of moduleNames) {
69
+ if (!isIdentifierSpelling(name) || isReservedWord(name))
70
+ continue;
71
+ if (RESERVED_NAMESPACES.includes(name))
72
+ continue;
73
+ usable.add(name);
72
74
  }
73
- descend(node, (child) => visit(child, moduleNames, typeOf, calls));
74
- }
75
- /** The module name a node's receiver is, or undefined when the node is not a
76
- * module call. `a.b.c(x)` is not one — its receiver is a member access, not a
77
- * bare identifier. */
78
- function moduleCallReceiver(node, moduleNames) {
79
- if (node.op !== "rcall" || !Array.isArray(node.args))
80
- return undefined;
81
- const [method, receiver, args] = node.args;
82
- if (typeof method !== "string" || !Array.isArray(args))
83
- return undefined;
84
- if (!isNode(receiver) || receiver.op !== "id")
85
- return undefined;
86
- const name = receiver.args;
87
- return moduleNames.has(name) ? name : undefined;
75
+ return usable;
88
76
  }
89
- function rewrite(node, qualified, args, typeOf) {
90
- const macro = {
91
- teloModuleCall: true,
92
- qualified,
93
- args,
94
- typeCheck(chk, m, ctx) {
95
- // The arguments are checked in the CURRENT context, so a mistake inside
96
- // one is reported where it is written — and each argument's type is kept,
97
- // so whoever knows the callee's signature can compare against it.
98
- m.argumentTypes = m.args.map((arg) => typeNameOf(chk.check(arg, ctx)));
99
- // cel-js derives an rcall's asyncness from `receiverWithArgs`, which only
100
- // the rcall checker this macro replaces fills in — so it is stated: the
101
- // dispatch is synchronous, so the node is async exactly when one of its
102
- // arguments is. Asked only now, once each argument is checked: an
103
- // argument's answer is cached on first reading, and read before its own
104
- // check it is the conservative `true` for every operator and call.
105
- metaOf(node).setMeta("async", m.args.some((arg) => arg.maybeAsync === true));
106
- const declared = typeOf?.(m.qualified);
107
- return declared === undefined ? chk.dynType : chk.getType(declared);
108
- },
109
- evaluate(ev, m, ctx) {
110
- const table = ctx.getValue(MODULE_CALL_DISPATCH_KEY);
111
- const fn = table?.get(m.qualified);
112
- if (!fn)
113
- throw new Error(unboundModuleCallMessage(m.qualified));
114
- const values = m.args.map((arg) => ev.eval(arg, ctx));
115
- // A callable's `call` is synchronous; an ARGUMENT need not be, and every
116
- // other dispatch in cel-js settles its operands first. Handing a pending
117
- // Promise to the function instead would pass it a value of the wrong type
118
- // with nothing reported.
119
- return values.some((value) => value instanceof Promise)
120
- ? Promise.all(values).then((settled) => fn(settled))
121
- : fn(values);
122
- },
123
- };
124
- metaOf(node)
125
- // A comprehension macro expands into an `alternate`, which cel-js consults
126
- // BEFORE `macro`. Left in place, `Billing.map(i, i)` would evaluate as a
127
- // comprehension over an identifier rather than as the module call it names.
128
- .setMeta("alternate", undefined)
129
- .setMeta("macro", macro);
130
- }
131
- /** Every resolved module call in `ast` with its node, outermost first. */
132
- export function moduleCallNodes(ast) {
77
+ const NO_NAMES = new Set();
78
+ /** Every resolved module call in the tree with its node, outermost first. */
79
+ export function moduleCallNodes(root) {
133
80
  const out = [];
134
- if (ast)
135
- walk(ast);
81
+ if (root)
82
+ walk(root);
136
83
  return out;
137
84
  function walk(node) {
138
85
  const call = moduleCallOf(node);
139
- if (call) {
86
+ if (call)
140
87
  out.push({ node, call });
141
- for (const arg of call.args)
142
- walk(arg);
143
- return;
144
- }
145
- descend(node, walk);
88
+ for (const child of childNodes(node))
89
+ walk(child);
146
90
  }
147
91
  }
148
- /** The name of a checker type object (`int`, `list<string>`), or `dyn` for one
149
- * that carries none. */
150
- function typeNameOf(type) {
151
- const name = type?.name;
152
- return typeof name === "string" ? name : "dyn";
92
+ /** Every qualified call's name, as written. */
93
+ export function moduleCallNames(root) {
94
+ return root ? qualifiedCalls(root).map((call) => call.qualifiedName) : [];
153
95
  }
154
96
  /**
155
- * Bare identifiers used as a call receiver that resolved to NO module name.
97
+ * Bare identifiers that are the RECEIVER of an unresolved call (`dbb.query(1)`), so the
98
+ * caller reporting the root can ask whether such a name could denote a module at all rather
99
+ * than telling an author to add an `imports:` alias for a typo.
156
100
  *
157
- * `Foo.bar()` with no `Foo` import is an unknown identifier like any other, but
158
- * the repair is not the usual one — it needs an `imports:` alias, not a
159
- * different spelling — so the caller that reports the root says so.
101
+ * A call the name set resolved is a `qcall` and is not here — it names a module, so reading
102
+ * its receiver as a root would invent a dependency the expression never states.
160
103
  */
161
- export function unresolvedCallReceivers(ast, moduleNames = NO_NAMES) {
104
+ export function unresolvedCallReceivers(root, moduleNames = NO_NAMES) {
162
105
  const out = new Set();
163
- if (ast)
164
- walk(ast);
106
+ if (root)
107
+ walk(root);
165
108
  return out;
166
109
  function walk(node) {
167
- if (moduleCallOf(node)) {
168
- for (const arg of node.args[2])
169
- walk(arg);
170
- return;
171
- }
172
- if (node.op === "rcall" && Array.isArray(node.args)) {
173
- const receiver = node.args[1];
174
- if (isNode(receiver) && receiver.op === "id" && !moduleNames.has(receiver.args)) {
175
- out.add(receiver.args);
176
- }
110
+ if (node.kind === "receiverCall" &&
111
+ node.receiver.kind === "ident" &&
112
+ !moduleNames.has(node.receiver.name)) {
113
+ out.add(node.receiver.name);
177
114
  }
178
- descend(node, walk);
115
+ for (const child of childNodes(node))
116
+ walk(child);
179
117
  }
180
118
  }
181
119
  /**
182
- * Names bound INSIDE the expression — a comprehension variable, a `cel.bind`
183
- * name — that collide with one of the module's names.
120
+ * Names bound INSIDE the expression — a comprehension variable, a `cel.bind` name — that
121
+ * collide with one of the module's names.
184
122
  *
185
- * Such a binding is unreachable through a call: `Billing.f(x)` inside it
186
- * resolves to the module, never to the bound value. Reported as a reserved name
187
- * rather than silently shadowed, which is the rule every other name in CEL
188
- * scope already follows.
123
+ * Such a binding is unreachable through a call: `Billing.f(x)` inside it resolves to the
124
+ * module, never to the bound value. Reported as a reserved name rather than silently
125
+ * shadowed, which is the rule every other name in CEL scope already follows.
126
+ *
127
+ * The binding forms come from the engine's own enumeration (`BINDING_FORMS`) rather than a
128
+ * list kept in step with it here — the previous spelling carried a hand-written set of
129
+ * comprehension method names, mirrored in a second file.
189
130
  */
190
- export function moduleNameBindings(ast, moduleNames = NO_NAMES) {
131
+ export function moduleNameBindings(root, moduleNames = NO_NAMES) {
191
132
  const out = [];
192
- if (ast && moduleNames.size > 0)
193
- walk(ast);
133
+ if (root && moduleNames.size > 0)
134
+ walk(root);
194
135
  return [...new Set(out)];
195
136
  function walk(node) {
196
137
  const bound = boundNameOf(node);
197
138
  if (bound !== undefined && moduleNames.has(bound))
198
139
  out.push(bound);
199
- descend(node, walk);
140
+ for (const child of childNodes(node))
141
+ walk(child);
200
142
  }
201
143
  }
202
- /** The name a binding form introduces: `cel.bind(<name>, …)`'s first argument,
203
- * or a comprehension macro's iteration variable. */
144
+ /**
145
+ * The name a binding form introduces: a comprehension macro's iteration variable, or
146
+ * `cel.bind(<name>, …)`'s first argument.
147
+ *
148
+ * **The binding is asked of the engine by NAME AND ARITY.** `BINDING_FORMS` is a list of
149
+ * `"map/2"`, `"map/3"`, `"cel.bind/3"` — a bare `"map"` matches none of them, so a set over
150
+ * the list and a `.has(node.name)` test silently never fires.
151
+ *
152
+ * **And `cel` is a RESERVED namespace** (`cel`, `optional`), which a host can never claim —
153
+ * so `cel.bind(c, …)` is not a qualified call at all: it reads as a receiver call on the
154
+ * ident `cel`, which is in no scope and never will be. Reading it as a `qcall` matches
155
+ * nothing, and the cost is not just a missed binding: the bound name and the bare `cel` then
156
+ * leak out as member-access chains, so a comprehension variable is reported as an undeclared
157
+ * field and `cel` itself as an unknown identifier.
158
+ */
204
159
  function boundNameOf(node) {
205
- if (node.op !== "rcall" || !Array.isArray(node.args))
206
- return undefined;
207
- if (moduleCallOf(node))
160
+ if (node.kind !== "receiverCall")
208
161
  return undefined;
209
- const [method, receiver, args] = node.args;
210
- if (!Array.isArray(args) || args.length === 0)
162
+ const binding = node.receiver.kind === "ident" && node.receiver.name === "cel"
163
+ ? namespaceMacroBinding("cel", node.name, node.args.length)
164
+ : receiverMacroBinding(node.name, node.args.length);
165
+ if (!binding)
211
166
  return undefined;
212
- const first = args[0];
213
- if (!isNode(first) || first.op !== "id")
214
- return undefined;
215
- if (method === "bind") {
216
- return isNode(receiver) && receiver.op === "id" && receiver.args === "cel"
217
- ? first.args
218
- : undefined;
219
- }
220
- return COMPREHENSION_METHODS.has(method) ? first.args : undefined;
221
- }
222
- /** cel-js's comprehension macros, which bind their first argument as the
223
- * iteration variable. Mirrors the set `analyze.ts` walks with. */
224
- const COMPREHENSION_METHODS = new Set(["filter", "map", "exists", "all", "exists_one"]);
225
- /** Generic descent over a node's operands — an array, a single node, or an
226
- * array of arrays (a map literal's entries). */
227
- function descend(node, fn) {
228
- const args = node.args;
229
- if (Array.isArray(args)) {
230
- for (const arg of args) {
231
- if (isNode(arg))
232
- fn(arg);
233
- else if (Array.isArray(arg))
234
- for (const item of arg)
235
- if (isNode(item))
236
- fn(item);
237
- }
238
- return;
239
- }
240
- if (isNode(args))
241
- fn(args);
242
- }
243
- function isNode(v) {
244
- return v !== null && typeof v === "object" && "op" in v;
167
+ const held = node.args[binding.variableArgument];
168
+ return held?.kind === "ident" ? held.name : undefined;
245
169
  }
@@ -0,0 +1,18 @@
1
+ import type { CelEnvironment } from "@telorun/cel";
2
+ /**
3
+ * Register every nominal value brand on an analysis environment, with what a brand may do:
4
+ * convert to its base with the base's own conversion (`int(ports.http)`,
5
+ * `string(variables.dataDir)`), render as text (`string(ports.http)`), and — for a host
6
+ * path — be extended with `.joinPath(relative)` and stay one.
7
+ *
8
+ * **Declared, not implemented.** A nominal type is a checking concept: the engine takes the
9
+ * conversions and members as signatures over `Self` and needs no function body for any of
10
+ * them, because at runtime a branded value IS its base (an int, a string) and the standard
11
+ * library's own overloads are what evaluate. The previous engine required an implementation
12
+ * per registration, so this file carried three stubs a brand's values never reached.
13
+ *
14
+ * `buildCelEnvironment` registers none of it: a caller typing a site registers the brands on
15
+ * its own clone.
16
+ */
17
+ export declare function registerValueBrands(environment: CelEnvironment): void;
18
+ //# sourceMappingURL=value-brands.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value-brands.d.ts","sourceRoot":"","sources":["../../src/cel/value-brands.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAGnD;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,mBAAmB,CAAC,WAAW,EAAE,cAAc,GAAG,IAAI,CAerE"}
@@ -0,0 +1,33 @@
1
+ import { VALUE_TYPES, valueBrandBases } from "@telorun/sdk";
2
+ /**
3
+ * Register every nominal value brand on an analysis environment, with what a brand may do:
4
+ * convert to its base with the base's own conversion (`int(ports.http)`,
5
+ * `string(variables.dataDir)`), render as text (`string(ports.http)`), and — for a host
6
+ * path — be extended with `.joinPath(relative)` and stay one.
7
+ *
8
+ * **Declared, not implemented.** A nominal type is a checking concept: the engine takes the
9
+ * conversions and members as signatures over `Self` and needs no function body for any of
10
+ * them, because at runtime a branded value IS its base (an int, a string) and the standard
11
+ * library's own overloads are what evaluate. The previous engine required an implementation
12
+ * per registration, so this file carried three stubs a brand's values never reached.
13
+ *
14
+ * `buildCelEnvironment` registers none of it: a caller typing a site registers the brands on
15
+ * its own clone.
16
+ */
17
+ export function registerValueBrands(environment) {
18
+ for (const [brand, base] of Object.entries(valueBrandBases())) {
19
+ const conversions = [`${base}(Self): ${base}`];
20
+ // Every base CEL can render as text renders its brand too — the runtime value IS the
21
+ // base — so `string(ports.http)` and a port in an `!interpolate` hole convert as the
22
+ // integer they are.
23
+ if (base !== "string")
24
+ conversions.push("string(Self): string");
25
+ const fromHost = VALUE_TYPES.get(brand)?.fromHost !== undefined;
26
+ environment.registerType({
27
+ name: brand,
28
+ base,
29
+ conversions,
30
+ ...(fromHost ? { members: ["Self.joinPath(string): Self"] } : {}),
31
+ });
32
+ }
33
+ }