simpler-forms 0.1.0-alpha.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 (76) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +111 -0
  3. package/dist/src/decorators/index.d.ts +4 -0
  4. package/dist/src/decorators/index.js +7 -0
  5. package/dist/src/decorators/index.js.map +1 -0
  6. package/dist/src/decorators/meta.d.ts +22 -0
  7. package/dist/src/decorators/meta.js +35 -0
  8. package/dist/src/decorators/meta.js.map +1 -0
  9. package/dist/src/decorators/shared.d.ts +49 -0
  10. package/dist/src/decorators/shared.js +149 -0
  11. package/dist/src/decorators/shared.js.map +1 -0
  12. package/dist/src/decorators/ui.d.ts +41 -0
  13. package/dist/src/decorators/ui.js +106 -0
  14. package/dist/src/decorators/ui.js.map +1 -0
  15. package/dist/src/decorators/validation.d.ts +25 -0
  16. package/dist/src/decorators/validation.js +91 -0
  17. package/dist/src/decorators/validation.js.map +1 -0
  18. package/dist/src/emitter.d.ts +15 -0
  19. package/dist/src/emitter.js +224 -0
  20. package/dist/src/emitter.js.map +1 -0
  21. package/dist/src/emitters/block-index.d.ts +4 -0
  22. package/dist/src/emitters/block-index.js +19 -0
  23. package/dist/src/emitters/block-index.js.map +1 -0
  24. package/dist/src/emitters/block-ui.d.ts +18 -0
  25. package/dist/src/emitters/block-ui.js +203 -0
  26. package/dist/src/emitters/block-ui.js.map +1 -0
  27. package/dist/src/emitters/field-occurrences.d.ts +18 -0
  28. package/dist/src/emitters/field-occurrences.js +237 -0
  29. package/dist/src/emitters/field-occurrences.js.map +1 -0
  30. package/dist/src/emitters/overlay.d.ts +16 -0
  31. package/dist/src/emitters/overlay.js +449 -0
  32. package/dist/src/emitters/overlay.js.map +1 -0
  33. package/dist/src/emitters/override-condition.d.ts +12 -0
  34. package/dist/src/emitters/override-condition.js +25 -0
  35. package/dist/src/emitters/override-condition.js.map +1 -0
  36. package/dist/src/index.d.ts +51 -0
  37. package/dist/src/index.js +55 -0
  38. package/dist/src/index.js.map +1 -0
  39. package/dist/src/lib.d.ts +361 -0
  40. package/dist/src/lib.js +194 -0
  41. package/dist/src/lib.js.map +1 -0
  42. package/dist/src/linter.d.ts +1 -0
  43. package/dist/src/linter.js +337 -0
  44. package/dist/src/linter.js.map +1 -0
  45. package/dist/src/model.d.ts +134 -0
  46. package/dist/src/model.js +203 -0
  47. package/dist/src/model.js.map +1 -0
  48. package/dist/src/public.d.ts +33 -0
  49. package/dist/src/public.js +40 -0
  50. package/dist/src/public.js.map +1 -0
  51. package/dist/src/validate.d.ts +15 -0
  52. package/dist/src/validate.js +737 -0
  53. package/dist/src/validate.js.map +1 -0
  54. package/lib/main.tsp +4 -0
  55. package/lib/meta.tsp +33 -0
  56. package/lib/types.tsp +134 -0
  57. package/lib/ui.tsp +78 -0
  58. package/lib/validation.tsp +134 -0
  59. package/package.json +54 -0
  60. package/src/decorators/index.ts +6 -0
  61. package/src/decorators/meta.ts +52 -0
  62. package/src/decorators/shared.ts +187 -0
  63. package/src/decorators/ui.ts +192 -0
  64. package/src/decorators/validation.ts +220 -0
  65. package/src/emitter.ts +272 -0
  66. package/src/emitters/block-index.ts +24 -0
  67. package/src/emitters/block-ui.ts +254 -0
  68. package/src/emitters/field-occurrences.ts +345 -0
  69. package/src/emitters/overlay.ts +596 -0
  70. package/src/emitters/override-condition.ts +37 -0
  71. package/src/index.ts +59 -0
  72. package/src/lib.ts +195 -0
  73. package/src/linter.ts +380 -0
  74. package/src/model.ts +400 -0
  75. package/src/public.ts +94 -0
  76. package/src/validate.ts +887 -0
@@ -0,0 +1,345 @@
1
+ import type {
2
+ Model,
3
+ ModelProperty,
4
+ Program,
5
+ Scalar,
6
+ Type,
7
+ } from "@typespec/compiler";
8
+ import {
9
+ type Block,
10
+ type ResponseRole,
11
+ blockAncestry,
12
+ modelProperties,
13
+ propResponseRole,
14
+ readBlock,
15
+ typeResponseRole,
16
+ } from "../model.js";
17
+
18
+ /** Portable attribution for one field or group in a form's canonical response tree. */
19
+ export interface FieldOccurrence {
20
+ path: string;
21
+ leaf: boolean;
22
+ blockIds: string[];
23
+ responseRole?: ResponseRole;
24
+ }
25
+
26
+ interface TemplateContext {
27
+ model: Model;
28
+ blockIds: string[];
29
+ responseRole?: ResponseRole;
30
+ }
31
+
32
+ /**
33
+ * Emit the field graph from the TypeSpec type graph while that graph is still available.
34
+ *
35
+ * JSON Schema faithfully preserves validation and composition but deliberately does not retain
36
+ * authoring facts such as a property arriving through `...Question`, or a form-local subtype
37
+ * overriding one member of a canonical question. Capturing those facts in the portable index keeps
38
+ * analyzers and other consumers off the TypeSpec AST without inventing JSON Schema keywords.
39
+ */
40
+ export function emitFieldOccurrences(
41
+ program: Program,
42
+ form: Block,
43
+ ): FieldOccurrence[] {
44
+ if (form.kind !== "form" || form.model.kind !== "Model") return [];
45
+
46
+ const found = new Map<string, FieldOccurrence>();
47
+
48
+ const record = (occurrence: FieldOccurrence): void => {
49
+ const existing = found.get(occurrence.path);
50
+ if (!existing) {
51
+ found.set(occurrence.path, occurrence);
52
+ return;
53
+ }
54
+ existing.leaf = existing.leaf && occurrence.leaf;
55
+ existing.blockIds = sortedUnique([
56
+ ...existing.blockIds,
57
+ ...occurrence.blockIds,
58
+ ]);
59
+ existing.responseRole ??= occurrence.responseRole;
60
+ };
61
+
62
+ const walkModel = (
63
+ model: Model,
64
+ parentPath: string,
65
+ templates: TemplateContext[],
66
+ inheritedRole: ResponseRole | undefined,
67
+ stack: Set<Model>,
68
+ ): void => {
69
+ if (stack.has(model)) return;
70
+ const nextStack = new Set(stack).add(model);
71
+
72
+ for (const property of modelProperties(model)) {
73
+ const path = `${parentPath}/${escapePointer(property.name)}`;
74
+ const matches = matchingTemplates(property.name, templates);
75
+ const sources = sourceProperties(property);
76
+ const directIds = blockIdsForType(program, property.type);
77
+ const sourceIds = sources.flatMap((source) =>
78
+ blockIdsForPropertyOwner(program, source),
79
+ );
80
+ const blockIds = sortedUnique([
81
+ ...directIds,
82
+ ...sourceIds,
83
+ ...matches.flatMap((match) => match.context.blockIds),
84
+ ...matches.flatMap((match) => [
85
+ ...blockIdsForPropertyOwner(program, match.property),
86
+ ...sourceProperties(match.property).flatMap((source) =>
87
+ blockIdsForPropertyOwner(program, source),
88
+ ),
89
+ ]),
90
+ ]);
91
+ const responseRole = firstDefined(
92
+ propResponseRole(program, property),
93
+ roleForType(program, property.type),
94
+ ...sources.map((source) => propResponseRole(program, source)),
95
+ ...matches.map((match) => propResponseRole(program, match.property)),
96
+ ...sources.map((source) => roleForPropertyOwner(program, source)),
97
+ ...matches.map((match) => match.context.responseRole),
98
+ inheritedRole,
99
+ );
100
+
101
+ const arrayItem = arrayItemType(property.type);
102
+ if (arrayItem) {
103
+ record({
104
+ path,
105
+ leaf: false,
106
+ blockIds,
107
+ ...(responseRole ? { responseRole } : {}),
108
+ });
109
+ }
110
+ const valuePath = arrayItem ? `${path}/[]` : path;
111
+ const valueType = arrayItem ?? property.type;
112
+ const childModel = objectModel(valueType);
113
+
114
+ if (!childModel) {
115
+ record({
116
+ path: valuePath,
117
+ leaf: true,
118
+ blockIds: sortedUnique([
119
+ ...blockIds,
120
+ ...blockIdsForType(program, valueType),
121
+ ]),
122
+ ...(responseRole ? { responseRole } : {}),
123
+ });
124
+ continue;
125
+ }
126
+
127
+ const childTemplates = templateContextsForChild(
128
+ program,
129
+ property,
130
+ valueType,
131
+ matches,
132
+ sources,
133
+ );
134
+ record({
135
+ path: valuePath,
136
+ leaf: false,
137
+ blockIds: sortedUnique([
138
+ ...blockIds,
139
+ ...blockIdsForType(program, valueType),
140
+ ]),
141
+ ...(responseRole ? { responseRole } : {}),
142
+ });
143
+ walkModel(childModel, valuePath, childTemplates, responseRole, nextStack);
144
+ }
145
+ };
146
+
147
+ const inheritedTemplates: TemplateContext[] = [];
148
+ for (let base = form.model.baseModel; base; base = base.baseModel) {
149
+ const block = readBlock(program, base);
150
+ inheritedTemplates.push({
151
+ model: base,
152
+ blockIds: block?.kind === "question" ? blockAncestry(program, base) : [],
153
+ responseRole: block?.responseRole,
154
+ });
155
+ }
156
+ walkModel(form.model, "", inheritedTemplates, form.responseRole, new Set());
157
+ return [...found.values()].sort((a, b) => a.path.localeCompare(b.path));
158
+ }
159
+
160
+ function matchingTemplates(name: string, contexts: TemplateContext[]) {
161
+ return contexts.flatMap((context) => {
162
+ const property = modelProperties(context.model).find(
163
+ (candidate) => candidate.name === name,
164
+ );
165
+ return property ? [{ context, property }] : [];
166
+ });
167
+ }
168
+
169
+ function templateContextsForChild(
170
+ program: Program,
171
+ property: ModelProperty,
172
+ valueType: Type,
173
+ matches: ReturnType<typeof matchingTemplates>,
174
+ sources: ModelProperty[],
175
+ ): TemplateContext[] {
176
+ const contexts: TemplateContext[] = [];
177
+
178
+ for (const { context, property: templateProperty } of matches) {
179
+ const templateType =
180
+ arrayItemType(templateProperty.type) ?? templateProperty.type;
181
+ const templateModel = objectModel(templateType);
182
+ if (!templateModel) continue;
183
+ contexts.push({
184
+ model: templateModel,
185
+ blockIds: sortedUnique([
186
+ ...context.blockIds,
187
+ ...blockIdsForType(program, templateProperty.type),
188
+ ]),
189
+ responseRole: firstDefined(
190
+ propResponseRole(program, templateProperty),
191
+ roleForType(program, templateProperty.type),
192
+ context.responseRole,
193
+ ),
194
+ });
195
+ }
196
+
197
+ for (const source of sources) {
198
+ const sourceType = arrayItemType(source.type) ?? source.type;
199
+ const sourceModel = objectModel(sourceType);
200
+ if (!sourceModel) continue;
201
+ contexts.push({
202
+ model: sourceModel,
203
+ blockIds: sortedUnique([
204
+ ...blockIdsForPropertyOwner(program, source),
205
+ ...blockIdsForType(program, source.type),
206
+ ]),
207
+ responseRole: firstDefined(
208
+ propResponseRole(program, source),
209
+ roleForType(program, source.type),
210
+ ),
211
+ });
212
+ }
213
+
214
+ const currentModel = objectModel(valueType);
215
+ if (currentModel) {
216
+ for (
217
+ let candidate: Model | undefined = currentModel;
218
+ candidate;
219
+ candidate = candidate.baseModel
220
+ ) {
221
+ const block = readBlock(program, candidate);
222
+ if (!block) continue;
223
+ contexts.push({
224
+ model: candidate,
225
+ blockIds: blockAncestry(program, candidate),
226
+ responseRole: block.responseRole,
227
+ });
228
+ }
229
+ }
230
+
231
+ return deduplicateContexts(contexts);
232
+ }
233
+
234
+ function sourceProperties(property: ModelProperty): ModelProperty[] {
235
+ const found: ModelProperty[] = [];
236
+ const seen = new Set<ModelProperty>();
237
+ const addChain = (candidate: ModelProperty | undefined): void => {
238
+ for (
239
+ let current = candidate;
240
+ current && !seen.has(current);
241
+ current = current.sourceProperty
242
+ ) {
243
+ found.push(current);
244
+ seen.add(current);
245
+ }
246
+ };
247
+ addChain(property.sourceProperty);
248
+ // A derived model may deliberately narrow or decorate an inherited member by
249
+ // redeclaring it. TypeSpec does not retain that base member in sourceProperty, so
250
+ // recover the same-named declaration through the base chain to preserve its semantic
251
+ // question lineage in portable occurrence analysis.
252
+ for (let base = property.model?.baseModel; base; base = base.baseModel) {
253
+ addChain(base.properties.get(property.name));
254
+ }
255
+ return found;
256
+ }
257
+
258
+ function blockIdsForPropertyOwner(
259
+ program: Program,
260
+ property: ModelProperty,
261
+ ): string[] {
262
+ const owner = property.model;
263
+ return owner ? blockAncestry(program, owner) : [];
264
+ }
265
+
266
+ function roleForPropertyOwner(
267
+ program: Program,
268
+ property: ModelProperty,
269
+ ): ResponseRole | undefined {
270
+ const owner = property.model;
271
+ if (!owner) return undefined;
272
+ for (
273
+ let current: Model | undefined = owner;
274
+ current;
275
+ current = current.baseModel
276
+ ) {
277
+ const block = readBlock(program, current);
278
+ if (block?.responseRole) return block.responseRole;
279
+ }
280
+ return undefined;
281
+ }
282
+
283
+ function blockIdsForType(program: Program, type: Type): string[] {
284
+ const ids: string[] = [];
285
+ const visit = (candidate: Type): void => {
286
+ if (candidate.kind === "Model" || candidate.kind === "Scalar") {
287
+ ids.push(...blockAncestry(program, candidate));
288
+ }
289
+ const item = arrayItemType(candidate);
290
+ if (item && item !== candidate) visit(item);
291
+ };
292
+ visit(type);
293
+ return sortedUnique(ids);
294
+ }
295
+
296
+ function roleForType(program: Program, type: Type): ResponseRole | undefined {
297
+ const visit = (candidate: Type): ResponseRole | undefined => {
298
+ if (candidate.kind === "Model" || candidate.kind === "Scalar") {
299
+ for (let current: Model | Scalar | undefined = candidate; current;) {
300
+ const role = typeResponseRole(program, current);
301
+ if (role) return role;
302
+ current =
303
+ current.kind === "Model" ? current.baseModel : current.baseScalar;
304
+ }
305
+ }
306
+ const item = arrayItemType(candidate);
307
+ return item && item !== candidate ? visit(item) : undefined;
308
+ };
309
+ return visit(type);
310
+ }
311
+
312
+ function arrayItemType(type: Type): Type | undefined {
313
+ return type.kind === "Model" && type.indexer ? type.indexer.value : undefined;
314
+ }
315
+
316
+ function objectModel(type: Type): Model | undefined {
317
+ return type.kind === "Model" && !type.indexer ? type : undefined;
318
+ }
319
+
320
+ function deduplicateContexts(contexts: TemplateContext[]): TemplateContext[] {
321
+ const found: TemplateContext[] = [];
322
+ for (const context of contexts) {
323
+ const duplicate = found.some(
324
+ (candidate) =>
325
+ candidate.model === context.model &&
326
+ candidate.responseRole === context.responseRole &&
327
+ candidate.blockIds.length === context.blockIds.length &&
328
+ candidate.blockIds.every((id, index) => id === context.blockIds[index]),
329
+ );
330
+ if (!duplicate) found.push(context);
331
+ }
332
+ return found;
333
+ }
334
+
335
+ function firstDefined<T>(...values: Array<T | undefined>): T | undefined {
336
+ return values.find((value): value is T => value !== undefined);
337
+ }
338
+
339
+ function sortedUnique(values: string[]): string[] {
340
+ return [...new Set(values)].sort();
341
+ }
342
+
343
+ function escapePointer(value: string): string {
344
+ return value.replaceAll("~", "~0").replaceAll("/", "~1");
345
+ }