@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,194 @@
1
+ /**
2
+ * v1 surface-plan directives -> the internal transformation model.
3
+ *
4
+ * Every authored directive is surface syntax for one or more Identity / Route /
5
+ * Collect facts. This module is the whole of that translation, and it is the
6
+ * only place in the package that knows the v1 directive names: past here, the
7
+ * engine sees primitives.
8
+ *
9
+ * The translation must be EXACT, not approximate — v1 profiles keep emitting
10
+ * byte-identically (src/byte-neutral.test.ts is the gate), so every quirk of
11
+ * the original directives is reproduced here deliberately, with the quirk named:
12
+ *
13
+ * - `subText` visits the node itself, not only its descendants;
14
+ * - `subButtonText` accepts only label-bearing components, then falls back to
15
+ * an audited lift of the first direct text under the sub;
16
+ * - `textProp`/`textChildProp` overwrite, while everything else is first-wins;
17
+ * - `subTable` accepts a header-cell wherever a cell is expected, concatenates
18
+ * multiple header rows into one flat column list, and takes the first caption;
19
+ * - `structuralPassthrough` wins over any consumed value.
20
+ *
21
+ * Reading this file top to bottom is also the v1 -> internal mapping table.
22
+ */
23
+ import type { ComponentPlan, SurfacePlanDirectives } from "./profiles.js";
24
+ import { parseSurfaceV2 } from "./parse-v2.js";
25
+ import { emptySurfaceModel, WriteOrder, type Collect, type Route, type SurfaceModel } from "./model.js";
26
+
27
+ /** Cache: a plan's model is derived once, not per emitted node. */
28
+ const cache = new WeakMap<ComponentPlan, SurfaceModel>();
29
+
30
+ /**
31
+ * The internal model for a plan, whichever language it is written in: a v2
32
+ * `surface` block parses directly; v1 directives desugar. Both languages meet
33
+ * the engine only as this model — the engine cannot tell them apart, which is
34
+ * the compatibility guarantee.
35
+ */
36
+ export function surfaceModelOf(plan: ComponentPlan): SurfaceModel {
37
+ const hit = cache.get(plan);
38
+ if (hit) return hit;
39
+ const model = plan.surface ? parseSurfaceV2(plan.surface, plan, "/surface") : desugar(plan.surfacePlan ?? {});
40
+ cache.set(plan, model);
41
+ return model;
42
+ }
43
+
44
+ export function desugar(sp: SurfacePlanDirectives): SurfaceModel {
45
+ const model = emptySurfaceModel();
46
+ const route = (r: Route) => model.routes.push(r);
47
+
48
+ // structuralPassthrough: CSR props copied verbatim into same-named slots.
49
+ // Listed first because it wins over consumed values, and the engine applies
50
+ // routes in order with first-write-wins.
51
+ for (const key of sp.structuralPassthrough ?? []) {
52
+ route({
53
+ order: WriteOrder.Verbatim,
54
+ from: [{ kind: "self-prop", prop: key }],
55
+ to: { kind: "prop", name: key },
56
+ origin: "structuralPassthrough",
57
+ });
58
+ }
59
+
60
+ // subText: descendant text -> a named prop. One route per target prop; the
61
+ // selector names every sub that feeds it, which is also how two sub ids
62
+ // sharing one destination stay a single first-wins decision.
63
+ const bySubTextTarget = new Map<string, string[]>();
64
+ for (const [sub, prop] of Object.entries(sp.subText ?? {})) {
65
+ bySubTextTarget.set(prop, [...(bySubTextTarget.get(prop) ?? []), sub]);
66
+ }
67
+ for (const [prop, subs] of bySubTextTarget) {
68
+ route({ order: WriteOrder.Consume, from: [{ kind: "sub-text", subs }], to: { kind: "prop", name: prop }, origin: "subText" });
69
+ }
70
+
71
+ // subButtonText: the label of a label-bearing component inside the sub, else
72
+ // an audited lift of the first direct text under it. One route, two ordered
73
+ // selectors — the fallback that motivated Selector[].
74
+ for (const [sub, prop] of Object.entries(sp.subButtonText ?? {})) {
75
+ route({
76
+ order: WriteOrder.Consume,
77
+ from: [
78
+ { kind: "sub-label", subs: [sub] },
79
+ { kind: "sub-text-lift", subs: [sub] },
80
+ ],
81
+ to: { kind: "prop", name: prop },
82
+ origin: "subButtonText",
83
+ });
84
+ }
85
+
86
+ // subTable: three nested collects plus a caption route. Nothing here is
87
+ // table-shaped except the sub ids the profile names — the depth is nesting,
88
+ // the record key is an item field name, and the drops are ordinary drops.
89
+ if (sp.subTable) {
90
+ const t = sp.subTable;
91
+ route({
92
+ order: WriteOrder.CollectLead,
93
+ from: [{ kind: "subtree-text", subs: [t.caption] }],
94
+ to: { kind: "prop", name: t.targetCaption },
95
+ origin: "subTable.caption",
96
+ });
97
+
98
+ /** A row's cells: one flat scalar list. A header-cell counts as a cell. */
99
+ const cells = (origin: string): Collect => ({
100
+ order: WriteOrder.Collect,
101
+ flatten: false,
102
+ of: [t.row],
103
+ as: { kind: "inline" },
104
+ scalar: {
105
+ order: WriteOrder.Collect,
106
+ from: [{ kind: "subtree-text", subs: [t.cell, t.headerCell] }],
107
+ to: { kind: "prop", name: "cells" },
108
+ origin,
109
+ },
110
+ origin,
111
+ });
112
+
113
+ // Header: every header row's cells concatenate into ONE flat column list —
114
+ // v1 passes the same array to each header row, so two header rows append.
115
+ model.collects.push({
116
+ order: WriteOrder.Collect,
117
+ flatten: true,
118
+ of: [t.header],
119
+ as: { kind: "prop", name: t.targetColumns },
120
+ item: { cells: cells("subTable.header") },
121
+ origin: "subTable.header",
122
+ });
123
+
124
+ // Body: one record per row, its cells under the `cells` field. That field
125
+ // name was a literal in the v1 emitter; here it is data.
126
+ model.collects.push({
127
+ order: WriteOrder.Collect,
128
+ flatten: false,
129
+ of: [t.body],
130
+ as: { kind: "prop", name: t.targetRows },
131
+ item: { cells: cells("subTable.body") },
132
+ origin: "subTable.body",
133
+ });
134
+
135
+ Object.assign(model.drops, t.drops);
136
+ }
137
+
138
+ // textProp / textChildProp: the node's own text. Both OVERWRITE, unlike every
139
+ // consuming directive — v1's one asymmetry, kept explicit.
140
+ if (sp.textProp) {
141
+ route({
142
+ order: WriteOrder.SelfText,
143
+ from: [{ kind: "self-text" }],
144
+ to: { kind: "prop", name: sp.textProp },
145
+ overwrite: true,
146
+ origin: "textProp",
147
+ });
148
+ }
149
+ if (sp.textChildProp) {
150
+ route({
151
+ order: WriteOrder.SelfTextChild,
152
+ from: [{ kind: "self-text" }],
153
+ to: { kind: "text-child", name: sp.textChildProp },
154
+ overwrite: true,
155
+ origin: "textChildProp",
156
+ });
157
+ }
158
+
159
+ // actionProp: no source in the subtree; the event name is a deterministic
160
+ // slug the engine derives. Ordered after the text routes because v1 reads a
161
+ // prop those routes may have written when naming the event.
162
+ if (sp.actionProp) {
163
+ route({
164
+ order: WriteOrder.Action,
165
+ from: [{ kind: "synthesized-action" }],
166
+ to: { kind: "action", name: sp.actionProp },
167
+ origin: "actionProp",
168
+ });
169
+ }
170
+
171
+ // subFlatten: the two halves are Identity decisions about named subs —
172
+ // dissolve, or become the synthesized text primitive with a variant.
173
+ for (const sub of sp.subFlatten?.transparent ?? []) {
174
+ model.subIdentity[sub] = { kind: "transparent" };
175
+ }
176
+ for (const [sub, variant] of Object.entries(sp.subFlatten?.asText ?? {})) {
177
+ model.subIdentity[sub] = { kind: "as-text", variant };
178
+ }
179
+
180
+ // childProp / childrenProp: children as instance references.
181
+ if (sp.childrenProp) {
182
+ route({ order: WriteOrder.Children, from: [{ kind: "children" }], to: { kind: "slots", name: sp.childrenProp }, origin: "childrenProp" });
183
+ } else if (sp.childProp) {
184
+ route({ order: WriteOrder.Children, from: [{ kind: "children" }], to: { kind: "slot", name: sp.childProp }, origin: "childProp" });
185
+ }
186
+
187
+ // Derived, never authored: a plan that absorbs its subtree into props does
188
+ // not also emit that subtree as children. v1 computed this from the presence
189
+ // of subText/subButtonText/subTable — the same three, now expressed as the
190
+ // routes and collects that consume descendants.
191
+ model.consumesSubtree = Boolean(sp.subText || sp.subButtonText || sp.subTable);
192
+
193
+ return model;
194
+ }
@@ -14,6 +14,48 @@ import { INLINED_DEFS } from "./inline-defs.js";
14
14
  import type { MappingResult } from "./mapping.js";
15
15
  import type { Profile } from "./profiles.js";
16
16
 
17
+ /**
18
+ * The catalog's function surface, from the profile's declared FunctionPlans:
19
+ * a documentation section, the name enum for FunctionCall.call, and the
20
+ * per-function anyFunction branches that validate each call's args shape.
21
+ * Definitions are declarative schemas — implementations live in renderers,
22
+ * exactly as A2UI intends; nothing executable exists here.
23
+ */
24
+ function buildFunctions(profile: Profile): {
25
+ section: Record<string, Json>;
26
+ names: string[];
27
+ anyFunction: Json;
28
+ } | null {
29
+ const declared = profile.functions;
30
+ if (!declared || Object.keys(declared).length === 0) return null;
31
+ const names = Object.keys(declared).sort();
32
+ const section: Record<string, Json> = {};
33
+ const branches: Json[] = [];
34
+ for (const name of names) {
35
+ const fn = declared[name];
36
+ section[name] = {
37
+ description: fn.description,
38
+ returnType: fn.returns,
39
+ ...(fn.args ? { args: fn.args as Json } : {}),
40
+ } as Json;
41
+ const requiredArgs =
42
+ fn.args && Array.isArray((fn.args as { required?: unknown }).required) &&
43
+ ((fn.args as { required: unknown[] }).required.length > 0);
44
+ branches.push({
45
+ type: "object",
46
+ properties: {
47
+ call: { const: name },
48
+ ...(fn.args ? { args: fn.args as Json } : { args: { type: "object", maxProperties: 0 } }),
49
+ returnType: { const: fn.returns },
50
+ },
51
+ // A function whose declared args carry required params cannot be called
52
+ // without them — the args object itself becomes required.
53
+ required: requiredArgs ? ["call", "args"] : ["call"],
54
+ } as Json);
55
+ }
56
+ return { section, names, anyFunction: { oneOf: branches } as Json };
57
+ }
58
+
17
59
  const VER_SEGMENT: Record<A2uiVersion, string> = {
18
60
  "0.9.1": "v0_9_1",
19
61
  "1.0": "v1_0",
@@ -37,6 +79,27 @@ export function emitCatalog(
37
79
  anyComponent,
38
80
  };
39
81
 
82
+ // Declared functions: the profile's function vocabulary becomes the
83
+ // catalog's `functions` section, and — the fail-closed half — FunctionCall
84
+ // regains the upstream anyFunction constraint our inlined copy dropped
85
+ // (inline-defs.ts) back when no catalog declared any functions to call.
86
+ // With the constraint restored, an instance calling an UNDECLARED function
87
+ // fails gate A3 instead of passing silently. Profiles that declare no
88
+ // functions (every v1 profile) emit byte-identically to before.
89
+ const functions = buildFunctions(profile);
90
+ if (functions) {
91
+ $defs.anyFunction = functions.anyFunction;
92
+ const fc = $defs.FunctionCall as { properties: { call: Json }; oneOf?: Json[] };
93
+ fc.properties.call = {
94
+ type: "string",
95
+ description: "The name of the function to call. Must be declared in this catalog's functions section.",
96
+ enum: functions.names,
97
+ } as Json;
98
+ // The upstream shape our inline copy dropped: the whole call object must
99
+ // match one declared function's branch (name AND args shape together).
100
+ fc.oneOf = [{ $ref: "#/$defs/anyFunction" } as Json];
101
+ }
102
+
40
103
  if (version === "0.9.1") {
41
104
  $defs.theme = themeDef(result);
42
105
  } else {
@@ -50,6 +113,7 @@ export function emitCatalog(
50
113
  description: profile.catalogDescription,
51
114
  catalogId: id,
52
115
  components: result.components,
116
+ ...(functions ? { functions: functions.section } : {}),
53
117
  $defs,
54
118
  };
55
119
 
@@ -63,6 +127,7 @@ export function emitCatalog(
63
127
  instructions: profile.instructions,
64
128
  catalogId: catalog.catalogId,
65
129
  components: catalog.components,
130
+ ...(catalog.functions ? { functions: catalog.functions } : {}),
66
131
  $defs: catalog.$defs,
67
132
  };
68
133
  }
@@ -4,6 +4,7 @@
4
4
  import type { A2uiCatalog, A2uiVersion, DspackDoc } from "../types.js";
5
5
  import { shadcnProfile, type Profile } from "./profiles.js";
6
6
  import { mapDspack, type MappingResult } from "./mapping.js";
7
+ import { validateProfileAgainstContract } from "./validate-v2.js";
7
8
  import { emitCatalog } from "./emit.js";
8
9
  import { buildReport, type ReportBundle } from "./report.js";
9
10
  import { validateCatalog, type ValidationReport } from "../validate/ajv.js";
@@ -27,6 +28,10 @@ export function transform(
27
28
  throw new Error(`Unsupported dspack major version '${doc.dspack}'. This PoC targets dspack 0.x.`);
28
29
  }
29
30
 
31
+ // v2 profiles fail closed against the contract before anything emits;
32
+ // v1 behaviour is frozen (report + --strict-coverage instead).
33
+ validateProfileAgainstContract(profile, doc);
34
+
30
35
  const mapping = mapDspack(doc, profile);
31
36
  const catalog = emitCatalog(mapping, version, profile);
32
37
  const validation = validateCatalog(catalog, version, surface);
@@ -4,6 +4,8 @@
4
4
  * for unsupported constructs, and the design-token projection. It reads only the
5
5
  * dspack JSON and the profile — never framework code.
6
6
  */
7
+ import { surfaceModelOf } from "./desugar.js";
8
+ import { deriveSubCoverage } from "./model.js";
7
9
  import type {
8
10
  CoverageEntry,
9
11
  DspackComponent,
@@ -38,6 +40,18 @@ export function mapDspack(doc: DspackDoc, profile: Profile): MappingResult {
38
40
  const warnings: Warning[] = [];
39
41
 
40
42
  const addComponent = (plan: ComponentPlan) => {
43
+ // T1: a transparent plan emits no catalog entry — nothing can instantiate
44
+ // a component that dissolves at emission. Its disposition is recorded in
45
+ // coverage; its absence from anyComponent is what keeps A3 honest.
46
+ if (surfaceModelOf(plan).transparent) {
47
+ fidelity.push({
48
+ source: `plan.${plan.dspackId ?? plan.a2ui}`,
49
+ target: "(no catalog entry)",
50
+ class: "synthesis-defaults",
51
+ note: "transparent identity: dissolves at emission; children rise to the parent's destination.",
52
+ });
53
+ return;
54
+ }
41
55
  const dspackComp = plan.dspackId ? doc.components?.[plan.dspackId] : undefined;
42
56
  if (plan.dspackId && !dspackComp) {
43
57
  warnings.push({
@@ -107,9 +121,13 @@ function computeCoverage(
107
121
  fidelity: FidelityEntry[],
108
122
  ): CoverageEntry[] {
109
123
  const mapped = new Map<string, string>(); // dspackId -> A2UI name
124
+ const byId = new Map<string, ComponentPlan>();
110
125
  // Both `components` and any `synthesized` plan that declares a `dspackId` count as mapped.
111
126
  for (const p of [...profile.components, ...profile.synthesized]) {
112
- if (p.dspackId) mapped.set(p.dspackId, p.a2ui);
127
+ if (p.dspackId) {
128
+ mapped.set(p.dspackId, p.a2ui);
129
+ byId.set(p.dspackId, p);
130
+ }
113
131
  }
114
132
 
115
133
  const casualties = new Map<string, (typeof profile.casualtyComponents)[number]>();
@@ -120,7 +138,11 @@ function computeCoverage(
120
138
  const coverage: CoverageEntry[] = [];
121
139
  for (const id of Object.keys(doc.components ?? {})) {
122
140
  if (mapped.has(id)) {
123
- coverage.push({ id, disposition: "mapped", detail: `-> ${mapped.get(id)}` });
141
+ coverage.push({
142
+ id,
143
+ disposition: "mapped",
144
+ detail: byId.get(id) && surfaceModelOf(byId.get(id)!).transparent ? "-> (transparent: dissolves at emission)" : `-> ${mapped.get(id)}`,
145
+ });
124
146
  } else if (omitted.has(id)) {
125
147
  coverage.push({ id, disposition: "omitted", detail: "declared in profile.intentionallyOmitted" });
126
148
  } else if (casualties.has(id)) {
@@ -145,6 +167,42 @@ function computeCoverage(
145
167
  });
146
168
  }
147
169
  }
170
+
171
+ // Sub-component coverage, DERIVED from each mapped compound's transformation
172
+ // model rather than authored prose (the old `subCoverage` field is v1
173
+ // documentation the engine never read; the model is what actually happens).
174
+ // An unresolved sub is an unclassified coverage entry: for v1 profiles this
175
+ // reports (and fails under --strict-coverage); v2 profiles never get here —
176
+ // validateProfileAgainstContract refuses them at entry. Measured before
177
+ // enforcement: pinned shadcn v2.3.0, astryx and acme all derive zero
178
+ // unresolved subs, so no shipped artifact changes.
179
+ for (const [id, component] of Object.entries(doc.components ?? {})) {
180
+ const subs =
181
+ (component as { composition?: { subComponents?: Array<{ id: string }> } }).composition?.subComponents ?? [];
182
+ if (subs.length === 0) continue;
183
+ const plan = byId.get(id);
184
+ if (!plan) continue; // unmapped/casualty parents: the parent entry above carries the family
185
+ const model = surfaceModelOf(plan);
186
+ const derived = deriveSubCoverage(model, subs.map((s) => s.id));
187
+ for (const sub of subs) {
188
+ const how = derived.get(sub.id);
189
+ if (how) {
190
+ coverage.push({ id: `${id}.${sub.id}`, disposition: "mapped", detail: how });
191
+ } else {
192
+ coverage.push({ id: `${id}.${sub.id}`, disposition: "unclassified" });
193
+ warnings.push({
194
+ code: `unresolved-sub:${id}.${sub.id}`,
195
+ message: `sub-component '${sub.id}' of mapped compound '${id}' is unresolved: no route consumes it, no collect gathers it, no disposition dissolves or drops it.`,
196
+ });
197
+ fidelity.push({
198
+ source: `components.${id}.composition.${sub.id}`,
199
+ target: "(silently dropped)",
200
+ class: "cannot-represent",
201
+ note: "Unresolved by the compound's transformation model. This is a coverage gap, not a deliberate casualty.",
202
+ });
203
+ }
204
+ }
205
+ }
148
206
  return coverage;
149
207
  }
150
208