@aestheticfunction/dspack-emit 0.3.1 → 0.4.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 +31 -3
- package/dist/cli.d.ts +1 -1
- package/dist/cli.js +34 -8
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/targets/a2ui/surface.js +159 -5
- package/dist/targets/json-render/profile.d.ts +10 -0
- package/dist/targets/json-render/profile.js +12 -0
- package/dist/transform/profile-load.d.ts +19 -0
- package/dist/transform/profile-load.js +58 -0
- package/dist/transform/profile.v1.schema.json +153 -0
- package/dist/transform/profiles.d.ts +41 -0
- package/dist/transform/profiles.js +60 -7
- package/dist/transform/scaffold.d.ts +40 -0
- package/dist/transform/scaffold.js +171 -0
- package/package.json +5 -2
- package/src/cli.ts +35 -9
- package/src/index.ts +2 -0
- package/src/json-render-emit.test.ts +48 -1
- package/src/profile-load.test.ts +144 -0
- package/src/profile-parity.test.ts +96 -0
- package/src/targets/a2ui/surface.ts +160 -5
- package/src/targets/json-render/profile.ts +13 -0
- package/src/transform/profile-load.ts +73 -0
- package/src/transform/profile.v1.schema.json +153 -0
- package/src/transform/profiles.ts +101 -7
- package/src/transform/scaffold.ts +229 -0
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile-as-data (PR: profile JSON schema + loadProfile + scaffoldProfile).
|
|
3
|
+
*
|
|
4
|
+
* The center-of-gravity contract for the catalog composer: a Profile expressed
|
|
5
|
+
* as JSON, validated at runtime against schema/profile.v1.schema.json, must
|
|
6
|
+
* drive the engine byte-identically to the same Profile expressed as the
|
|
7
|
+
* in-repo TypeScript object. Judgment stays with the author; the loader only
|
|
8
|
+
* gates shape.
|
|
9
|
+
*/
|
|
10
|
+
import { describe, expect, it } from "vitest";
|
|
11
|
+
import { readFileSync } from "node:fs";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { loadProfile, ProfileLoadError } from "./transform/profile-load.js";
|
|
14
|
+
import { scaffoldProfile } from "./transform/scaffold.js";
|
|
15
|
+
import { shadcnProfile } from "./transform/profiles.js";
|
|
16
|
+
import { transformFromJson } from "./transform/index.js";
|
|
17
|
+
import { emitSurface, EmitSurfaceError } from "./targets/a2ui/surface.js";
|
|
18
|
+
import type { DspackDoc, DspackSurface } from "./types.js";
|
|
19
|
+
|
|
20
|
+
const repo = (p: string) => fileURLToPath(new URL(`../${p}`, import.meta.url));
|
|
21
|
+
const load = (p: string) => JSON.parse(readFileSync(repo(p), "utf8"));
|
|
22
|
+
|
|
23
|
+
const shadcnDoc = load("input/shadcn-ui.dspack.json") as DspackDoc;
|
|
24
|
+
const astryxDoc = load("input/astryx.dspack.json") as DspackDoc;
|
|
25
|
+
|
|
26
|
+
/** The TS profile, round-tripped through JSON exactly as a user file would be. */
|
|
27
|
+
const shadcnProfileJson = () => ({ profileVersion: "1", ...structuredClone(shadcnProfile) });
|
|
28
|
+
|
|
29
|
+
describe("loadProfile", () => {
|
|
30
|
+
it("accepts the canonical shadcn profile expressed as JSON", () => {
|
|
31
|
+
const profile = loadProfile(shadcnProfileJson());
|
|
32
|
+
expect(profile.catalogTitle).toBe(shadcnProfile.catalogTitle);
|
|
33
|
+
// profileVersion is loader envelope, not engine input.
|
|
34
|
+
expect((profile as Record<string, unknown>).profileVersion).toBeUndefined();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("drives the engine byte-identically to the TypeScript profile", () => {
|
|
38
|
+
const fromTs = transformFromJson(shadcnDoc, { profile: shadcnProfile });
|
|
39
|
+
const fromJson = transformFromJson(shadcnDoc, { profile: loadProfile(shadcnProfileJson()) });
|
|
40
|
+
expect(JSON.stringify(fromJson.catalog)).toBe(JSON.stringify(fromTs.catalog));
|
|
41
|
+
expect(fromJson.validation.pass).toBe(true);
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("rejects a malformed profile with typed, pathed findings", () => {
|
|
45
|
+
const bad = shadcnProfileJson() as any;
|
|
46
|
+
bad.components[0].propMap.variant.kind = "vibes";
|
|
47
|
+
expect(() => loadProfile(bad)).toThrowError(ProfileLoadError);
|
|
48
|
+
try {
|
|
49
|
+
loadProfile(bad);
|
|
50
|
+
} catch (e) {
|
|
51
|
+
const issues = (e as ProfileLoadError).issues;
|
|
52
|
+
expect(issues.length).toBeGreaterThan(0);
|
|
53
|
+
expect(issues.some((i) => i.path.includes("propMap"))).toBe(true);
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("rejects a non-https catalogIdBase", () => {
|
|
58
|
+
const bad = shadcnProfileJson() as any;
|
|
59
|
+
bad.catalogIdBase = "http://insecure.example/catalog";
|
|
60
|
+
expect(() => loadProfile(bad)).toThrowError(ProfileLoadError);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("rejects a missing profileVersion", () => {
|
|
64
|
+
const { profileVersion: _dropped, ...bare } = shadcnProfileJson();
|
|
65
|
+
expect(() => loadProfile(bare)).toThrowError(ProfileLoadError);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("fails closed on unknown keys — x-* is the only extension surface (dspack convention)", () => {
|
|
69
|
+
const topLevel = shadcnProfileJson() as Record<string, unknown>;
|
|
70
|
+
topLevel.customBehavior = true;
|
|
71
|
+
expect(() => loadProfile(topLevel)).toThrowError(ProfileLoadError);
|
|
72
|
+
|
|
73
|
+
const inPlan = shadcnProfileJson() as any;
|
|
74
|
+
inPlan.components[0].renderHint = "fancy";
|
|
75
|
+
expect(() => loadProfile(inPlan)).toThrowError(ProfileLoadError);
|
|
76
|
+
|
|
77
|
+
// The sanctioned extension slot passes and is preserved for tools.
|
|
78
|
+
const extended = shadcnProfileJson() as Record<string, unknown>;
|
|
79
|
+
extended["x-composer"] = { note: "authored in the studio composer" };
|
|
80
|
+
const profile = loadProfile(extended) as unknown as Record<string, unknown>;
|
|
81
|
+
expect(profile["x-composer"]).toEqual({ note: "authored in the studio composer" });
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("casualty refusal cites the authored reason", () => {
|
|
86
|
+
it("names the declared casualty and its reason when a surface uses it", () => {
|
|
87
|
+
const surface: DspackSurface = {
|
|
88
|
+
dspackSurface: "0.1",
|
|
89
|
+
system: shadcnDoc.name as string,
|
|
90
|
+
intent: "destructive-action",
|
|
91
|
+
root: { component: "dropdown-menu", props: {} },
|
|
92
|
+
} as DspackSurface;
|
|
93
|
+
try {
|
|
94
|
+
emitSurface(surface, shadcnDoc, { profile: shadcnProfile });
|
|
95
|
+
expect.unreachable("surface using a casualty component must refuse");
|
|
96
|
+
} catch (e) {
|
|
97
|
+
expect(e).toBeInstanceOf(EmitSurfaceError);
|
|
98
|
+
const msg = (e as EmitSurfaceError).message;
|
|
99
|
+
// The profile's authored reason, not just "unknown component".
|
|
100
|
+
expect(msg).toContain("declared casualty");
|
|
101
|
+
expect(msg).toContain("dropdown menu");
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe("scaffoldProfile", () => {
|
|
107
|
+
it("produces a loadProfile-valid profile from a bare contract", () => {
|
|
108
|
+
const { profile } = scaffoldProfile(astryxDoc, {
|
|
109
|
+
catalogIdBase: "https://example.test/catalogs/astryx-scaffold",
|
|
110
|
+
});
|
|
111
|
+
// The scaffold is itself a valid v1 profile document.
|
|
112
|
+
const loaded = loadProfile(profile);
|
|
113
|
+
expect(loaded.catalogIdBase).toBe("https://example.test/catalogs/astryx-scaffold");
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it("maps every contract component (full coverage, no judgment invented)", () => {
|
|
117
|
+
const { profile, notes } = scaffoldProfile(astryxDoc, {
|
|
118
|
+
catalogIdBase: "https://example.test/catalogs/astryx-scaffold",
|
|
119
|
+
});
|
|
120
|
+
const loaded = loadProfile(profile);
|
|
121
|
+
const out = transformFromJson(astryxDoc, { profile: loaded });
|
|
122
|
+
expect(out.validation.gates.find((g) => g.name === "schema-compile + no-external-ref")?.pass).toBe(true);
|
|
123
|
+
expect(out.validation.gates.find((g) => g.name === "catalog-shape")?.pass).toBe(true);
|
|
124
|
+
const unclassified = out.mapping.coverage.filter((c) => c.disposition === "unclassified");
|
|
125
|
+
expect(unclassified).toEqual([]);
|
|
126
|
+
// Mechanical means mechanical: no casualties invented, judgment flagged in notes.
|
|
127
|
+
expect(loaded.casualtyComponents).toEqual([]);
|
|
128
|
+
expect(notes.length).toBeGreaterThan(0);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it("seeds enum props verbatim (identity projection, no valueMap)", () => {
|
|
132
|
+
const { profile } = scaffoldProfile(shadcnDoc, {
|
|
133
|
+
catalogIdBase: "https://example.test/catalogs/shadcn-scaffold",
|
|
134
|
+
});
|
|
135
|
+
const loaded = loadProfile(profile);
|
|
136
|
+
const button = loaded.components.find((c) => c.dspackId === "button");
|
|
137
|
+
expect(button).toBeDefined();
|
|
138
|
+
const variant = button?.propMap?.variant;
|
|
139
|
+
expect(variant?.kind).toBe("enum");
|
|
140
|
+
// Verbatim contract vocabulary, not the hand-tuned lossy projection.
|
|
141
|
+
expect(variant?.targetEnum).toEqual(["default", "destructive", "outline", "secondary", "ghost", "link"]);
|
|
142
|
+
expect(variant?.valueMap).toBeUndefined();
|
|
143
|
+
});
|
|
144
|
+
});
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Emit-profile parity with the canonical shadcn contract (P1.E).
|
|
3
|
+
*
|
|
4
|
+
* Two invariants, born from two latent defects found the day governance
|
|
5
|
+
* first reached the table and card sub-families (both were nominally mapped
|
|
6
|
+
* while their sub-families refused to emit):
|
|
7
|
+
*
|
|
8
|
+
* 1. EVERY worked example in the synced contract emits. The example set is
|
|
9
|
+
* the contract's own definition of "expressible"; it grows via check-sync
|
|
10
|
+
* and this suite grows with it.
|
|
11
|
+
* 2. EVERY component id in the contract — including compound sub-components —
|
|
12
|
+
* is classified as exactly one of: directly mapped; consumed by a named
|
|
13
|
+
* parent strategy (the parent's explicit subCoverage entry); explicitly
|
|
14
|
+
* unsupported with a reason (casualtyComponents, covering its subs); or
|
|
15
|
+
* UNCLASSIFIED, which fails here. A parent mapping never implies support
|
|
16
|
+
* for its sub-family: only its own subCoverage declarations do.
|
|
17
|
+
*/
|
|
18
|
+
import { readFileSync } from "node:fs";
|
|
19
|
+
import { describe, expect, it } from "vitest";
|
|
20
|
+
import type { DspackDoc, DspackSurface } from "./types.js";
|
|
21
|
+
import { emitSurface } from "./targets/a2ui/surface.js";
|
|
22
|
+
import { shadcnProfile } from "./transform/profiles.js";
|
|
23
|
+
|
|
24
|
+
const doc = JSON.parse(readFileSync("input/shadcn-ui.dspack.json", "utf8")) as DspackDoc & {
|
|
25
|
+
examples: Array<{ id: string; intent: string; surface: DspackSurface }>;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
describe("every synced worked example emits through the shadcn profile", () => {
|
|
29
|
+
it("the contract carries at least two examples (destructive-action + record-collection)", () => {
|
|
30
|
+
expect(doc.examples.length).toBeGreaterThanOrEqual(2);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it.each(doc.examples.map((e) => [e.id] as const))("%s", (id) => {
|
|
34
|
+
const example = doc.examples.find((e) => e.id === id)!;
|
|
35
|
+
const { messages, warnings } = emitSurface(example.surface, doc, { profile: shadcnProfile });
|
|
36
|
+
expect(messages).toHaveLength(2);
|
|
37
|
+
// Nothing is silent: lossy projections surface as warnings, never throws.
|
|
38
|
+
for (const w of warnings) expect(w.code).toMatch(/^surface-/);
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
describe("card sub-family flattens instead of refusing", () => {
|
|
43
|
+
it("card-header/card-title/card-description/card-content emit as grouped text", () => {
|
|
44
|
+
const surface: DspackSurface = {
|
|
45
|
+
dspackSurface: "0.1",
|
|
46
|
+
system: "shadcn/ui",
|
|
47
|
+
intent: "record-collection",
|
|
48
|
+
root: {
|
|
49
|
+
component: "card",
|
|
50
|
+
children: [
|
|
51
|
+
{ component: "card-header", children: [{ component: "card-title", text: "Open tickets" }] },
|
|
52
|
+
{ component: "card-content", text: "Three tickets are open." },
|
|
53
|
+
],
|
|
54
|
+
},
|
|
55
|
+
} as unknown as DspackSurface;
|
|
56
|
+
const { messages } = emitSurface(surface, doc, { profile: shadcnProfile });
|
|
57
|
+
const flat = JSON.stringify(messages);
|
|
58
|
+
expect(flat).toContain("Open tickets");
|
|
59
|
+
expect(flat).toContain("Three tickets are open.");
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe("completeness: every contract id is classified, sub-components included", () => {
|
|
64
|
+
const mapped = new Map(shadcnProfile.components.map((p) => [p.dspackId, p]));
|
|
65
|
+
const casualties = new Map(shadcnProfile.casualtyComponents.map((c) => [c.dspackId, c.reason]));
|
|
66
|
+
|
|
67
|
+
it("every component is directly mapped or explicitly unsupported with a reason", () => {
|
|
68
|
+
for (const id of Object.keys(doc.components ?? {})) {
|
|
69
|
+
const classification = mapped.has(id) ? "mapped" : casualties.has(id) ? "casualty" : "UNCLASSIFIED";
|
|
70
|
+
expect(`${id}: ${classification}`).not.toContain("UNCLASSIFIED");
|
|
71
|
+
if (casualties.has(id)) expect(casualties.get(id)!.length).toBeGreaterThan(10);
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("every sub-component of a mapped compound is classified by its parent's subCoverage", () => {
|
|
76
|
+
for (const [id, component] of Object.entries(doc.components ?? {})) {
|
|
77
|
+
const subs = (component as { composition?: { subComponents?: Array<{ id: string }> } }).composition?.subComponents ?? [];
|
|
78
|
+
if (subs.length === 0) continue;
|
|
79
|
+
if (casualties.has(id)) continue; // the parent's reason covers its family
|
|
80
|
+
const plan = mapped.get(id)!;
|
|
81
|
+
const coverage = plan.subCoverage ?? {};
|
|
82
|
+
for (const sub of subs) {
|
|
83
|
+
const disposition = coverage[sub.id];
|
|
84
|
+
expect(`${id} → ${sub.id}: ${disposition ?? "UNCLASSIFIED"}`).not.toContain("UNCLASSIFIED");
|
|
85
|
+
expect((disposition ?? "").length).toBeGreaterThan(5);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("the profile maps nothing the contract does not declare", () => {
|
|
91
|
+
const contractIds = new Set(Object.keys(doc.components ?? {}));
|
|
92
|
+
for (const plan of shadcnProfile.components) {
|
|
93
|
+
expect(contractIds.has(plan.dspackId)).toBe(true);
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
});
|
|
@@ -131,6 +131,16 @@ class SurfaceEmitter {
|
|
|
131
131
|
emitNode(node: SurfaceNode, path: string): string {
|
|
132
132
|
const plan = this.byDspackId.get(node.component);
|
|
133
133
|
if (!plan) {
|
|
134
|
+
// A declared casualty refuses with its authored reason — the refusal is
|
|
135
|
+
// the profile author's decision, and the message should say so.
|
|
136
|
+
const casualty = this.profile.casualtyComponents.find((c) => c.dspackId === node.component);
|
|
137
|
+
if (casualty) {
|
|
138
|
+
throw new EmitSurfaceError(
|
|
139
|
+
`component '${node.component}' is a declared casualty (${casualty.class}) of the ` +
|
|
140
|
+
`'${this.profile.catalogTitle}' profile: ${casualty.reason}`,
|
|
141
|
+
path,
|
|
142
|
+
);
|
|
143
|
+
}
|
|
134
144
|
throw new EmitSurfaceError(
|
|
135
145
|
`unknown component '${node.component}': not a mapped component of the '${this.profile.catalogTitle}' profile ` +
|
|
136
146
|
`(sub-components are consumed by their compound parent and cannot be emitted standalone)`,
|
|
@@ -148,7 +158,7 @@ class SurfaceEmitter {
|
|
|
148
158
|
|
|
149
159
|
this.applyPropMap(node, plan, instance, path);
|
|
150
160
|
const sp = plan.surfacePlan ?? {};
|
|
151
|
-
const consumesSubtree = Boolean(sp.subText || sp.subButtonText);
|
|
161
|
+
const consumesSubtree = Boolean(sp.subText || sp.subButtonText || sp.subTable);
|
|
152
162
|
|
|
153
163
|
if (sp.structuralPassthrough) {
|
|
154
164
|
for (const key of sp.structuralPassthrough) {
|
|
@@ -157,6 +167,7 @@ class SurfaceEmitter {
|
|
|
157
167
|
}
|
|
158
168
|
}
|
|
159
169
|
if (sp.subText || sp.subButtonText) this.applySubContent(node, sp.subText ?? {}, sp.subButtonText ?? {}, instance, path);
|
|
170
|
+
if (sp.subTable) this.applySubTable(node, sp.subTable, instance, path);
|
|
160
171
|
if (sp.textProp && node.text !== undefined) instance[sp.textProp] = node.text;
|
|
161
172
|
if (sp.textChildProp && node.text !== undefined) {
|
|
162
173
|
instance[sp.textChildProp] = this.emitTextPrimitive(node.text, `${id}_label`, path);
|
|
@@ -171,9 +182,13 @@ class SurfaceEmitter {
|
|
|
171
182
|
}
|
|
172
183
|
|
|
173
184
|
if (!consumesSubtree) {
|
|
174
|
-
const childNodes = collectChildren(node);
|
|
185
|
+
const childNodes = sp.subFlatten ? this.flattenSubs(node, sp.subFlatten, path) : collectChildren(node);
|
|
175
186
|
if (childNodes.length > 0) {
|
|
176
|
-
const childIds = childNodes.map((child, i) =>
|
|
187
|
+
const childIds = childNodes.map((child, i) =>
|
|
188
|
+
"textVariant" in child
|
|
189
|
+
? this.emitTextPrimitive(child.text, `${id}_${slug(child.textVariant)}`, path, child.textVariant)
|
|
190
|
+
: this.emitNode(child.node, `${path}${child.suffix}[${i}]`),
|
|
191
|
+
);
|
|
177
192
|
if (sp.childrenProp) {
|
|
178
193
|
instance[sp.childrenProp] = childIds;
|
|
179
194
|
} else if (sp.childProp) {
|
|
@@ -278,10 +293,12 @@ class SurfaceEmitter {
|
|
|
278
293
|
});
|
|
279
294
|
}
|
|
280
295
|
|
|
281
|
-
private emitTextPrimitive(text: string, preferredId: string, path: string): string {
|
|
296
|
+
private emitTextPrimitive(text: string, preferredId: string, path: string, variant?: string): string {
|
|
282
297
|
const { textComponent, textProp } = this.profile.surfaceSynthesis;
|
|
283
298
|
const id = this.allocateId(preferredId, path);
|
|
284
|
-
|
|
299
|
+
const instance: Json = { id, component: textComponent, [textProp]: text };
|
|
300
|
+
if (variant !== undefined) instance.variant = variant;
|
|
301
|
+
this.components.push(instance);
|
|
285
302
|
this.warnings.push({
|
|
286
303
|
code: "surface-synthesized-text",
|
|
287
304
|
message: `${path}: node text projected as a synthesized ${textComponent} child ('${id}') — the surface format has no text primitive.`,
|
|
@@ -289,6 +306,144 @@ class SurfaceEmitter {
|
|
|
289
306
|
return id;
|
|
290
307
|
}
|
|
291
308
|
|
|
309
|
+
/**
|
|
310
|
+
* Named parent strategy "subFlatten" (e.g. Card): grouping sub-components
|
|
311
|
+
* splice their children inline in document order (their own structure is a
|
|
312
|
+
* warned, documented loss); text-bearing sub-components synthesize the
|
|
313
|
+
* profile's text primitive with the declared variant. Everything else
|
|
314
|
+
* passes through to ordinary child emission.
|
|
315
|
+
*/
|
|
316
|
+
private flattenSubs(
|
|
317
|
+
node: SurfaceNode,
|
|
318
|
+
spec: NonNullable<NonNullable<ComponentPlan["surfacePlan"]>["subFlatten"]>,
|
|
319
|
+
path: string,
|
|
320
|
+
): Array<{ node: SurfaceNode; suffix: string } | { text: string; textVariant: string }> {
|
|
321
|
+
const out: Array<{ node: SurfaceNode; suffix: string } | { text: string; textVariant: string }> = [];
|
|
322
|
+
const visit = (n: SurfaceNode, suffix: string): void => {
|
|
323
|
+
if (spec.transparent.includes(n.component)) {
|
|
324
|
+
this.warnings.push({
|
|
325
|
+
code: "surface-sub-flattened",
|
|
326
|
+
message: `${path}: grouping sub-component '${n.component}' spliced inline (subFlatten strategy); its own structure is not carried.`,
|
|
327
|
+
});
|
|
328
|
+
if (n.text !== undefined && n.text !== "") out.push({ text: n.text, textVariant: "body" });
|
|
329
|
+
for (const child of collectChildren(n)) visit(child.node, child.suffix);
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
const variant = spec.asText[n.component];
|
|
333
|
+
if (variant !== undefined) {
|
|
334
|
+
const text = this.subtreeText(n);
|
|
335
|
+
if (text !== "") {
|
|
336
|
+
out.push({ text, textVariant: variant });
|
|
337
|
+
} else {
|
|
338
|
+
this.warnings.push({
|
|
339
|
+
code: "surface-sub-dropped",
|
|
340
|
+
message: `${path}: '${n.component}' carried no text to synthesize; dropped.`,
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
out.push({ node: n, suffix });
|
|
346
|
+
};
|
|
347
|
+
for (const child of collectChildren(node)) visit(child.node, child.suffix);
|
|
348
|
+
return out;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Named parent strategy "subTable": consume the tabular sub tree into the
|
|
353
|
+
* synthesized caption/columns/rows shape. Domain-neutral: a cell is its
|
|
354
|
+
* subtree's text in document order — nested component structure and props
|
|
355
|
+
* are a per-cell warned loss, never re-interpreted into semantic fields.
|
|
356
|
+
* structuralPassthrough values (the props path) win over consumed ones.
|
|
357
|
+
*/
|
|
358
|
+
private applySubTable(
|
|
359
|
+
node: SurfaceNode,
|
|
360
|
+
spec: NonNullable<NonNullable<ComponentPlan["surfacePlan"]>["subTable"]>,
|
|
361
|
+
instance: Json,
|
|
362
|
+
path: string,
|
|
363
|
+
): void {
|
|
364
|
+
const columns: string[] = [];
|
|
365
|
+
const rows: Json[] = [];
|
|
366
|
+
let caption: string | undefined;
|
|
367
|
+
|
|
368
|
+
const cellText = (cell: SurfaceNode, cellPath: string): string => {
|
|
369
|
+
const nested = collectChildren(cell);
|
|
370
|
+
const text = this.subtreeText(cell);
|
|
371
|
+
if (nested.length > 0) {
|
|
372
|
+
const names = nested.map((c) => `'${c.node.component}'`).join(", ");
|
|
373
|
+
this.warnings.push({
|
|
374
|
+
code: "surface-table-cell-flattened",
|
|
375
|
+
message: `${cellPath}: nested ${names} flattened to cell text; component structure and props are not carried by the synthesized table shape.`,
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
return text;
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
const consumeRow = (row: SurfaceNode, rowPath: string, into: string[]): void => {
|
|
382
|
+
for (const child of collectChildren(row)) {
|
|
383
|
+
const c = child.node.component;
|
|
384
|
+
if (c === spec.cell || c === spec.headerCell) {
|
|
385
|
+
into.push(cellText(child.node, `${rowPath}${child.suffix}`));
|
|
386
|
+
} else if (spec.drops[c] !== undefined) {
|
|
387
|
+
this.warnings.push({ code: "surface-sub-dropped", message: `${rowPath}: '${c}' dropped: ${spec.drops[c]}.` });
|
|
388
|
+
} else {
|
|
389
|
+
this.warnings.push({
|
|
390
|
+
code: "surface-sub-dropped",
|
|
391
|
+
message: `${rowPath}: '${c}' has no slot in a synthesized table row; dropped (its text is not lifted).`,
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
};
|
|
396
|
+
|
|
397
|
+
for (const child of collectChildren(node)) {
|
|
398
|
+
const c = child.node.component;
|
|
399
|
+
const childPath = `${path}${child.suffix}`;
|
|
400
|
+
if (c === spec.caption) {
|
|
401
|
+
caption ??= cellText(child.node, childPath);
|
|
402
|
+
} else if (c === spec.header) {
|
|
403
|
+
for (const inner of collectChildren(child.node)) {
|
|
404
|
+
if (inner.node.component === spec.row) consumeRow(inner.node, `${childPath}${inner.suffix}`, columns);
|
|
405
|
+
else this.warnings.push({ code: "surface-sub-dropped", message: `${childPath}: '${inner.node.component}' inside '${spec.header}' has no slot; dropped.` });
|
|
406
|
+
}
|
|
407
|
+
} else if (c === spec.body) {
|
|
408
|
+
for (const inner of collectChildren(child.node)) {
|
|
409
|
+
if (inner.node.component === spec.row) {
|
|
410
|
+
const cells: string[] = [];
|
|
411
|
+
consumeRow(inner.node, `${childPath}${inner.suffix}`, cells);
|
|
412
|
+
rows.push({ cells });
|
|
413
|
+
} else {
|
|
414
|
+
this.warnings.push({ code: "surface-sub-dropped", message: `${childPath}: '${inner.node.component}' inside '${spec.body}' has no slot; dropped.` });
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
} else if (spec.drops[c] !== undefined) {
|
|
418
|
+
this.warnings.push({ code: "surface-sub-dropped", message: `${childPath}: '${c}' dropped: ${spec.drops[c]}.` });
|
|
419
|
+
} else {
|
|
420
|
+
this.warnings.push({
|
|
421
|
+
code: "surface-sub-dropped",
|
|
422
|
+
message: `${childPath}: '${c}' has no slot in the synthesized table shape; dropped.`,
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
if (instance[spec.targetCaption] === undefined && caption !== undefined) instance[spec.targetCaption] = caption;
|
|
428
|
+
if (instance[spec.targetColumns] === undefined && columns.length > 0) instance[spec.targetColumns] = columns;
|
|
429
|
+
if (instance[spec.targetRows] === undefined && rows.length > 0) instance[spec.targetRows] = rows;
|
|
430
|
+
this.warnings.push({
|
|
431
|
+
code: "surface-composition-flattened",
|
|
432
|
+
message: `${path}: compound '${node.component}' subtree consumed into the synthesized table shape (documented casualty; cell content beyond text is not carried).`,
|
|
433
|
+
});
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** All text in a node's subtree, document order, space-joined. */
|
|
437
|
+
private subtreeText(node: SurfaceNode): string {
|
|
438
|
+
const parts: string[] = [];
|
|
439
|
+
const visit = (n: SurfaceNode): void => {
|
|
440
|
+
if (n.text !== undefined && n.text !== "") parts.push(n.text);
|
|
441
|
+
for (const child of collectChildren(n)) visit(child.node);
|
|
442
|
+
};
|
|
443
|
+
visit(node);
|
|
444
|
+
return parts.join(" ");
|
|
445
|
+
}
|
|
446
|
+
|
|
292
447
|
private wrapInColumn(childIds: string[], parentId: string, path: string): string {
|
|
293
448
|
const { wrapComponent, wrapChildrenProp } = this.profile.surfaceSynthesis;
|
|
294
449
|
const id = this.allocateId(`${parentId}_col`, path);
|
|
@@ -24,3 +24,16 @@ export interface JsonRenderProfile {
|
|
|
24
24
|
export const shadcnJsonRenderProfile: JsonRenderProfile = {
|
|
25
25
|
intentionallyOmitted: [],
|
|
26
26
|
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Astryx (facebook/astryx @ v0.1.4, nine-component slice). Also empty — the
|
|
30
|
+
* asymmetry finding's prediction held for a SECOND contract with a different
|
|
31
|
+
* idiom: nothing needs projecting, because the catalog carries the contract's
|
|
32
|
+
* own vocabulary. Astryx's data-driven array props (table.data/columns,
|
|
33
|
+
* dropdown-menu.items) are EXCLUDED by the codegen's prop typing with
|
|
34
|
+
* documented reasons — the lossy edge of the "lossless" target, visible in
|
|
35
|
+
* the generated catalog header and the model's excluded-props record.
|
|
36
|
+
*/
|
|
37
|
+
export const astryxJsonRenderProfile: JsonRenderProfile = {
|
|
38
|
+
intentionallyOmitted: [],
|
|
39
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Profile-as-data: load a mapping Profile from JSON.
|
|
3
|
+
*
|
|
4
|
+
* The Profile has always been pure data (profiles.ts); this module makes it
|
|
5
|
+
* *reachable* as data. `loadProfile` validates an untrusted JSON value against
|
|
6
|
+
* schema `profile.v1.schema.json` (a one-to-one mirror of the TypeScript
|
|
7
|
+
* `Profile` interface plus a `profileVersion` envelope const) and returns the
|
|
8
|
+
* engine-ready `Profile`. Validation gates shape only — every judgment call
|
|
9
|
+
* (valueMaps, casualties, surface plans) stays with the profile's author.
|
|
10
|
+
*
|
|
11
|
+
* Toolchain: ajv draft 2020-12, `strict: false`, ajv-formats — the same
|
|
12
|
+
* convention as the catalog gates (src/validate/ajv.ts) and the dspack
|
|
13
|
+
* harness.
|
|
14
|
+
*/
|
|
15
|
+
import { readFileSync } from "node:fs";
|
|
16
|
+
import { fileURLToPath } from "node:url";
|
|
17
|
+
import Ajv2020, { type ValidateFunction } from "ajv/dist/2020.js";
|
|
18
|
+
import addFormats from "ajv-formats";
|
|
19
|
+
import type { Profile } from "./profiles.js";
|
|
20
|
+
|
|
21
|
+
export interface ProfileLoadIssue {
|
|
22
|
+
/** JSON path into the offending document (ajv instancePath). */
|
|
23
|
+
path: string;
|
|
24
|
+
message: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class ProfileLoadError extends Error {
|
|
28
|
+
constructor(readonly issues: ProfileLoadIssue[]) {
|
|
29
|
+
super(
|
|
30
|
+
`profile does not conform to profile.v1.schema.json (${issues.length} issue${issues.length === 1 ? "" : "s"}): ` +
|
|
31
|
+
issues
|
|
32
|
+
.slice(0, 3)
|
|
33
|
+
.map((i) => `${i.path || "$"}: ${i.message}`)
|
|
34
|
+
.join("; ") +
|
|
35
|
+
(issues.length > 3 ? "; …" : ""),
|
|
36
|
+
);
|
|
37
|
+
this.name = "ProfileLoadError";
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const schemaPath = fileURLToPath(new URL("./profile.v1.schema.json", import.meta.url));
|
|
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"));
|
|
45
|
+
|
|
46
|
+
let compiled: ValidateFunction | undefined;
|
|
47
|
+
|
|
48
|
+
function validator(): ValidateFunction {
|
|
49
|
+
if (!compiled) {
|
|
50
|
+
const ajv = new Ajv2020({ strict: false, allErrors: true, validateFormats: true });
|
|
51
|
+
addFormats(ajv);
|
|
52
|
+
compiled = ajv.compile(profileSchema);
|
|
53
|
+
}
|
|
54
|
+
return compiled;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Validate `json` as a v1 profile document and return the engine-ready
|
|
59
|
+
* `Profile`. The `profileVersion` envelope field is stripped — it belongs to
|
|
60
|
+
* the document format, not the engine. Top-level `x-` extension keys are
|
|
61
|
+
* preserved (the engine ignores them; provenance tools may not).
|
|
62
|
+
*/
|
|
63
|
+
export function loadProfile(json: unknown): Profile {
|
|
64
|
+
const validate = validator();
|
|
65
|
+
if (!validate(json)) {
|
|
66
|
+
const errors = (validate.errors ?? []) as Array<{ instancePath?: string; message?: string }>;
|
|
67
|
+
throw new ProfileLoadError(
|
|
68
|
+
errors.map((e) => ({ path: e.instancePath ?? "", message: e.message ?? "invalid" })),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
const { profileVersion: _envelope, ...profile } = json as Record<string, unknown> & { profileVersion: string };
|
|
72
|
+
return profile as unknown as Profile;
|
|
73
|
+
}
|