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/layout.ts ADDED
@@ -0,0 +1,592 @@
1
+ import type {
2
+ DecoratorApplication,
3
+ Diagnostic,
4
+ DiagnosticTarget,
5
+ Model,
6
+ ModelProperty,
7
+ Program,
8
+ Type,
9
+ } from "@typespec/compiler";
10
+ import { type Node, SyntaxKind } from "@typespec/compiler/ast";
11
+ import { createDiagnostic } from "./lib.js";
12
+ import { literal } from "./decorators/shared.js";
13
+ import {
14
+ type AtomicCondition,
15
+ entryField,
16
+ modelProperties,
17
+ propEnabledWhen,
18
+ propFlatten,
19
+ propOmit,
20
+ propReadOnlyWhen,
21
+ propRequiredWhen,
22
+ propVisibleWhen,
23
+ } from "./model.js";
24
+
25
+ /** A rule JSON Forms applies to one entry: the effect, while the condition holds. */
26
+ export interface LayoutCondition {
27
+ effect: "SHOW" | "ENABLE" | "DISABLE";
28
+ /** Its source path is relative to the entry's scope, as a JSON Forms rule's is. */
29
+ condition: AtomicCondition;
30
+ }
31
+
32
+ /** One field placed by a layout. */
33
+ export interface FieldNode {
34
+ kind: "field";
35
+ entry: ModelProperty;
36
+ /** The field's data path from the presented model, through list entries. */
37
+ path: string[];
38
+ /** The field at that occurrence. */
39
+ prop: ModelProperty;
40
+ /** The path of the list whose entries this field sits in; scopes are relative to it. */
41
+ scopeBase: string[];
42
+ /** The entries `@UI.field` lists for this field's contents, laid out before the rest. */
43
+ listed?: LayoutNode[];
44
+ flatten: boolean;
45
+ condition?: LayoutCondition;
46
+ }
47
+
48
+ /** A group a layout makes up, holding only what it lists. */
49
+ export interface GroupNode {
50
+ kind: "group";
51
+ entry: ModelProperty;
52
+ children: LayoutNode[];
53
+ condition?: LayoutCondition;
54
+ }
55
+
56
+ export type LayoutNode = FieldNode | GroupNode;
57
+
58
+ export interface ResolvedLayout {
59
+ layout: Model;
60
+ presented: Model;
61
+ /** One node per section, in order. */
62
+ sections: LayoutNode[];
63
+ /** Every path placed by an entry or omitted by one, as `a.b.c`. */
64
+ placed: Set<string>;
65
+ omitted: Set<string>;
66
+ diagnostics: Diagnostic[];
67
+ }
68
+
69
+ /** Where the entries of one model of entries are resolved. */
70
+ interface Context {
71
+ /** The type of the `@UI.field` being laid out, which relative references start from. */
72
+ model?: Model;
73
+ /** That field's path from the presented model. */
74
+ path: string[];
75
+ /** The entry laying it out, for messages. */
76
+ label: string;
77
+ /** The path of the enclosing list, which scopes are relative to. */
78
+ scopeBase: string[];
79
+ }
80
+
81
+ /** The display conditions an entry can carry, and what each does while it holds. */
82
+ const CONDITIONS: Record<
83
+ string,
84
+ {
85
+ effect: LayoutCondition["effect"];
86
+ negated: boolean;
87
+ /** The field is usable only while the predicate holds, rather than only while it does not. */
88
+ whileTrue: boolean;
89
+ verb: string;
90
+ }
91
+ > = {
92
+ "@visibleWhen": {
93
+ effect: "SHOW",
94
+ negated: false,
95
+ whileTrue: true,
96
+ verb: "shown when",
97
+ },
98
+ "@hiddenWhen": {
99
+ effect: "SHOW",
100
+ negated: true,
101
+ whileTrue: false,
102
+ verb: "hidden when",
103
+ },
104
+ "@enabledWhen": {
105
+ effect: "ENABLE",
106
+ negated: false,
107
+ whileTrue: true,
108
+ verb: "enabled when",
109
+ },
110
+ "@disabledWhen": {
111
+ effect: "ENABLE",
112
+ negated: true,
113
+ whileTrue: false,
114
+ verb: "disabled when",
115
+ },
116
+ "@readOnlyWhen": {
117
+ effect: "DISABLE",
118
+ negated: false,
119
+ whileTrue: false,
120
+ verb: "read-only when",
121
+ },
122
+ };
123
+
124
+ const key = (path: string[]) => path.join(".");
125
+
126
+ const resolved = new WeakMap<Program, Map<Model, ResolvedLayout>>();
127
+
128
+ /**
129
+ * Resolve a `@UI.layout` model: every reference to the occurrence it means, what each
130
+ * entry places or omits, and the conditions entries set. Resolved once per program, so
131
+ * validation and emission share one result.
132
+ */
133
+ export function resolveLayout(
134
+ program: Program,
135
+ layout: Model,
136
+ presented: Model,
137
+ ): ResolvedLayout {
138
+ let cache = resolved.get(program);
139
+ if (!cache) resolved.set(program, (cache = new Map()));
140
+ let result = cache.get(layout);
141
+ if (!result)
142
+ cache.set(layout, (result = resolve(program, layout, presented)));
143
+ return result;
144
+ }
145
+
146
+ function resolve(
147
+ program: Program,
148
+ layout: Model,
149
+ presented: Model,
150
+ ): ResolvedLayout {
151
+ const diagnostics: Diagnostic[] = [];
152
+ const report = (
153
+ code: Parameters<typeof createDiagnostic>[0]["code"],
154
+ target: DiagnosticTarget,
155
+ format: Record<string, string>,
156
+ messageId?: string,
157
+ ) =>
158
+ diagnostics.push(
159
+ createDiagnostic({ code, target, format, messageId } as Parameters<
160
+ typeof createDiagnostic
161
+ >[0]),
162
+ );
163
+
164
+ const placed = new Map<string, ModelProperty>();
165
+ const omitted = new Set<string>();
166
+ const sourcesRead: { entry: ModelProperty; source: string[] }[] = [];
167
+
168
+ const place = (path: string[], entry: ModelProperty) => {
169
+ if (placed.has(key(path)))
170
+ report("layout-placed-twice", entry, {
171
+ path: key(path),
172
+ layout: layout.name,
173
+ });
174
+ placed.set(key(path), entry);
175
+ };
176
+
177
+ /**
178
+ * The occurrence a reference means. The compiler has resolved it to a property; this
179
+ * finds which occurrence of that property: relative to the field being laid out here,
180
+ * or absolute from the presented model.
181
+ */
182
+ const resolveRef = (
183
+ node: Node | undefined,
184
+ property: Type,
185
+ ctx: Context,
186
+ target: ModelProperty,
187
+ ): { path: string[]; prop: ModelProperty } | undefined => {
188
+ // A reference the compiler could not resolve is already an error of its own.
189
+ if (property.kind !== "ModelProperty") return undefined;
190
+ const ref = node && parseRef(node);
191
+ if (!ref) return undefined;
192
+ if (ctx.model) {
193
+ const found = walk(ctx.model, ref.steps);
194
+ if (found && sameProperty(found, property))
195
+ return { path: [...ctx.path, ...ref.steps], prop: found };
196
+ }
197
+ const found = walk(presented, ref.steps);
198
+ if (found && sameProperty(found, property)) {
199
+ if (!startsWith(ref.steps, ctx.scopeBase)) {
200
+ report(
201
+ "layout-ref-root",
202
+ target,
203
+ { ref: ref.text, list: key(ctx.scopeBase) },
204
+ "list",
205
+ );
206
+ return undefined;
207
+ }
208
+ return { path: ref.steps, prop: found };
209
+ }
210
+ report("layout-ref-root", target, {
211
+ ref: ref.text,
212
+ root: ref.root,
213
+ here: ctx.model
214
+ ? `${ctx.model.name || "the field's type"} (from ${ctx.label})`
215
+ : presented.name,
216
+ start: ctx.model?.name || presented.name,
217
+ presented: presented.name,
218
+ });
219
+ return undefined;
220
+ };
221
+
222
+ /** A placing entry is named for its field, or for a step on the way to it. */
223
+ const checkName = (entry: ModelProperty, path: string[]) => {
224
+ if (path.includes(entry.name)) return;
225
+ report("layout-entry-name", entry, {
226
+ name: entry.name,
227
+ path: key(path),
228
+ field: path[path.length - 1],
229
+ });
230
+ };
231
+
232
+ /** Validity belongs to the form, not to the layout presenting it. */
233
+ const checkNoValidation = (entry: ModelProperty) => {
234
+ for (const app of entry.decorators) {
235
+ const definition = app.definition;
236
+ if (definition?.namespace.name !== "Validation") continue;
237
+ report("layout-validation", entry, {
238
+ decorator: definition.name.slice(1),
239
+ name: entry.name,
240
+ presented: presented.name,
241
+ });
242
+ }
243
+ };
244
+
245
+ /** The display condition an entry sets, if any, with its source resolved like a reference. */
246
+ const conditionOf = (
247
+ entry: ModelProperty,
248
+ ctx: Context,
249
+ target?: { path: string[]; prop: ModelProperty },
250
+ ): LayoutCondition | undefined => {
251
+ const apps = entry.decorators.filter(
252
+ (app) =>
253
+ app.definition?.namespace.name === "UI" &&
254
+ app.definition.name in CONDITIONS,
255
+ );
256
+ if (!apps.length) return undefined;
257
+ if (apps.length > 1)
258
+ report("layout-condition-count", entry, {
259
+ name: entry.name,
260
+ count: String(apps.length),
261
+ });
262
+ const app = apps[apps.length - 1];
263
+ const kind = CONDITIONS[app.definition!.name];
264
+ const source = resolveRef(
265
+ sourceNode(app),
266
+ app.args[0]?.value as Type,
267
+ ctx,
268
+ entry,
269
+ );
270
+ if (!source) return undefined;
271
+ const value = literal(app.args[1]?.jsValue);
272
+ const text = `${kind.verb} ${key(source.path)} = ${String(value)}`;
273
+ sourcesRead.push({ entry, source: source.path });
274
+
275
+ if (target) {
276
+ const t = key(target.path);
277
+ const s = key(source.path);
278
+ if (s === t || s.startsWith(`${t}.`))
279
+ report("layout-condition-self", entry, { name: entry.name, source: s });
280
+
281
+ if (ownConditionCount(program, target.prop))
282
+ report("layout-condition-replaces", entry, {
283
+ name: entry.name,
284
+ field: target.prop.name,
285
+ });
286
+
287
+ const required = requiredness(program, target.prop, target.path);
288
+ const same = (r: { source: string; value: unknown }) =>
289
+ r.source === s && r.value === value;
290
+ if (required === "always")
291
+ report(
292
+ "layout-condition-hides-required",
293
+ entry,
294
+ { name: entry.name, condition: text },
295
+ "always",
296
+ );
297
+ else if (kind.whileTrue && required.some((r) => !same(r)))
298
+ report(
299
+ "layout-condition-hides-required",
300
+ entry,
301
+ {
302
+ name: entry.name,
303
+ required: required
304
+ .map((r) => `${r.source} = ${String(r.value)}`)
305
+ .join(" or "),
306
+ condition: `${key(source.path)} = ${String(value)}`,
307
+ },
308
+ "narrower",
309
+ );
310
+ else if (!kind.whileTrue && required.some(same))
311
+ report(
312
+ "layout-condition-hides-required",
313
+ entry,
314
+ { name: entry.name, condition: `${s} = ${String(value)}` },
315
+ "inverted",
316
+ );
317
+ }
318
+
319
+ const sourceIsArray =
320
+ source.prop.type.kind === "Model" && !!source.prop.type.indexer;
321
+ const sourcePath = source.path.slice(ctx.scopeBase.length);
322
+ return {
323
+ effect: kind.effect,
324
+ condition: kind.negated
325
+ ? {
326
+ operator: "notEquals",
327
+ sourcePath,
328
+ sourceIsArray,
329
+ value,
330
+ ...(app.definition!.name === "@hiddenWhen"
331
+ ? { requiresAnswer: true }
332
+ : {}),
333
+ }
334
+ : { operator: "equals", sourcePath, sourceIsArray, value },
335
+ };
336
+ };
337
+
338
+ /** One entry of a model of entries. */
339
+ const entryNode = (
340
+ entry: ModelProperty,
341
+ ctx: Context,
342
+ ): LayoutNode | undefined => {
343
+ checkNoValidation(entry);
344
+ const bound = entryField(program, entry);
345
+ const type = entry.type;
346
+
347
+ if (type.kind === "ModelProperty") {
348
+ const r = resolveRef(refNode(entry), type, ctx, entry);
349
+ if (!r) return undefined;
350
+ checkName(entry, r.path);
351
+ if (propOmit(program, entry)) {
352
+ omitted.add(key(r.path));
353
+ return undefined;
354
+ }
355
+ place(r.path, entry);
356
+ return fieldNode(entry, r, ctx);
357
+ }
358
+
359
+ if (type.kind !== "Model") {
360
+ if (type.kind !== "Intrinsic")
361
+ report("layout-entry-invalid", entry, { name: entry.name });
362
+ return undefined;
363
+ }
364
+
365
+ if (!bound) {
366
+ const children = entries(type, ctx);
367
+ return {
368
+ kind: "group",
369
+ entry,
370
+ children,
371
+ condition: conditionOf(entry, ctx),
372
+ };
373
+ }
374
+
375
+ const r = resolveRef(bound.node, bound.field, ctx, entry);
376
+ if (!r) return undefined;
377
+ checkName(entry, r.path);
378
+ if (propOmit(program, entry)) {
379
+ omitted.add(key(r.path));
380
+ return undefined;
381
+ }
382
+ place(r.path, entry);
383
+ const inner = behind(r.prop.type);
384
+ const listed = inner
385
+ ? entries(type, {
386
+ model: inner,
387
+ path: r.path,
388
+ label: entry.name,
389
+ scopeBase: isList(r.prop.type) ? r.path : ctx.scopeBase,
390
+ })
391
+ : [];
392
+ return { ...fieldNode(entry, r, ctx), listed };
393
+ };
394
+
395
+ const fieldNode = (
396
+ entry: ModelProperty,
397
+ r: { path: string[]; prop: ModelProperty },
398
+ ctx: Context,
399
+ ): FieldNode => {
400
+ const flatten = propFlatten(program, entry);
401
+ if (flatten && (!behind(r.prop.type) || isList(r.prop.type)))
402
+ report("layout-flatten-leaf", entry, { name: entry.name });
403
+ return {
404
+ kind: "field",
405
+ entry,
406
+ path: r.path,
407
+ prop: r.prop,
408
+ scopeBase: ctx.scopeBase,
409
+ flatten,
410
+ condition: conditionOf(entry, ctx, r),
411
+ };
412
+ };
413
+
414
+ const entries = (container: Model, ctx: Context): LayoutNode[] =>
415
+ [...container.properties.values()]
416
+ .map((entry) => entryNode(entry, ctx))
417
+ .filter((node): node is LayoutNode => !!node);
418
+
419
+ const sections: LayoutNode[] = [];
420
+ const root: Context = { path: [], label: layout.name, scopeBase: [] };
421
+ for (const section of layout.properties.values()) {
422
+ // A field left out of every section is the one reference a layout may make at the top.
423
+ const omittedField =
424
+ section.type.kind === "ModelProperty" && propOmit(program, section);
425
+ if (section.type.kind !== "Model" && !omittedField) {
426
+ checkNoValidation(section);
427
+ report("layout-section-not-group", section, {
428
+ layout: layout.name,
429
+ name: section.name,
430
+ });
431
+ continue;
432
+ }
433
+ const node = entryNode(section, root);
434
+ if (node) sections.push(node);
435
+ }
436
+
437
+ for (const prop of modelProperties(presented)) {
438
+ if (propOmit(program, prop)) continue;
439
+ if (placed.has(prop.name) || omitted.has(prop.name)) continue;
440
+ const parts = [...placed.keys()].filter((k) =>
441
+ k.startsWith(`${prop.name}.`),
442
+ );
443
+ report(
444
+ "layout-unplaced",
445
+ layout,
446
+ {
447
+ presented: presented.name,
448
+ field: prop.name,
449
+ layout: layout.name,
450
+ parts: parts.join(", "),
451
+ },
452
+ parts.length ? "partial" : undefined,
453
+ );
454
+ }
455
+
456
+ for (const { entry, source } of sourcesRead) {
457
+ const s = key(source);
458
+ if ([...omitted].some((o) => s === o || s.startsWith(`${o}.`)))
459
+ report("layout-condition-source-omitted", entry, {
460
+ name: entry.name,
461
+ source: s,
462
+ });
463
+ }
464
+
465
+ return {
466
+ layout,
467
+ presented,
468
+ sections,
469
+ placed: new Set(placed.keys()),
470
+ omitted,
471
+ diagnostics,
472
+ };
473
+ }
474
+
475
+ // --- references -----------------------------------------------------------
476
+
477
+ /** The syntax of a field reference used as an entry's type. */
478
+ const refNode = (entry: ModelProperty): Node | undefined => {
479
+ const node = entry.node;
480
+ return node?.kind === SyntaxKind.ModelProperty ? node.value : undefined;
481
+ };
482
+
483
+ /** The syntax of a condition decorator's source argument. */
484
+ const sourceNode = (app: DecoratorApplication): Node | undefined =>
485
+ app.node && "arguments" in app.node ? app.node.arguments[0] : undefined;
486
+
487
+ interface Segment {
488
+ selector: "" | "." | "::";
489
+ name: string;
490
+ }
491
+
492
+ function segments(node: Node): Segment[] | undefined {
493
+ switch (node.kind) {
494
+ case SyntaxKind.TypeReference:
495
+ return segments(node.target);
496
+ case SyntaxKind.MemberExpression: {
497
+ const base = segments(node.base);
498
+ return base && [...base, { selector: node.selector, name: node.id.sv }];
499
+ }
500
+ case SyntaxKind.Identifier:
501
+ return [{ selector: "", name: node.sv }];
502
+ default:
503
+ return undefined;
504
+ }
505
+ }
506
+
507
+ /**
508
+ * The property steps of `A.b::type.c`, and the text of what they start from. `::type`
509
+ * navigates to a property's type; it is not a step of its own.
510
+ */
511
+ function parseRef(
512
+ node: Node,
513
+ ): { steps: string[]; root: string; text: string } | undefined {
514
+ const segs = segments(node);
515
+ if (!segs) return undefined;
516
+ const first = segs.findIndex(
517
+ (s, i) =>
518
+ i > 0 &&
519
+ s.selector === "." &&
520
+ (segs[i + 1]?.selector === "::" || i === segs.length - 1),
521
+ );
522
+ if (first < 0) return undefined;
523
+ const text = (list: Segment[]) =>
524
+ list.map((s) => s.selector + s.name).join("");
525
+ return {
526
+ steps: segs
527
+ .slice(first)
528
+ .filter((s) => s.selector === ".")
529
+ .map((s) => s.name),
530
+ root: text(segs.slice(0, first)),
531
+ text: text(segs),
532
+ };
533
+ }
534
+
535
+ /** The same property, or one copied from the other by a spread, `is` or `OmitProperties`. */
536
+ function sameProperty(found: ModelProperty, wanted: ModelProperty): boolean {
537
+ for (let p: ModelProperty | undefined = found; p; p = p.sourceProperty)
538
+ if (p === wanted) return true;
539
+ for (let p: ModelProperty | undefined = wanted; p; p = p.sourceProperty)
540
+ if (p === found) return true;
541
+ return false;
542
+ }
543
+
544
+ /** Follow property names from `model`, through list entries, to where they end. */
545
+ function walk(model: Model, steps: string[]): ModelProperty | undefined {
546
+ let current: Model | undefined = model;
547
+ let prop: ModelProperty | undefined;
548
+ for (const step of steps) {
549
+ prop = current && modelProperties(current).find((p) => p.name === step);
550
+ if (!prop) return undefined;
551
+ current = behind(prop.type);
552
+ }
553
+ return prop;
554
+ }
555
+
556
+ /** The object a field holds, directly or as a list's entries. */
557
+ export function behind(type: Type): Model | undefined {
558
+ if (type.kind !== "Model") return undefined;
559
+ if (!type.indexer) return type;
560
+ return type.indexer.value.kind === "Model" ? type.indexer.value : undefined;
561
+ }
562
+
563
+ export const isList = (type: Type) => type.kind === "Model" && !!type.indexer;
564
+
565
+ const startsWith = (path: string[], prefix: string[]) =>
566
+ prefix.every((step, i) => path[i] === step);
567
+
568
+ // --- what the form says about a field -------------------------------------
569
+
570
+ const ownConditionCount = (program: Program, prop: ModelProperty) =>
571
+ propVisibleWhen(program, prop).length +
572
+ propEnabledWhen(program, prop).length +
573
+ propReadOnlyWhen(program, prop).length;
574
+
575
+ /** When the form requires a field: always, or under each of these conditions. */
576
+ function requiredness(
577
+ program: Program,
578
+ prop: ModelProperty,
579
+ path: string[],
580
+ ): "always" | { source: string; value: unknown }[] {
581
+ if (!prop.optional) return "always";
582
+ return propRequiredWhen(program, prop).flatMap((c) =>
583
+ c.operator === "equals"
584
+ ? [
585
+ {
586
+ source: key([...path.slice(0, -1), ...c.sourcePath]),
587
+ value: c.value,
588
+ },
589
+ ]
590
+ : [],
591
+ );
592
+ }