@aestheticfunction/dspack-emit 0.4.1 → 0.6.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 (72) hide show
  1. package/README.md +16 -6
  2. package/dist/cli.js +56 -0
  3. package/dist/index.d.ts +4 -1
  4. package/dist/index.js +3 -1
  5. package/dist/targets/a2ui/diagnostics.d.ts +110 -0
  6. package/dist/targets/a2ui/diagnostics.js +94 -0
  7. package/dist/targets/a2ui/surface.d.ts +28 -9
  8. package/dist/targets/a2ui/surface.js +1004 -222
  9. package/dist/transform/desugar.d.ts +32 -0
  10. package/dist/transform/desugar.js +156 -0
  11. package/dist/transform/emit.js +59 -0
  12. package/dist/transform/index.js +4 -0
  13. package/dist/transform/mapping.d.ts +0 -6
  14. package/dist/transform/mapping.js +66 -2
  15. package/dist/transform/model.d.ts +368 -0
  16. package/dist/transform/model.js +169 -0
  17. package/dist/transform/parse-v2.d.ts +109 -0
  18. package/dist/transform/parse-v2.js +479 -0
  19. package/dist/transform/profile-load.d.ts +7 -6
  20. package/dist/transform/profile-load.js +99 -20
  21. package/dist/transform/profile-schema-v2.d.ts +9 -0
  22. package/dist/transform/profile-schema-v2.js +608 -0
  23. package/dist/transform/profiles.d.ts +31 -1
  24. package/dist/transform/scaffold.d.ts +29 -12
  25. package/dist/transform/scaffold.js +91 -31
  26. package/dist/transform/shadcn-v2-respelling.d.ts +77 -0
  27. package/dist/transform/shadcn-v2-respelling.js +456 -0
  28. package/dist/transform/validate-v2.d.ts +36 -0
  29. package/dist/transform/validate-v2.js +136 -0
  30. package/dist/types.d.ts +23 -0
  31. package/dist/validate/ajv.js +96 -25
  32. package/dist/validate/meta/a2ui-catalog.meta.0_9_1.json +39 -0
  33. package/dist/validate/meta/a2ui-catalog.meta.1_0.json +34 -0
  34. package/package.json +2 -2
  35. package/src/__snapshots__/byte-neutral.test.ts.snap +66 -0
  36. package/src/a0.test.ts +235 -0
  37. package/src/byte-neutral.test.ts +246 -0
  38. package/src/cli.ts +60 -0
  39. package/src/dangling-ref.test.ts +118 -0
  40. package/src/desugar.test.ts +162 -0
  41. package/src/engine-boundary.test.ts +248 -0
  42. package/src/eval-fixture.test.ts +102 -0
  43. package/src/functions.test.ts +182 -0
  44. package/src/index.ts +4 -1
  45. package/src/layered-dissolution.test.ts +139 -0
  46. package/src/profile-load.test.ts +99 -1
  47. package/src/profile-v2.test.ts +369 -0
  48. package/src/review-findings.test.ts +292 -0
  49. package/src/schema-mirrors.test.ts +5 -0
  50. package/src/surface-fidelity.test.ts +213 -0
  51. package/src/t1.test.ts +369 -0
  52. package/src/t2.test.ts +222 -0
  53. package/src/t3.test.ts +304 -0
  54. package/src/t4.test.ts +278 -0
  55. package/src/targets/a2ui/diagnostics.ts +136 -0
  56. package/src/targets/a2ui/surface.ts +1468 -235
  57. package/src/transform/desugar.ts +194 -0
  58. package/src/transform/emit.ts +65 -0
  59. package/src/transform/index.ts +5 -0
  60. package/src/transform/mapping.ts +60 -2
  61. package/src/transform/model.ts +427 -0
  62. package/src/transform/parse-v2.ts +608 -0
  63. package/src/transform/profile-load.ts +104 -22
  64. package/src/transform/profile-schema-v2.ts +608 -0
  65. package/src/transform/profile.v2.schema.json +600 -0
  66. package/src/transform/profiles.ts +32 -1
  67. package/src/transform/scaffold.ts +124 -47
  68. package/src/transform/shadcn-v2-respelling.ts +475 -0
  69. package/src/transform/validate-v2.ts +174 -0
  70. package/src/types.ts +32 -0
  71. package/src/validate/ajv.ts +89 -26
  72. package/src/validate-v2.test.ts +173 -0
@@ -0,0 +1,368 @@
1
+ /**
2
+ * The internal surface transformation model.
3
+ *
4
+ * Ten authored surface-plan directives accumulated one at a time, each named
5
+ * after the collision that produced it (`subText`, `subButtonText`, `subTable`,
6
+ * `subFlatten`, …). Measured against two design systems and a second emit
7
+ * target, that vocabulary does not generalize: only four directives are used by
8
+ * both shipped profiles, the second target reuses none of them, and the
9
+ * scaffolder can reach two. They are not a vocabulary for projecting design
10
+ * systems onto protocols — they are point-fixes for one collision, shadcn's
11
+ * compound sub-component composition meeting A2UI's fixed catalog vocabulary.
12
+ *
13
+ * Underneath, every one of them answers exactly one of three questions:
14
+ *
15
+ * Identity does this node become an instance, and as which component?
16
+ * Routing for each piece of this node's subtree, where does it land?
17
+ * Repetition when a sub-structure repeats, how does it become an array?
18
+ *
19
+ * This module is that answer as types. The authored directives become surface
20
+ * syntax that desugars here (see desugar.ts); the engine reads only this.
21
+ *
22
+ * THREE THINGS IMPLEMENTATION CONTACT ADDED, and why they are refinements of
23
+ * the model rather than escapes from it:
24
+ *
25
+ * 1. `Selector[]`, not `Selector`. The audited label lift — "take the sub's
26
+ * label, else the first direct text under it" — is a fallback chain, not a
27
+ * special case. Ordered selectors express it, and express nothing else.
28
+ * 2. `subs: string[]`, not `sub: string`. A table body accepts both a cell and
29
+ * a header-cell in the same row. One selector naming a set is honest; two
30
+ * collects racing for one destination is not.
31
+ * 3. First-write-wins as a uniform WRITE DISCIPLINE rather than per-directive
32
+ * `??=` scattered through the emitter. Every v1 directive already behaved
33
+ * this way; only `textProp`/`textChildProp` overwrote, and that asymmetry
34
+ * is preserved explicitly (see `Route.overwrite`) rather than silently.
35
+ *
36
+ * What is deliberately NOT here, and must not arrive later: JSON paths,
37
+ * expression evaluation, predicates, computed destination names, and writes to
38
+ * arbitrary other instances. Those would each buy generality at the cost of the
39
+ * property that makes emission auditable — that a plan can be checked against
40
+ * its contract before it runs.
41
+ */
42
+ import type { Json } from "../types.js";
43
+ /**
44
+ * Whether a dspack node becomes an emitted instance, and under which catalog
45
+ * identity. `transparent` and `as-text` are v1's `subFlatten` halves: today
46
+ * they are declared by a compound about its own sub-family (subs have no plans
47
+ * of their own), which is why they live in `Plan.subIdentity` rather than on
48
+ * the node's plan. When plans gain identities of their own, that field becomes
49
+ * the narrower spelling of this same idea.
50
+ */
51
+ export type Identity =
52
+ /** Emits one instance of the named A2UI component. */
53
+ {
54
+ kind: "mapped";
55
+ a2ui: string;
56
+ }
57
+ /**
58
+ * Emits nothing; its children rise to the destination its parent would use.
59
+ * A transparent identity may carry DONATIONS (T1's constrained control
60
+ * donation): each dissolved instance harvests the donated values from its
61
+ * own subtree and hands them to the single eligible emitted control within
62
+ * that subtree — the boundary IS the scope, which is what makes a
63
+ * three-field form donate three labels to three inputs instead of refusing.
64
+ */
65
+ | {
66
+ kind: "transparent";
67
+ donate?: Donation[];
68
+ }
69
+ /** Emits as the profile's synthesized text primitive, carrying its subtree text. */
70
+ | {
71
+ kind: "as-text";
72
+ variant: string;
73
+ };
74
+ /**
75
+ * One constrained control donation (the ratified `onto: "control"` mechanism):
76
+ * a value harvested inside a dissolving boundary, written onto the boundary's
77
+ * single eligible control. Fail-closed at emit: zero or multiple eligible
78
+ * controls refuse; the receiving plan must declare the destination. The write
79
+ * is carried EXPLICITLY in the rewritten child list and applied as a pre-write
80
+ * when the control's own emission constructs its instance — never by mutating
81
+ * another node's committed instance mid-traversal.
82
+ */
83
+ export interface Donation {
84
+ /** Where the value comes from, inside the dissolving boundary. */
85
+ from: Selector;
86
+ /** The receiving prop on the control (`prop:` destinations only). */
87
+ to: Destination;
88
+ /** Which authored rule produced this — provenance for the ledger. */
89
+ origin: string;
90
+ }
91
+ /**
92
+ * Where information comes from. A closed enumeration: every selector is
93
+ * statically checkable against the contract before emission runs.
94
+ */
95
+ export type Selector =
96
+ /** This node's own `text`. */
97
+ {
98
+ kind: "self-text";
99
+ }
100
+ /** One declared prop on this node, verbatim. */
101
+ | {
102
+ kind: "self-prop";
103
+ prop: string;
104
+ }
105
+ /** `text` of any descendant whose component is one of `subs`. */
106
+ | {
107
+ kind: "sub-text";
108
+ subs: string[];
109
+ }
110
+ /**
111
+ * The label of a descendant inside one of `subs` — a component whose own plan
112
+ * projects text as a child label. Incidental text elsewhere never qualifies.
113
+ */
114
+ | {
115
+ kind: "sub-label";
116
+ subs: string[];
117
+ }
118
+ /**
119
+ * First direct text under one of `subs`, document order, the sub's own text
120
+ * included. This is a LIFT: relocation of text that exists, never synthesis.
121
+ * Only ever reached as a fallback, and always recorded.
122
+ */
123
+ | {
124
+ kind: "sub-text-lift";
125
+ subs: string[];
126
+ }
127
+ /** All text in a descendant's subtree, document order, space-joined. */
128
+ | {
129
+ kind: "subtree-text";
130
+ subs: string[];
131
+ }
132
+ /** This node's ordered children, emitted as instances. */
133
+ | {
134
+ kind: "children";
135
+ }
136
+ /**
137
+ * T4: the ordered children of exactly one descendant instance of the named
138
+ * sub, emitted as instances into a slot. The sub is the compound's declared
139
+ * structural region (dialog-content, sheet-footer, …); its children go
140
+ * through the same rewriting as any parent's child list, so nested subs
141
+ * resolve by THIS plan's dispositions and claimed subs are skipped. Zero
142
+ * instances leave the destination absent (gate A3 arbitrates); more than
143
+ * one refuses — a slot names one region, never a series.
144
+ */
145
+ | {
146
+ kind: "sub-children";
147
+ subs: string[];
148
+ }
149
+ /**
150
+ * T2: the node's own surface `id`. Item-local — only valid inside a
151
+ * collect's item fields, where each repeated item contributes the one
152
+ * datum it carries (the production radio-group-item carries nothing else).
153
+ */
154
+ | {
155
+ kind: "self-id";
156
+ }
157
+ /** No source: a declarative A2UI action synthesized from a deterministic slug. */
158
+ | {
159
+ kind: "synthesized-action";
160
+ };
161
+ /**
162
+ * Where information lands on the emitted instance. Names are literal — never
163
+ * computed — so a plan's destinations can be validated against its own
164
+ * `propMap`/`structural` at load time. This matters more than it looks: gate A3
165
+ * validates emitted messages against the catalog and so catches an OMITTED
166
+ * property, but an INVENTED one would pass. Load-time checking is the only
167
+ * place that asymmetry can be closed.
168
+ */
169
+ export type Destination =
170
+ /** A scalar property. */
171
+ {
172
+ kind: "prop";
173
+ name: string;
174
+ }
175
+ /** A ComponentId property referencing a synthesized text primitive. */
176
+ | {
177
+ kind: "text-child";
178
+ name: string;
179
+ }
180
+ /** A single ComponentId property (>1 child is wrapped in the profile's wrap component). */
181
+ | {
182
+ kind: "slot";
183
+ name: string;
184
+ }
185
+ /** A ChildList property. */
186
+ | {
187
+ kind: "slots";
188
+ name: string;
189
+ }
190
+ /** An Action property. */
191
+ | {
192
+ kind: "action";
193
+ name: string;
194
+ };
195
+ /**
196
+ * Legacy write order. The engine applies routes and collects sorted by this,
197
+ * never by declaration index or traversal happenstance — the same discipline
198
+ * the diagnostics use, applied to output. Object key order is observable
199
+ * (artifacts are hashed), so "when a route writes" is part of the contract and
200
+ * belongs in data rather than in the shape of a method.
201
+ */
202
+ export declare enum WriteOrder {
203
+ /** Props copied verbatim into same-named destinations; wins over consumption. */
204
+ Verbatim = 10,
205
+ /** Sub-scoped consumption. Resolved by ONE document-order walk (see below). */
206
+ Consume = 20,
207
+ /** Repetition, and the caption-style routes that belong to the same strategy. */
208
+ CollectLead = 30,
209
+ Collect = 31,
210
+ /** The node's own text. */
211
+ SelfText = 40,
212
+ SelfTextChild = 41,
213
+ /** T4 slot routes: instance emission from a named sub's children. */
214
+ Slot = 45,
215
+ /** Synthesis with no source in the subtree. */
216
+ Action = 50,
217
+ /** Children as instance references, once every value destination is settled. */
218
+ Children = 60
219
+ }
220
+ /** Moves one piece of information from a source to a destination. */
221
+ export interface Route {
222
+ /** When this route writes, relative to every other route and collect. */
223
+ order: WriteOrder;
224
+ /** Tried in order; the first that yields a value wins. */
225
+ from: Selector[];
226
+ to: Destination;
227
+ /**
228
+ * v1 asymmetry, preserved deliberately: `textProp`/`textChildProp` overwrite
229
+ * whatever is already there, while every consuming directive is first-wins.
230
+ * Making it a field rather than a code path keeps the difference reviewable.
231
+ */
232
+ overwrite?: boolean;
233
+ /** Which authored directive produced this route — provenance for fidelity. */
234
+ origin: string;
235
+ }
236
+ /**
237
+ * Collects a repeated sub-structure into an array-valued destination. Item
238
+ * fields are themselves routes or nested collects, which is why a paired group
239
+ * (an options array whose value is a child reference) needs no separate
240
+ * primitive — and why `subTable`'s three-level shape is nesting rather than a
241
+ * grammar.
242
+ */
243
+ export interface Collect {
244
+ /** When this collect writes, relative to every other route and collect. */
245
+ order: WriteOrder;
246
+ /**
247
+ * T2 item-mode: each descendant matching `of` becomes ONE RECORD whose
248
+ * fields resolve against that item's own node (self.text, self.props.X,
249
+ * self.id — never joins, never siblings). Mutually exclusive with the
250
+ * table-mode `item`/`scalar` nesting.
251
+ */
252
+ fields?: Record<string, Selector>;
253
+ /**
254
+ * T3: a DECLARED key join onto a sibling sub-family. Each left item's key
255
+ * must match exactly one counterpart (0..1 when `optional`); zero, several,
256
+ * duplicate keys on either side, missing key values, and dangling
257
+ * counterparts all refuse. The profile names the relation; the emitter
258
+ * never infers one — no positions, no nearest-sibling, no similarity.
259
+ */
260
+ join?: CollectJoin;
261
+ /**
262
+ * True when every repetition's cells concatenate into ONE flat list (v1's
263
+ * header semantics: two header rows append into a single column list);
264
+ * false when each repetition becomes its own record. Declared data — the
265
+ * engine must never infer this from names or origins.
266
+ */
267
+ flatten: boolean;
268
+ /** Descendant components that open one repetition. */
269
+ of: string[];
270
+ /** Array-valued destination, or `inline` when nested into a parent's field. */
271
+ as: Destination | {
272
+ kind: "inline";
273
+ };
274
+ /**
275
+ * Per-item fields. A collect whose item is a single unnamed scalar (a table
276
+ * row's cells) uses `scalar` instead.
277
+ */
278
+ item?: Record<string, Route | Collect>;
279
+ /** The item IS one scalar, produced by this route. Mutually exclusive with `item`. */
280
+ scalar?: Route;
281
+ /** Declared join key for paired collections. Never inferred. Reserved for T3. */
282
+ joinOn?: string;
283
+ origin: string;
284
+ }
285
+ /**
286
+ * Sub-components this plan explicitly discards, with the authored reason. v1
287
+ * spelled this inside `subTable.drops`; it is not a table concept.
288
+ */
289
+ export interface CollectJoin {
290
+ /** The joined sub-family (sub-component or component ids). */
291
+ with: string[];
292
+ /** Key extracted from each LEFT item (item-local selector). */
293
+ leftKey: Selector;
294
+ /** Key extracted from each JOINED item (item-local selector). */
295
+ rightKey: Selector;
296
+ /**
297
+ * Record fields sourced from the JOINED side. Item-local selectors, plus
298
+ * the slot-valued `{ kind: "joined-children" }`: the counterpart's children
299
+ * emit as instances and the record carries the reference — Repetition's
300
+ * ratified slot-valued field, NOT T4 multi-slot routing.
301
+ */
302
+ fields: Record<string, Selector | {
303
+ kind: "joined-children";
304
+ }>;
305
+ /** 0..1 counterparts allowed when true; exactly one required otherwise. */
306
+ optional: boolean;
307
+ /** Which authored rule produced this — provenance for the ledger. */
308
+ origin: string;
309
+ }
310
+ export type Drops = Record<string, string>;
311
+ /** The whole of how a plan projects a surface node. Replaces SurfacePlanDirectives. */
312
+ export interface SurfaceModel {
313
+ /**
314
+ * T1: this plan's component emits NO instance — every node using it
315
+ * dissolves at its parent's child-collection, children rising in place,
316
+ * with `transparentDonate` harvested per dissolved instance. Refused for
317
+ * the surface root at emit (renderers need components[0] = "root").
318
+ */
319
+ transparent: boolean;
320
+ /** Donations carried by a transparent PLAN (per dissolved instance). */
321
+ transparentDonate: Donation[];
322
+ routes: Route[];
323
+ collects: Collect[];
324
+ /** Identity assigned to named sub-components met inside this node's subtree. */
325
+ subIdentity: Record<string, Identity>;
326
+ drops: Drops;
327
+ /**
328
+ * True when this plan absorbs its whole subtree into props, so children are
329
+ * not emitted as instances. Derived during desugaring from the presence of
330
+ * consuming routes/collects — never authored.
331
+ */
332
+ consumesSubtree: boolean;
333
+ }
334
+ export declare const emptySurfaceModel: () => SurfaceModel;
335
+ /** Narrowing helper: a collect's field may be either shape. */
336
+ export declare const isCollect: (field: Route | Collect) => field is Collect;
337
+ /** Human-readable selector, for warnings and fidelity provenance. */
338
+ export declare function describeSelector(s: Selector): string;
339
+ /** Human-readable destination, for warnings and fidelity provenance. */
340
+ export declare const describeDestination: (d: Destination | {
341
+ kind: "inline";
342
+ }) => string;
343
+ /** Every destination a model writes, for load-time validation against the plan. */
344
+ export declare function destinationsOf(model: SurfaceModel): Destination[];
345
+ /**
346
+ * Every sub-component id this model accounts for, and how. This is what makes
347
+ * `subCoverage` derivable instead of authored: a sub is covered when the model
348
+ * consumes it, dissolves it, re-identifies it, or drops it with a reason —
349
+ * and anything else is an unclassified sub, which must fail closed.
350
+ */
351
+ export declare function referencedSubs(model: SurfaceModel): Map<string, string>;
352
+ /**
353
+ * Derived sub-component coverage: the honest replacement for the authored
354
+ * `subCoverage` prose, which the engine never reads and which only a test in
355
+ * another repo enforces.
356
+ *
357
+ * A sub is covered when the model NAMES it, or — when the plan absorbs its
358
+ * whole subtree — when it is merely walked through on the way to a named one.
359
+ * That second case is not slack: `alert-dialog-content`, `-header` and
360
+ * `-footer` are real, and a derivation that only counted named subs would
361
+ * report them missing while the emitter handles them correctly.
362
+ *
363
+ * Anything else is unclassified, and must fail closed.
364
+ */
365
+ export declare function deriveSubCoverage(model: SurfaceModel, contractSubs: string[]): Map<string, string>;
366
+ /** Subs the contract declares that the model cannot account for at all. */
367
+ export declare const unclassifiedSubs: (model: SurfaceModel, contractSubs: string[]) => string[];
368
+ export type { Json };
@@ -0,0 +1,169 @@
1
+ /**
2
+ * Legacy write order. The engine applies routes and collects sorted by this,
3
+ * never by declaration index or traversal happenstance — the same discipline
4
+ * the diagnostics use, applied to output. Object key order is observable
5
+ * (artifacts are hashed), so "when a route writes" is part of the contract and
6
+ * belongs in data rather than in the shape of a method.
7
+ */
8
+ export var WriteOrder;
9
+ (function (WriteOrder) {
10
+ /** Props copied verbatim into same-named destinations; wins over consumption. */
11
+ WriteOrder[WriteOrder["Verbatim"] = 10] = "Verbatim";
12
+ /** Sub-scoped consumption. Resolved by ONE document-order walk (see below). */
13
+ WriteOrder[WriteOrder["Consume"] = 20] = "Consume";
14
+ /** Repetition, and the caption-style routes that belong to the same strategy. */
15
+ WriteOrder[WriteOrder["CollectLead"] = 30] = "CollectLead";
16
+ WriteOrder[WriteOrder["Collect"] = 31] = "Collect";
17
+ /** The node's own text. */
18
+ WriteOrder[WriteOrder["SelfText"] = 40] = "SelfText";
19
+ WriteOrder[WriteOrder["SelfTextChild"] = 41] = "SelfTextChild";
20
+ /** T4 slot routes: instance emission from a named sub's children. */
21
+ WriteOrder[WriteOrder["Slot"] = 45] = "Slot";
22
+ /** Synthesis with no source in the subtree. */
23
+ WriteOrder[WriteOrder["Action"] = 50] = "Action";
24
+ /** Children as instance references, once every value destination is settled. */
25
+ WriteOrder[WriteOrder["Children"] = 60] = "Children";
26
+ })(WriteOrder || (WriteOrder = {}));
27
+ export const emptySurfaceModel = () => ({
28
+ transparent: false,
29
+ transparentDonate: [],
30
+ routes: [],
31
+ collects: [],
32
+ subIdentity: {},
33
+ drops: {},
34
+ consumesSubtree: false,
35
+ });
36
+ /** Narrowing helper: a collect's field may be either shape. */
37
+ export const isCollect = (field) => field.of !== undefined;
38
+ /** Human-readable selector, for warnings and fidelity provenance. */
39
+ export function describeSelector(s) {
40
+ switch (s.kind) {
41
+ case "self-text":
42
+ return "self.text";
43
+ case "self-prop":
44
+ return `self.props.${s.prop}`;
45
+ case "sub-text":
46
+ return `sub(${s.subs.join("|")}).text`;
47
+ case "sub-label":
48
+ return `sub(${s.subs.join("|")}).label`;
49
+ case "sub-text-lift":
50
+ return `sub(${s.subs.join("|")}).firstText`;
51
+ case "subtree-text":
52
+ return `sub(${s.subs.join("|")}).subtreeText`;
53
+ case "children":
54
+ return "children";
55
+ case "sub-children":
56
+ return `sub(${s.subs.join("|")}).children`;
57
+ case "self-id":
58
+ return "self.id";
59
+ case "synthesized-action":
60
+ return "synthesized.action";
61
+ }
62
+ }
63
+ /** Human-readable destination, for warnings and fidelity provenance. */
64
+ export const describeDestination = (d) => d.kind === "inline" ? "inline" : `${d.kind}:${d.name}`;
65
+ /** Every destination a model writes, for load-time validation against the plan. */
66
+ export function destinationsOf(model) {
67
+ const out = model.routes.map((r) => r.to);
68
+ const walk = (c) => {
69
+ if (c.as.kind !== "inline")
70
+ out.push(c.as);
71
+ for (const field of Object.values(c.item ?? {})) {
72
+ if (isCollect(field))
73
+ walk(field);
74
+ else
75
+ out.push(field.to);
76
+ }
77
+ if (c.scalar)
78
+ out.push(c.scalar.to);
79
+ };
80
+ for (const c of model.collects)
81
+ walk(c);
82
+ return out;
83
+ }
84
+ /**
85
+ * Every sub-component id this model accounts for, and how. This is what makes
86
+ * `subCoverage` derivable instead of authored: a sub is covered when the model
87
+ * consumes it, dissolves it, re-identifies it, or drops it with a reason —
88
+ * and anything else is an unclassified sub, which must fail closed.
89
+ */
90
+ export function referencedSubs(model) {
91
+ const subs = new Map();
92
+ const note = (id, how) => {
93
+ if (!subs.has(id))
94
+ subs.set(id, how);
95
+ };
96
+ const fromSelector = (s, how) => {
97
+ if (s.kind === "sub-text" || s.kind === "sub-label" || s.kind === "sub-text-lift" || s.kind === "subtree-text" || s.kind === "sub-children") {
98
+ for (const id of s.subs)
99
+ note(id, how);
100
+ }
101
+ };
102
+ for (const r of model.routes) {
103
+ for (const s of r.from) {
104
+ fromSelector(s, s.kind === "sub-children" ? `routed to ${describeDestination(r.to)}` : `consumed into ${describeDestination(r.to)}`);
105
+ }
106
+ }
107
+ const walk = (c) => {
108
+ for (const id of c.of)
109
+ note(id, `collected into ${describeDestination(c.as)}`);
110
+ if (c.join)
111
+ for (const id of c.join.with)
112
+ note(id, `joined into ${describeDestination(c.as)}`);
113
+ for (const field of Object.values(c.item ?? {})) {
114
+ if (isCollect(field))
115
+ walk(field);
116
+ else
117
+ for (const s of field.from)
118
+ fromSelector(s, `collected into ${describeDestination(field.to)}`);
119
+ }
120
+ if (c.scalar)
121
+ for (const s of c.scalar.from)
122
+ fromSelector(s, `collected as a scalar item`);
123
+ };
124
+ for (const c of model.collects)
125
+ walk(c);
126
+ for (const [id, identity] of Object.entries(model.subIdentity)) {
127
+ if (identity.kind === "as-text") {
128
+ note(id, `re-identified as text (${identity.variant})`);
129
+ }
130
+ else if (identity.kind === "transparent") {
131
+ note(id, "transparent grouping");
132
+ for (const d of identity.donate ?? [])
133
+ fromSelector(d.from, `donated to ${describeDestination(d.to)}`);
134
+ }
135
+ }
136
+ for (const d of model.transparentDonate)
137
+ fromSelector(d.from, `donated to ${describeDestination(d.to)}`);
138
+ for (const [id, reason] of Object.entries(model.drops))
139
+ note(id, `dropped: ${reason}`);
140
+ return subs;
141
+ }
142
+ /**
143
+ * Derived sub-component coverage: the honest replacement for the authored
144
+ * `subCoverage` prose, which the engine never reads and which only a test in
145
+ * another repo enforces.
146
+ *
147
+ * A sub is covered when the model NAMES it, or — when the plan absorbs its
148
+ * whole subtree — when it is merely walked through on the way to a named one.
149
+ * That second case is not slack: `alert-dialog-content`, `-header` and
150
+ * `-footer` are real, and a derivation that only counted named subs would
151
+ * report them missing while the emitter handles them correctly.
152
+ *
153
+ * Anything else is unclassified, and must fail closed.
154
+ */
155
+ export function deriveSubCoverage(model, contractSubs) {
156
+ const covered = referencedSubs(model);
157
+ for (const sub of contractSubs) {
158
+ if (covered.has(sub))
159
+ continue;
160
+ if (model.consumesSubtree)
161
+ covered.set(sub, "transparent grouping, traversed by the subtree consumer");
162
+ }
163
+ return covered;
164
+ }
165
+ /** Subs the contract declares that the model cannot account for at all. */
166
+ export const unclassifiedSubs = (model, contractSubs) => {
167
+ const covered = deriveSubCoverage(model, contractSubs);
168
+ return contractSubs.filter((s) => !covered.has(s));
169
+ };
@@ -0,0 +1,109 @@
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: the grammar admits exactly what the engine can emit. T1
18
+ * (transparent identity + constrained control donation) landed as the
19
+ * `transparent` / `donate` spellings below; spellings whose engine support
20
+ * has not landed (`joinOn` pairing — T3, multi-slot children routing — T4,
21
+ * variant identity — T5) are still refused, never reinterpreted.
22
+ */
23
+ import type { ComponentPlan } from "./profiles.js";
24
+ import { type Destination, type Selector, type SurfaceModel } from "./model.js";
25
+ /** One pathed finding; parse-v2 refusals reuse the loader's issue shape. */
26
+ export interface ParseIssue {
27
+ path: string;
28
+ message: string;
29
+ }
30
+ export declare class ProfileParseError extends Error {
31
+ readonly issues: ParseIssue[];
32
+ constructor(issues: ParseIssue[]);
33
+ }
34
+ export interface SurfaceV2Route {
35
+ /** Ordered fallback chain; the first selector that yields a value wins. */
36
+ from: string[];
37
+ to: string;
38
+ overwrite?: boolean;
39
+ }
40
+ export interface SurfaceV2Collect {
41
+ /** Sections (table-mode) or the repeated items themselves (item-mode). */
42
+ of: string[];
43
+ /** Array-valued destination (`prop:<name>`). */
44
+ into: string;
45
+ /** table-mode: `flat` concatenates all rows into one list; `records` keeps one record per row. */
46
+ shape?: "flat" | "records";
47
+ /** table-mode: record field carrying each row's cells; required when shape is `records`. */
48
+ field?: string;
49
+ /** table-mode: repetitions inside a section. */
50
+ row?: string[];
51
+ /** table-mode: cells inside a repetition, flattened to their subtree text. */
52
+ cells?: string[];
53
+ /**
54
+ * T2 item-mode: each matching descendant becomes one record; every field
55
+ * resolves against THAT item's own node. Selectors: `self.text`,
56
+ * `self.props.<prop>`, `self.id`.
57
+ */
58
+ item?: Record<string, string>;
59
+ /**
60
+ * T3: a DECLARED key join onto a sibling sub-family. `on.left` extracts the
61
+ * key from each item, `on.right` from each counterpart; `fields` source
62
+ * record fields from the JOINED side (item-local selectors, or the literal
63
+ * `"children"` for the slot-valued field: the counterpart's children emit
64
+ * as instances and the record carries the reference). `optional: true`
65
+ * relaxes exactly-one to 0..1. The profile names the relation; nothing is
66
+ * ever inferred from position or adjacency.
67
+ */
68
+ join?: {
69
+ with: string[];
70
+ on: {
71
+ left: string;
72
+ right: string;
73
+ };
74
+ fields: Record<string, string>;
75
+ optional?: boolean;
76
+ };
77
+ }
78
+ export interface SurfaceV2Donate {
79
+ /** `self.text` or `sub(<id>|…).text` — where the donated value comes from. */
80
+ from: string;
81
+ /** `prop:<name>` on the boundary's single eligible control. */
82
+ to: string;
83
+ }
84
+ export type SurfaceV2Sub = "transparent" | {
85
+ transparent: true | {
86
+ donate: SurfaceV2Donate[];
87
+ };
88
+ } | {
89
+ asText: string;
90
+ } | {
91
+ drop: string;
92
+ };
93
+ export interface SurfaceV2 {
94
+ /**
95
+ * T1 transparent identity: this component emits no instance; children rise.
96
+ * `true`, or an object carrying constrained control donations scoped to
97
+ * each dissolved instance's subtree.
98
+ */
99
+ transparent?: true | {
100
+ donate: SurfaceV2Donate[];
101
+ };
102
+ routes?: SurfaceV2Route[];
103
+ collects?: SurfaceV2Collect[];
104
+ /** Identity or disposition of named sub-components met in this node's subtree. */
105
+ subs?: Record<string, SurfaceV2Sub>;
106
+ }
107
+ export declare function parseSelector(raw: string): Selector | null;
108
+ export declare function parseDestination(raw: string): Destination | null;
109
+ export declare function parseSurfaceV2(surface: SurfaceV2, plan: ComponentPlan, path: string): SurfaceModel;