@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
@@ -1,17 +1,34 @@
1
1
  /**
2
- * scaffoldProfile: a mechanical 1:1 draft Profile from a dspack contract.
2
+ * scaffoldProfile: a mechanical draft Profile from a dspack contract — in the
3
+ * v2 primitive language, honest about what it cannot decide.
3
4
  *
4
- * Seeded from the json-render CatalogModel (the judgment-free 1:1 IR): every
5
- * top-level contract component becomes a ComponentPlan with verbatim prop
6
- * projections; compound sub-components get subFlatten directives derived from
7
- * their *declared* `acceptsChildren` (text → asText, otherwise transparent).
5
+ * The old scaffold had two measured failures. For props-based contracts it
6
+ * emitted no surface plan at all, so any surface with children hit a hard
7
+ * throw — catalog-valid, surface-unusable, for exactly the idiom the docs
8
+ * call typical. And for compounds it INVENTED the judgment it should have
9
+ * surfaced: every sub auto-classified transparent-or-asText from
10
+ * `acceptsChildren`, which is precisely the guess that is wrong for a
11
+ * repeated item like `radio-group-item` (the measured T2 gap).
8
12
  *
9
- * The scaffold invents no judgment: no valueMaps, no casualties, no lossy
10
- * projections. Everything a human must review is returned as `notes` — the
11
- * scaffold's analog of dspack-export's `awaitingAuthorship`. The result is a
12
- * valid v1 profile document (loadProfile accepts it) whose transform passes
13
- * gates A1/A2 with full coverage; whether each mapping is *right* is the
14
- * author's call.
13
+ * This scaffold derives instead of guessing:
14
+ *
15
+ * - identity and verbatim prop projections, as before — provable 1:1;
16
+ * - a `children` route where the contract OBSERVABLY supports children:
17
+ * the component appears with child nodes in a worked example, or declares
18
+ * sub-components. Provenance lands in the synthNote;
19
+ * - a `text` route where a worked example shows the component carrying text;
20
+ * - sub-components are NOT decided. Each one is an explicit unresolved
21
+ * decision: listed in `notes`, recorded under `x-scaffold.unresolved`, and
22
+ * absent from the surface block — which the v2 contract gate then refuses
23
+ * to transform, per sub, until the author decides (route, collect,
24
+ * transparent, asText, or drop with a reason). A scaffold that transforms
25
+ * before those decisions exist would be inventing them.
26
+ *
27
+ * The result loads (loadProfile accepts it). For a contract with no compound
28
+ * sub-families it transforms immediately; for one with compounds, transform
29
+ * refuses with the exact per-sub checklist. That refusal is the deliverable:
30
+ * the scaffold's job is to make the remaining judgment explicit, not to
31
+ * pretend it has been exercised.
15
32
  */
16
33
  import type { DspackDoc, DspackProp } from "../types.js";
17
34
  import { buildCatalogModel, type CatalogComponent } from "../targets/json-render/model.js";
@@ -33,7 +50,7 @@ export interface ScaffoldNote {
33
50
  }
34
51
 
35
52
  export interface ScaffoldResult {
36
- /** A v1 profile *document* (includes profileVersion; loadProfile accepts it). */
53
+ /** A v2 profile *document* (includes profileVersion; loadProfile accepts it). */
37
54
  profile: Record<string, unknown>;
38
55
  /** Everything that needs human judgment before the profile is trustworthy. */
39
56
  notes: ScaffoldNote[];
@@ -41,7 +58,6 @@ export interface ScaffoldResult {
41
58
 
42
59
  const DYN_STRING = { $ref: "#/$defs/DynamicString" };
43
60
  const CHILD_LIST = { $ref: "#/$defs/ChildList" };
44
- const COMP_ID = { $ref: "#/$defs/ComponentId" };
45
61
 
46
62
  const TEXT_PLAN = {
47
63
  a2ui: "Text",
@@ -83,7 +99,6 @@ const COLUMN_PLAN = {
83
99
 
84
100
  interface SubDecl {
85
101
  id: string;
86
- acceptsChildren?: string;
87
102
  }
88
103
 
89
104
  function subsOf(doc: DspackDoc, id: string): SubDecl[] {
@@ -96,14 +111,46 @@ function subsOf(doc: DspackDoc, id: string): SubDecl[] {
96
111
  return out;
97
112
  }
98
113
 
114
+ interface Observation {
115
+ children?: string; // example id where the component was seen with child nodes
116
+ text?: string; // example id where it was seen carrying text
117
+ }
118
+
119
+ /**
120
+ * What the contract's own worked examples show each component doing. The
121
+ * example corpus is the contract's definition of "expressible", which makes
122
+ * this measurement, not judgment: a component observed with children needs a
123
+ * children story; one observed with text needs a text story.
124
+ */
125
+ function observeExamples(doc: DspackDoc): Map<string, Observation> {
126
+ const seen = new Map<string, Observation>();
127
+ const examples = (doc as { examples?: Array<{ id?: string; surface?: { root?: unknown } }> }).examples ?? [];
128
+ const visit = (node: unknown, exampleId: string): void => {
129
+ if (!node || typeof node !== "object") return;
130
+ const n = node as { component?: string; text?: unknown; children?: unknown[]; slots?: Record<string, unknown[]> };
131
+ if (typeof n.component === "string") {
132
+ const entry = seen.get(n.component) ?? {};
133
+ const childNodes = [...(n.children ?? []), ...Object.values(n.slots ?? {}).flat()];
134
+ if (childNodes.length > 0 && entry.children === undefined) entry.children = exampleId;
135
+ if (typeof n.text === "string" && entry.text === undefined) entry.text = exampleId;
136
+ seen.set(n.component, entry);
137
+ for (const child of childNodes) visit(child, exampleId);
138
+ }
139
+ };
140
+ for (const ex of examples) visit(ex.surface?.root, ex.id ?? "(unnamed example)");
141
+ return seen;
142
+ }
143
+
99
144
  export function scaffoldProfile(doc: DspackDoc, options: ScaffoldOptions): ScaffoldResult {
100
145
  const notes: ScaffoldNote[] = [];
101
146
  const model = buildCatalogModel(doc, {});
102
147
  const byId = new Map<string, CatalogComponent>(model.components.map((c) => [c.dspackId, c]));
103
148
  const topLevelIds = Object.keys(doc.components ?? {});
104
149
  const name = (id: string) => byId.get(id)?.name ?? id;
150
+ const observed = observeExamples(doc);
105
151
 
106
152
  const usedNames = new Set(topLevelIds.map((id) => name(id)));
153
+ const unresolvedByComponent: Record<string, string[]> = {};
107
154
 
108
155
  const components = topLevelIds.map((id) => {
109
156
  const entry = byId.get(id)!;
@@ -131,46 +178,68 @@ export function scaffoldProfile(doc: DspackDoc, options: ScaffoldOptions): Scaff
131
178
  .map(([propName]) => propName)
132
179
  .filter((propName) => propName in propMap);
133
180
 
181
+ const structural: Record<string, unknown> = {};
182
+ const routes: Array<Record<string, unknown>> = [];
183
+ const subs = subsOf(doc, id);
184
+ const seen = observed.get(id) ?? {};
185
+
186
+ // Children: only where the contract observably supports them — a worked
187
+ // example shows child nodes, or the component declares sub-components
188
+ // (whose instances arrive as its children in every surface).
189
+ const childrenBecause = seen.children
190
+ ? `observed with child nodes in worked example '${seen.children}'`
191
+ : subs.length > 0
192
+ ? `declares ${subs.length} sub-component(s), which arrive as its children in surfaces`
193
+ : undefined;
194
+ if (childrenBecause) {
195
+ structural.children = {
196
+ schema: CHILD_LIST,
197
+ description: "Child component IDs, in order.",
198
+ synthNote: `Scaffolded: ${childrenBecause}.`,
199
+ };
200
+ required.push("children");
201
+ routes.push({ from: ["children"], to: "slots:children" });
202
+ }
203
+
204
+ // Text: only where a worked example shows the component carrying it.
205
+ if (seen.text) {
206
+ structural.text = {
207
+ schema: DYN_STRING,
208
+ description: "The component's text content.",
209
+ synthNote: `Scaffolded: observed carrying text in worked example '${seen.text}'.`,
210
+ };
211
+ routes.push({ from: ["self.text"], to: "prop:text" });
212
+ }
213
+
214
+ if (!childrenBecause && !seen.text && Object.keys(propMap).length === 0) {
215
+ notes.push({
216
+ target: id,
217
+ note: "no declarative props, and never observed with children or text in a worked example; the contract likely needs enrichment before this mapping is useful.",
218
+ });
219
+ }
220
+
134
221
  const plan: Record<string, unknown> = {
135
222
  a2ui: entry.name,
136
223
  dspackId: id,
137
224
  commons: ["ComponentCommon"],
138
- structural: {},
225
+ structural,
139
226
  required,
140
227
  };
141
228
  if (Object.keys(propMap).length > 0) plan.propMap = propMap;
142
- else
143
- notes.push({
144
- target: id,
145
- note: "no declarative props were scaffolded; the contract likely needs prop enrichment before this mapping is useful.",
146
- });
229
+ if (routes.length > 0) plan.surface = { routes };
147
230
 
148
- const subs = subsOf(doc, id);
231
+ // Sub-components: explicit unresolved decisions, never guesses. The v2
232
+ // contract gate refuses to transform until each is decided — that refusal,
233
+ // with its per-sub checklist, is the scaffold handing judgment to its
234
+ // owner rather than exercising it.
149
235
  if (subs.length > 0) {
150
- const transparent = subs.filter((s) => s.acceptsChildren !== "text").map((s) => s.id);
151
- const asText = Object.fromEntries(subs.filter((s) => s.acceptsChildren === "text").map((s) => [s.id, "body"]));
152
- plan.structural = {
153
- child: {
154
- schema: COMP_ID,
155
- description: "The ID of the single child component. Wrap multiple elements in a Column and pass its ID.",
156
- synthNote:
157
- "Scaffolded: the compound's sub-components flatten (subFlatten) and collapse to a single, possibly Column-wrapped, child slot.",
158
- },
159
- };
160
- (plan.required as string[]).push("child");
161
- plan.surfacePlan = { childProp: "child", subFlatten: { transparent, asText } };
162
- plan.subCoverage = Object.fromEntries(
163
- subs.map((s) => [
164
- s.id,
165
- s.acceptsChildren === "text"
166
- ? "text -> synthesized Text (variant body) [scaffolded from acceptsChildren: text — review]"
167
- : "transparent grouping: children splice inline, in order [scaffolded — review]",
168
- ]),
169
- );
170
- notes.push({
171
- target: id,
172
- note: `compound scaffolded from declared acceptsChildren (${subs.length} sub-components: ${transparent.length} transparent, ${Object.keys(asText).length} asText); review each subCoverage line and pick text variants.`,
173
- });
236
+ unresolvedByComponent[id] = subs.map((s) => s.id);
237
+ for (const sub of subs) {
238
+ notes.push({
239
+ target: `${id}.${sub.id}`,
240
+ note: "unresolved sub-component: decide route (consume its text/label), collect (repeated items), transparent (dissolve), asText (re-identify), or drop (with a reason). transform() refuses until this is decided.",
241
+ });
242
+ }
174
243
  }
175
244
 
176
245
  return plan;
@@ -201,11 +270,11 @@ export function scaffoldProfile(doc: DspackDoc, options: ScaffoldOptions): Scaff
201
270
  }
202
271
 
203
272
  const profile: Record<string, unknown> = {
204
- profileVersion: "1",
273
+ profileVersion: "2",
205
274
  catalogTitle: options.catalogTitle ?? `${doc.name} — A2UI catalog (compiled from dspack)`,
206
275
  catalogDescription:
207
276
  options.catalogDescription ??
208
- `A2UI catalog compiled from the ${doc.name} dspack contract by scaffoldProfile. Mechanical 1:1 draft: review the scaffold notes before trusting any mapping.`,
277
+ `A2UI catalog compiled from the ${doc.name} dspack contract by scaffoldProfile. Mechanical draft in the v2 primitive language: review the scaffold notes, and resolve every listed sub-component decision, before trusting any mapping.`,
209
278
  catalogIdBase: options.catalogIdBase,
210
279
  instructions: options.instructions ?? "For layout, use the Column component to organize other components.",
211
280
  primaryColorToken,
@@ -218,6 +287,14 @@ export function scaffoldProfile(doc: DspackDoc, options: ScaffoldOptions): Scaff
218
287
  wrapComponent: "Column",
219
288
  wrapChildrenProp: "children",
220
289
  },
290
+ ...(Object.keys(unresolvedByComponent).length > 0
291
+ ? {
292
+ "x-scaffold": {
293
+ unresolved: unresolvedByComponent,
294
+ note: "Every listed sub-component is an authoring decision the scaffold deliberately did not make. transform() refuses until each is resolved in the owning plan's surface block.",
295
+ },
296
+ }
297
+ : {}),
221
298
  };
222
299
 
223
300
  notes.push({