metano-runtime 2.1.0 → 2.3.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 +139 -0
- package/dist/system/equality.d.ts +8 -0
- package/dist/system/equality.js +13 -5
- package/dist/system/json/index.d.ts +1 -0
- package/dist/system/json/index.js +1 -0
- package/dist/system/json/union-guard-registry.d.ts +30 -0
- package/dist/system/json/union-guard-registry.js +34 -0
- package/dist/system/linq/compare-keys.d.ts +3 -2
- package/dist/system/linq/compare-keys.js +9 -7
- package/dist/system/linq/expr-tree-visitor.d.ts +173 -0
- package/dist/system/linq/expr-tree-visitor.js +417 -0
- package/dist/system/linq/expr-tree.d.ts +156 -0
- package/dist/system/linq/expr-tree.js +18 -0
- package/dist/system/linq/index.d.ts +29 -23
- package/dist/system/linq/index.js +25 -70
- package/dist/system/linq/linq.d.ts +107 -0
- package/dist/system/linq/linq.js +46 -0
- package/dist/system/linq/operators.d.ts +34 -0
- package/dist/system/linq/operators.js +352 -0
- package/dist/system/linq/terminals.d.ts +22 -0
- package/dist/system/linq/terminals.js +314 -0
- package/dist/system/linq/types.d.ts +249 -0
- package/dist/system/linq/types.js +0 -0
- package/package.json +2 -1
- package/dist/system/linq/append-enumerable.d.ts +0 -8
- package/dist/system/linq/append-enumerable.js +0 -15
- package/dist/system/linq/concat-enumerable.d.ts +0 -8
- package/dist/system/linq/concat-enumerable.js +0 -15
- package/dist/system/linq/distinct-by-enumerable.d.ts +0 -8
- package/dist/system/linq/distinct-by-enumerable.js +0 -21
- package/dist/system/linq/distinct-enumerable.d.ts +0 -7
- package/dist/system/linq/distinct-enumerable.js +0 -19
- package/dist/system/linq/enumerable-base.d.ts +0 -98
- package/dist/system/linq/enumerable-base.js +0 -306
- package/dist/system/linq/enumerable.d.ts +0 -16
- package/dist/system/linq/enumerable.js +0 -33
- package/dist/system/linq/except-enumerable.d.ts +0 -8
- package/dist/system/linq/except-enumerable.js +0 -22
- package/dist/system/linq/group-by-enumerable.d.ts +0 -8
- package/dist/system/linq/group-by-enumerable.js +0 -31
- package/dist/system/linq/intersect-enumerable.d.ts +0 -8
- package/dist/system/linq/intersect-enumerable.js +0 -22
- package/dist/system/linq/order-by-enumerable.d.ts +0 -9
- package/dist/system/linq/order-by-enumerable.js +0 -22
- package/dist/system/linq/prepend-enumerable.d.ts +0 -8
- package/dist/system/linq/prepend-enumerable.js +0 -15
- package/dist/system/linq/reverse-enumerable.d.ts +0 -7
- package/dist/system/linq/reverse-enumerable.js +0 -15
- package/dist/system/linq/select-enumerable.d.ts +0 -8
- package/dist/system/linq/select-enumerable.js +0 -17
- package/dist/system/linq/select-many-enumerable.d.ts +0 -8
- package/dist/system/linq/select-many-enumerable.js +0 -17
- package/dist/system/linq/skip-enumerable.d.ts +0 -8
- package/dist/system/linq/skip-enumerable.js +0 -21
- package/dist/system/linq/skip-while-enumerable.d.ts +0 -8
- package/dist/system/linq/skip-while-enumerable.js +0 -21
- package/dist/system/linq/source-enumerable.d.ts +0 -7
- package/dist/system/linq/source-enumerable.js +0 -12
- package/dist/system/linq/take-enumerable.d.ts +0 -8
- package/dist/system/linq/take-enumerable.js +0 -20
- package/dist/system/linq/take-while-enumerable.d.ts +0 -8
- package/dist/system/linq/take-while-enumerable.js +0 -19
- package/dist/system/linq/then-by-enumerable.d.ts +0 -9
- package/dist/system/linq/then-by-enumerable.js +0 -61
- package/dist/system/linq/union-enumerable.d.ts +0 -8
- package/dist/system/linq/union-enumerable.js +0 -27
- package/dist/system/linq/where-enumerable.d.ts +0 -8
- package/dist/system/linq/where-enumerable.js +0 -18
- package/dist/system/linq/zip-enumerable.d.ts +0 -9
- package/dist/system/linq/zip-enumerable.js +0 -24
package/README.md
CHANGED
|
@@ -65,6 +65,145 @@ const result = Enumerable.from([1, 2, 3, 4, 5])
|
|
|
65
65
|
`lastOrDefault`, `single`, `singleOrDefault`, `any`, `all`, `count`, `sum`, `average`,
|
|
66
66
|
`min`, `max`, `minBy`, `maxBy`, `contains`, `aggregate`
|
|
67
67
|
|
|
68
|
+
### `linq()` — descriptor-based pipeline + provider introspection
|
|
69
|
+
|
|
70
|
+
For IQueryable-style scenarios, the runtime also exposes a tagged-descriptor
|
|
71
|
+
pipeline: each operator (`where`, `select`, `orderBy`, …) is a standalone
|
|
72
|
+
factory that returns a `{ kind, …closures, apply, queryable? }` descriptor.
|
|
73
|
+
`linq(source, ...stages)` runs the chain by calling each stage's `apply`,
|
|
74
|
+
returning a lazy `Iterable<T>` (or a scalar, for terminal-bearing chains).
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { getStages, linq, select, where } from "metano-runtime";
|
|
78
|
+
|
|
79
|
+
const query = linq(
|
|
80
|
+
users,
|
|
81
|
+
where((u) => u.age >= 18),
|
|
82
|
+
select((u) => u.name),
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
for (const name of query) {
|
|
86
|
+
console.log(name);
|
|
87
|
+
}
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
When the result is an object (the lazy `Iterable<T>` from a composition chain,
|
|
91
|
+
or any object-typed terminal result like `toArray`), the runtime attaches the
|
|
92
|
+
descriptor list at a non-enumerable symbol-keyed slot — `LINQ_STAGES`,
|
|
93
|
+
registered via `Symbol.for("metano-runtime/system/linq:stages")`. An
|
|
94
|
+
introspecting consumer (IQueryable provider, query planner, debugger) reads
|
|
95
|
+
it via `getStages`:
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { getStages, linq, select, where } from "metano-runtime";
|
|
99
|
+
|
|
100
|
+
const query = linq(items, where((x) => x.active), select((x) => x.name));
|
|
101
|
+
const stages = getStages(query); // [WhereOp, SelectOp] | undefined
|
|
102
|
+
|
|
103
|
+
if (stages) {
|
|
104
|
+
// walk `stages[i].queryable` (ExprTree) to translate into SQL / GraphQL / …
|
|
105
|
+
}
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Contract:
|
|
109
|
+
|
|
110
|
+
- The slot is **non-enumerable**, so `JSON.stringify`, `{ ...spread }`, and
|
|
111
|
+
`Object.keys` do not leak the chain into the wire shape.
|
|
112
|
+
- `getStages` returns `undefined` for primitive results (scalar terminals
|
|
113
|
+
like `count` / `sum`) and for values produced outside this runtime.
|
|
114
|
+
- The slot is scoped via `Symbol.for`, so multiple module copies (duplicate
|
|
115
|
+
npm resolutions) share the same key — `getStages` from any copy sees the
|
|
116
|
+
stages attached by any other.
|
|
117
|
+
- The closure side-channel (`stage.apply`) keeps working in parallel; a
|
|
118
|
+
provider that cannot introspect a given stage may fall back to it.
|
|
119
|
+
|
|
120
|
+
### `ExprTree` — queryable expression trees
|
|
121
|
+
|
|
122
|
+
When a LINQ chain opts into queryable mode (`IQueryable<T>` receiver or a
|
|
123
|
+
`[Queryable]` parameter on the C# side), the transpiler emits a paired
|
|
124
|
+
expression tree (`QueryableMeta`) alongside the closure. Providers walk the
|
|
125
|
+
tree by `kind` to translate predicates into SQL, OData, GraphQL, etc.
|
|
126
|
+
|
|
127
|
+
Supported node kinds today: `param`, `capture`, `literal`, `member`, `call`,
|
|
128
|
+
`binary`, `unary`, `conditional`, `new` (object-initializer projections
|
|
129
|
+
such as `select(u => new UserDto { Name = u.Name })`), and `lambda`
|
|
130
|
+
(nested arrows like `where(u => u.orders.some(o => o.active))`).
|
|
131
|
+
|
|
132
|
+
Param, capture, literal, and `new` nodes carry an optional **qualified**
|
|
133
|
+
`type` field shaped `{ name, from? }` — `name` is the simple identifier
|
|
134
|
+
the provider dispatches on, and `from` is the cross-package origin (the
|
|
135
|
+
`[EmitPackage]` id) when the type lives in another package. Local and
|
|
136
|
+
primitive types omit `from`. This disambiguates same-named types defined
|
|
137
|
+
in different packages without forcing providers to parse module paths.
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
// where((p: Product) => p.unitPrice >= minPrice)
|
|
141
|
+
//
|
|
142
|
+
// `Product` lives in another package consumed via [EmitPackage("inventory")];
|
|
143
|
+
// `number` is a primitive (no origin), so `from` is omitted.
|
|
144
|
+
{
|
|
145
|
+
kind: "binary",
|
|
146
|
+
op: ">=",
|
|
147
|
+
left: {
|
|
148
|
+
kind: "member",
|
|
149
|
+
target: { kind: "param", name: "p", type: { name: "Product", from: "inventory" } },
|
|
150
|
+
member: "unitPrice",
|
|
151
|
+
},
|
|
152
|
+
right: { kind: "capture", name: "minPrice", type: { name: "number" } },
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### `ExprTreeVisitor` — generic walker for IQueryable provider authors
|
|
157
|
+
|
|
158
|
+
`metano-runtime/system/linq` ships a stable visitor surface so providers
|
|
159
|
+
that translate `QueryableMeta` into another dialect (SQL, GraphQL, OData,
|
|
160
|
+
HTTP query strings) do **not** need to reimplement parameter scoping, the
|
|
161
|
+
recursive walker, captures lookup, or the primitive comparator. Three
|
|
162
|
+
entry points cover the common cases:
|
|
163
|
+
|
|
164
|
+
- `evaluateExprTree(tree, scope)` — pure JS evaluation. Default behavior
|
|
165
|
+
for mock / in-memory providers; resolves the param, looks up captures,
|
|
166
|
+
short-circuits boolean ops, mirrors JS semantics on arithmetic.
|
|
167
|
+
- `compileLambdaBody(meta, fallbackParamName?)` — wraps the evaluator
|
|
168
|
+
into a `(item) => unknown` predicate by closing over `meta.captures`.
|
|
169
|
+
Pass `fallbackParamName` (defaults to `"x"`) for literal-only bodies
|
|
170
|
+
that never mention the param.
|
|
171
|
+
- `ExprTreeVisitor<R>` — abstract base with one `visitX` method per node
|
|
172
|
+
kind. Default implementations recurse, so subclasses override only the
|
|
173
|
+
nodes they translate. Helpers `readParamName` and `compareValues` are
|
|
174
|
+
exported for providers that bypass the visitor.
|
|
175
|
+
|
|
176
|
+
```typescript
|
|
177
|
+
import {
|
|
178
|
+
ExprTreeVisitor,
|
|
179
|
+
type ExprBinary,
|
|
180
|
+
type ExprLiteral,
|
|
181
|
+
type ExprMember,
|
|
182
|
+
type ExprParam,
|
|
183
|
+
} from "metano-runtime";
|
|
184
|
+
|
|
185
|
+
class SqlPredicate extends ExprTreeVisitor<string> {
|
|
186
|
+
protected override visitParam(node: ExprParam): string {
|
|
187
|
+
return node.name;
|
|
188
|
+
}
|
|
189
|
+
protected override visitLiteral(node: ExprLiteral): string {
|
|
190
|
+
return JSON.stringify(node.value);
|
|
191
|
+
}
|
|
192
|
+
protected override visitMember(node: ExprMember): string {
|
|
193
|
+
return `${this.visit(node.target)}.${node.member}`;
|
|
194
|
+
}
|
|
195
|
+
protected override visitBinary(node: ExprBinary): string {
|
|
196
|
+
return `(${this.visit(node.left)} ${node.op} ${this.visit(node.right)})`;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// new SqlPredicate().visit(meta.tree) → "(u.age >= 18)"
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
The array-backed sample (`targets/js/sample-queryable-arrays`) uses
|
|
204
|
+
`compileLambdaBody` directly — its provider is now ~50 LOC of stage
|
|
205
|
+
dispatch with all generic walker concerns delegated to the runtime.
|
|
206
|
+
|
|
68
207
|
### `UUID` — branded UUID type
|
|
69
208
|
|
|
70
209
|
A branded primitive that `System.Guid` maps to. At runtime it's literally a
|
|
@@ -36,3 +36,11 @@ export declare function equals(a: unknown, b: unknown): boolean;
|
|
|
36
36
|
* Cycle handling matches `equals` — undefined behavior on cyclic graphs.
|
|
37
37
|
*/
|
|
38
38
|
export declare function hashCode(value: unknown): number;
|
|
39
|
+
/**
|
|
40
|
+
* Alias the compiler emits inside synthesized record `equals` methods.
|
|
41
|
+
* Same contract as {@link equals}; the rename keeps the call
|
|
42
|
+
* sites readable next to the surrounding `equals(other)` method
|
|
43
|
+
* declaration (a bare `equals(this.x, other.x)` would shadow into a
|
|
44
|
+
* recursive method-call ambiguity for human readers).
|
|
45
|
+
*/
|
|
46
|
+
export declare const valueEquals: typeof equals;
|
package/dist/system/equality.js
CHANGED
|
@@ -3,11 +3,11 @@
|
|
|
3
3
|
* Mirror the C# .NET semantics: structural equality for compounds, NaN-equals-NaN for
|
|
4
4
|
* floats, custom `equals(other)` / `hashCode()` methods on user types take precedence.
|
|
5
5
|
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
6
|
+
* The compiler also calls into {@link valueEquals} from synthesized record
|
|
7
|
+
* `equals` methods whenever a constructor parameter has a non-primitive type that
|
|
8
|
+
* exposes a value-equality contract (Decimal, Temporal types, nested records, …).
|
|
9
|
+
* The previous lowering used `===` for every field which silently diverged from C#
|
|
10
|
+
* semantics for value-wrapper types — see #202.
|
|
11
11
|
*/
|
|
12
12
|
import { HashCode } from "./hash-code.js";
|
|
13
13
|
/**
|
|
@@ -176,3 +176,11 @@ export function hashCode(value) {
|
|
|
176
176
|
// Primitives.
|
|
177
177
|
return HashCode.combine(value);
|
|
178
178
|
}
|
|
179
|
+
/**
|
|
180
|
+
* Alias the compiler emits inside synthesized record `equals` methods.
|
|
181
|
+
* Same contract as {@link equals}; the rename keeps the call
|
|
182
|
+
* sites readable next to the surrounding `equals(other)` method
|
|
183
|
+
* declaration (a bare `equals(this.x, other.x)` would shadow into a
|
|
184
|
+
* recursive method-call ambiguity for human readers).
|
|
185
|
+
*/
|
|
186
|
+
export const valueEquals = equals;
|
|
@@ -3,3 +3,4 @@ export { SerializerContext } from "./serializer-context.ts";
|
|
|
3
3
|
export { JsonSerializer, BoundSerializer } from "./json-serializer.ts";
|
|
4
4
|
export { PropertyNamingPolicy } from "./property-naming-policy.ts";
|
|
5
5
|
export { collectProperties } from "./converters.ts";
|
|
6
|
+
export { UnionGuardRegistry, registerUnionGuard, getUnionGuard, } from "./union-guard-registry.ts";
|
|
@@ -2,3 +2,4 @@ export { SerializerContext } from "./serializer-context.js";
|
|
|
2
2
|
export { JsonSerializer, BoundSerializer } from "./json-serializer.js";
|
|
3
3
|
export { PropertyNamingPolicy } from "./property-naming-policy.js";
|
|
4
4
|
export { collectProperties } from "./converters.js";
|
|
5
|
+
export { UnionGuardRegistry, registerUnionGuard, getUnionGuard, } from "./union-guard-registry.js";
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Late-bound dispatch table backing the `[StrictUnionGuard]` opt-in path
|
|
3
|
+
* for sealed-hierarchy guards.
|
|
4
|
+
*
|
|
5
|
+
* Generated code for an abstract base with `[StrictUnionGuard]` looks
|
|
6
|
+
* the variant guard up by discriminator value and delegates to it,
|
|
7
|
+
* giving callers full shape validation without forcing the base file to
|
|
8
|
+
* import every variant module (which would create an ESM cycle).
|
|
9
|
+
*
|
|
10
|
+
* Each variant module registers its guard at load time via
|
|
11
|
+
* `registerUnionGuard("Circle", isCircle)`. If a discriminator value has
|
|
12
|
+
* no entry — e.g., the variant module hasn't been loaded yet — the
|
|
13
|
+
* generated base guard falls back to accepting the discriminator-only
|
|
14
|
+
* narrow so the strict path is never more restrictive than the default.
|
|
15
|
+
*/
|
|
16
|
+
export declare const UnionGuardRegistry: Map<string, (value: unknown) => boolean>;
|
|
17
|
+
/**
|
|
18
|
+
* Registers a variant guard under its discriminator value. Idempotent:
|
|
19
|
+
* a re-registration overwrites the previous entry, which lets HMR and
|
|
20
|
+
* test fixtures replace a guard without resetting the whole map.
|
|
21
|
+
*/
|
|
22
|
+
export declare function registerUnionGuard(discriminator: string, guard: (value: unknown) => boolean): void;
|
|
23
|
+
/**
|
|
24
|
+
* Returns the variant guard registered for the given discriminator
|
|
25
|
+
* value, or `undefined` when no variant module has registered for it.
|
|
26
|
+
* Callers should treat the missing case as "fall back to a looser
|
|
27
|
+
* narrow" — never as a hard failure — so the strict path degrades
|
|
28
|
+
* gracefully when variant modules aren't loaded.
|
|
29
|
+
*/
|
|
30
|
+
export declare function getUnionGuard(discriminator: string): ((value: unknown) => boolean) | undefined;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Late-bound dispatch table backing the `[StrictUnionGuard]` opt-in path
|
|
3
|
+
* for sealed-hierarchy guards.
|
|
4
|
+
*
|
|
5
|
+
* Generated code for an abstract base with `[StrictUnionGuard]` looks
|
|
6
|
+
* the variant guard up by discriminator value and delegates to it,
|
|
7
|
+
* giving callers full shape validation without forcing the base file to
|
|
8
|
+
* import every variant module (which would create an ESM cycle).
|
|
9
|
+
*
|
|
10
|
+
* Each variant module registers its guard at load time via
|
|
11
|
+
* `registerUnionGuard("Circle", isCircle)`. If a discriminator value has
|
|
12
|
+
* no entry — e.g., the variant module hasn't been loaded yet — the
|
|
13
|
+
* generated base guard falls back to accepting the discriminator-only
|
|
14
|
+
* narrow so the strict path is never more restrictive than the default.
|
|
15
|
+
*/
|
|
16
|
+
export const UnionGuardRegistry = new Map();
|
|
17
|
+
/**
|
|
18
|
+
* Registers a variant guard under its discriminator value. Idempotent:
|
|
19
|
+
* a re-registration overwrites the previous entry, which lets HMR and
|
|
20
|
+
* test fixtures replace a guard without resetting the whole map.
|
|
21
|
+
*/
|
|
22
|
+
export function registerUnionGuard(discriminator, guard) {
|
|
23
|
+
UnionGuardRegistry.set(discriminator, guard);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Returns the variant guard registered for the given discriminator
|
|
27
|
+
* value, or `undefined` when no variant module has registered for it.
|
|
28
|
+
* Callers should treat the missing case as "fall back to a looser
|
|
29
|
+
* narrow" — never as a hard failure — so the strict path degrades
|
|
30
|
+
* gracefully when variant modules aren't loaded.
|
|
31
|
+
*/
|
|
32
|
+
export function getUnionGuard(discriminator) {
|
|
33
|
+
return UnionGuardRegistry.get(discriminator);
|
|
34
|
+
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generic key comparator that handles primitives, Temporal types, and
|
|
3
|
-
* objects with a custom
|
|
3
|
+
* objects with a custom `compare()` method (or whose constructor exposes
|
|
4
|
+
* a static `compare(a, b)` — Temporal API shape).
|
|
4
5
|
*
|
|
5
|
-
* Returns negative
|
|
6
|
+
* Returns negative when `a < b`, positive when `a > b`, zero when equal.
|
|
6
7
|
*/
|
|
7
8
|
export declare function compareKeys(a: unknown, b: unknown): number;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Generic key comparator that handles primitives, Temporal types, and
|
|
3
|
-
* objects with a custom
|
|
3
|
+
* objects with a custom `compare()` method (or whose constructor exposes
|
|
4
|
+
* a static `compare(a, b)` — Temporal API shape).
|
|
4
5
|
*
|
|
5
|
-
* Returns negative
|
|
6
|
+
* Returns negative when `a < b`, positive when `a > b`, zero when equal.
|
|
6
7
|
*/
|
|
7
8
|
export function compareKeys(a, b) {
|
|
8
9
|
if (a === b)
|
|
@@ -11,18 +12,19 @@ export function compareKeys(a, b) {
|
|
|
11
12
|
return b == null ? 0 : -1;
|
|
12
13
|
if (b == null)
|
|
13
14
|
return 1;
|
|
14
|
-
//
|
|
15
|
-
|
|
15
|
+
// Object with instance compareTo() / compare() — common .NET-style
|
|
16
|
+
// ergonomics surface when the consumer ports a C# IComparable.
|
|
17
|
+
if (typeof a === "object" && typeof a.compare === "function") {
|
|
16
18
|
return a.compare(b);
|
|
17
19
|
}
|
|
18
|
-
// Temporal types expose static compare()
|
|
19
|
-
if (typeof a === "object"
|
|
20
|
+
// Temporal types expose static compare() on the constructor.
|
|
21
|
+
if (typeof a === "object") {
|
|
20
22
|
const ctor = a.constructor;
|
|
21
23
|
if (ctor && typeof ctor.compare === "function") {
|
|
22
24
|
return ctor.compare(a, b);
|
|
23
25
|
}
|
|
24
26
|
}
|
|
25
|
-
// Primitives (string, number, bigint, boolean):
|
|
27
|
+
// Primitives (string, number, bigint, boolean): default `<` / `>`.
|
|
26
28
|
if (a < b)
|
|
27
29
|
return -1;
|
|
28
30
|
if (a > b)
|
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic visitor surface over {@link ExprTree} — the IR shape the
|
|
3
|
+
* compiler emits next to every `[Queryable]` lambda. IQueryable
|
|
4
|
+
* provider authors extend this layer to translate the tree into SQL,
|
|
5
|
+
* GraphQL, OData, HTTP query strings, or any other dialect; they do
|
|
6
|
+
* **not** need to reimplement the recursive walker, parameter scoping,
|
|
7
|
+
* or the primitive comparator.
|
|
8
|
+
*
|
|
9
|
+
* Three entry points cover the common cases:
|
|
10
|
+
*
|
|
11
|
+
* - {@link evaluateExprTree} — pure JS evaluator. Resolves the param,
|
|
12
|
+
* reads captures, applies operators with the same semantics the
|
|
13
|
+
* closure path would have used. Mock / in-memory providers can use
|
|
14
|
+
* it as-is.
|
|
15
|
+
* - {@link compileLambdaBody} — wraps {@link evaluateExprTree} into a
|
|
16
|
+
* `(item) => unknown` predicate/selector by closing over the
|
|
17
|
+
* captures bundle and the lambda's first parameter name.
|
|
18
|
+
* - {@link ExprTreeVisitor} — abstract base with one `visitX` per node
|
|
19
|
+
* kind. The default implementations recurse, so subclasses override
|
|
20
|
+
* only the nodes they care about (e.g. a SQL emitter overrides
|
|
21
|
+
* {@link ExprTreeVisitor.visitBinary} to produce operators).
|
|
22
|
+
*
|
|
23
|
+
* The scope helpers ({@link readParamName}, {@link compareValues}) are
|
|
24
|
+
* exported because real providers usually need a parameter alias and a
|
|
25
|
+
* heterogeneous primitive ordering that matches the JS engine.
|
|
26
|
+
*/
|
|
27
|
+
import type { ExprBinary, ExprCall, ExprCapture, ExprConditional, ExprLambda, ExprLet, ExprLiteral, ExprMember, ExprNew, ExprParam, ExprRef, ExprTree, ExprUnary, QueryableMeta } from "./expr-tree.ts";
|
|
28
|
+
/**
|
|
29
|
+
* Lexical scope passed to {@link evaluateExprTree}. Mirrors the way
|
|
30
|
+
* the closure path resolved the lambda body: the param name is bound
|
|
31
|
+
* to the current item, captures are looked up by name in a flat
|
|
32
|
+
* record (the same shape the compiler emits in `QueryableMeta`).
|
|
33
|
+
*
|
|
34
|
+
* `refs` carries the values introduced by enclosing {@link ExprLet}
|
|
35
|
+
* bindings — the evaluator pushes one entry per binding when it
|
|
36
|
+
* descends into a `let` node and looks `ref` nodes up against this
|
|
37
|
+
* record. Callers normally leave it `undefined`; the evaluator
|
|
38
|
+
* materializes it on demand.
|
|
39
|
+
*/
|
|
40
|
+
export interface ExprTreeScope {
|
|
41
|
+
readonly paramName: string;
|
|
42
|
+
readonly paramValue: unknown;
|
|
43
|
+
readonly captures: Readonly<Record<string, unknown>>;
|
|
44
|
+
readonly refs?: Readonly<Record<string, unknown>>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Recursive scan for the first {@link ExprParam} node bound at the
|
|
48
|
+
* *current* lambda level. Visits every child of every node kind so a
|
|
49
|
+
* predicate that mentions the parameter inside a conditional branch,
|
|
50
|
+
* a method-call argument, or any other nested position still
|
|
51
|
+
* resolves. Returns `null` when the body has no param reference —
|
|
52
|
+
* callers default to a fallback (typically `"x"`) so a literal-only
|
|
53
|
+
* body still materializes.
|
|
54
|
+
*
|
|
55
|
+
* Lambda / new boundaries are NOT crossed: a nested lambda binds its
|
|
56
|
+
* own param symbol, and walking through it would shadow the outer
|
|
57
|
+
* binding with the inner one. The outer caller (e.g.
|
|
58
|
+
* {@link compileLambdaBody}) is responsible for resolving the body
|
|
59
|
+
* of a top-level lambda before consulting this helper, if applicable.
|
|
60
|
+
*/
|
|
61
|
+
export declare function readParamName(tree: ExprTree): string | null;
|
|
62
|
+
/**
|
|
63
|
+
* Generic ordering comparator for heterogeneous primitives. Numbers,
|
|
64
|
+
* strings, and bigints fall through to the native `<`/`>` operators
|
|
65
|
+
* after a uniform cast — the same approach the runtime's `compareKeys`
|
|
66
|
+
* helper uses. Lets the dynamic evaluator compare values without
|
|
67
|
+
* burning `as any` casts into every binary branch.
|
|
68
|
+
*/
|
|
69
|
+
export declare function compareValues(left: unknown, right: unknown): number;
|
|
70
|
+
/**
|
|
71
|
+
* Evaluates an {@link ExprTree} against a scope and returns the JS
|
|
72
|
+
* value the tree denotes. Default behavior for mock providers; same
|
|
73
|
+
* semantics the closure path would have produced for the same lambda.
|
|
74
|
+
*
|
|
75
|
+
* Throws when the tree mentions a kind the MVP evaluator does not
|
|
76
|
+
* support (currently `lambda` / `new`) or when a `param` node's name
|
|
77
|
+
* does not match {@link ExprTreeScope.paramName}.
|
|
78
|
+
*/
|
|
79
|
+
export declare function evaluateExprTree(tree: ExprTree, scope: ExprTreeScope): unknown;
|
|
80
|
+
/**
|
|
81
|
+
* Closes over the captures bundle and returns a `(item) => unknown`
|
|
82
|
+
* function the caller can apply directly — predicate for `where`,
|
|
83
|
+
* projector for `select`, key selector for `orderBy`, etc.
|
|
84
|
+
*
|
|
85
|
+
* The parameter name is read from the first {@link ExprParam} node in
|
|
86
|
+
* `meta.tree`; pass {@link fallbackParamName} (defaults to `"x"`) to
|
|
87
|
+
* support literal-only bodies that never mention the param.
|
|
88
|
+
*/
|
|
89
|
+
export declare function compileLambdaBody(meta: QueryableMeta, fallbackParamName?: string): (item: unknown) => unknown;
|
|
90
|
+
/**
|
|
91
|
+
* Abstract visitor over {@link ExprTree}. Each `visitX` method handles
|
|
92
|
+
* one node kind. Default implementations recurse via {@link visit},
|
|
93
|
+
* so a subclass only overrides the methods it needs.
|
|
94
|
+
*
|
|
95
|
+
* Generic parameter `R` is the value the visitor produces — a SQL
|
|
96
|
+
* fragment, a GraphQL field, a JS value, an AST node in another IR,
|
|
97
|
+
* etc. Implementations decide how children's results combine in their
|
|
98
|
+
* override (e.g. a SQL emitter's `visitBinary` calls `this.visit(left)`
|
|
99
|
+
* and `this.visit(right)` and emits `(left) op (right)`).
|
|
100
|
+
*
|
|
101
|
+
* Example:
|
|
102
|
+
*
|
|
103
|
+
* ```ts
|
|
104
|
+
* class SqlPredicate extends ExprTreeVisitor<string> {
|
|
105
|
+
* protected visitBinary(node: ExprBinary): string {
|
|
106
|
+
* return `(${this.visit(node.left)} ${node.op} ${this.visit(node.right)})`;
|
|
107
|
+
* }
|
|
108
|
+
* protected visitMember(node: ExprMember): string {
|
|
109
|
+
* return `${this.visit(node.target)}.${node.member}`;
|
|
110
|
+
* }
|
|
111
|
+
* protected visitParam(node: ExprParam): string {
|
|
112
|
+
* return node.name;
|
|
113
|
+
* }
|
|
114
|
+
* protected visitLiteral(node: ExprLiteral): string {
|
|
115
|
+
* return JSON.stringify(node.value);
|
|
116
|
+
* }
|
|
117
|
+
* // capture / call / unary / conditional inherit the default recurse
|
|
118
|
+
* // behavior; override the ones the dialect actually translates.
|
|
119
|
+
* }
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export declare abstract class ExprTreeVisitor<R> {
|
|
123
|
+
/**
|
|
124
|
+
* Dispatches to the appropriate `visitX` method. Subclasses can
|
|
125
|
+
* override to add tracing or to short-circuit on a specific node
|
|
126
|
+
* shape, but the default implementation is usually enough.
|
|
127
|
+
*/
|
|
128
|
+
visit(tree: ExprTree): R;
|
|
129
|
+
/**
|
|
130
|
+
* Combines the results of recursing into multiple children. Default
|
|
131
|
+
* returns the **last** result, which suits visitors that produce a
|
|
132
|
+
* value per node and only need the final aggregate (e.g. a side-effect
|
|
133
|
+
* collector). When the child list is empty (e.g. a free-function
|
|
134
|
+
* call with no args, an empty `new` expression), the visitor falls
|
|
135
|
+
* back to {@link leaf} so subclasses still get one well-defined
|
|
136
|
+
* extension point. Override to fold lists, concatenate strings, etc.
|
|
137
|
+
*/
|
|
138
|
+
protected combine(results: readonly R[]): R;
|
|
139
|
+
protected visitParam(_node: ExprParam): R;
|
|
140
|
+
protected visitCapture(_node: ExprCapture): R;
|
|
141
|
+
protected visitLiteral(_node: ExprLiteral): R;
|
|
142
|
+
protected visitMember(node: ExprMember): R;
|
|
143
|
+
protected visitCall(node: ExprCall): R;
|
|
144
|
+
protected visitBinary(node: ExprBinary): R;
|
|
145
|
+
protected visitUnary(node: ExprUnary): R;
|
|
146
|
+
protected visitConditional(node: ExprConditional): R;
|
|
147
|
+
protected visitLambda(node: ExprLambda): R;
|
|
148
|
+
protected visitNew(node: ExprNew): R;
|
|
149
|
+
/**
|
|
150
|
+
* Hoisted common-subexpression wrapper produced by the compiler
|
|
151
|
+
* (#209). Default behavior recurses through every binding value
|
|
152
|
+
* (in declaration order) and then the body, threading the children
|
|
153
|
+
* through {@link combine}.
|
|
154
|
+
*/
|
|
155
|
+
protected visitLet(node: ExprLet): R;
|
|
156
|
+
/**
|
|
157
|
+
* Reference to a binding introduced by an enclosing {@link ExprLet}.
|
|
158
|
+
* Default behavior is to treat it as a leaf — subclasses that build
|
|
159
|
+
* a SQL/expression dialect should override and emit the alias the
|
|
160
|
+
* matching binding generated.
|
|
161
|
+
*/
|
|
162
|
+
protected visitRef(_node: ExprRef): R;
|
|
163
|
+
/**
|
|
164
|
+
* Default result for true leaf nodes ({@link visitParam},
|
|
165
|
+
* {@link visitCapture}, {@link visitLiteral}) AND for interior
|
|
166
|
+
* nodes whose recurse-list is empty (e.g. a zero-arg method call,
|
|
167
|
+
* a `new` expression with no args and no initializers). Subclasses
|
|
168
|
+
* that inherit the default recurse must override <c>leaf</c> so
|
|
169
|
+
* {@link combine} has something to fold — the base throws to
|
|
170
|
+
* force a deliberate choice.
|
|
171
|
+
*/
|
|
172
|
+
protected leaf(): R;
|
|
173
|
+
}
|