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.
- package/LICENSE.md +21 -0
- package/README.md +111 -0
- package/dist/src/decorators/index.d.ts +4 -0
- package/dist/src/decorators/index.js +7 -0
- package/dist/src/decorators/index.js.map +1 -0
- package/dist/src/decorators/meta.d.ts +22 -0
- package/dist/src/decorators/meta.js +35 -0
- package/dist/src/decorators/meta.js.map +1 -0
- package/dist/src/decorators/shared.d.ts +49 -0
- package/dist/src/decorators/shared.js +149 -0
- package/dist/src/decorators/shared.js.map +1 -0
- package/dist/src/decorators/ui.d.ts +41 -0
- package/dist/src/decorators/ui.js +106 -0
- package/dist/src/decorators/ui.js.map +1 -0
- package/dist/src/decorators/validation.d.ts +25 -0
- package/dist/src/decorators/validation.js +91 -0
- package/dist/src/decorators/validation.js.map +1 -0
- package/dist/src/emitter.d.ts +15 -0
- package/dist/src/emitter.js +224 -0
- package/dist/src/emitter.js.map +1 -0
- package/dist/src/emitters/block-index.d.ts +4 -0
- package/dist/src/emitters/block-index.js +19 -0
- package/dist/src/emitters/block-index.js.map +1 -0
- package/dist/src/emitters/block-ui.d.ts +18 -0
- package/dist/src/emitters/block-ui.js +203 -0
- package/dist/src/emitters/block-ui.js.map +1 -0
- package/dist/src/emitters/field-occurrences.d.ts +18 -0
- package/dist/src/emitters/field-occurrences.js +237 -0
- package/dist/src/emitters/field-occurrences.js.map +1 -0
- package/dist/src/emitters/overlay.d.ts +16 -0
- package/dist/src/emitters/overlay.js +449 -0
- package/dist/src/emitters/overlay.js.map +1 -0
- package/dist/src/emitters/override-condition.d.ts +12 -0
- package/dist/src/emitters/override-condition.js +25 -0
- package/dist/src/emitters/override-condition.js.map +1 -0
- package/dist/src/index.d.ts +51 -0
- package/dist/src/index.js +55 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/lib.d.ts +361 -0
- package/dist/src/lib.js +194 -0
- package/dist/src/lib.js.map +1 -0
- package/dist/src/linter.d.ts +1 -0
- package/dist/src/linter.js +337 -0
- package/dist/src/linter.js.map +1 -0
- package/dist/src/model.d.ts +134 -0
- package/dist/src/model.js +203 -0
- package/dist/src/model.js.map +1 -0
- package/dist/src/public.d.ts +33 -0
- package/dist/src/public.js +40 -0
- package/dist/src/public.js.map +1 -0
- package/dist/src/validate.d.ts +15 -0
- package/dist/src/validate.js +737 -0
- package/dist/src/validate.js.map +1 -0
- package/lib/main.tsp +4 -0
- package/lib/meta.tsp +33 -0
- package/lib/types.tsp +134 -0
- package/lib/ui.tsp +78 -0
- package/lib/validation.tsp +134 -0
- package/package.json +54 -0
- package/src/decorators/index.ts +6 -0
- package/src/decorators/meta.ts +52 -0
- package/src/decorators/shared.ts +187 -0
- package/src/decorators/ui.ts +192 -0
- package/src/decorators/validation.ts +220 -0
- package/src/emitter.ts +272 -0
- package/src/emitters/block-index.ts +24 -0
- package/src/emitters/block-ui.ts +254 -0
- package/src/emitters/field-occurrences.ts +345 -0
- package/src/emitters/overlay.ts +596 -0
- package/src/emitters/override-condition.ts +37 -0
- package/src/index.ts +59 -0
- package/src/lib.ts +195 -0
- package/src/linter.ts +380 -0
- package/src/model.ts +400 -0
- package/src/public.ts +94 -0
- package/src/validate.ts +887 -0
package/src/validate.ts
ADDED
|
@@ -0,0 +1,887 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Enum,
|
|
3
|
+
Model,
|
|
4
|
+
ModelProperty,
|
|
5
|
+
Namespace,
|
|
6
|
+
Program,
|
|
7
|
+
Scalar,
|
|
8
|
+
Type,
|
|
9
|
+
} from "@typespec/compiler";
|
|
10
|
+
import { reportDiagnostic } from "./lib.js";
|
|
11
|
+
import {
|
|
12
|
+
Block,
|
|
13
|
+
Condition,
|
|
14
|
+
allBlocks,
|
|
15
|
+
cardinalityAtLeastOnePathWhenPresent,
|
|
16
|
+
cardinalityPositiveDecimalStringWhenPathPresent,
|
|
17
|
+
cardinalityRequiredPaths,
|
|
18
|
+
cardinalityRequiredPathWhenPositiveDecimalString,
|
|
19
|
+
cardinalityRequiredWhen,
|
|
20
|
+
childBlock,
|
|
21
|
+
conditionSourceModel,
|
|
22
|
+
orderedProps,
|
|
23
|
+
propCalculationMaterialization,
|
|
24
|
+
propComputed,
|
|
25
|
+
readBlock,
|
|
26
|
+
propComputedFrom,
|
|
27
|
+
propEncodedCheckboxGroup,
|
|
28
|
+
modelProperties,
|
|
29
|
+
propEnabledWhen,
|
|
30
|
+
propNotBefore,
|
|
31
|
+
propOmit,
|
|
32
|
+
propReadOnlyWhen,
|
|
33
|
+
propRequiredWhen,
|
|
34
|
+
propSection,
|
|
35
|
+
propVisibleWhen,
|
|
36
|
+
propValidationConstraintsWhen,
|
|
37
|
+
scalarType,
|
|
38
|
+
} from "./model.js";
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Whole-program checks whose failure means the emitted artifacts would be wrong.
|
|
42
|
+
*
|
|
43
|
+
* These are errors, so they run here rather than in the linter, which may only warn. Each
|
|
44
|
+
* one exists because the defect it catches is currently invisible: a question named after a
|
|
45
|
+
* form, two blocks colliding on one output path, a condition comparing against a value the
|
|
46
|
+
* source enum does not have, a calculation that depends on itself, a field that is required
|
|
47
|
+
* but can be hidden, and a field in no section.
|
|
48
|
+
*/
|
|
49
|
+
export function $onValidate(program: Program): void {
|
|
50
|
+
const blocks = allBlocks(program);
|
|
51
|
+
|
|
52
|
+
checkQuestionIds(program, blocks);
|
|
53
|
+
checkDuplicateIds(program, blocks);
|
|
54
|
+
|
|
55
|
+
for (const block of blocks) {
|
|
56
|
+
if (block.model.kind !== "Model") continue;
|
|
57
|
+
checkSections(program, block);
|
|
58
|
+
checkOverridePaths(program, block);
|
|
59
|
+
for (const prop of block.model.properties.values()) {
|
|
60
|
+
checkConditions(program, prop);
|
|
61
|
+
checkRequiredButHidden(program, prop);
|
|
62
|
+
checkEncodedCheckboxGroup(program, prop);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
checkDateOrders(program);
|
|
67
|
+
checkCardinalityPaths(program);
|
|
68
|
+
checkCalculationCycles(program, blocks);
|
|
69
|
+
checkCalculationMaterialization(program, blocks);
|
|
70
|
+
checkComputedPaths(program, blocks);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function checkCardinalityPaths(program: Program): void {
|
|
74
|
+
const visit = (namespace: Namespace): void => {
|
|
75
|
+
for (const model of namespace.models.values()) {
|
|
76
|
+
const modelPaths = cardinalityRequiredPaths(program, model);
|
|
77
|
+
const modelConditions = cardinalityRequiredWhen(program, model);
|
|
78
|
+
const modelAlternatives = cardinalityAtLeastOnePathWhenPresent(
|
|
79
|
+
program,
|
|
80
|
+
model,
|
|
81
|
+
);
|
|
82
|
+
const modelPositiveConditions = [
|
|
83
|
+
...cardinalityRequiredPathWhenPositiveDecimalString(program, model),
|
|
84
|
+
...cardinalityPositiveDecimalStringWhenPathPresent(program, model),
|
|
85
|
+
];
|
|
86
|
+
if (
|
|
87
|
+
(modelPaths.length ||
|
|
88
|
+
modelConditions.length ||
|
|
89
|
+
modelAlternatives.length ||
|
|
90
|
+
modelPositiveConditions.length) &&
|
|
91
|
+
!readBlock(program, model)
|
|
92
|
+
) {
|
|
93
|
+
reportDiagnostic(program, {
|
|
94
|
+
code: "cardinality-model-not-emitted",
|
|
95
|
+
target: model,
|
|
96
|
+
format: { model: model.name || "an anonymous model" },
|
|
97
|
+
});
|
|
98
|
+
} else {
|
|
99
|
+
checkCardinalityTarget(program, model, model);
|
|
100
|
+
}
|
|
101
|
+
for (const property of model.properties.values()) {
|
|
102
|
+
const root = objectBehind(program, property);
|
|
103
|
+
if (!root) {
|
|
104
|
+
for (const path of [
|
|
105
|
+
...cardinalityRequiredPaths(program, property),
|
|
106
|
+
...cardinalityRequiredWhen(program, property).flatMap((entry) => [
|
|
107
|
+
entry.targetPath,
|
|
108
|
+
entry.sourcePath,
|
|
109
|
+
]),
|
|
110
|
+
...cardinalityAtLeastOnePathWhenPresent(program, property).flatMap(
|
|
111
|
+
(entry) => [entry.sourcePath, ...entry.targetPaths],
|
|
112
|
+
),
|
|
113
|
+
...cardinalityRequiredPathWhenPositiveDecimalString(
|
|
114
|
+
program,
|
|
115
|
+
property,
|
|
116
|
+
).flatMap((entry) => [entry.targetPath, entry.sourcePath]),
|
|
117
|
+
...cardinalityPositiveDecimalStringWhenPathPresent(
|
|
118
|
+
program,
|
|
119
|
+
property,
|
|
120
|
+
).flatMap((entry) => [entry.targetPath, entry.sourcePath]),
|
|
121
|
+
]) {
|
|
122
|
+
reportDiagnostic(program, {
|
|
123
|
+
code: "cardinality-path-unresolved",
|
|
124
|
+
target: property,
|
|
125
|
+
format: {
|
|
126
|
+
path,
|
|
127
|
+
model: property.name,
|
|
128
|
+
reason: "the decorated value is not an object",
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
checkCardinalityTarget(program, property, root);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
for (const child of namespace.namespaces.values()) visit(child);
|
|
138
|
+
};
|
|
139
|
+
visit(program.getGlobalNamespaceType());
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function checkCardinalityTarget(
|
|
143
|
+
program: Program,
|
|
144
|
+
target: Model | ModelProperty,
|
|
145
|
+
root: Model,
|
|
146
|
+
): void {
|
|
147
|
+
const modelName =
|
|
148
|
+
root.name || (target.kind === "ModelProperty" ? target.name : "the model");
|
|
149
|
+
for (const path of cardinalityRequiredPaths(program, target)) {
|
|
150
|
+
reportCardinalityPath(program, target, root, modelName, path);
|
|
151
|
+
}
|
|
152
|
+
for (const entry of cardinalityRequiredWhen(program, target)) {
|
|
153
|
+
reportCardinalityPath(program, target, root, modelName, entry.targetPath);
|
|
154
|
+
const source = resolvedProperty(root, entry.sourcePath.split("."));
|
|
155
|
+
if (!source) {
|
|
156
|
+
reportCardinalityPath(program, target, root, modelName, entry.sourcePath);
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
const enumeration = enumOf(source.type);
|
|
160
|
+
if (!enumeration) continue;
|
|
161
|
+
const members = [...enumeration.members.values()].map(
|
|
162
|
+
(member) => member.value ?? member.name,
|
|
163
|
+
);
|
|
164
|
+
if (!members.includes(entry.value as string | number)) {
|
|
165
|
+
reportDiagnostic(program, {
|
|
166
|
+
code: "condition-value-not-in-enum",
|
|
167
|
+
target,
|
|
168
|
+
format: {
|
|
169
|
+
value: String(entry.value),
|
|
170
|
+
enumName: enumeration.name,
|
|
171
|
+
members:
|
|
172
|
+
members.slice(0, 6).join(", ") +
|
|
173
|
+
(members.length > 6 ? ", ..." : ""),
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
for (const entry of cardinalityAtLeastOnePathWhenPresent(program, target)) {
|
|
179
|
+
reportCardinalityPath(program, target, root, modelName, entry.sourcePath);
|
|
180
|
+
for (const path of entry.targetPaths) {
|
|
181
|
+
reportCardinalityPath(program, target, root, modelName, path);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
for (const entry of cardinalityRequiredPathWhenPositiveDecimalString(
|
|
185
|
+
program,
|
|
186
|
+
target,
|
|
187
|
+
)) {
|
|
188
|
+
reportCardinalityPath(program, target, root, modelName, entry.targetPath);
|
|
189
|
+
reportCardinalityPath(program, target, root, modelName, entry.sourcePath);
|
|
190
|
+
reportPositiveDecimalStringTarget(
|
|
191
|
+
program,
|
|
192
|
+
target,
|
|
193
|
+
root,
|
|
194
|
+
modelName,
|
|
195
|
+
entry.sourcePath,
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
for (const entry of cardinalityPositiveDecimalStringWhenPathPresent(
|
|
199
|
+
program,
|
|
200
|
+
target,
|
|
201
|
+
)) {
|
|
202
|
+
reportCardinalityPath(program, target, root, modelName, entry.targetPath);
|
|
203
|
+
reportCardinalityPath(program, target, root, modelName, entry.sourcePath);
|
|
204
|
+
reportPositiveDecimalStringTarget(
|
|
205
|
+
program,
|
|
206
|
+
target,
|
|
207
|
+
root,
|
|
208
|
+
modelName,
|
|
209
|
+
entry.targetPath,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function reportPositiveDecimalStringTarget(
|
|
215
|
+
program: Program,
|
|
216
|
+
target: Model | ModelProperty,
|
|
217
|
+
root: Model,
|
|
218
|
+
modelName: string,
|
|
219
|
+
path: string,
|
|
220
|
+
): void {
|
|
221
|
+
const property = resolvedProperty(root, path.split("."));
|
|
222
|
+
if (
|
|
223
|
+
!property ||
|
|
224
|
+
(property.type.kind === "Scalar" && scalarType(property.type) === "string")
|
|
225
|
+
) {
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
reportDiagnostic(program, {
|
|
229
|
+
code: "positive-decimal-string-target-invalid",
|
|
230
|
+
target,
|
|
231
|
+
format: { path, model: modelName, type: property.type.kind },
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
function reportCardinalityPath(
|
|
236
|
+
program: Program,
|
|
237
|
+
target: Model | ModelProperty,
|
|
238
|
+
root: Model,
|
|
239
|
+
modelName: string,
|
|
240
|
+
path: string,
|
|
241
|
+
): void {
|
|
242
|
+
const reason = resolvePath(program, root, path.split("."));
|
|
243
|
+
if (!reason) return;
|
|
244
|
+
reportDiagnostic(program, {
|
|
245
|
+
code: "cardinality-path-unresolved",
|
|
246
|
+
target,
|
|
247
|
+
format: { path, model: modelName, reason },
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function resolvedProperty(
|
|
252
|
+
model: Model,
|
|
253
|
+
steps: string[],
|
|
254
|
+
): ModelProperty | undefined {
|
|
255
|
+
let current: Model = model;
|
|
256
|
+
let property: ModelProperty | undefined;
|
|
257
|
+
for (const [index, step] of steps.entries()) {
|
|
258
|
+
property = allProperties(current).get(step);
|
|
259
|
+
if (!property) return undefined;
|
|
260
|
+
if (index === steps.length - 1) return property;
|
|
261
|
+
const next = property.type;
|
|
262
|
+
if (next.kind !== "Model" || next.indexer) return undefined;
|
|
263
|
+
current = next;
|
|
264
|
+
}
|
|
265
|
+
return property;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function checkDateOrders(program: Program): void {
|
|
269
|
+
const visit = (namespace: Namespace): void => {
|
|
270
|
+
for (const model of namespace.models.values()) {
|
|
271
|
+
for (const prop of model.properties.values()) {
|
|
272
|
+
const source = propNotBefore(program, prop);
|
|
273
|
+
if (!source || (source !== prop && source.model === prop.model))
|
|
274
|
+
continue;
|
|
275
|
+
reportDiagnostic(program, {
|
|
276
|
+
code: "date-order-source-invalid",
|
|
277
|
+
target: prop,
|
|
278
|
+
format: { target: prop.name, source: source.name },
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
for (const child of namespace.namespaces.values()) visit(child);
|
|
283
|
+
};
|
|
284
|
+
visit(program.getGlobalNamespaceType());
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
function checkEncodedCheckboxGroup(
|
|
288
|
+
program: Program,
|
|
289
|
+
prop: ModelProperty,
|
|
290
|
+
): void {
|
|
291
|
+
const contract = propEncodedCheckboxGroup(program, prop);
|
|
292
|
+
if (!contract) return;
|
|
293
|
+
|
|
294
|
+
const fail = (reason: string): void =>
|
|
295
|
+
reportDiagnostic(program, {
|
|
296
|
+
code: "encoded-checkbox-contract-invalid",
|
|
297
|
+
target: prop,
|
|
298
|
+
format: { name: prop.name, reason },
|
|
299
|
+
});
|
|
300
|
+
const enumeration = enumOf(prop.type);
|
|
301
|
+
if (!enumeration) {
|
|
302
|
+
fail("the field type is not an enum");
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
if (Object.keys(contract).sort().join(",") !== "choices,combinations") {
|
|
306
|
+
fail("exactly choices and combinations must be declared");
|
|
307
|
+
return;
|
|
308
|
+
}
|
|
309
|
+
const choices = contract.choices;
|
|
310
|
+
const combinations = contract.combinations;
|
|
311
|
+
if (
|
|
312
|
+
!Array.isArray(choices) ||
|
|
313
|
+
!Array.isArray(combinations) ||
|
|
314
|
+
!choices.length
|
|
315
|
+
) {
|
|
316
|
+
fail("choices and combinations must be non-empty arrays");
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const typedChoices = choices as Record<string, unknown>[];
|
|
320
|
+
if (
|
|
321
|
+
typedChoices.some(
|
|
322
|
+
(choice) =>
|
|
323
|
+
!choice ||
|
|
324
|
+
typeof choice !== "object" ||
|
|
325
|
+
Array.isArray(choice) ||
|
|
326
|
+
Object.keys(choice).sort().join(",") !== "code,label" ||
|
|
327
|
+
typeof choice.code !== "string" ||
|
|
328
|
+
!choice.code ||
|
|
329
|
+
typeof choice.label !== "string" ||
|
|
330
|
+
!choice.label,
|
|
331
|
+
)
|
|
332
|
+
) {
|
|
333
|
+
fail("each choice must contain one non-empty code and label");
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
const codes = typedChoices.map((choice) => choice.code as string);
|
|
337
|
+
const labels = typedChoices.map((choice) => choice.label as string);
|
|
338
|
+
if (
|
|
339
|
+
new Set(codes).size !== codes.length ||
|
|
340
|
+
new Set(labels).size !== labels.length
|
|
341
|
+
) {
|
|
342
|
+
fail("choice codes and labels must be unique");
|
|
343
|
+
return;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const values = new Set<string>();
|
|
347
|
+
for (const raw of combinations as Record<string, unknown>[]) {
|
|
348
|
+
if (
|
|
349
|
+
!raw ||
|
|
350
|
+
typeof raw !== "object" ||
|
|
351
|
+
Array.isArray(raw) ||
|
|
352
|
+
Object.keys(raw).sort().join(",") !== "members,value" ||
|
|
353
|
+
typeof raw.value !== "string" ||
|
|
354
|
+
!raw.value ||
|
|
355
|
+
!Array.isArray(raw.members) ||
|
|
356
|
+
!raw.members.length ||
|
|
357
|
+
raw.members.some(
|
|
358
|
+
(member) => typeof member !== "string" || !codes.includes(member),
|
|
359
|
+
) ||
|
|
360
|
+
new Set(raw.members).size !== raw.members.length ||
|
|
361
|
+
values.has(raw.value)
|
|
362
|
+
) {
|
|
363
|
+
fail(
|
|
364
|
+
"every combination must have a unique value and unique known members",
|
|
365
|
+
);
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
values.add(raw.value);
|
|
369
|
+
}
|
|
370
|
+
const enumValues = [...enumeration.members.values()].map((member) =>
|
|
371
|
+
String(member.value ?? member.name),
|
|
372
|
+
);
|
|
373
|
+
if (
|
|
374
|
+
enumValues.length !== values.size ||
|
|
375
|
+
enumValues.some((value) => !values.has(value))
|
|
376
|
+
) {
|
|
377
|
+
fail("combination values must exactly match the field enum");
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// ---------------------------------------------------------------------------
|
|
382
|
+
// cross-boundary calculation paths
|
|
383
|
+
|
|
384
|
+
function arrayItem(type: Type): Type | undefined {
|
|
385
|
+
return type.kind === "Model" && type.indexer ? type.indexer.value : undefined;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function resolves(model: Model, path: string): boolean {
|
|
389
|
+
let current: Type = model;
|
|
390
|
+
for (const raw of path.split(".")) {
|
|
391
|
+
const repeated = raw.endsWith("[*]");
|
|
392
|
+
const name = repeated ? raw.slice(0, -3) : raw;
|
|
393
|
+
if (current.kind !== "Model") return false;
|
|
394
|
+
const property = current.properties.get(name);
|
|
395
|
+
if (!property) return false;
|
|
396
|
+
current = property.type;
|
|
397
|
+
if (repeated) {
|
|
398
|
+
const item = arrayItem(current);
|
|
399
|
+
if (!item) return false;
|
|
400
|
+
current = item;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
return true;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
function contains(
|
|
407
|
+
model: Model,
|
|
408
|
+
target: Model,
|
|
409
|
+
seen = new Set<Model>(),
|
|
410
|
+
): boolean {
|
|
411
|
+
if (model === target) return true;
|
|
412
|
+
if (seen.has(model)) return false;
|
|
413
|
+
seen.add(model);
|
|
414
|
+
for (const property of model.properties.values()) {
|
|
415
|
+
let type: Type = property.type;
|
|
416
|
+
const item = arrayItem(type);
|
|
417
|
+
if (item) type = item;
|
|
418
|
+
if (type.kind === "Model" && contains(type, target, seen)) return true;
|
|
419
|
+
}
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Immediate composition parents of a model within one form's reachable type graph. */
|
|
424
|
+
function compositionParents(
|
|
425
|
+
model: Model,
|
|
426
|
+
target: Model,
|
|
427
|
+
found = new Set<Model>(),
|
|
428
|
+
visiting = new Set<Model>(),
|
|
429
|
+
): Set<Model> {
|
|
430
|
+
if (visiting.has(model)) return found;
|
|
431
|
+
const next = new Set(visiting).add(model);
|
|
432
|
+
for (const property of model.properties.values()) {
|
|
433
|
+
let type: Type = property.type;
|
|
434
|
+
const item = arrayItem(type);
|
|
435
|
+
if (item) type = item;
|
|
436
|
+
if (type.kind !== "Model") continue;
|
|
437
|
+
if (type === target) found.add(model);
|
|
438
|
+
compositionParents(type, target, found, next);
|
|
439
|
+
}
|
|
440
|
+
return found;
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
function checkComputedPaths(program: Program, blocks: Block[]): void {
|
|
444
|
+
const forms = blocks.filter(
|
|
445
|
+
(block): block is Block & { model: Model } =>
|
|
446
|
+
block.kind === "form" && block.model.kind === "Model",
|
|
447
|
+
);
|
|
448
|
+
const models: Model[] = [];
|
|
449
|
+
const visit = (namespace: Namespace): void => {
|
|
450
|
+
models.push(...namespace.models.values());
|
|
451
|
+
for (const child of namespace.namespaces.values()) visit(child);
|
|
452
|
+
};
|
|
453
|
+
visit(program.getGlobalNamespaceType());
|
|
454
|
+
|
|
455
|
+
for (const model of models) {
|
|
456
|
+
for (const property of model.properties.values()) {
|
|
457
|
+
const computed = propComputedFrom(program, property);
|
|
458
|
+
if (!computed) continue;
|
|
459
|
+
const owner = property.model;
|
|
460
|
+
if (!owner) continue;
|
|
461
|
+
for (const path of computed.paths) {
|
|
462
|
+
const absolute = path.startsWith("/");
|
|
463
|
+
const parentRelative = path.startsWith("../");
|
|
464
|
+
const candidate = absolute
|
|
465
|
+
? path.slice(1)
|
|
466
|
+
: parentRelative
|
|
467
|
+
? path.slice(3)
|
|
468
|
+
: path;
|
|
469
|
+
const containingForms = absolute
|
|
470
|
+
? forms.filter((form) => contains(form.model, owner))
|
|
471
|
+
: [];
|
|
472
|
+
const parents = parentRelative
|
|
473
|
+
? new Set(
|
|
474
|
+
forms.flatMap((form) => [
|
|
475
|
+
...compositionParents(form.model, owner),
|
|
476
|
+
]),
|
|
477
|
+
)
|
|
478
|
+
: new Set<Model>();
|
|
479
|
+
const valid = absolute
|
|
480
|
+
? // A question-bank file can compile without any form in scope. Defer its absolute
|
|
481
|
+
// composition paths until a form actually composes the block; every containing
|
|
482
|
+
// form must then satisfy the declared relationship.
|
|
483
|
+
containingForms.length === 0 ||
|
|
484
|
+
containingForms.every((form) => resolves(form.model, candidate))
|
|
485
|
+
: parentRelative
|
|
486
|
+
? parents.size === 0 ||
|
|
487
|
+
[...parents].every((parent) => resolves(parent, candidate))
|
|
488
|
+
: resolves(owner, candidate);
|
|
489
|
+
if (!valid) {
|
|
490
|
+
reportDiagnostic(program, {
|
|
491
|
+
code: "calculation-path-unresolved",
|
|
492
|
+
target: property,
|
|
493
|
+
format: {
|
|
494
|
+
path,
|
|
495
|
+
scope: absolute
|
|
496
|
+
? "its containing form"
|
|
497
|
+
: parentRelative
|
|
498
|
+
? "its containing block"
|
|
499
|
+
: owner.name,
|
|
500
|
+
},
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// ---------------------------------------------------------------------------
|
|
509
|
+
// identity
|
|
510
|
+
|
|
511
|
+
/** Words that name a form rather than a meaning. */
|
|
512
|
+
const FORM_WORDS = [
|
|
513
|
+
"sf424",
|
|
514
|
+
"sf-424",
|
|
515
|
+
"sf424a",
|
|
516
|
+
"sf424b",
|
|
517
|
+
"sf424c",
|
|
518
|
+
"sf424d",
|
|
519
|
+
"rr",
|
|
520
|
+
"sflll",
|
|
521
|
+
"cd511",
|
|
522
|
+
"epa",
|
|
523
|
+
"neh",
|
|
524
|
+
"phs",
|
|
525
|
+
"key-contacts",
|
|
526
|
+
"key_contacts",
|
|
527
|
+
"lobbying",
|
|
528
|
+
"profile",
|
|
529
|
+
"short",
|
|
530
|
+
];
|
|
531
|
+
|
|
532
|
+
function checkQuestionIds(program: Program, blocks: Block[]): void {
|
|
533
|
+
for (const block of blocks) {
|
|
534
|
+
if (block.kind !== "question") continue;
|
|
535
|
+
const id = block.id.toLowerCase();
|
|
536
|
+
const pathSegments = id.split("/");
|
|
537
|
+
const wordSegments = id.split(/[/\-_]/);
|
|
538
|
+
const offender = FORM_WORDS.find(
|
|
539
|
+
(word) => pathSegments.includes(word) || wordSegments.includes(word),
|
|
540
|
+
);
|
|
541
|
+
if (offender) {
|
|
542
|
+
reportDiagnostic(program, {
|
|
543
|
+
code: "form-scoped-question-id",
|
|
544
|
+
target: block.model,
|
|
545
|
+
format: { id: block.id },
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Two blocks claiming one id would write to one output path, and the second would win
|
|
553
|
+
* silently. The usual cause is `model X is Y`, which copies the base's decorators --
|
|
554
|
+
* including its identity -- where `extends` would not.
|
|
555
|
+
*/
|
|
556
|
+
function checkDuplicateIds(program: Program, blocks: Block[]): void {
|
|
557
|
+
const byId = new Map<string, Block>();
|
|
558
|
+
for (const block of blocks) {
|
|
559
|
+
const first = byId.get(block.id);
|
|
560
|
+
if (first) {
|
|
561
|
+
reportDiagnostic(program, {
|
|
562
|
+
code: "duplicate-block-id",
|
|
563
|
+
target: block.model,
|
|
564
|
+
format: {
|
|
565
|
+
id: block.id,
|
|
566
|
+
first: name(first.model),
|
|
567
|
+
second: name(block.model),
|
|
568
|
+
},
|
|
569
|
+
});
|
|
570
|
+
continue;
|
|
571
|
+
}
|
|
572
|
+
byId.set(block.id, block);
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
const name = (type: Model | Scalar) => type.name || "an anonymous model";
|
|
577
|
+
|
|
578
|
+
// ---------------------------------------------------------------------------
|
|
579
|
+
// conditions
|
|
580
|
+
|
|
581
|
+
/** Every condition on a property, whichever effect it drives. */
|
|
582
|
+
function conditionsOf(program: Program, prop: ModelProperty): Condition[] {
|
|
583
|
+
return [
|
|
584
|
+
...propVisibleWhen(program, prop),
|
|
585
|
+
...propEnabledWhen(program, prop),
|
|
586
|
+
...propReadOnlyWhen(program, prop),
|
|
587
|
+
...propRequiredWhen(program, prop),
|
|
588
|
+
...propValidationConstraintsWhen(program, prop).map(
|
|
589
|
+
(item) => item.condition,
|
|
590
|
+
),
|
|
591
|
+
];
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
/**
|
|
595
|
+
* A condition compares a source property against a literal. If the source is enumerated
|
|
596
|
+
* and the literal is not one of its members, the condition can never hold -- so the field
|
|
597
|
+
* it governs is permanently hidden, permanently read-only, or never required, and no test notices.
|
|
598
|
+
*/
|
|
599
|
+
function checkConditions(program: Program, prop: ModelProperty): void {
|
|
600
|
+
const model = prop.model;
|
|
601
|
+
if (!model) return;
|
|
602
|
+
const atomic = (
|
|
603
|
+
condition: Condition,
|
|
604
|
+
): Exclude<Condition, { operator: "any" }>[] =>
|
|
605
|
+
condition.operator === "any" ? condition.predicates : [condition];
|
|
606
|
+
for (const condition of conditionsOf(program, prop).flatMap(atomic)) {
|
|
607
|
+
const declaringModel = conditionSourceModel(condition);
|
|
608
|
+
if (declaringModel && declaringModel !== model) {
|
|
609
|
+
reportDiagnostic(program, {
|
|
610
|
+
code: "condition-source-not-sibling",
|
|
611
|
+
target: prop,
|
|
612
|
+
format: { source: condition.sourcePath.join("."), target: prop.name },
|
|
613
|
+
});
|
|
614
|
+
continue;
|
|
615
|
+
}
|
|
616
|
+
const source = conditionSource(model, condition.sourcePath);
|
|
617
|
+
if (!source) {
|
|
618
|
+
reportDiagnostic(program, {
|
|
619
|
+
code: "condition-path-unresolved",
|
|
620
|
+
target: prop,
|
|
621
|
+
format: { path: condition.sourcePath.join("."), model: model.name },
|
|
622
|
+
});
|
|
623
|
+
continue;
|
|
624
|
+
}
|
|
625
|
+
const enumeration = enumOf(source.type);
|
|
626
|
+
if (!enumeration) continue;
|
|
627
|
+
const members = [...enumeration.members.values()].map(
|
|
628
|
+
(m) => m.value ?? m.name,
|
|
629
|
+
);
|
|
630
|
+
const values =
|
|
631
|
+
condition.operator === "in"
|
|
632
|
+
? condition.values
|
|
633
|
+
: condition.operator === "equals"
|
|
634
|
+
? [condition.value]
|
|
635
|
+
: [];
|
|
636
|
+
for (const value of values) {
|
|
637
|
+
if (members.includes(value as string | number)) continue;
|
|
638
|
+
reportDiagnostic(program, {
|
|
639
|
+
code: "condition-value-not-in-enum",
|
|
640
|
+
target: prop,
|
|
641
|
+
format: {
|
|
642
|
+
value: String(value),
|
|
643
|
+
enumName: enumeration.name,
|
|
644
|
+
members:
|
|
645
|
+
members.slice(0, 6).join(", ") +
|
|
646
|
+
(members.length > 6 ? ", ..." : ""),
|
|
647
|
+
},
|
|
648
|
+
});
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
function conditionSource(
|
|
654
|
+
model: Model,
|
|
655
|
+
path: string[],
|
|
656
|
+
): ModelProperty | undefined {
|
|
657
|
+
let current: Model = model;
|
|
658
|
+
let property: ModelProperty | undefined;
|
|
659
|
+
for (const [index, step] of path.entries()) {
|
|
660
|
+
property = modelProperties(current).find((item) => item.name === step);
|
|
661
|
+
if (!property) return undefined;
|
|
662
|
+
if (index === path.length - 1) return property;
|
|
663
|
+
if (property.type.kind !== "Model" || property.type.indexer)
|
|
664
|
+
return undefined;
|
|
665
|
+
current = property.type;
|
|
666
|
+
}
|
|
667
|
+
return property;
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
/** The enum behind a property's type, looking through an array. */
|
|
671
|
+
function enumOf(type: Type): Enum | undefined {
|
|
672
|
+
if (type.kind === "Enum") return type;
|
|
673
|
+
if (type.kind === "Model" && type.indexer) return enumOf(type.indexer.value);
|
|
674
|
+
return undefined;
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
/**
|
|
678
|
+
* A field that is always required but only sometimes visible is a dead end: the applicant
|
|
679
|
+
* cannot submit and cannot see why. This is the check that is impossible in the shipping
|
|
680
|
+
* architecture, where requiredness lives in the JSON Schema and visibility in the UI
|
|
681
|
+
* schema, in different languages.
|
|
682
|
+
*/
|
|
683
|
+
function checkRequiredButHidden(program: Program, prop: ModelProperty): void {
|
|
684
|
+
if (prop.optional) return;
|
|
685
|
+
if (!propVisibleWhen(program, prop).length) return;
|
|
686
|
+
reportDiagnostic(program, {
|
|
687
|
+
code: "required-but-unreachable",
|
|
688
|
+
target: prop,
|
|
689
|
+
format: { name: prop.name },
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// ---------------------------------------------------------------------------
|
|
694
|
+
// sections and overrides
|
|
695
|
+
|
|
696
|
+
function checkSections(program: Program, block: Block): void {
|
|
697
|
+
if (block.kind !== "form" || !block.sections) return;
|
|
698
|
+
for (const prop of orderedProps(program, block)) {
|
|
699
|
+
if (propOmit(program, prop)) continue;
|
|
700
|
+
if (propSection(program, prop)) continue;
|
|
701
|
+
if (typeof block.overrides[prop.name]?.section === "string") continue;
|
|
702
|
+
reportDiagnostic(program, {
|
|
703
|
+
code: "section-orphan",
|
|
704
|
+
target: prop,
|
|
705
|
+
format: { name: prop.name },
|
|
706
|
+
});
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* An override addresses a field by the path an applicant's answer takes. A path that does
|
|
712
|
+
* not resolve is a silently ignored override -- the CommonGrants bank's stringly-typed
|
|
713
|
+
* override table, caught at compile time instead of at website build time.
|
|
714
|
+
*/
|
|
715
|
+
function checkOverridePaths(program: Program, block: Block): void {
|
|
716
|
+
const paths = Object.keys(block.overrides);
|
|
717
|
+
for (const [overridePath, override] of Object.entries(block.overrides)) {
|
|
718
|
+
if (override.visibleReadOnly === true && override.readOnly !== true) {
|
|
719
|
+
reportDiagnostic(program, {
|
|
720
|
+
code: "visible-read-only-without-read-only",
|
|
721
|
+
target: block.model,
|
|
722
|
+
format: { path: overridePath },
|
|
723
|
+
});
|
|
724
|
+
}
|
|
725
|
+
const condition = override.enabledWhen;
|
|
726
|
+
const enabledWhen = condition as Record<string, unknown> | undefined;
|
|
727
|
+
if (
|
|
728
|
+
enabledWhen &&
|
|
729
|
+
typeof enabledWhen === "object" &&
|
|
730
|
+
!Array.isArray(enabledWhen) &&
|
|
731
|
+
typeof enabledWhen.path === "string"
|
|
732
|
+
) {
|
|
733
|
+
paths.push(enabledWhen.path);
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
for (const path of paths) {
|
|
737
|
+
const reason = resolvePath(program, block.model as Model, path.split("."));
|
|
738
|
+
if (!reason) continue;
|
|
739
|
+
reportDiagnostic(program, {
|
|
740
|
+
code: "override-path-unresolved",
|
|
741
|
+
target: block.model,
|
|
742
|
+
format: { path, reason },
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
/** Undefined when the path resolves; otherwise why it did not. */
|
|
748
|
+
function resolvePath(
|
|
749
|
+
program: Program,
|
|
750
|
+
model: Model,
|
|
751
|
+
steps: string[],
|
|
752
|
+
): string | undefined {
|
|
753
|
+
let current: Model | undefined = model;
|
|
754
|
+
for (const [index, step] of steps.entries()) {
|
|
755
|
+
if (!current) return `${steps.slice(0, index).join(".")} is not an object`;
|
|
756
|
+
const prop = allProperties(current).get(step);
|
|
757
|
+
if (!prop) {
|
|
758
|
+
const known = [...allProperties(current).keys()].slice(0, 8).join(", ");
|
|
759
|
+
return `${current.name || "the model"} has no property "${step}" (has ${known})`;
|
|
760
|
+
}
|
|
761
|
+
if (index === steps.length - 1) return undefined;
|
|
762
|
+
current = objectBehind(program, prop);
|
|
763
|
+
}
|
|
764
|
+
return undefined;
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
/** Own and inherited properties, the derived declaration winning. */
|
|
768
|
+
function allProperties(model: Model): Map<string, ModelProperty> {
|
|
769
|
+
const out = new Map<string, ModelProperty>();
|
|
770
|
+
const chain: Model[] = [];
|
|
771
|
+
for (let m: Model | undefined = model; m; m = m.baseModel) chain.unshift(m);
|
|
772
|
+
for (const m of chain)
|
|
773
|
+
for (const prop of m.properties.values()) out.set(prop.name, prop);
|
|
774
|
+
return out;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/** The object a property holds, whether directly or as the entries of a list. */
|
|
778
|
+
function objectBehind(
|
|
779
|
+
program: Program,
|
|
780
|
+
prop: ModelProperty,
|
|
781
|
+
): Model | undefined {
|
|
782
|
+
const type = prop.type;
|
|
783
|
+
if (type.kind !== "Model") return undefined;
|
|
784
|
+
if (type.indexer) {
|
|
785
|
+
const item = type.indexer.value;
|
|
786
|
+
return item.kind === "Model" ? item : undefined;
|
|
787
|
+
}
|
|
788
|
+
const block = childBlock(program, prop);
|
|
789
|
+
if (block && block.scalar) return undefined;
|
|
790
|
+
return type;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/** A widget declaration naming a section the form does not have would render nothing. */
|
|
794
|
+
// ---------------------------------------------------------------------------
|
|
795
|
+
// calculations
|
|
796
|
+
|
|
797
|
+
/** A materialization policy is meaningful only on a declared calculation. */
|
|
798
|
+
function checkCalculationMaterialization(
|
|
799
|
+
program: Program,
|
|
800
|
+
blocks: Block[],
|
|
801
|
+
): void {
|
|
802
|
+
const seen = new Set<Model>();
|
|
803
|
+
for (const block of blocks) {
|
|
804
|
+
if (block.model.kind !== "Model") continue;
|
|
805
|
+
walkModels(program, block.model, seen, (model) => {
|
|
806
|
+
for (const prop of model.properties.values()) {
|
|
807
|
+
if (!propCalculationMaterialization(program, prop)) continue;
|
|
808
|
+
if (propComputed(program, prop) || propComputedFrom(program, prop))
|
|
809
|
+
continue;
|
|
810
|
+
reportDiagnostic(program, {
|
|
811
|
+
code: "calculation-materialization-without-calculation",
|
|
812
|
+
target: prop,
|
|
813
|
+
format: { name: prop.name },
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
/**
|
|
821
|
+
* A calculated value that depends on itself has no evaluation order, so a consumer would
|
|
822
|
+
* emit a rule the runtime cannot satisfy. The cycle is reported once, naming the loop.
|
|
823
|
+
*/
|
|
824
|
+
function checkCalculationCycles(program: Program, blocks: Block[]): void {
|
|
825
|
+
const seen = new Set<Model>();
|
|
826
|
+
for (const block of blocks) {
|
|
827
|
+
if (block.model.kind !== "Model") continue;
|
|
828
|
+
walkModels(program, block.model, seen, (model) => {
|
|
829
|
+
const edges = new Map<string, string[]>();
|
|
830
|
+
for (const prop of model.properties.values()) {
|
|
831
|
+
const computed = propComputed(program, prop);
|
|
832
|
+
if (computed) edges.set(prop.name, computed.refs);
|
|
833
|
+
}
|
|
834
|
+
for (const start of edges.keys()) {
|
|
835
|
+
const cycle = findCycle(start, edges);
|
|
836
|
+
if (!cycle) continue;
|
|
837
|
+
reportDiagnostic(program, {
|
|
838
|
+
code: "calculation-cycle",
|
|
839
|
+
target: model.properties.get(start)!,
|
|
840
|
+
format: { cycle: cycle.join(" -> ") },
|
|
841
|
+
});
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
function findCycle(
|
|
849
|
+
start: string,
|
|
850
|
+
edges: Map<string, string[]>,
|
|
851
|
+
): string[] | undefined {
|
|
852
|
+
const path: string[] = [];
|
|
853
|
+
const onPath = new Set<string>();
|
|
854
|
+
const visit = (node: string): string[] | undefined => {
|
|
855
|
+
if (onPath.has(node)) return [...path.slice(path.indexOf(node)), node];
|
|
856
|
+
if (!edges.has(node)) return undefined;
|
|
857
|
+
path.push(node);
|
|
858
|
+
onPath.add(node);
|
|
859
|
+
for (const next of edges.get(node)!) {
|
|
860
|
+
const found = visit(next);
|
|
861
|
+
if (found) return found;
|
|
862
|
+
}
|
|
863
|
+
path.pop();
|
|
864
|
+
onPath.delete(node);
|
|
865
|
+
return undefined;
|
|
866
|
+
};
|
|
867
|
+
return visit(start);
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
/** Every model reachable from a block, each visited once. */
|
|
871
|
+
function walkModels(
|
|
872
|
+
program: Program,
|
|
873
|
+
model: Model,
|
|
874
|
+
seen: Set<Model>,
|
|
875
|
+
visit: (model: Model) => void,
|
|
876
|
+
): void {
|
|
877
|
+
if (seen.has(model)) return;
|
|
878
|
+
seen.add(model);
|
|
879
|
+
visit(model);
|
|
880
|
+
for (const prop of model.properties.values()) {
|
|
881
|
+
const next = objectBehind(program, prop);
|
|
882
|
+
if (next) walkModels(program, next, seen, visit);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
/** The emitter needs the same notion of "all properties", derived declaration winning. */
|
|
887
|
+
export { allProperties as inheritedProperties };
|