@aestheticfunction/dspack-emit 0.4.0 → 0.5.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 +16 -6
- package/dist/cli.js +56 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +3 -1
- package/dist/targets/a2ui/diagnostics.d.ts +110 -0
- package/dist/targets/a2ui/diagnostics.js +94 -0
- package/dist/targets/a2ui/surface.d.ts +28 -9
- package/dist/targets/a2ui/surface.js +566 -224
- package/dist/transform/desugar.d.ts +32 -0
- package/dist/transform/desugar.js +156 -0
- package/dist/transform/emit.js +59 -0
- package/dist/transform/index.js +4 -0
- package/dist/transform/mapping.d.ts +0 -6
- package/dist/transform/mapping.js +50 -0
- package/dist/transform/model.d.ts +275 -0
- package/dist/transform/model.js +148 -0
- package/dist/transform/parse-v2.d.ts +78 -0
- package/dist/transform/parse-v2.js +305 -0
- package/dist/transform/profile-load.d.ts +8 -7
- package/dist/transform/profile-load.js +101 -24
- package/dist/transform/profile-schema-v2.d.ts +9 -0
- package/dist/transform/profile-schema-v2.js +430 -0
- package/dist/transform/profile-schema.d.ts +8 -0
- package/dist/transform/profile-schema.js +382 -0
- package/dist/transform/profiles.d.ts +31 -1
- package/dist/transform/scaffold.d.ts +29 -12
- package/dist/transform/scaffold.js +91 -31
- package/dist/transform/shadcn-v2-respelling.d.ts +12 -0
- package/dist/transform/shadcn-v2-respelling.js +49 -0
- package/dist/transform/validate-v2.d.ts +36 -0
- package/dist/transform/validate-v2.js +100 -0
- package/dist/types.d.ts +19 -0
- package/dist/validate/ajv.js +98 -29
- package/dist/validate/meta/catalog-meta.d.ts +10 -0
- package/dist/validate/meta/catalog-meta.js +157 -0
- package/package.json +3 -3
- package/src/__snapshots__/byte-neutral.test.ts.snap +66 -0
- package/src/browser-boundary.test.ts +47 -0
- package/src/byte-neutral.test.ts +246 -0
- package/src/cli.ts +60 -0
- package/src/dangling-ref.test.ts +118 -0
- package/src/desugar.test.ts +162 -0
- package/src/engine-boundary.test.ts +248 -0
- package/src/eval-fixture.test.ts +89 -0
- package/src/functions.test.ts +182 -0
- package/src/index.ts +4 -1
- package/src/profile-load.test.ts +99 -1
- package/src/profile-v2.test.ts +363 -0
- package/src/review-findings.test.ts +292 -0
- package/src/schema-mirrors.test.ts +35 -0
- package/src/surface-fidelity.test.ts +213 -0
- package/src/targets/a2ui/diagnostics.ts +136 -0
- package/src/targets/a2ui/surface.ts +920 -242
- package/src/transform/desugar.ts +194 -0
- package/src/transform/emit.ts +65 -0
- package/src/transform/index.ts +5 -0
- package/src/transform/mapping.ts +42 -0
- package/src/transform/model.ts +325 -0
- package/src/transform/parse-v2.ts +397 -0
- package/src/transform/profile-load.ts +107 -28
- package/src/transform/profile-schema-v2.ts +430 -0
- package/src/transform/profile-schema.ts +382 -0
- package/src/transform/profile.v2.schema.json +422 -0
- package/src/transform/profiles.ts +32 -1
- package/src/transform/scaffold.ts +124 -47
- package/src/transform/shadcn-v2-respelling.ts +61 -0
- package/src/transform/validate-v2.ts +138 -0
- package/src/types.ts +20 -0
- package/src/validate/ajv.ts +91 -32
- package/src/validate/meta/catalog-meta.ts +167 -0
- package/src/validate-v2.test.ts +173 -0
- package/dist/transform/profile.v1.schema.json +0 -153
- package/dist/validate/meta/a2ui-catalog.meta.0_9_1.json +0 -39
- package/dist/validate/meta/a2ui-catalog.meta.1_0.json +0 -34
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile v2 surface syntax → the internal transformation model.
|
|
3
|
+
*
|
|
4
|
+
* v2 is the first profile language that authors the internal model DIRECTLY:
|
|
5
|
+
* where v1 accumulated ten directives named after collisions (`subText`,
|
|
6
|
+
* `subButtonText`, `subTable`, …), a v2 plan writes Identity / Route / Collect
|
|
7
|
+
* facts in a closed string syntax. This module parses that syntax, derives the
|
|
8
|
+
* things v1 made authors imply (write order, subtree consumption), and REFUSES
|
|
9
|
+
* everything else — a v2 plan that parses is a plan the engine can emit.
|
|
10
|
+
*
|
|
11
|
+
* The grammar is deliberately closed. Selectors and destinations are fixed
|
|
12
|
+
* string forms with two parameterized holes (a prop name, a sub-component id
|
|
13
|
+
* set); there is no expression language, no JSON paths, no predicates, no
|
|
14
|
+
* computed destination names, and no cross-instance routing. Those absences
|
|
15
|
+
* are the design: a plan you cannot write is a plan nobody has to audit.
|
|
16
|
+
*
|
|
17
|
+
* Capability note: v2 in this foundation expresses exactly the transformation
|
|
18
|
+
* surface the engine has today — the v1 semantic surface, respelled. The
|
|
19
|
+
* grammar refuses spellings whose engine support has not landed (plan-level
|
|
20
|
+
* `transparent` identity, `onto` donation, `joinOn` pairing, multi-slot
|
|
21
|
+
* children routing). Each arrives with its capability class (T1–T5), as an
|
|
22
|
+
* addition to this grammar, never as a reinterpretation of it.
|
|
23
|
+
*/
|
|
24
|
+
import type { ComponentPlan } from "./profiles.js";
|
|
25
|
+
import {
|
|
26
|
+
WriteOrder,
|
|
27
|
+
emptySurfaceModel,
|
|
28
|
+
isCollect,
|
|
29
|
+
type Collect,
|
|
30
|
+
type Destination,
|
|
31
|
+
type Route,
|
|
32
|
+
type Selector,
|
|
33
|
+
type SurfaceModel,
|
|
34
|
+
} from "./model.js";
|
|
35
|
+
|
|
36
|
+
/** One pathed finding; parse-v2 refusals reuse the loader's issue shape. */
|
|
37
|
+
export interface ParseIssue {
|
|
38
|
+
path: string;
|
|
39
|
+
message: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export class ProfileParseError extends Error {
|
|
43
|
+
constructor(readonly issues: ParseIssue[]) {
|
|
44
|
+
super(
|
|
45
|
+
`profile v2 surface syntax is invalid (${issues.length} issue${issues.length === 1 ? "" : "s"}): ` +
|
|
46
|
+
issues
|
|
47
|
+
.slice(0, 3)
|
|
48
|
+
.map((i) => `${i.path || "$"}: ${i.message}`)
|
|
49
|
+
.join("; ") +
|
|
50
|
+
(issues.length > 3 ? "; …" : ""),
|
|
51
|
+
);
|
|
52
|
+
this.name = "ProfileParseError";
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
// The authored v2 shapes (what the JSON schema admits)
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
export interface SurfaceV2Route {
|
|
61
|
+
/** Ordered fallback chain; the first selector that yields a value wins. */
|
|
62
|
+
from: string[];
|
|
63
|
+
to: string;
|
|
64
|
+
overwrite?: boolean;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface SurfaceV2Collect {
|
|
68
|
+
/** Sections: direct children that open the collection. */
|
|
69
|
+
of: string[];
|
|
70
|
+
/** Array-valued destination (`prop:<name>`). */
|
|
71
|
+
into: string;
|
|
72
|
+
/** `flat`: all rows concatenate into one list. `records`: one record per row. */
|
|
73
|
+
shape: "flat" | "records";
|
|
74
|
+
/** Record field carrying each row's cells; required when shape is `records`. */
|
|
75
|
+
field?: string;
|
|
76
|
+
/** Repetitions inside a section. */
|
|
77
|
+
row: string[];
|
|
78
|
+
/** Cells inside a repetition, flattened to their subtree text. */
|
|
79
|
+
cells: string[];
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export type SurfaceV2Sub = "transparent" | { asText: string } | { drop: string };
|
|
83
|
+
|
|
84
|
+
export interface SurfaceV2 {
|
|
85
|
+
routes?: SurfaceV2Route[];
|
|
86
|
+
collects?: SurfaceV2Collect[];
|
|
87
|
+
/** Identity or disposition of named sub-components met in this node's subtree. */
|
|
88
|
+
subs?: Record<string, SurfaceV2Sub>;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// ---------------------------------------------------------------------------
|
|
92
|
+
// Selector / destination string grammar
|
|
93
|
+
// ---------------------------------------------------------------------------
|
|
94
|
+
|
|
95
|
+
const ID = "[a-z][a-z0-9-]*";
|
|
96
|
+
const PROP = "[A-Za-z][A-Za-z0-9]*";
|
|
97
|
+
const SELECTOR = new RegExp(
|
|
98
|
+
`^(self\\.text|self\\.props\\.(${PROP})|sub\\((${ID}(?:\\|${ID})*)\\)\\.(text|label|firstText|subtreeText)|children|synthesized\\.action)$`,
|
|
99
|
+
);
|
|
100
|
+
const DESTINATION = new RegExp(`^(prop|textChild|slot|slots|action):(${PROP})$`);
|
|
101
|
+
|
|
102
|
+
export function parseSelector(raw: string): Selector | null {
|
|
103
|
+
const m = SELECTOR.exec(raw);
|
|
104
|
+
if (!m) return null;
|
|
105
|
+
if (m[1] === "self.text") return { kind: "self-text" };
|
|
106
|
+
if (m[1] === "children") return { kind: "children" };
|
|
107
|
+
if (m[1] === "synthesized.action") return { kind: "synthesized-action" };
|
|
108
|
+
if (m[2]) return { kind: "self-prop", prop: m[2] };
|
|
109
|
+
const subs = m[3].split("|");
|
|
110
|
+
switch (m[4]) {
|
|
111
|
+
case "text":
|
|
112
|
+
return { kind: "sub-text", subs };
|
|
113
|
+
case "label":
|
|
114
|
+
return { kind: "sub-label", subs };
|
|
115
|
+
case "firstText":
|
|
116
|
+
return { kind: "sub-text-lift", subs };
|
|
117
|
+
default:
|
|
118
|
+
return { kind: "subtree-text", subs };
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function parseDestination(raw: string): Destination | null {
|
|
123
|
+
const m = DESTINATION.exec(raw);
|
|
124
|
+
if (!m) return null;
|
|
125
|
+
const name = m[2];
|
|
126
|
+
switch (m[1]) {
|
|
127
|
+
case "prop":
|
|
128
|
+
return { kind: "prop", name };
|
|
129
|
+
case "textChild":
|
|
130
|
+
return { kind: "text-child", name };
|
|
131
|
+
case "slot":
|
|
132
|
+
return { kind: "slot", name };
|
|
133
|
+
case "slots":
|
|
134
|
+
return { kind: "slots", name };
|
|
135
|
+
default:
|
|
136
|
+
return { kind: "action", name };
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// ---------------------------------------------------------------------------
|
|
141
|
+
// Derived write order — v2 authors never write WriteOrder; it is a function
|
|
142
|
+
// of what the route reads and writes, reproducing v1's phase semantics.
|
|
143
|
+
// ---------------------------------------------------------------------------
|
|
144
|
+
|
|
145
|
+
function orderOfSelector(s: Selector, to: Destination): WriteOrder {
|
|
146
|
+
switch (s.kind) {
|
|
147
|
+
case "self-prop":
|
|
148
|
+
return WriteOrder.Verbatim;
|
|
149
|
+
case "sub-text":
|
|
150
|
+
case "sub-label":
|
|
151
|
+
case "sub-text-lift":
|
|
152
|
+
return WriteOrder.Consume;
|
|
153
|
+
case "subtree-text":
|
|
154
|
+
return WriteOrder.CollectLead;
|
|
155
|
+
case "self-text":
|
|
156
|
+
return to.kind === "text-child" ? WriteOrder.SelfTextChild : WriteOrder.SelfText;
|
|
157
|
+
case "synthesized-action":
|
|
158
|
+
return WriteOrder.Action;
|
|
159
|
+
case "children":
|
|
160
|
+
return WriteOrder.Children;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Selector-kind → destination-kind compatibility (closed, refused otherwise). */
|
|
165
|
+
function compatible(s: Selector, to: Destination): boolean {
|
|
166
|
+
switch (s.kind) {
|
|
167
|
+
case "children":
|
|
168
|
+
return to.kind === "slot" || to.kind === "slots";
|
|
169
|
+
case "synthesized-action":
|
|
170
|
+
return to.kind === "action";
|
|
171
|
+
case "self-text":
|
|
172
|
+
return to.kind === "prop" || to.kind === "text-child";
|
|
173
|
+
default:
|
|
174
|
+
return to.kind === "prop";
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// ---------------------------------------------------------------------------
|
|
179
|
+
// The parser
|
|
180
|
+
// ---------------------------------------------------------------------------
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Parse and statically validate one plan's v2 surface block.
|
|
184
|
+
*
|
|
185
|
+
* `path` roots the pathed findings (e.g. `/components/3/surface`). Validation
|
|
186
|
+
* here needs only the plan itself: destinations against its own declared
|
|
187
|
+
* structural slots and propMap targets, conflict detection under the write
|
|
188
|
+
* discipline, and grammar closure. Contract-facing checks (sub ids exist,
|
|
189
|
+
* self-props are declared, derived coverage is complete) run at transform
|
|
190
|
+
* time, where the contract is in hand — see validate-v2.ts.
|
|
191
|
+
*/
|
|
192
|
+
export function parseSurfaceV2(surface: SurfaceV2, plan: ComponentPlan, path: string): SurfaceModel {
|
|
193
|
+
const issues: ParseIssue[] = [];
|
|
194
|
+
const model = emptySurfaceModel();
|
|
195
|
+
|
|
196
|
+
// The plan's own declared destination names: structural slots plus the
|
|
197
|
+
// a2ui-side names its propMap writes. Gate A3 catches an OMITTED property
|
|
198
|
+
// but silently passes an INVENTED one, so this is where invention dies.
|
|
199
|
+
const declared = new Set<string>([
|
|
200
|
+
...Object.keys(plan.structural ?? {}),
|
|
201
|
+
...Object.values(plan.propMap ?? {}).map((p) => p.a2ui),
|
|
202
|
+
]);
|
|
203
|
+
|
|
204
|
+
// Write-conflict detection under the declared discipline. Two writers on one
|
|
205
|
+
// destination are LEGAL when they resolve in distinct write orders — the
|
|
206
|
+
// outcome is then fully determined by order plus the overwrite flag (v1's
|
|
207
|
+
// Table does exactly this: a passthrough prop wins over the collect harvest
|
|
208
|
+
// because Verbatim runs first and later writers are first-wins). What is
|
|
209
|
+
// refused is a SAME-order double-claim, where nothing but declaration order
|
|
210
|
+
// would decide the winner — that is ambiguity, not layering.
|
|
211
|
+
const claimed = new Map<string, Array<{ order: WriteOrder; by: string }>>();
|
|
212
|
+
const claim = (name: string, order: WriteOrder, by: string, at: string): void => {
|
|
213
|
+
const prior = claimed.get(name) ?? [];
|
|
214
|
+
// The children destination is exclusive in BOTH directions (see the
|
|
215
|
+
// children-route check below for the rationale).
|
|
216
|
+
const children = prior.find((c) => c.order === WriteOrder.Children);
|
|
217
|
+
if (children && order !== WriteOrder.Children) {
|
|
218
|
+
issues.push({
|
|
219
|
+
path: at,
|
|
220
|
+
message: `destination '${name}' is claimed by the children route (${children.by}); instance references do not participate in first-wins layering`,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
const clash = prior.find((c) => c.order === order);
|
|
224
|
+
if (clash) {
|
|
225
|
+
issues.push({
|
|
226
|
+
path: at,
|
|
227
|
+
message: `destination '${name}' is already claimed in the same write phase by ${clash.by}; two same-phase writers would be ordered only by declaration position (fallbacks belong inside a single route's 'from' chain)`,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
prior.push({ order, by });
|
|
231
|
+
claimed.set(name, prior);
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
let childrenRoutes = 0;
|
|
235
|
+
|
|
236
|
+
for (const [i, r] of (surface.routes ?? []).entries()) {
|
|
237
|
+
const at = `${path}/routes/${i}`;
|
|
238
|
+
const to = parseDestination(r.to);
|
|
239
|
+
if (!to) {
|
|
240
|
+
issues.push({ path: `${at}/to`, message: `'${r.to}' is not a destination (expected prop:|textChild:|slot:|slots:|action: followed by a name)` });
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (!declared.has(to.name)) {
|
|
244
|
+
issues.push({
|
|
245
|
+
path: `${at}/to`,
|
|
246
|
+
message: `destination '${to.name}' is not declared by this plan (declare it in structural or map a prop onto it; inventing properties is refused because gate A3 cannot catch them)`,
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
const from: Selector[] = [];
|
|
251
|
+
let order: WriteOrder | undefined;
|
|
252
|
+
for (const [j, rawSel] of r.from.entries()) {
|
|
253
|
+
const sel = parseSelector(rawSel);
|
|
254
|
+
if (!sel) {
|
|
255
|
+
issues.push({ path: `${at}/from/${j}`, message: `'${rawSel}' is not a selector (closed forms: self.text, self.props.<prop>, sub(<id>|…).<text|label|firstText|subtreeText>, children, synthesized.action)` });
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
if (!compatible(sel, to)) {
|
|
259
|
+
issues.push({ path: `${at}/from/${j}`, message: `selector '${rawSel}' cannot feed destination kind '${to.kind}'` });
|
|
260
|
+
continue;
|
|
261
|
+
}
|
|
262
|
+
const selOrder = orderOfSelector(sel, to);
|
|
263
|
+
if (order === undefined) order = selOrder;
|
|
264
|
+
else if (order !== selOrder) {
|
|
265
|
+
issues.push({
|
|
266
|
+
path: `${at}/from/${j}`,
|
|
267
|
+
message: `fallback chain mixes write phases ('${rawSel}' resolves in a different phase than the first selector); a route resolves in exactly one phase`,
|
|
268
|
+
});
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
from.push(sel);
|
|
272
|
+
}
|
|
273
|
+
if (from.length === 0) {
|
|
274
|
+
issues.push({ path: `${at}/from`, message: "route has no valid selectors" });
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
// firstText is a FALLBACK selector: the engine serves it only after a
|
|
279
|
+
// primary label/text selector yielded nothing. As a primary it would
|
|
280
|
+
// parse, derive Consume, and then never resolve — consuming the subtree
|
|
281
|
+
// while writing nothing. Refuse the dead spelling instead of shipping it.
|
|
282
|
+
if (from[0].kind === "sub-text-lift") {
|
|
283
|
+
issues.push({
|
|
284
|
+
path: `${at}/from/0`,
|
|
285
|
+
message:
|
|
286
|
+
"sub(…).firstText is a fallback selector and cannot lead a route; give the route a primary (sub(…).label or sub(…).text) and put firstText after it",
|
|
287
|
+
});
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if (from[0].kind === "children") {
|
|
292
|
+
childrenRoutes++;
|
|
293
|
+
if (from.length > 1) {
|
|
294
|
+
issues.push({ path: `${at}/from`, message: "a children route takes no fallbacks" });
|
|
295
|
+
}
|
|
296
|
+
if (childrenRoutes > 1) {
|
|
297
|
+
issues.push({ path: at, message: "only one children route is supported (multiple named slots arrive with capability class T4)" });
|
|
298
|
+
}
|
|
299
|
+
// A children route produces INSTANCES, not a value: it cannot join
|
|
300
|
+
// first-wins layering, so its destination is exclusively its own. Two
|
|
301
|
+
// writers here would either orphan a synthesized instance in the hashed
|
|
302
|
+
// artifact or silently discard the children — refuse the pair.
|
|
303
|
+
const shared = claimed.get(to.name);
|
|
304
|
+
if (shared && shared.length > 0) {
|
|
305
|
+
issues.push({
|
|
306
|
+
path: `${at}/to`,
|
|
307
|
+
message: `destination '${to.name}' is already claimed by ${shared[0].by}; a children route's destination cannot be layered with value routes (instance references do not participate in first-wins)`,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
claim(to.name, order!, `routes/${i}`, `${at}/to`);
|
|
313
|
+
const route: Route = { order: order!, from, to, origin: `v2:routes/${i}` };
|
|
314
|
+
if (r.overwrite) route.overwrite = true;
|
|
315
|
+
model.routes.push(route);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
for (const [i, c] of (surface.collects ?? []).entries()) {
|
|
319
|
+
const at = `${path}/collects/${i}`;
|
|
320
|
+
const into = parseDestination(c.into);
|
|
321
|
+
if (!into || into.kind !== "prop") {
|
|
322
|
+
issues.push({ path: `${at}/into`, message: `'${c.into}' is not an array-valued destination (collects write prop:<name>)` });
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
if (!declared.has(into.name)) {
|
|
326
|
+
issues.push({ path: `${at}/into`, message: `destination '${into.name}' is not declared by this plan` });
|
|
327
|
+
}
|
|
328
|
+
if (c.shape === "records" && !c.field) {
|
|
329
|
+
issues.push({ path: `${at}/field`, message: "a records collect names the field its rows' cells land in" });
|
|
330
|
+
continue;
|
|
331
|
+
}
|
|
332
|
+
claim(into.name, WriteOrder.Collect, `collects/${i}`, `${at}/into`);
|
|
333
|
+
|
|
334
|
+
const origin = `v2:collects/${i}`;
|
|
335
|
+
const inner: Collect = {
|
|
336
|
+
order: WriteOrder.Collect,
|
|
337
|
+
flatten: false,
|
|
338
|
+
of: c.row,
|
|
339
|
+
as: { kind: "inline" },
|
|
340
|
+
scalar: {
|
|
341
|
+
order: WriteOrder.Collect,
|
|
342
|
+
from: [{ kind: "subtree-text", subs: c.cells }],
|
|
343
|
+
to: { kind: "prop", name: c.field ?? "cells" },
|
|
344
|
+
origin,
|
|
345
|
+
},
|
|
346
|
+
origin,
|
|
347
|
+
};
|
|
348
|
+
model.collects.push({
|
|
349
|
+
order: WriteOrder.Collect,
|
|
350
|
+
flatten: c.shape === "flat",
|
|
351
|
+
of: c.of,
|
|
352
|
+
as: into,
|
|
353
|
+
item: { [c.field ?? "cells"]: inner },
|
|
354
|
+
origin,
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
for (const [sub, disposition] of Object.entries(surface.subs ?? {})) {
|
|
359
|
+
const at = `${path}/subs/${sub}`;
|
|
360
|
+
if (disposition === "transparent") {
|
|
361
|
+
model.subIdentity[sub] = { kind: "transparent" };
|
|
362
|
+
} else if ("asText" in disposition) {
|
|
363
|
+
model.subIdentity[sub] = { kind: "as-text", variant: disposition.asText };
|
|
364
|
+
} else {
|
|
365
|
+
model.drops[sub] = disposition.drop;
|
|
366
|
+
}
|
|
367
|
+
// A sub with an identity/drop must not also be a route or collect source —
|
|
368
|
+
// two dispositions for one sub is a contradiction, not a preference.
|
|
369
|
+
const alsoRouted = model.routes.some((r) => r.from.some((s) => "subs" in s && (s.subs as string[]).includes(sub)));
|
|
370
|
+
const inCollect = (col: Collect): boolean =>
|
|
371
|
+
col.of.includes(sub) ||
|
|
372
|
+
Boolean(col.scalar?.from.some((s) => "subs" in s && (s.subs as string[]).includes(sub))) ||
|
|
373
|
+
Object.values(col.item ?? {}).some((f) => (isCollect(f) ? inCollect(f) : f.from.some((s) => "subs" in s && (s.subs as string[]).includes(sub))));
|
|
374
|
+
const alsoCollected = model.collects.some(inCollect);
|
|
375
|
+
if (alsoRouted || alsoCollected) {
|
|
376
|
+
issues.push({ path: at, message: `sub-component '${sub}' has a disposition here and is also consumed by a ${alsoRouted ? "route" : "collect"}; one sub, one disposition` });
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
// Derived, never authored: a plan that reads its subtree into props does not
|
|
381
|
+
// also emit that subtree as children.
|
|
382
|
+
model.consumesSubtree =
|
|
383
|
+
model.collects.length > 0 ||
|
|
384
|
+
model.routes.some((r) => r.order === WriteOrder.Consume || r.order === WriteOrder.CollectLead);
|
|
385
|
+
|
|
386
|
+
const childrenRoute = model.routes.find((r) => r.from[0].kind === "children");
|
|
387
|
+
if (model.consumesSubtree && childrenRoute) {
|
|
388
|
+
issues.push({
|
|
389
|
+
path,
|
|
390
|
+
message:
|
|
391
|
+
"this plan both consumes its subtree into props and routes children as instances; the two are contradictory (the consumed subtree has no children left to emit)",
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (issues.length > 0) throw new ProfileParseError(issues);
|
|
396
|
+
return model;
|
|
397
|
+
}
|
|
@@ -2,21 +2,38 @@
|
|
|
2
2
|
* Profile-as-data: load a mapping Profile from JSON.
|
|
3
3
|
*
|
|
4
4
|
* The Profile has always been pure data (profiles.ts); this module makes it
|
|
5
|
-
* *reachable* as data. `loadProfile`
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* *reachable* as data. `loadProfile` dispatches EXPLICITLY on the document's
|
|
6
|
+
* `profileVersion`:
|
|
7
|
+
*
|
|
8
|
+
* "1" — the original surface-plan directive language, validated against
|
|
9
|
+
* profile.v1.schema.json. Frozen: v1 documents keep loading and
|
|
10
|
+
* emitting byte-identically, indefinitely.
|
|
11
|
+
* "2" — the primitive language, validated against profile.v2.schema.json,
|
|
12
|
+
* then every plan's `surface` block is parsed and statically checked
|
|
13
|
+
* (parse-v2.ts) so that a v2 profile that loads is a profile the
|
|
14
|
+
* engine can emit. The returned Profile is stamped `language: "v2"`,
|
|
15
|
+
* which is what keys the transform layer's fail-closed contract
|
|
16
|
+
* validation — no document is ever silently interpreted under the
|
|
17
|
+
* wrong version.
|
|
18
|
+
*
|
|
19
|
+
* Anything else refuses with a pathed finding naming the supported versions.
|
|
20
|
+
* Validation gates shape and coherence only — every judgment call (valueMaps,
|
|
21
|
+
* casualties, routes) stays with the profile's author.
|
|
10
22
|
*
|
|
11
23
|
* Toolchain: ajv draft 2020-12, `strict: false`, ajv-formats — the same
|
|
12
24
|
* convention as the catalog gates (src/validate/ajv.ts) and the dspack
|
|
13
25
|
* harness.
|
|
14
26
|
*/
|
|
15
|
-
import { readFileSync } from "node:fs";
|
|
16
|
-
import { fileURLToPath } from "node:url";
|
|
17
27
|
import Ajv2020, { type ValidateFunction } from "ajv/dist/2020.js";
|
|
18
28
|
import addFormats from "ajv-formats";
|
|
19
|
-
import
|
|
29
|
+
import { profileSchema } from "./profile-schema.js";
|
|
30
|
+
import { profileSchemaV2 } from "./profile-schema-v2.js";
|
|
31
|
+
import { ProfileParseError, parseSurfaceV2 } from "./parse-v2.js";
|
|
32
|
+
import type { ComponentPlan, Profile } from "./profiles.js";
|
|
33
|
+
|
|
34
|
+
/** The profile schemas, as shipped (also exported for hosts that surface them in UI). */
|
|
35
|
+
export { profileSchema } from "./profile-schema.js";
|
|
36
|
+
export { profileSchemaV2 } from "./profile-schema-v2.js";
|
|
20
37
|
|
|
21
38
|
export interface ProfileLoadIssue {
|
|
22
39
|
/** JSON path into the offending document (ajv instancePath). */
|
|
@@ -25,9 +42,12 @@ export interface ProfileLoadIssue {
|
|
|
25
42
|
}
|
|
26
43
|
|
|
27
44
|
export class ProfileLoadError extends Error {
|
|
28
|
-
constructor(
|
|
45
|
+
constructor(
|
|
46
|
+
readonly issues: ProfileLoadIssue[],
|
|
47
|
+
schemaName = "profile.v1.schema.json",
|
|
48
|
+
) {
|
|
29
49
|
super(
|
|
30
|
-
`profile does not conform to
|
|
50
|
+
`profile does not conform to ${schemaName} (${issues.length} issue${issues.length === 1 ? "" : "s"}): ` +
|
|
31
51
|
issues
|
|
32
52
|
.slice(0, 3)
|
|
33
53
|
.map((i) => `${i.path || "$"}: ${i.message}`)
|
|
@@ -38,36 +58,95 @@ export class ProfileLoadError extends Error {
|
|
|
38
58
|
}
|
|
39
59
|
}
|
|
40
60
|
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
/** The profile schema, as shipped (also exported for hosts that surface it in UI). */
|
|
44
|
-
export const profileSchema: Record<string, unknown> = JSON.parse(readFileSync(schemaPath, "utf8"));
|
|
61
|
+
const compiled = new Map<string, ValidateFunction>();
|
|
45
62
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (!compiled) {
|
|
63
|
+
function validator(version: "1" | "2"): ValidateFunction {
|
|
64
|
+
let v = compiled.get(version);
|
|
65
|
+
if (!v) {
|
|
50
66
|
const ajv = new Ajv2020({ strict: false, allErrors: true, validateFormats: true });
|
|
51
67
|
addFormats(ajv);
|
|
52
|
-
|
|
68
|
+
v = ajv.compile(version === "1" ? profileSchema : profileSchemaV2);
|
|
69
|
+
compiled.set(version, v);
|
|
53
70
|
}
|
|
54
|
-
return
|
|
71
|
+
return v;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function ajvIssues(validate: ValidateFunction): ProfileLoadIssue[] {
|
|
75
|
+
const errors = (validate.errors ?? []) as Array<{ instancePath?: string; message?: string }>;
|
|
76
|
+
return errors.map((e) => ({ path: e.instancePath ?? "", message: e.message ?? "invalid" }));
|
|
55
77
|
}
|
|
56
78
|
|
|
57
79
|
/**
|
|
58
|
-
* Validate `json` as a
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
80
|
+
* Validate `json` as a profile document and return the engine-ready `Profile`.
|
|
81
|
+
* The `profileVersion` envelope field is stripped — it belongs to the document
|
|
82
|
+
* format, not the engine. Top-level `x-` extension keys are preserved (the
|
|
83
|
+
* engine ignores them; provenance tools may not).
|
|
62
84
|
*/
|
|
63
85
|
export function loadProfile(json: unknown): Profile {
|
|
64
|
-
const
|
|
65
|
-
if (
|
|
66
|
-
const errors = (validate.errors ?? []) as Array<{ instancePath?: string; message?: string }>;
|
|
86
|
+
const version = (json as { profileVersion?: unknown } | null)?.profileVersion;
|
|
87
|
+
if (version !== "1" && version !== "2") {
|
|
67
88
|
throw new ProfileLoadError(
|
|
68
|
-
|
|
89
|
+
[
|
|
90
|
+
{
|
|
91
|
+
path: "/profileVersion",
|
|
92
|
+
message: `unknown profile version ${JSON.stringify(version)} — this loader speaks "1" (surface-plan directives) and "2" (the primitive language)`,
|
|
93
|
+
},
|
|
94
|
+
],
|
|
95
|
+
"any supported profile schema",
|
|
69
96
|
);
|
|
70
97
|
}
|
|
98
|
+
|
|
99
|
+
const validate = validator(version);
|
|
100
|
+
if (!validate(json)) {
|
|
101
|
+
throw new ProfileLoadError(ajvIssues(validate), `profile.v${version}.schema.json`);
|
|
102
|
+
}
|
|
103
|
+
|
|
71
104
|
const { profileVersion: _envelope, ...profile } = json as Record<string, unknown> & { profileVersion: string };
|
|
105
|
+
|
|
106
|
+
if (version === "2") {
|
|
107
|
+
// Declared function args must be SELF-CONTAINED schemas. A $ref inside
|
|
108
|
+
// them couples the function's signature to emit internals at best and, at
|
|
109
|
+
// worst, is a dangling reference that ajv defers past gate A1's compile
|
|
110
|
+
// and then throws raw out of gate A3 — pass-or-crash, never a finding.
|
|
111
|
+
// Refuse the whole class at load, with the path.
|
|
112
|
+
const functions = (profile as { functions?: Record<string, { args?: unknown }> }).functions ?? {};
|
|
113
|
+
const refIssues: ProfileLoadIssue[] = [];
|
|
114
|
+
const findRefs = (value: unknown, at: string): void => {
|
|
115
|
+
if (Array.isArray(value)) value.forEach((v, i) => findRefs(v, `${at}/${i}`));
|
|
116
|
+
else if (value && typeof value === "object") {
|
|
117
|
+
for (const [k, v] of Object.entries(value)) {
|
|
118
|
+
if (k === "$ref") refIssues.push({ path: at, message: `function args must be self-contained schemas; '$ref' is refused (found ${JSON.stringify(v)})` });
|
|
119
|
+
else findRefs(v, `${at}/${k}`);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
for (const [name, fn] of Object.entries(functions)) {
|
|
124
|
+
if (fn?.args) findRefs(fn.args, `/functions/${name}/args`);
|
|
125
|
+
}
|
|
126
|
+
if (refIssues.length > 0) throw new ProfileLoadError(refIssues, "profile.v2.schema.json (functions)");
|
|
127
|
+
|
|
128
|
+
// Parse every plan's surface block now, so a v2 profile that LOADS is a
|
|
129
|
+
// profile the engine can EMIT — grammar errors surface at load with
|
|
130
|
+
// pathed findings, not mid-emission.
|
|
131
|
+
const collections: Array<["components" | "synthesized", ComponentPlan[]]> = [
|
|
132
|
+
["components", (profile.components as ComponentPlan[]) ?? []],
|
|
133
|
+
["synthesized", (profile.synthesized as ComponentPlan[]) ?? []],
|
|
134
|
+
];
|
|
135
|
+
for (const [key, plans] of collections) {
|
|
136
|
+
for (const [i, plan] of plans.entries()) {
|
|
137
|
+
if (!plan.surface) continue;
|
|
138
|
+
try {
|
|
139
|
+
parseSurfaceV2(plan.surface, plan, `/${key}/${i}/surface`);
|
|
140
|
+
} catch (e) {
|
|
141
|
+
if (e instanceof ProfileParseError) {
|
|
142
|
+
throw new ProfileLoadError(e.issues, "profile.v2.schema.json (surface syntax)");
|
|
143
|
+
}
|
|
144
|
+
throw e;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
(profile as Record<string, unknown>).language = "v2";
|
|
149
|
+
}
|
|
150
|
+
|
|
72
151
|
return profile as unknown as Profile;
|
|
73
152
|
}
|