simpler-forms 0.1.0-alpha.1 → 0.1.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 (86) hide show
  1. package/README.md +221 -76
  2. package/dist/src/casing.d.ts +43 -0
  3. package/dist/src/casing.js +146 -0
  4. package/dist/src/casing.js.map +1 -0
  5. package/dist/src/decorators/index.d.ts +1 -0
  6. package/dist/src/decorators/index.js +1 -0
  7. package/dist/src/decorators/index.js.map +1 -1
  8. package/dist/src/decorators/mapping.d.ts +11 -0
  9. package/dist/src/decorators/mapping.js +22 -0
  10. package/dist/src/decorators/mapping.js.map +1 -0
  11. package/dist/src/decorators/meta.d.ts +13 -3
  12. package/dist/src/decorators/meta.js +31 -4
  13. package/dist/src/decorators/meta.js.map +1 -1
  14. package/dist/src/decorators/ui.d.ts +14 -12
  15. package/dist/src/decorators/ui.js +20 -43
  16. package/dist/src/decorators/ui.js.map +1 -1
  17. package/dist/src/emitter.d.ts +8 -0
  18. package/dist/src/emitter.js +48 -13
  19. package/dist/src/emitter.js.map +1 -1
  20. package/dist/src/emitters/block-ui.d.ts +14 -3
  21. package/dist/src/emitters/block-ui.js +81 -119
  22. package/dist/src/emitters/block-ui.js.map +1 -1
  23. package/dist/src/emitters/layout-ui.d.ts +12 -0
  24. package/dist/src/emitters/layout-ui.js +225 -0
  25. package/dist/src/emitters/layout-ui.js.map +1 -0
  26. package/dist/src/emitters/overlay.d.ts +3 -6
  27. package/dist/src/emitters/overlay.js +3 -86
  28. package/dist/src/emitters/overlay.js.map +1 -1
  29. package/dist/src/forms.d.ts +23 -0
  30. package/dist/src/forms.js +54 -0
  31. package/dist/src/forms.js.map +1 -0
  32. package/dist/src/index.d.ts +15 -6
  33. package/dist/src/index.js +12 -3
  34. package/dist/src/index.js.map +1 -1
  35. package/dist/src/layout.d.ts +50 -0
  36. package/dist/src/layout.js +397 -0
  37. package/dist/src/layout.js.map +1 -0
  38. package/dist/src/lib.d.ts +485 -39
  39. package/dist/src/lib.js +215 -15
  40. package/dist/src/lib.js.map +1 -1
  41. package/dist/src/linter.js +2 -41
  42. package/dist/src/linter.js.map +1 -1
  43. package/dist/src/mapping-from.d.ts +20 -0
  44. package/dist/src/mapping-from.js +149 -0
  45. package/dist/src/mapping-from.js.map +1 -0
  46. package/dist/src/mapping.d.ts +58 -0
  47. package/dist/src/mapping.js +209 -0
  48. package/dist/src/mapping.js.map +1 -0
  49. package/dist/src/model.d.ts +69 -17
  50. package/dist/src/model.js +65 -19
  51. package/dist/src/model.js.map +1 -1
  52. package/dist/src/public.d.ts +20 -4
  53. package/dist/src/public.js +28 -4
  54. package/dist/src/public.js.map +1 -1
  55. package/dist/src/validate.d.ts +2 -1
  56. package/dist/src/validate.js +180 -58
  57. package/dist/src/validate.js.map +1 -1
  58. package/lib/main.tsp +1 -0
  59. package/lib/mapping.tsp +71 -0
  60. package/lib/meta.tsp +23 -4
  61. package/lib/types.tsp +13 -65
  62. package/lib/ui.tsp +34 -17
  63. package/package.json +7 -1
  64. package/src/casing.ts +172 -0
  65. package/src/decorators/index.ts +1 -0
  66. package/src/decorators/mapping.ts +57 -0
  67. package/src/decorators/meta.ts +50 -11
  68. package/src/decorators/ui.ts +43 -59
  69. package/src/emitter.ts +59 -13
  70. package/src/emitters/block-ui.ts +83 -135
  71. package/src/emitters/layout-ui.ts +280 -0
  72. package/src/emitters/overlay.ts +3 -103
  73. package/src/forms.ts +82 -0
  74. package/src/index.ts +12 -3
  75. package/src/layout.ts +592 -0
  76. package/src/lib.ts +221 -15
  77. package/src/linter.ts +1 -42
  78. package/src/mapping-from.ts +193 -0
  79. package/src/mapping.ts +351 -0
  80. package/src/model.ts +170 -37
  81. package/src/public.ts +50 -6
  82. package/src/validate.ts +188 -59
  83. package/dist/src/emitters/override-condition.d.ts +0 -12
  84. package/dist/src/emitters/override-condition.js +0 -25
  85. package/dist/src/emitters/override-condition.js.map +0 -1
  86. package/src/emitters/override-condition.ts +0 -37
package/src/mapping.ts ADDED
@@ -0,0 +1,351 @@
1
+ import type {
2
+ EnumMember,
3
+ Model,
4
+ ModelProperty,
5
+ Program,
6
+ Type,
7
+ } from "@typespec/compiler";
8
+ import { isArrayModelType } from "@typespec/compiler";
9
+ import {
10
+ type MappingPathLink,
11
+ modelMappedTo,
12
+ modelMappingPaths,
13
+ modelProperties,
14
+ propMappedTo,
15
+ } from "./model.js";
16
+
17
+ /**
18
+ * How a linked source property relates to its target:
19
+ *
20
+ * leaf a value, linked to a value
21
+ * object an object, linked to an object; its children are linked within that object
22
+ * array a list, linked to a list; for a list of objects, its children are linked within
23
+ * the target's item
24
+ * group an object with no link of its own whose descendants are linked: flattened, its
25
+ * children landing in the parent's target
26
+ */
27
+ export type MappingNodeKind = "leaf" | "object" | "array" | "group";
28
+
29
+ export interface MappingNode {
30
+ /** Property names from the mapped model to this one, this one last. */
31
+ sourcePath: string[];
32
+ source: ModelProperty;
33
+ /** What the source corresponds to. A group has none. */
34
+ target?: ModelProperty;
35
+ kind: MappingNodeKind;
36
+ /** The linked properties below an object, a list of objects, or a group, in source order. */
37
+ children: MappingNode[];
38
+ }
39
+
40
+ /** A model's mapping in one target, resolved through every model it composes. */
41
+ export interface ResolvedMapping {
42
+ mapping: EnumMember;
43
+ source: Model;
44
+ target: Model;
45
+ children: MappingNode[];
46
+ }
47
+
48
+ /** A link that does not hold, ready to report as a diagnostic. */
49
+ export interface MappingProblem {
50
+ code:
51
+ | "mapping-path-unresolved"
52
+ | "mapping-target-out-of-context"
53
+ | "mapping-type-mismatch"
54
+ | "mapping-cardinality-mismatch"
55
+ | "mapping-target-duplicate";
56
+ target: Model | ModelProperty;
57
+ format: Record<string, string>;
58
+ }
59
+
60
+ /**
61
+ * `model`'s mapping in `mapping`, or undefined if `model` has no `@Mapping.to` in it.
62
+ *
63
+ * A property takes the first link that applies:
64
+ *
65
+ * 1. a `@Mapping.path` naming it, from the outermost composing model that has one
66
+ * 2. its own `@Mapping.to`
67
+ *
68
+ * and an object-valued property then resolves its own properties against what it is linked
69
+ * to, so a question linked once at the level its mapping is shared needs nothing more from
70
+ * the forms that compose it. Links that do not hold are left out; `$onValidate` reports them.
71
+ */
72
+ export function resolveMapping(
73
+ program: Program,
74
+ model: Model,
75
+ mapping: EnumMember,
76
+ ): ResolvedMapping | undefined {
77
+ return resolveMappingWithProblems(program, model, mapping)?.resolved;
78
+ }
79
+
80
+ /** `resolveMapping`, with every link that did not hold. */
81
+ export function resolveMappingWithProblems(
82
+ program: Program,
83
+ model: Model,
84
+ mapping: EnumMember,
85
+ ): { resolved: ResolvedMapping; problems: MappingProblem[] } | undefined {
86
+ const target = modelMappedTo(program, model, mapping);
87
+ if (!target) return undefined;
88
+ const walk: Walk = {
89
+ program,
90
+ mapping,
91
+ problems: [],
92
+ owned: [],
93
+ consumed: new Set(),
94
+ };
95
+ const children = resolveModel(walk, model, target, [], [], new Map(), [
96
+ model,
97
+ ]);
98
+ for (const { link, owner } of walk.owned) {
99
+ if (walk.consumed.has(link)) continue;
100
+ walk.problems.push({
101
+ code: "mapping-path-unresolved",
102
+ target: owner,
103
+ format: {
104
+ path: link.sourcePath,
105
+ name: owner.name || "an anonymous model",
106
+ reason: unresolvedReason(owner, link.sourcePath),
107
+ },
108
+ });
109
+ }
110
+ return {
111
+ resolved: { mapping, source: model, target, children },
112
+ problems: walk.problems,
113
+ };
114
+ }
115
+
116
+ // ---------------------------------------------------------------------------
117
+
118
+ interface Walk {
119
+ program: Program;
120
+ mapping: EnumMember;
121
+ problems: MappingProblem[];
122
+ /** Every `@Mapping.path` met so far, with the model that declares it. */
123
+ owned: { link: MappingPathLink; owner: Model }[];
124
+ /** The ones that landed on a property. */
125
+ consumed: Set<MappingPathLink>;
126
+ }
127
+
128
+ /** A `@Mapping.path` on its way down: the steps still to take from the current model. */
129
+ interface PendingPath {
130
+ link: MappingPathLink;
131
+ owner: Model;
132
+ steps: string[];
133
+ }
134
+
135
+ /**
136
+ * `model`'s linked properties, each resolved within `context`, the model `model` (or its
137
+ * nearest linked ancestor, for a group) corresponds to.
138
+ *
139
+ * `claimed` is shared by everything that lands in one target object, groups included, so two
140
+ * sources landing on one target property are caught wherever they were declared.
141
+ */
142
+ function resolveModel(
143
+ walk: Walk,
144
+ model: Model,
145
+ context: Model,
146
+ inherited: PendingPath[],
147
+ prefix: string[],
148
+ claimed: Map<ModelProperty, string>,
149
+ stack: Model[],
150
+ ): MappingNode[] {
151
+ const own = modelMappingPaths(walk.program, model, walk.mapping).map(
152
+ (link) => {
153
+ walk.owned.push({ link, owner: model });
154
+ return { link, owner: model, steps: stepsOf(link.sourcePath) };
155
+ },
156
+ );
157
+ // Outermost first, so `find` below prefers the composing model furthest out.
158
+ const pending = [...inherited, ...own];
159
+ const nodes: MappingNode[] = [];
160
+
161
+ for (const source of modelProperties(model)) {
162
+ const here = pending.filter((p) => p.steps[0] === source.name);
163
+ const exact = here.filter((p) => p.steps.length === 1);
164
+ for (const p of exact) walk.consumed.add(p.link);
165
+ const deeper = here
166
+ .filter((p) => p.steps.length > 1)
167
+ .map((p) => ({ ...p, steps: p.steps.slice(1) }));
168
+ const path = exact[0];
169
+ const target =
170
+ path?.link.mappedTo ?? propMappedTo(walk.program, source, walk.mapping);
171
+ const sourcePath = [...prefix, source.name];
172
+ const at = path?.owner ?? source;
173
+ const shape = shapeOf(source.type);
174
+
175
+ if (!target) {
176
+ // Unlinked: flattened into the parent's target if anything below it is linked.
177
+ if (!shape.object || shape.array || stack.includes(shape.object))
178
+ continue;
179
+ const children = resolveModel(
180
+ walk,
181
+ shape.object,
182
+ context,
183
+ deeper,
184
+ sourcePath,
185
+ claimed,
186
+ [...stack, shape.object],
187
+ );
188
+ if (children.length)
189
+ nodes.push({ sourcePath, source, kind: "group", children });
190
+ continue;
191
+ }
192
+
193
+ const node = resolveLink(
194
+ walk,
195
+ source,
196
+ target,
197
+ context,
198
+ deeper,
199
+ sourcePath,
200
+ at,
201
+ stack,
202
+ );
203
+ if (!node) continue;
204
+ const first = claimed.get(target);
205
+ if (first !== undefined) {
206
+ walk.problems.push({
207
+ code: "mapping-target-duplicate",
208
+ target: at,
209
+ format: {
210
+ mapping: walk.mapping.name,
211
+ first,
212
+ second: sourcePath.join("."),
213
+ target: describe(target),
214
+ },
215
+ });
216
+ continue;
217
+ }
218
+ claimed.set(target, sourcePath.join("."));
219
+ nodes.push(node);
220
+ }
221
+ return nodes;
222
+ }
223
+
224
+ function resolveLink(
225
+ walk: Walk,
226
+ source: ModelProperty,
227
+ target: ModelProperty,
228
+ context: Model,
229
+ deeper: PendingPath[],
230
+ sourcePath: string[],
231
+ at: Model | ModelProperty,
232
+ stack: Model[],
233
+ ): MappingNode | undefined {
234
+ const fail = (code: MappingProblem["code"], format: Record<string, string>) =>
235
+ void walk.problems.push({
236
+ code,
237
+ target: at,
238
+ format: {
239
+ mapping: walk.mapping.name,
240
+ source: sourcePath.join("."),
241
+ target: describe(target),
242
+ ...format,
243
+ },
244
+ });
245
+
246
+ if (!isMemberOf(target, context)) {
247
+ fail("mapping-target-out-of-context", {
248
+ context: context.name || "an anonymous model",
249
+ });
250
+ return undefined;
251
+ }
252
+ const from = shapeOf(source.type);
253
+ const to = shapeOf(target.type);
254
+ if (from.array !== to.array) {
255
+ fail("mapping-cardinality-mismatch", {
256
+ sourceShape: from.array ? "a list" : "a single value",
257
+ targetShape: to.array ? "a list" : "a single value",
258
+ });
259
+ return undefined;
260
+ }
261
+ const kind = from.array ? "array" : from.object ? "object" : "leaf";
262
+ if (!from.object) {
263
+ if (to.object) {
264
+ fail("mapping-type-mismatch", {
265
+ reason: "the source is a value and the target is an object",
266
+ });
267
+ return undefined;
268
+ }
269
+ return { sourcePath, source, target, kind, children: [] };
270
+ }
271
+ if (!to.object) {
272
+ fail("mapping-type-mismatch", {
273
+ reason: "the source is an object and the target is a value",
274
+ });
275
+ return undefined;
276
+ }
277
+ const linked = modelMappedTo(walk.program, from.object, walk.mapping);
278
+ if (linked && linked !== to.object) {
279
+ fail("mapping-type-mismatch", {
280
+ reason: `${from.object.name} maps to ${linked.name}, not ${to.object.name}`,
281
+ });
282
+ return undefined;
283
+ }
284
+ const children = stack.includes(from.object)
285
+ ? []
286
+ : resolveModel(
287
+ walk,
288
+ from.object,
289
+ to.object,
290
+ deeper,
291
+ sourcePath,
292
+ new Map(),
293
+ [...stack, from.object],
294
+ );
295
+ return { sourcePath, source, target, kind, children };
296
+ }
297
+
298
+ /** Whether a type is a list, and the object it holds (directly or as each entry), if any. */
299
+ export function shapeOf(type: Type): { array: boolean; object?: Model } {
300
+ if (type.kind !== "Model") return { array: false };
301
+ if (isArrayModelType(type)) {
302
+ const item = type.indexer!.value;
303
+ return { array: true, object: item.kind === "Model" ? item : undefined };
304
+ }
305
+ return { array: false, object: type };
306
+ }
307
+
308
+ /**
309
+ * Whether `target` is one of `context`'s properties. A property `context` has by spread or
310
+ * `is` is a copy, so the copy's source counts too.
311
+ */
312
+ function isMemberOf(target: ModelProperty, context: Model): boolean {
313
+ return modelProperties(context).some((candidate) => {
314
+ for (
315
+ let p: ModelProperty | undefined = candidate;
316
+ p;
317
+ p = p.sourceProperty
318
+ ) {
319
+ if (p === target) return true;
320
+ }
321
+ return false;
322
+ });
323
+ }
324
+
325
+ /** A dotted path's property names. A list is stepped through, so `[*]` is optional. */
326
+ const stepsOf = (path: string) =>
327
+ path.split(".").map((step) => step.replace(/\[\*\]$/, ""));
328
+
329
+ const describe = (prop: ModelProperty) =>
330
+ prop.model?.name ? `${prop.model.name}.${prop.name}` : prop.name;
331
+
332
+ /** Why a path that never landed did not: it names nothing, or nothing above it is linked. */
333
+ function unresolvedReason(model: Model, path: string): string {
334
+ let current: Model | undefined = model;
335
+ const steps = stepsOf(path);
336
+ for (const [index, step] of steps.entries()) {
337
+ if (!current) return `${steps.slice(0, index).join(".")} is not an object`;
338
+ const prop: ModelProperty | undefined = modelProperties(current).find(
339
+ (p) => p.name === step,
340
+ );
341
+ if (!prop) {
342
+ const known = modelProperties(current)
343
+ .map((p) => p.name)
344
+ .slice(0, 8)
345
+ .join(", ");
346
+ return `${current.name || "the model"} has no property "${step}" (has ${known})`;
347
+ }
348
+ current = shapeOf(prop.type).object;
349
+ }
350
+ return "a list or a value on the way to it has no link of its own in this mapping, so it has no target to land in";
351
+ }
package/src/model.ts CHANGED
@@ -14,6 +14,7 @@ import {
14
14
  getMaxItems,
15
15
  getMinItems,
16
16
  } from "@typespec/compiler";
17
+ import type { Node } from "@typespec/compiler/ast";
17
18
  import { stateKeys } from "./lib.js";
18
19
 
19
20
  interface ConditionBase {
@@ -97,9 +98,7 @@ export interface Block {
97
98
  responseRole?: ResponseRole;
98
99
  label?: string;
99
100
  doc?: string;
100
- sections?: Enum;
101
101
  order?: string[];
102
- overrides: Record<string, Record<string, unknown>>;
103
102
  }
104
103
 
105
104
  export type ResponseRole =
@@ -129,10 +128,10 @@ export function readBlock(
129
128
  ): Block | undefined {
130
129
  const q = g(program, stateKeys.questionMeta, model) as
131
130
  Record<string, unknown> | undefined;
132
- const f = g(program, stateKeys.formMeta, model) as
131
+ const s = g(program, stateKeys.formMeta, model) as
133
132
  Record<string, unknown> | undefined;
134
- if (!q && !f) return undefined;
135
- const meta = (q ?? f)!;
133
+ if (!q && !s) return undefined;
134
+ const meta = (q ?? s)!;
136
135
  return {
137
136
  model,
138
137
  kind: q ? "question" : "form",
@@ -147,16 +146,41 @@ export function readBlock(
147
146
  ResponseRole | undefined,
148
147
  label: g(program, stateKeys.label, model) as string | undefined,
149
148
  doc: getDoc(program, model),
150
- sections: g(program, stateKeys.sections, model) as Enum | undefined,
151
149
  order: g(program, stateKeys.order, model) as string[] | undefined,
152
- overrides:
153
- (g(program, stateKeys.overrides, model) as Record<
154
- string,
155
- Record<string, unknown>
156
- >) ?? {},
157
150
  };
158
151
  }
159
152
 
153
+ /** A form's identity, from `@Meta.form`. */
154
+ export interface FormMeta {
155
+ id: string;
156
+ formName: string;
157
+ revision?: string;
158
+ }
159
+
160
+ /** `@Meta.form` on a model, if it has one. */
161
+ export const modelFormMeta = (p: Program, model: Model) =>
162
+ g(p, stateKeys.formMeta, model) as FormMeta | undefined;
163
+
164
+ /** Every model with `@Meta.form`, in the order the decorators ran. */
165
+ export const allFormMetas = (p: Program) =>
166
+ p.stateMap(stateKeys.formMeta) as Map<Model, FormMeta>;
167
+
168
+ /** Every `@UI.layout` model, with the model it presents. */
169
+ export const allLayouts = (p: Program) =>
170
+ p.stateMap(stateKeys.layout) as Map<Model, Model>;
171
+
172
+ /** A `@UI.field` on a layout entry: the field it lays out, and the reference's syntax. */
173
+ export interface LayoutFieldRef {
174
+ field: ModelProperty;
175
+ node: Node;
176
+ }
177
+
178
+ export const entryField = (p: Program, prop: ModelProperty) =>
179
+ g(p, stateKeys.layoutField, prop) as LayoutFieldRef | undefined;
180
+
181
+ export const propFlatten = (p: Program, prop: ModelProperty) =>
182
+ g(p, stateKeys.flatten, prop) === true;
183
+
160
184
  /** All blocks in the program, in declaration order. */
161
185
  export function allBlocks(program: Program): Block[] {
162
186
  const out: Block[] = [];
@@ -174,17 +198,62 @@ export function allBlocks(program: Program): Block[] {
174
198
  return out;
175
199
  }
176
200
 
201
+ /** A property's label: its own `@UI.label`, else the one its scalar, model or enum type declares. */
177
202
  export const propLabel = (p: Program, prop: ModelProperty) =>
178
- g(p, stateKeys.label, prop) as string | undefined;
203
+ (g(p, stateKeys.label, prop) ??
204
+ (prop.type.kind === "Scalar" ||
205
+ prop.type.kind === "Model" ||
206
+ prop.type.kind === "Enum"
207
+ ? g(p, stateKeys.label, prop.type)
208
+ : undefined)) as string | undefined;
179
209
  export const propHelpText = (p: Program, prop: ModelProperty) =>
180
210
  g(p, stateKeys.helpText, prop) as string | undefined;
181
- export const propWidget = (p: Program, prop: ModelProperty) =>
182
- g(p, stateKeys.widget, prop) as string | undefined;
211
+ /** A property's widget: its own `@UI.widget`, else its type's, or a base scalar's. */
212
+ export const propWidget = (p: Program, prop: ModelProperty) => {
213
+ const own = g(p, stateKeys.widget, prop) as string | undefined;
214
+ if (own) return own;
215
+ if (prop.type.kind === "Model")
216
+ return g(p, stateKeys.widget, prop.type) as string | undefined;
217
+ for (
218
+ let s = prop.type.kind === "Scalar" ? prop.type : undefined;
219
+ s;
220
+ s = s.baseScalar
221
+ ) {
222
+ const widget = g(p, stateKeys.widget, s) as string | undefined;
223
+ if (widget) return widget;
224
+ }
225
+ return undefined;
226
+ };
227
+
228
+ /** Every enum a namespace declares with `@Meta.tags`: the tags the program catalogues by. */
229
+ export const declaredTagSets = (p: Program) =>
230
+ new Set(p.stateMap(stateKeys.tagSets).values() as Iterable<Enum>);
231
+
232
+ /** Each question or scalar given a `@Meta.tag`, with the enum members it named. */
233
+ export const tagMembers = (p: Program) =>
234
+ p.stateMap(stateKeys.tagMembers) as Map<Model | Scalar, EnumMember[]>;
235
+
236
+ /** Every enum a namespace declares with `@Meta.entities`: what questions are asked about. */
237
+ export const declaredEntitySets = (p: Program) =>
238
+ new Set(p.stateMap(stateKeys.entitySets).values() as Iterable<Enum>);
239
+
240
+ /** Each question that names an entity, with the enum member it named. */
241
+ export const entityMembers = (p: Program) =>
242
+ p.stateMap(stateKeys.entityMember) as Map<Model | Scalar, EnumMember>;
243
+
244
+ /** Every enum a namespace declares with `@UI.widgets`: the widgets the program provides. */
245
+ export const declaredWidgetSets = (p: Program) =>
246
+ new Set(p.stateMap(stateKeys.widgets).values() as Iterable<Enum>);
247
+
248
+ /** Each property given a `@UI.widget`, with the enum member it named. */
249
+ export const widgetMembers = (p: Program) =>
250
+ p.stateMap(stateKeys.widgetMember) as Map<
251
+ ModelProperty | Scalar | Model,
252
+ EnumMember
253
+ >;
183
254
  export const propEncodedCheckboxGroup = (p: Program, prop: ModelProperty) =>
184
255
  g(p, stateKeys.encodedCheckboxGroup, prop) as
185
256
  Record<string, unknown> | undefined;
186
- export const propSection = (p: Program, prop: ModelProperty) =>
187
- g(p, stateKeys.section, prop) as { name: string; label?: string } | undefined;
188
257
  export const propReadOnly = (p: Program, prop: ModelProperty) =>
189
258
  g(p, stateKeys.readOnly, prop) === true;
190
259
  export const propResponseRole = (p: Program, prop: ModelProperty) =>
@@ -205,6 +274,90 @@ export const propRequiredWhen = (p: Program, prop: ModelProperty) =>
205
274
  (g(p, stateKeys.requiredWhen, prop) as Condition[]) ?? [];
206
275
  export const propNotBefore = (p: Program, prop: ModelProperty) =>
207
276
  g(p, stateKeys.notBefore, prop) as ModelProperty | undefined;
277
+
278
+ // --- mapping ----------------------------------------------------------------
279
+
280
+ /** One `@Mapping.to`: in `mapping`, the decorated type corresponds to `mappedTo`. */
281
+ export interface MappingLink {
282
+ mapping: EnumMember;
283
+ mappedTo: Model | ModelProperty;
284
+ }
285
+
286
+ /** One `@Mapping.path`: in `mapping`, the dotted source path corresponds to `mappedTo`. */
287
+ export interface MappingPathLink {
288
+ mapping: EnumMember;
289
+ sourcePath: string;
290
+ mappedTo: ModelProperty;
291
+ }
292
+
293
+ /** One `@Mapping.from`: in `mapping`, the decorated target takes its value from `source`. */
294
+ export interface MappingFromLink {
295
+ mapping: EnumMember;
296
+ source: Model | ModelProperty;
297
+ }
298
+
299
+ /** Every `@Mapping.from` on each target model and property, in every mapping. */
300
+ export const allMappingFromLinks = (p: Program) =>
301
+ p.stateMap(stateKeys.mappedFrom) as Map<
302
+ Model | ModelProperty,
303
+ MappingFromLink[]
304
+ >;
305
+
306
+ /** The fields a target property is filled from in `mapping`, one per form that fills it. */
307
+ export const propMappedFrom = (
308
+ p: Program,
309
+ prop: ModelProperty,
310
+ mapping: EnumMember,
311
+ ) =>
312
+ ((g(p, stateKeys.mappedFrom, prop) as MappingFromLink[] | undefined) ?? [])
313
+ .filter(
314
+ (link) =>
315
+ link.mapping === mapping && link.source.kind === "ModelProperty",
316
+ )
317
+ .map((link) => link.source as ModelProperty);
318
+
319
+ /** Every enum a namespace declares with `@Mapping.targets`: the mappings the program produces. */
320
+ export const declaredMappingSets = (p: Program) =>
321
+ new Set(p.stateMap(stateKeys.mappingTargets).values() as Iterable<Enum>);
322
+
323
+ /** Every member of a declared mapping set, in declaration order. */
324
+ export const mappingTargets = (p: Program): EnumMember[] =>
325
+ [...declaredMappingSets(p)].flatMap((e) => [...e.members.values()]);
326
+
327
+ /** Every `@Mapping.to` on each model and property, in every mapping. */
328
+ export const allMappingLinks = (p: Program) =>
329
+ p.stateMap(stateKeys.mappedTo) as Map<Model | ModelProperty, MappingLink[]>;
330
+
331
+ /** Every `@Mapping.path` on each model, in every mapping. */
332
+ export const allMappingPathLinks = (p: Program) =>
333
+ p.stateMap(stateKeys.mappingPaths) as Map<Model, MappingPathLink[]>;
334
+
335
+ /** The model a model corresponds to in `mapping`; the first, if it is linked twice. */
336
+ export const modelMappedTo = (p: Program, model: Model, mapping: EnumMember) =>
337
+ ((g(p, stateKeys.mappedTo, model) as MappingLink[] | undefined) ?? []).find(
338
+ (link) => link.mapping === mapping && link.mappedTo.kind === "Model",
339
+ )?.mappedTo as Model | undefined;
340
+
341
+ /** The property a property corresponds to in `mapping`; the first, if it is linked twice. */
342
+ export const propMappedTo = (
343
+ p: Program,
344
+ prop: ModelProperty,
345
+ mapping: EnumMember,
346
+ ) =>
347
+ ((g(p, stateKeys.mappedTo, prop) as MappingLink[] | undefined) ?? []).find(
348
+ (link) =>
349
+ link.mapping === mapping && link.mappedTo.kind === "ModelProperty",
350
+ )?.mappedTo as ModelProperty | undefined;
351
+
352
+ /** A model's `@Mapping.path` links in `mapping`. */
353
+ export const modelMappingPaths = (
354
+ p: Program,
355
+ model: Model,
356
+ mapping: EnumMember,
357
+ ) =>
358
+ (
359
+ (g(p, stateKeys.mappingPaths, model) as MappingPathLink[] | undefined) ?? []
360
+ ).filter((link) => link.mapping === mapping);
208
361
  export const propValidationConstraints = (p: Program, prop: ModelProperty) =>
209
362
  (g(p, stateKeys.validationConstraints, prop) as
210
363
  Record<string, unknown> | undefined) ?? {};
@@ -291,26 +444,6 @@ export const modelLabel = (p: Program, model: Model) =>
291
444
  export const modelOrder = (p: Program, model: Model) =>
292
445
  g(p, stateKeys.order, model) as string[] | undefined;
293
446
 
294
- /**
295
- * `@UI.overrides` for any model, block or not, keyed by dotted path relative to that model.
296
- *
297
- * A block's own table also reaches emitters through `Block.overrides`. This accessor exists
298
- * for the other case: a form-local model composed by a form, whose overrides describe the
299
- * thing being composed rather than one occurrence of it. A form asking for the same contact
300
- * four times states such an override once, and an emitter rebases it onto each occurrence.
301
- */
302
- export const modelOverrides = (p: Program, model: Model) =>
303
- (g(p, stateKeys.overrides, model) as Record<
304
- string,
305
- Record<string, unknown>
306
- >) ?? {};
307
-
308
- export const propOverrides = (p: Program, prop: ModelProperty) =>
309
- (g(p, stateKeys.overrides, prop) as Record<
310
- string,
311
- Record<string, unknown>
312
- >) ?? {};
313
-
314
447
  /** Own and inherited properties in declaration order, with the derived declaration winning. */
315
448
  export function modelProperties(model: Model): ModelProperty[] {
316
449
  const chain: Model[] = [];
@@ -333,7 +466,7 @@ export function modelProperties(model: Model): ModelProperty[] {
333
466
  export function orderedProps(program: Program, block: Block): ModelProperty[] {
334
467
  if (block.model.kind !== "Model") return [];
335
468
  // A form may extend a question to preserve flat response fields with explicit semantic
336
- // lineage. Its inherited fields remain form members for sectioning and presentation.
469
+ // lineage. Its inherited fields remain its members for presentation.
337
470
  // Question artifacts themselves keep their local surface and compose their base through
338
471
  // JSON Schema `allOf`, so walking inherited properties there would duplicate that surface.
339
472
  const candidates =