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
package/src/mapping.ts ADDED
@@ -0,0 +1,358 @@
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
+ type MappingSelection,
12
+ modelMappedTo,
13
+ modelMappingPaths,
14
+ modelProperties,
15
+ propMappedTo,
16
+ } from "./model.js";
17
+
18
+ /**
19
+ * How a linked source property relates to its target:
20
+ *
21
+ * leaf a value, linked to a value
22
+ * object an object, linked to an object; its children are linked within that object
23
+ * array a list, linked to a list; for a list of objects, its children are linked within
24
+ * the target's item
25
+ * group an object with no link of its own whose descendants are linked: flattened, its
26
+ * children landing in the parent's target
27
+ */
28
+ export type MappingNodeKind = "leaf" | "object" | "array" | "group";
29
+
30
+ export interface MappingNode {
31
+ /** Property names from the mapped model to this one, this one last. */
32
+ sourcePath: string[];
33
+ source: ModelProperty;
34
+ /** What the source corresponds to. A group has none. */
35
+ target?: ModelProperty;
36
+ kind: MappingNodeKind;
37
+ /** The linked properties below an object, a list of objects, or a group, in source order. */
38
+ children: MappingNode[];
39
+ /**
40
+ * Which items of the list at `sourcePath` fill the target, when its `@Mapping.from` names
41
+ * them with `Item` or `Slice`. `kind` describes what's selected: one item of a list of
42
+ * objects is an `object`.
43
+ */
44
+ select?: MappingSelection;
45
+ }
46
+
47
+ /** A model's mapping in one target, resolved through every model it composes. */
48
+ export interface ResolvedMapping {
49
+ mapping: EnumMember;
50
+ source: Model;
51
+ target: Model;
52
+ children: MappingNode[];
53
+ }
54
+
55
+ /** A link that does not hold, ready to report as a diagnostic. */
56
+ export interface MappingProblem {
57
+ code:
58
+ | "mapping-path-unresolved"
59
+ | "mapping-target-out-of-context"
60
+ | "mapping-type-mismatch"
61
+ | "mapping-cardinality-mismatch"
62
+ | "mapping-target-duplicate";
63
+ target: Model | ModelProperty;
64
+ format: Record<string, string>;
65
+ }
66
+
67
+ /**
68
+ * `model`'s mapping in `mapping`, or undefined if `model` has no `@Mapping.to` in it.
69
+ *
70
+ * A property takes the first link that applies:
71
+ *
72
+ * 1. a `@Mapping.path` naming it, from the outermost composing model that has one
73
+ * 2. its own `@Mapping.to`
74
+ *
75
+ * and an object-valued property then resolves its own properties against what it is linked
76
+ * to, so a question linked once at the level its mapping is shared needs nothing more from
77
+ * the forms that compose it. Links that do not hold are left out; `$onValidate` reports them.
78
+ */
79
+ export function resolveMapping(
80
+ program: Program,
81
+ model: Model,
82
+ mapping: EnumMember,
83
+ ): ResolvedMapping | undefined {
84
+ return resolveMappingWithProblems(program, model, mapping)?.resolved;
85
+ }
86
+
87
+ /** `resolveMapping`, with every link that did not hold. */
88
+ export function resolveMappingWithProblems(
89
+ program: Program,
90
+ model: Model,
91
+ mapping: EnumMember,
92
+ ): { resolved: ResolvedMapping; problems: MappingProblem[] } | undefined {
93
+ const target = modelMappedTo(program, model, mapping);
94
+ if (!target) return undefined;
95
+ const walk: Walk = {
96
+ program,
97
+ mapping,
98
+ problems: [],
99
+ owned: [],
100
+ consumed: new Set(),
101
+ };
102
+ const children = resolveModel(walk, model, target, [], [], new Map(), [
103
+ model,
104
+ ]);
105
+ for (const { link, owner } of walk.owned) {
106
+ if (walk.consumed.has(link)) continue;
107
+ walk.problems.push({
108
+ code: "mapping-path-unresolved",
109
+ target: owner,
110
+ format: {
111
+ path: link.sourcePath,
112
+ name: owner.name || "an anonymous model",
113
+ reason: unresolvedReason(owner, link.sourcePath),
114
+ },
115
+ });
116
+ }
117
+ return {
118
+ resolved: { mapping, source: model, target, children },
119
+ problems: walk.problems,
120
+ };
121
+ }
122
+
123
+ // ---------------------------------------------------------------------------
124
+
125
+ interface Walk {
126
+ program: Program;
127
+ mapping: EnumMember;
128
+ problems: MappingProblem[];
129
+ /** Every `@Mapping.path` met so far, with the model that declares it. */
130
+ owned: { link: MappingPathLink; owner: Model }[];
131
+ /** The ones that landed on a property. */
132
+ consumed: Set<MappingPathLink>;
133
+ }
134
+
135
+ /** A `@Mapping.path` on its way down: the steps still to take from the current model. */
136
+ interface PendingPath {
137
+ link: MappingPathLink;
138
+ owner: Model;
139
+ steps: string[];
140
+ }
141
+
142
+ /**
143
+ * `model`'s linked properties, each resolved within `context`, the model `model` (or its
144
+ * nearest linked ancestor, for a group) corresponds to.
145
+ *
146
+ * `claimed` is shared by everything that lands in one target object, groups included, so two
147
+ * sources landing on one target property are caught wherever they were declared.
148
+ */
149
+ function resolveModel(
150
+ walk: Walk,
151
+ model: Model,
152
+ context: Model,
153
+ inherited: PendingPath[],
154
+ prefix: string[],
155
+ claimed: Map<ModelProperty, string>,
156
+ stack: Model[],
157
+ ): MappingNode[] {
158
+ const own = modelMappingPaths(walk.program, model, walk.mapping).map(
159
+ (link) => {
160
+ walk.owned.push({ link, owner: model });
161
+ return { link, owner: model, steps: stepsOf(link.sourcePath) };
162
+ },
163
+ );
164
+ // Outermost first, so `find` below prefers the composing model furthest out.
165
+ const pending = [...inherited, ...own];
166
+ const nodes: MappingNode[] = [];
167
+
168
+ for (const source of modelProperties(model)) {
169
+ const here = pending.filter((p) => p.steps[0] === source.name);
170
+ const exact = here.filter((p) => p.steps.length === 1);
171
+ for (const p of exact) walk.consumed.add(p.link);
172
+ const deeper = here
173
+ .filter((p) => p.steps.length > 1)
174
+ .map((p) => ({ ...p, steps: p.steps.slice(1) }));
175
+ const path = exact[0];
176
+ const target =
177
+ path?.link.mappedTo ?? propMappedTo(walk.program, source, walk.mapping);
178
+ const sourcePath = [...prefix, source.name];
179
+ const at = path?.owner ?? source;
180
+ const shape = shapeOf(source.type);
181
+
182
+ if (!target) {
183
+ // Unlinked: flattened into the parent's target if anything below it is linked.
184
+ if (!shape.object || shape.array || stack.includes(shape.object))
185
+ continue;
186
+ const children = resolveModel(
187
+ walk,
188
+ shape.object,
189
+ context,
190
+ deeper,
191
+ sourcePath,
192
+ claimed,
193
+ [...stack, shape.object],
194
+ );
195
+ if (children.length)
196
+ nodes.push({ sourcePath, source, kind: "group", children });
197
+ continue;
198
+ }
199
+
200
+ const node = resolveLink(
201
+ walk,
202
+ source,
203
+ target,
204
+ context,
205
+ deeper,
206
+ sourcePath,
207
+ at,
208
+ stack,
209
+ );
210
+ if (!node) continue;
211
+ const first = claimed.get(target);
212
+ if (first !== undefined) {
213
+ walk.problems.push({
214
+ code: "mapping-target-duplicate",
215
+ target: at,
216
+ format: {
217
+ mapping: walk.mapping.name,
218
+ first,
219
+ second: sourcePath.join("."),
220
+ target: describe(target),
221
+ },
222
+ });
223
+ continue;
224
+ }
225
+ claimed.set(target, sourcePath.join("."));
226
+ nodes.push(node);
227
+ }
228
+ return nodes;
229
+ }
230
+
231
+ function resolveLink(
232
+ walk: Walk,
233
+ source: ModelProperty,
234
+ target: ModelProperty,
235
+ context: Model,
236
+ deeper: PendingPath[],
237
+ sourcePath: string[],
238
+ at: Model | ModelProperty,
239
+ stack: Model[],
240
+ ): MappingNode | undefined {
241
+ const fail = (code: MappingProblem["code"], format: Record<string, string>) =>
242
+ void walk.problems.push({
243
+ code,
244
+ target: at,
245
+ format: {
246
+ mapping: walk.mapping.name,
247
+ source: sourcePath.join("."),
248
+ target: describe(target),
249
+ ...format,
250
+ },
251
+ });
252
+
253
+ if (!isMemberOf(target, context)) {
254
+ fail("mapping-target-out-of-context", {
255
+ context: context.name || "an anonymous model",
256
+ });
257
+ return undefined;
258
+ }
259
+ const from = shapeOf(source.type);
260
+ const to = shapeOf(target.type);
261
+ if (from.array !== to.array) {
262
+ fail("mapping-cardinality-mismatch", {
263
+ sourceShape: from.array ? "a list" : "a single value",
264
+ targetShape: to.array ? "a list" : "a single value",
265
+ });
266
+ return undefined;
267
+ }
268
+ const kind = from.array ? "array" : from.object ? "object" : "leaf";
269
+ if (!from.object) {
270
+ if (to.object) {
271
+ fail("mapping-type-mismatch", {
272
+ reason: "the source is a value and the target is an object",
273
+ });
274
+ return undefined;
275
+ }
276
+ return { sourcePath, source, target, kind, children: [] };
277
+ }
278
+ if (!to.object) {
279
+ fail("mapping-type-mismatch", {
280
+ reason: "the source is an object and the target is a value",
281
+ });
282
+ return undefined;
283
+ }
284
+ const linked = modelMappedTo(walk.program, from.object, walk.mapping);
285
+ if (linked && linked !== to.object) {
286
+ fail("mapping-type-mismatch", {
287
+ reason: `${from.object.name} maps to ${linked.name}, not ${to.object.name}`,
288
+ });
289
+ return undefined;
290
+ }
291
+ const children = stack.includes(from.object)
292
+ ? []
293
+ : resolveModel(
294
+ walk,
295
+ from.object,
296
+ to.object,
297
+ deeper,
298
+ sourcePath,
299
+ new Map(),
300
+ [...stack, from.object],
301
+ );
302
+ return { sourcePath, source, target, kind, children };
303
+ }
304
+
305
+ /** Whether a type is a list, and the object it holds (directly or as each entry), if any. */
306
+ export function shapeOf(type: Type): { array: boolean; object?: Model } {
307
+ if (type.kind !== "Model") return { array: false };
308
+ if (isArrayModelType(type)) {
309
+ const item = type.indexer!.value;
310
+ return { array: true, object: item.kind === "Model" ? item : undefined };
311
+ }
312
+ return { array: false, object: type };
313
+ }
314
+
315
+ /**
316
+ * Whether `target` is one of `context`'s properties. A property `context` has by spread or
317
+ * `is` is a copy, so the copy's source counts too.
318
+ */
319
+ function isMemberOf(target: ModelProperty, context: Model): boolean {
320
+ return modelProperties(context).some((candidate) => {
321
+ for (
322
+ let p: ModelProperty | undefined = candidate;
323
+ p;
324
+ p = p.sourceProperty
325
+ ) {
326
+ if (p === target) return true;
327
+ }
328
+ return false;
329
+ });
330
+ }
331
+
332
+ /** A dotted path's property names. A list is stepped through, so `[*]` is optional. */
333
+ const stepsOf = (path: string) =>
334
+ path.split(".").map((step) => step.replace(/\[\*\]$/, ""));
335
+
336
+ const describe = (prop: ModelProperty) =>
337
+ prop.model?.name ? `${prop.model.name}.${prop.name}` : prop.name;
338
+
339
+ /** Why a path that never landed did not: it names nothing, or nothing above it is linked. */
340
+ function unresolvedReason(model: Model, path: string): string {
341
+ let current: Model | undefined = model;
342
+ const steps = stepsOf(path);
343
+ for (const [index, step] of steps.entries()) {
344
+ if (!current) return `${steps.slice(0, index).join(".")} is not an object`;
345
+ const prop: ModelProperty | undefined = modelProperties(current).find(
346
+ (p) => p.name === step,
347
+ );
348
+ if (!prop) {
349
+ const known = modelProperties(current)
350
+ .map((p) => p.name)
351
+ .slice(0, 8)
352
+ .join(", ");
353
+ return `${current.name || "the model"} has no property "${step}" (has ${known})`;
354
+ }
355
+ current = shapeOf(prop.type).object;
356
+ }
357
+ 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";
358
+ }