simpler-forms 0.1.0-alpha.1 → 0.2.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 (89) hide show
  1. package/README.md +300 -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 +17 -0
  9. package/dist/src/decorators/mapping.js +122 -0
  10. package/dist/src/decorators/mapping.js.map +1 -0
  11. package/dist/src/decorators/meta.d.ts +15 -5
  12. package/dist/src/decorators/meta.js +31 -4
  13. package/dist/src/decorators/meta.js.map +1 -1
  14. package/dist/src/decorators/shared.d.ts +2 -2
  15. package/dist/src/decorators/shared.js.map +1 -1
  16. package/dist/src/decorators/ui.d.ts +14 -12
  17. package/dist/src/decorators/ui.js +20 -43
  18. package/dist/src/decorators/ui.js.map +1 -1
  19. package/dist/src/emitter.d.ts +8 -0
  20. package/dist/src/emitter.js +55 -16
  21. package/dist/src/emitter.js.map +1 -1
  22. package/dist/src/emitters/block-ui.d.ts +14 -3
  23. package/dist/src/emitters/block-ui.js +81 -119
  24. package/dist/src/emitters/block-ui.js.map +1 -1
  25. package/dist/src/emitters/layout-ui.d.ts +12 -0
  26. package/dist/src/emitters/layout-ui.js +225 -0
  27. package/dist/src/emitters/layout-ui.js.map +1 -0
  28. package/dist/src/emitters/overlay.d.ts +10 -8
  29. package/dist/src/emitters/overlay.js +30 -105
  30. package/dist/src/emitters/overlay.js.map +1 -1
  31. package/dist/src/forms.d.ts +23 -0
  32. package/dist/src/forms.js +54 -0
  33. package/dist/src/forms.js.map +1 -0
  34. package/dist/src/index.d.ts +22 -8
  35. package/dist/src/index.js +17 -3
  36. package/dist/src/index.js.map +1 -1
  37. package/dist/src/layout.d.ts +50 -0
  38. package/dist/src/layout.js +397 -0
  39. package/dist/src/layout.js.map +1 -0
  40. package/dist/src/lib.d.ts +585 -39
  41. package/dist/src/lib.js +258 -15
  42. package/dist/src/lib.js.map +1 -1
  43. package/dist/src/linter.js +8 -43
  44. package/dist/src/linter.js.map +1 -1
  45. package/dist/src/mapping-from.d.ts +21 -0
  46. package/dist/src/mapping-from.js +169 -0
  47. package/dist/src/mapping-from.js.map +1 -0
  48. package/dist/src/mapping.d.ts +65 -0
  49. package/dist/src/mapping.js +209 -0
  50. package/dist/src/mapping.js.map +1 -0
  51. package/dist/src/model.d.ts +143 -23
  52. package/dist/src/model.js +108 -21
  53. package/dist/src/model.js.map +1 -1
  54. package/dist/src/public.d.ts +20 -4
  55. package/dist/src/public.js +28 -4
  56. package/dist/src/public.js.map +1 -1
  57. package/dist/src/validate.d.ts +2 -1
  58. package/dist/src/validate.js +208 -58
  59. package/dist/src/validate.js.map +1 -1
  60. package/lib/main.tsp +1 -0
  61. package/lib/mapping.tsp +173 -0
  62. package/lib/meta.tsp +25 -6
  63. package/lib/types.tsp +24 -63
  64. package/lib/ui.tsp +34 -17
  65. package/package.json +17 -11
  66. package/src/casing.ts +172 -0
  67. package/src/decorators/index.ts +1 -0
  68. package/src/decorators/mapping.ts +200 -0
  69. package/src/decorators/meta.ts +56 -13
  70. package/src/decorators/shared.ts +6 -1
  71. package/src/decorators/ui.ts +43 -59
  72. package/src/emitter.ts +76 -16
  73. package/src/emitters/block-ui.ts +83 -135
  74. package/src/emitters/layout-ui.ts +280 -0
  75. package/src/emitters/overlay.ts +43 -116
  76. package/src/forms.ts +82 -0
  77. package/src/index.ts +17 -3
  78. package/src/layout.ts +592 -0
  79. package/src/lib.ts +264 -15
  80. package/src/linter.ts +17 -49
  81. package/src/mapping-from.ts +216 -0
  82. package/src/mapping.ts +358 -0
  83. package/src/model.ts +287 -46
  84. package/src/public.ts +60 -6
  85. package/src/validate.ts +221 -60
  86. package/dist/src/emitters/override-condition.d.ts +0 -12
  87. package/dist/src/emitters/override-condition.js +0 -25
  88. package/dist/src/emitters/override-condition.js.map +0 -1
  89. package/src/emitters/override-condition.ts +0 -37
@@ -7,19 +7,16 @@ import {
7
7
  childBlock,
8
8
  modelLabel,
9
9
  modelOrder,
10
- modelOverrides,
11
10
  modelProperties,
12
11
  propEnabledWhen,
13
12
  propLabel,
14
13
  propOmit,
15
14
  propReadOnly,
16
15
  propReadOnlyWhen,
17
- propSection,
18
16
  propVisibleWhen,
19
17
  propWidget,
20
18
  readBlock,
21
19
  } from "../model.js";
22
- import { normalizedOverrideEnabledWhen } from "./override-condition.js";
23
20
 
24
21
  /** An answered value: not null, not blank, not an empty list. */
25
22
  const answeredSchema = {
@@ -54,7 +51,9 @@ const rootConditionSchema = (
54
51
  return schema;
55
52
  };
56
53
 
57
- const jsonFormsCondition = (condition: Condition): Record<string, unknown> =>
54
+ export const jsonFormsCondition = (
55
+ condition: Condition,
56
+ ): Record<string, unknown> =>
58
57
  condition.operator === "any"
59
58
  ? {
60
59
  scope: "#",
@@ -79,56 +78,6 @@ export interface UiNode {
79
78
  rule?: Record<string, unknown>;
80
79
  }
81
80
 
82
- type Overrides = Record<string, Record<string, unknown>>;
83
-
84
- /** Convert a canonical JSON Forms scope into the dotted path addressed by @UI.overrides. */
85
- const dataPathFromScope = (scope: string): string | undefined => {
86
- if (!scope.startsWith("#/properties/")) return undefined;
87
- const tokens = scope.slice(2).split("/");
88
- const path: string[] = [];
89
- for (let index = 0; index < tokens.length;) {
90
- if (tokens[index] !== "properties" || !tokens[index + 1]) return undefined;
91
- path.push(tokens[index + 1]);
92
- index += 2;
93
- if (tokens[index] === "items") index += 1;
94
- }
95
- return path.join(".");
96
- };
97
-
98
- /** Emit the portable JSON Forms equivalent of a form-scoped enabledWhen override. */
99
- const overrideEnabledRule = (
100
- override: Record<string, unknown>,
101
- ): Record<string, unknown> | undefined => {
102
- const condition = normalizedOverrideEnabledWhen(override);
103
- if (!condition) return undefined;
104
- const scope = `#/${condition.sourcePath.map((step) => `properties/${step}`).join("/")}`;
105
- const schema =
106
- condition.operator === "in"
107
- ? { enum: condition.values }
108
- : { const: condition.value };
109
- return { effect: "ENABLE", condition: { scope, schema } };
110
- };
111
-
112
- /** Apply occurrence-specific behavior after recursively composing canonical child UI trees. */
113
- const applyOverrides = (node: UiNode, overrides: Overrides): UiNode => {
114
- const out: UiNode = { ...node };
115
- if (typeof node.scope === "string") {
116
- const path = dataPathFromScope(node.scope);
117
- const rule = path ? overrideEnabledRule(overrides[path] ?? {}) : undefined;
118
- if (rule && node.rule) {
119
- throw new Error(
120
- `@UI.overrides enabledWhen collides with intrinsic UI behavior at ${path}`,
121
- );
122
- }
123
- if (rule) out.rule = rule;
124
- }
125
- if (node.elements)
126
- out.elements = node.elements.map((child) =>
127
- applyOverrides(child, overrides),
128
- );
129
- return out;
130
- };
131
-
132
81
  /** Re-prefix every Control scope so a child's tree sits under `propName`. */
133
82
  export function rescopeUi(node: UiNode, propName: string): UiNode {
134
83
  const out: UiNode = { ...node };
@@ -187,19 +136,15 @@ function layoutProps(program: Program, model: Model): ModelProperty[] {
187
136
  }
188
137
 
189
138
  /** What laying out a model needs, which a block has and a form-local model can supply. */
190
- type LayoutSource = Pick<
191
- Block,
192
- "model" | "scalar" | "label" | "doc" | "sections" | "overrides"
193
- >;
139
+ type LayoutSource = Pick<Block, "model" | "scalar" | "label" | "doc">;
194
140
 
195
141
  /** A model laid out as a block would be, whether or not it is one. */
196
- const layoutSource = (program: Program, model: Model): LayoutSource =>
142
+ export const layoutSource = (program: Program, model: Model): LayoutSource =>
197
143
  readBlock(program, model) ?? {
198
144
  model,
199
145
  scalar: false,
200
146
  label: modelLabel(program, model),
201
147
  doc: getDoc(program, model),
202
- overrides: modelOverrides(program, model),
203
148
  };
204
149
 
205
150
  /**
@@ -219,104 +164,107 @@ function itemDetail(program: Program, prop: ModelProperty): UiNode | undefined {
219
164
  return emitBlockUi(program, layoutSource(program, item));
220
165
  }
221
166
 
167
+ /** The model a property's fields lay out from: a composed question, or a model of the form's own. */
168
+ function fieldSource(
169
+ program: Program,
170
+ prop: ModelProperty,
171
+ ): LayoutSource | undefined {
172
+ const child = childBlock(program, prop);
173
+ if (child) return child.scalar ? undefined : child;
174
+ const type = prop.type;
175
+ if (type.kind !== "Model" || type.indexer) return undefined;
176
+ return layoutSource(program, type);
177
+ }
178
+
222
179
  /**
223
- * The canonical UI artifact for one block. Scopes are relative to this block's own
224
- * root, so it renders standalone; children are incorporated and re-scoped.
180
+ * One property as its model lays it out, scoped to that model: a Group of a composed
181
+ * question's fields, or of a model the form declares itself, or a Control.
182
+ *
183
+ * A Group takes the property's label before the question's, and the property's doc comment
184
+ * as its description, so a question asked in several roles is told apart by role.
225
185
  */
226
- export function emitBlockUi(program: Program, block: LayoutSource): UiNode {
227
- // A single-value question renders as one Control at its own root.
228
- if (block.scalar) {
229
- const node: UiNode = { type: "Control", scope: "#" };
230
- if (block.label) node.label = block.label;
231
- return node;
186
+ export function propertyUi(program: Program, prop: ModelProperty): UiNode {
187
+ const child = fieldSource(program, prop);
188
+ if (child) {
189
+ const group = rescopeUi(emitBlockUi(program, child), prop.name);
190
+ const label = propLabel(program, prop);
191
+ if (label) group.label = label;
192
+ const description = getDoc(program, prop);
193
+ const widget = propWidget(program, prop);
194
+ if (description || widget)
195
+ group.options = {
196
+ ...(group.options ?? {}),
197
+ ...(description ? { description } : {}),
198
+ ...(widget ? { widget } : {}),
199
+ };
200
+ return group;
232
201
  }
233
202
 
234
- const nodeForProperty = (prop: ModelProperty): UiNode => {
235
- const child = childBlock(program, prop);
236
- if (child && !child.scalar) {
237
- return rescopeUi(emitBlockUi(program, child), prop.name);
238
- }
239
-
240
- const node: UiNode = {
241
- type: "Control",
242
- scope: `#/properties/${prop.name}`,
243
- };
244
- const label = propLabel(program, prop);
245
- if (label) node.label = label;
203
+ const node: UiNode = {
204
+ type: "Control",
205
+ scope: `#/properties/${prop.name}`,
206
+ };
207
+ const label = propLabel(program, prop);
208
+ if (label) node.label = label;
246
209
 
247
- const detail = itemDetail(program, prop);
248
- if (detail) node.options = { ...(node.options ?? {}), detail };
210
+ const detail = itemDetail(program, prop);
211
+ if (detail) node.options = { ...(node.options ?? {}), detail };
249
212
 
250
- const widget = propWidget(program, prop);
251
- if (widget) node.options = { ...(node.options ?? {}), widget };
252
- if (propReadOnly(program, prop))
253
- node.options = { ...(node.options ?? {}), readonly: true };
213
+ const widget = propWidget(program, prop);
214
+ if (widget) node.options = { ...(node.options ?? {}), widget };
215
+ if (propReadOnly(program, prop))
216
+ node.options = { ...(node.options ?? {}), readonly: true };
254
217
 
255
- const conds = propVisibleWhen(program, prop);
256
- if (conds.length === 1) {
257
- const c = conds[0];
218
+ const conds = propVisibleWhen(program, prop);
219
+ if (conds.length === 1) {
220
+ const c = conds[0];
221
+ node.rule = {
222
+ effect: "SHOW",
223
+ condition: jsonFormsCondition(c),
224
+ };
225
+ } else {
226
+ const enabled = propEnabledWhen(program, prop);
227
+ if (enabled.length === 1) {
228
+ const c = enabled[0];
258
229
  node.rule = {
259
- effect: "SHOW",
230
+ effect: "ENABLE",
260
231
  condition: jsonFormsCondition(c),
261
232
  };
262
233
  } else {
263
- const enabled = propEnabledWhen(program, prop);
264
- if (enabled.length === 1) {
265
- const c = enabled[0];
234
+ const readOnly = propReadOnlyWhen(program, prop);
235
+ if (readOnly.length === 1) {
236
+ const c = readOnly[0];
266
237
  node.rule = {
267
- effect: "ENABLE",
238
+ effect: "DISABLE",
268
239
  condition: jsonFormsCondition(c),
269
240
  };
270
- } else {
271
- const readOnly = propReadOnlyWhen(program, prop);
272
- if (readOnly.length === 1) {
273
- const c = readOnly[0];
274
- node.rule = {
275
- effect: "DISABLE",
276
- condition: jsonFormsCondition(c),
277
- };
278
- }
279
241
  }
280
242
  }
243
+ }
244
+
245
+ return node;
246
+ }
281
247
 
248
+ /**
249
+ * The canonical UI artifact for one block. Scopes are relative to this block's own
250
+ * root, so it renders standalone; children are incorporated and re-scoped.
251
+ */
252
+ export function emitBlockUi(program: Program, block: LayoutSource): UiNode {
253
+ // A single-value question renders as one Control at its own root.
254
+ if (block.scalar) {
255
+ const node: UiNode = { type: "Control", scope: "#" };
256
+ if (block.label) node.label = block.label;
282
257
  return node;
283
- };
258
+ }
259
+
260
+ const nodeForProperty = (prop: ModelProperty) => propertyUi(program, prop);
284
261
 
285
262
  const props =
286
263
  block.model.kind === "Model" ? layoutProps(program, block.model) : [];
287
- const elements: UiNode[] = [];
288
- const documentedStaticSections = block.sections
289
- ? [...block.sections.members.values()].filter(
290
- (member) =>
291
- getDoc(program, member) &&
292
- !props.some(
293
- (prop) => propSection(program, prop)?.name === member.name,
294
- ),
295
- )
296
- : [];
297
-
298
- if (documentedStaticSections.length && block.sections) {
299
- const emitted = new Set<string>();
300
- for (const member of block.sections.members.values()) {
301
- const description = getDoc(program, member);
302
- const members = props.filter(
303
- (prop) => propSection(program, prop)?.name === member.name,
304
- );
305
- if (!members.length && description)
306
- elements.push({ type: "Label", text: description });
307
- for (const prop of members) {
308
- elements.push(nodeForProperty(prop));
309
- emitted.add(prop.name);
310
- }
311
- }
312
- for (const prop of props)
313
- if (!emitted.has(prop.name)) elements.push(nodeForProperty(prop));
314
- } else {
315
- elements.push(...props.map(nodeForProperty));
316
- }
264
+ const elements = props.map(nodeForProperty);
317
265
 
318
266
  const group: UiNode = { type: "Group", elements };
319
267
  if (block.label) group.label = block.label;
320
268
  else if (block.doc) group.label = block.doc;
321
- return applyOverrides(group, block.overrides);
269
+ return group;
322
270
  }
@@ -0,0 +1,280 @@
1
+ import type { Diagnostic, Program } from "@typespec/compiler";
2
+ import { getDoc } from "@typespec/compiler";
3
+ import { createDiagnostic } from "../lib.js";
4
+ import {
5
+ type FieldNode,
6
+ type LayoutNode,
7
+ type ResolvedLayout,
8
+ behind,
9
+ isList,
10
+ } from "../layout.js";
11
+ import {
12
+ modelLabel,
13
+ propHelpText,
14
+ propLabel,
15
+ propWidget,
16
+ readBlock,
17
+ } from "../model.js";
18
+ import {
19
+ type UiNode,
20
+ emitBlockUi,
21
+ jsonFormsCondition,
22
+ layoutSource,
23
+ propertyUi,
24
+ rescopeUi,
25
+ } from "./block-ui.js";
26
+
27
+ const key = (path: string[]) => path.join(".");
28
+
29
+ const emitted = new WeakMap<
30
+ ResolvedLayout,
31
+ { ui: UiNode; diagnostics: Diagnostic[] }
32
+ >();
33
+
34
+ /**
35
+ * The JSON Forms UI for a layout. Each section is a top-level labelled Group. A field is laid
36
+ * out as its model lays it out, less whatever the layout places elsewhere or omits, with the
37
+ * entry's label, help text, widget and condition on top.
38
+ */
39
+ export function emitLayoutUi(
40
+ program: Program,
41
+ layout: ResolvedLayout,
42
+ ): { ui: UiNode; diagnostics: Diagnostic[] } {
43
+ let result = emitted.get(layout);
44
+ if (!result) emitted.set(layout, (result = build(program, layout)));
45
+ return result;
46
+ }
47
+
48
+ function build(
49
+ program: Program,
50
+ layout: ResolvedLayout,
51
+ ): { ui: UiNode; diagnostics: Diagnostic[] } {
52
+ const excluded = [...layout.placed, ...layout.omitted];
53
+ /** What is placed elsewhere or omitted from inside the field at `path`. */
54
+ const excludedUnder = (path: string[]) =>
55
+ excluded.filter((k) => k.startsWith(`${key(path)}.`));
56
+
57
+ const help = (entry: FieldNode["entry"]) =>
58
+ propHelpText(program, entry) ?? getDoc(program, entry);
59
+
60
+ /** Apply what an entry says about how its field is presented. */
61
+ const present = (
62
+ ui: UiNode,
63
+ node: LayoutNode,
64
+ fallbackLabel?: string,
65
+ ): UiNode => {
66
+ const out: UiNode = { ...ui };
67
+ const label = propLabel(program, node.entry) ?? fallbackLabel;
68
+ if (label) out.label = label;
69
+ const description = help(node.entry);
70
+ const widget = propWidget(program, node.entry);
71
+ if (description || widget)
72
+ out.options = {
73
+ ...(out.options ?? {}),
74
+ ...(description ? { description } : {}),
75
+ ...(widget ? { widget } : {}),
76
+ };
77
+ // A layout's condition replaces the field's own for this occurrence.
78
+ if (node.condition)
79
+ out.rule = {
80
+ effect: node.condition.effect,
81
+ condition: jsonFormsCondition(node.condition.condition),
82
+ };
83
+ return out;
84
+ };
85
+
86
+ /** The fields of `node`'s type that no entry lists, in their model's order. */
87
+ const remaining = (node: FieldNode): UiNode[] => {
88
+ const inner = behind(node.prop.type);
89
+ if (!inner) return [];
90
+ const list = isList(node.prop.type);
91
+ const base = list ? node.path : node.scopeBase;
92
+ const steps = list ? [] : node.path.slice(node.scopeBase.length);
93
+ const own = emitBlockUi(program, layoutSource(program, inner));
94
+ // A single-value question has no fields to lay out.
95
+ if (!own.elements) return [];
96
+ return own.elements
97
+ .map((child) => rescopeAll(child, steps))
98
+ .map((child) => prune(child, base, excludedUnder(node.path)))
99
+ .filter((child): child is UiNode => !!child);
100
+ };
101
+
102
+ const fieldUi = (node: FieldNode): UiNode[] => {
103
+ const own = rescopeAll(
104
+ propertyUi(program, node.prop),
105
+ node.path.slice(node.scopeBase.length, -1),
106
+ );
107
+ let ui: UiNode | undefined;
108
+ if (node.listed) {
109
+ const elements = [...node.listed.flatMap(nodeUi), ...remaining(node)];
110
+ if (isList(node.prop.type)) {
111
+ ui = {
112
+ ...own,
113
+ options: {
114
+ ...(own.options ?? {}),
115
+ detail: { type: "Group", elements },
116
+ },
117
+ };
118
+ } else if (own.type === "Group") {
119
+ ui = { ...own, elements };
120
+ } else {
121
+ // A field that lays out as one Control becomes a Group once a layout lists entries in it.
122
+ const inner = behind(node.prop.type);
123
+ const label =
124
+ propLabel(program, node.prop) ??
125
+ (inner ? modelLabel(program, inner) : undefined);
126
+ const description = getDoc(program, node.prop);
127
+ ui = {
128
+ type: "Group",
129
+ ...(label ? { label } : {}),
130
+ elements,
131
+ ...(description ? { options: { description } } : {}),
132
+ };
133
+ }
134
+ } else {
135
+ ui = prune(own, node.scopeBase, excludedUnder(node.path));
136
+ }
137
+ if (!ui) return [];
138
+ ui = present(ui, node);
139
+ if (node.flatten && ui.type === "Group" && !isList(node.prop.type)) {
140
+ const rule = ui.rule;
141
+ return (ui.elements ?? []).map((child) =>
142
+ rule && !child.rule ? { ...child, rule } : child,
143
+ );
144
+ }
145
+ return [ui];
146
+ };
147
+
148
+ const nodeUi = (node: LayoutNode): UiNode[] =>
149
+ node.kind === "field"
150
+ ? fieldUi(node)
151
+ : [
152
+ present(
153
+ { type: "Group", elements: node.children.flatMap(nodeUi) },
154
+ node,
155
+ node.entry.name,
156
+ ),
157
+ ];
158
+
159
+ /** A section is always a labelled Group, so a renderer shows it as one. */
160
+ const sectionUi = (node: LayoutNode): UiNode[] => {
161
+ const flat = node.kind === "field" ? { ...node, flatten: false } : node;
162
+ return nodeUi(flat).map((ui) => {
163
+ if (ui.type === "Group" && ui.label) return ui;
164
+ if (ui.type === "Group") return { ...ui, label: node.entry.name };
165
+ // The section carries the control's rule and description: they are about the section.
166
+ const { rule, ...control } = ui;
167
+ const { description, ...options } = control.options ?? {};
168
+ if (control.options)
169
+ if (Object.keys(options).length) control.options = options;
170
+ else delete control.options;
171
+ const label =
172
+ propLabel(program, node.entry) ??
173
+ (node.kind === "field"
174
+ ? (propLabel(program, node.prop) ?? node.prop.name)
175
+ : node.entry.name);
176
+ return {
177
+ type: "Group",
178
+ label,
179
+ elements: [control],
180
+ ...(rule ? { rule } : {}),
181
+ ...(description ? { options: { description } } : {}),
182
+ };
183
+ });
184
+ };
185
+
186
+ const block = readBlock(program, layout.presented);
187
+ const label =
188
+ modelLabel(program, layout.layout) ?? block?.label ?? block?.doc;
189
+ const ui: UiNode = {
190
+ type: "Group",
191
+ elements: layout.sections.flatMap(sectionUi),
192
+ ...(label ? { label } : {}),
193
+ };
194
+
195
+ return { ui, diagnostics: duplicateLabels(layout, ui) };
196
+ }
197
+
198
+ /** Re-prefix a node's scopes with each of `steps`, outermost first. */
199
+ function rescopeAll(node: UiNode, steps: string[]): UiNode {
200
+ return [...steps].reverse().reduce(rescopeUi, node);
201
+ }
202
+
203
+ /** The data path a Control's scope addresses, relative to its root. */
204
+ function dataPath(scope: string): string[] {
205
+ const tokens = scope.replace(/^#\/?/, "").split("/").filter(Boolean);
206
+ const path: string[] = [];
207
+ for (let i = 0; i < tokens.length; i++) {
208
+ if (tokens[i] === "properties" && tokens[i + 1]) path.push(tokens[++i]);
209
+ }
210
+ return path;
211
+ }
212
+
213
+ /**
214
+ * Drop every Control whose path is excluded, and any Group left empty by that. `base` is the
215
+ * path the node's scopes are relative to.
216
+ */
217
+ function prune(
218
+ node: UiNode,
219
+ base: string[],
220
+ excluded: string[],
221
+ ): UiNode | undefined {
222
+ if (!excluded.length) return node;
223
+ const out: UiNode = { ...node };
224
+ if (node.scope) {
225
+ const path = key([...base, ...dataPath(node.scope)]);
226
+ if (excluded.some((e) => path === e || path.startsWith(`${e}.`)))
227
+ return undefined;
228
+ const detail = node.options?.detail as UiNode | undefined;
229
+ if (detail) {
230
+ const pruned = prune(detail, path ? path.split(".") : base, excluded);
231
+ out.options = {
232
+ ...node.options,
233
+ detail: pruned ?? { ...detail, elements: [] },
234
+ };
235
+ }
236
+ }
237
+ if (node.elements) {
238
+ out.elements = node.elements
239
+ .map((child) => prune(child, base, excluded))
240
+ .filter((child): child is UiNode => !!child);
241
+ if (node.elements.length && !out.elements.length) return undefined;
242
+ }
243
+ return out;
244
+ }
245
+
246
+ /** Sibling entries that render under one label, which an applicant cannot tell apart. */
247
+ function duplicateLabels(layout: ResolvedLayout, ui: UiNode): Diagnostic[] {
248
+ const out: Diagnostic[] = [];
249
+ const name = (node: UiNode) =>
250
+ node.label ?? (node.scope ? dataPath(node.scope).slice(-1)[0] : undefined);
251
+ const visit = (node: UiNode, where: string) => {
252
+ const seen = new Map<string, UiNode>();
253
+ for (const child of node.elements ?? []) {
254
+ const label = name(child);
255
+ const first = label === undefined ? undefined : seen.get(label);
256
+ if (label !== undefined && first)
257
+ out.push(
258
+ createDiagnostic({
259
+ code: "layout-duplicate-label",
260
+ target: layout.layout,
261
+ format: {
262
+ where,
263
+ label,
264
+ first: describe(first),
265
+ second: describe(child),
266
+ },
267
+ }),
268
+ );
269
+ else if (label !== undefined) seen.set(label, child);
270
+ visit(child, child.label ?? where);
271
+ const detail = child.options?.detail as UiNode | undefined;
272
+ if (detail) visit(detail, label ?? where);
273
+ }
274
+ };
275
+ visit(ui, layout.layout.name);
276
+ return out;
277
+ }
278
+
279
+ const describe = (node: UiNode) =>
280
+ node.scope ?? (node.label ? `the group "${node.label}"` : "a group");