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/lib.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { createTypeSpecLibrary, paramMessage } from "@typespec/compiler";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Named diagnostics for everything that makes an artifact wrong, and state keys for
|
|
5
|
+
* everything the decorators record.
|
|
6
|
+
*
|
|
7
|
+
* The split between these and the linter rules in `linter.ts` is forced rather than
|
|
8
|
+
* chosen: a TypeSpec linter rule may only be a warning. So a check whose failure means the
|
|
9
|
+
* emitted artifact is broken is reported from `$onValidate` as an error, and the linter
|
|
10
|
+
* carries the checks that describe a specification worth tidying.
|
|
11
|
+
*/
|
|
12
|
+
export const $lib = createTypeSpecLibrary({
|
|
13
|
+
name: "simpler-forms",
|
|
14
|
+
// Pins the diagnostic short name, so moving the package into an npm scope later
|
|
15
|
+
// leaves every `#suppress "simpler-forms/<rule>"` in a specification working.
|
|
16
|
+
alias: "simpler-forms",
|
|
17
|
+
diagnostics: {
|
|
18
|
+
"form-scoped-question-id": {
|
|
19
|
+
severity: "error",
|
|
20
|
+
messages: {
|
|
21
|
+
default: paramMessage`Question id "${"id"}" names a form. Questions are named for what they mean; put form deltas in @UI.overrides.`,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
"duplicate-block-id": {
|
|
25
|
+
severity: "error",
|
|
26
|
+
messages: {
|
|
27
|
+
default: paramMessage`Block id "${"id"}" is claimed by both ${"first"} and ${"second"}. Two blocks would collide on one output path; a form-local extension should use \`extends\`, which carries no identity.`,
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
"condition-value-not-in-enum": {
|
|
31
|
+
severity: "error",
|
|
32
|
+
messages: {
|
|
33
|
+
default: paramMessage`"${"value"}" is not a member of ${"enumName"}, so this condition can never hold. Members: ${"members"}.`,
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
"condition-path-unresolved": {
|
|
37
|
+
severity: "error",
|
|
38
|
+
messages: {
|
|
39
|
+
default: paramMessage`Condition path "${"path"}" does not resolve from ${"model"}.`,
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
"condition-count-source-not-array": {
|
|
43
|
+
severity: "error",
|
|
44
|
+
messages: {
|
|
45
|
+
default: paramMessage`Count condition source ${"source"} must be an array property.`,
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
"condition-count-minimum-invalid": {
|
|
49
|
+
severity: "error",
|
|
50
|
+
messages: {
|
|
51
|
+
default: paramMessage`Count condition minimum must be a positive integer; received ${"minimum"}.`,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
"condition-source-not-sibling": {
|
|
55
|
+
severity: "error",
|
|
56
|
+
messages: {
|
|
57
|
+
default: paramMessage`Condition source ${"source"} must be a sibling of target ${"target"} in the same model.`,
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
"cardinality-path-unresolved": {
|
|
61
|
+
severity: "error",
|
|
62
|
+
messages: {
|
|
63
|
+
default: paramMessage`Cardinality path "${"path"}" does not resolve from ${"model"}: ${"reason"}.`,
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
"cardinality-model-not-emitted": {
|
|
67
|
+
severity: "error",
|
|
68
|
+
messages: {
|
|
69
|
+
default: paramMessage`Cardinality annotations on model ${"model"} would not be emitted. Put them on a semantic block or on the property where that block is composed.`,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
"block-not-emitted": {
|
|
73
|
+
severity: "error",
|
|
74
|
+
messages: {
|
|
75
|
+
default: paramMessage`${"name"} produced no schema under id "${"id"}", so none of its artifacts were written. The ids that were produced are: ${"available"}.`,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
"entity-id-mismatch": {
|
|
79
|
+
severity: "error",
|
|
80
|
+
messages: {
|
|
81
|
+
default: paramMessage`${"name"} declares entity "${"entity"}" but its id is "${"id"}". The first segment of the id is the entity, so these must agree.`,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
"at-least-one-invalid": {
|
|
85
|
+
severity: "error",
|
|
86
|
+
messages: {
|
|
87
|
+
default: paramMessage`@Validation.atLeastOneOf on ${"model"} must name at least two distinct properties owned by that model; received ${"properties"}.`,
|
|
88
|
+
},
|
|
89
|
+
},
|
|
90
|
+
"conditional-at-least-one-path-invalid": {
|
|
91
|
+
severity: "error",
|
|
92
|
+
messages: {
|
|
93
|
+
default: paramMessage`@Validation.atLeastOnePathWhenPresent on ${"model"} must name a source path and at least two distinct target paths; received ${"paths"}.`,
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
"positive-decimal-string-target-invalid": {
|
|
97
|
+
severity: "error",
|
|
98
|
+
messages: {
|
|
99
|
+
default: paramMessage`Positive decimal-string condition target "${"path"}" on ${"model"} must resolve to a string scalar; received ${"type"}.`,
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
"encoded-checkbox-contract-invalid": {
|
|
103
|
+
severity: "error",
|
|
104
|
+
messages: {
|
|
105
|
+
default: paramMessage`Encoded checkbox contract on ${"name"} is invalid: ${"reason"}.`,
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
"calculation-cycle": {
|
|
109
|
+
severity: "error",
|
|
110
|
+
messages: {
|
|
111
|
+
default: paramMessage`Calculation cycle: ${"cycle"}. A calculated value cannot depend on itself.`,
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
"calculation-path-unresolved": {
|
|
115
|
+
severity: "error",
|
|
116
|
+
messages: {
|
|
117
|
+
default: paramMessage`Calculation path "${"path"}" does not resolve from ${"scope"}. Cross-boundary calculations must name real canonical data paths.`,
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
"calculation-materialization-without-calculation": {
|
|
121
|
+
severity: "error",
|
|
122
|
+
messages: {
|
|
123
|
+
default: paramMessage`@Validation.materializeWhenAnySourcePresent on ${"name"} requires @Validation.computed or @Validation.computedFrom.`,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
"date-order-source-invalid": {
|
|
127
|
+
severity: "error",
|
|
128
|
+
messages: {
|
|
129
|
+
default: paramMessage`@Validation.notBefore on ${"target"} must reference a different sibling property; received ${"source"}.`,
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
"required-but-unreachable": {
|
|
133
|
+
severity: "error",
|
|
134
|
+
messages: {
|
|
135
|
+
default: paramMessage`${"name"} is always required but only sometimes visible, so an applicant can be blocked by a field they cannot see. Make it optional and use @Validation.requiredWhen, or drop the visibility condition.`,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
"override-path-unresolved": {
|
|
139
|
+
severity: "error",
|
|
140
|
+
messages: {
|
|
141
|
+
default: paramMessage`@UI.overrides path "${"path"}" does not resolve: ${"reason"}.`,
|
|
142
|
+
},
|
|
143
|
+
},
|
|
144
|
+
"visible-read-only-without-read-only": {
|
|
145
|
+
severity: "error",
|
|
146
|
+
messages: {
|
|
147
|
+
default: paramMessage`@UI.overrides path "${"path"}" requests visibleReadOnly without readOnly. A visible read-only control must also be marked readOnly so schema and UI cannot disagree.`,
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
"section-orphan": {
|
|
151
|
+
severity: "error",
|
|
152
|
+
messages: {
|
|
153
|
+
default: paramMessage`${"name"} is in no section, so it renders nowhere. Give it a @UI.section, or @UI.omit it if that is deliberate.`,
|
|
154
|
+
},
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
state: {
|
|
158
|
+
questionMeta: {},
|
|
159
|
+
formMeta: {},
|
|
160
|
+
tags: {},
|
|
161
|
+
entity: {},
|
|
162
|
+
responseRole: {},
|
|
163
|
+
label: {},
|
|
164
|
+
helpText: {},
|
|
165
|
+
widget: {},
|
|
166
|
+
encodedCheckboxGroup: {},
|
|
167
|
+
sections: {},
|
|
168
|
+
section: {},
|
|
169
|
+
order: {},
|
|
170
|
+
overrides: {},
|
|
171
|
+
readOnly: {},
|
|
172
|
+
omit: {},
|
|
173
|
+
visibleWhen: {},
|
|
174
|
+
enabledWhen: {},
|
|
175
|
+
readOnlyWhen: {},
|
|
176
|
+
requiredWhen: {},
|
|
177
|
+
notBefore: {},
|
|
178
|
+
validationConstraints: {},
|
|
179
|
+
exclusiveValues: {},
|
|
180
|
+
validationConstraintsWhen: {},
|
|
181
|
+
requiredPaths: {},
|
|
182
|
+
requiredPathWhen: {},
|
|
183
|
+
atLeastOnePathWhenPresent: {},
|
|
184
|
+
requiredPathWhenPositiveDecimalString: {},
|
|
185
|
+
positiveDecimalStringWhenPathPresent: {},
|
|
186
|
+
atLeastOneOf: {},
|
|
187
|
+
computed: {},
|
|
188
|
+
computedFrom: {},
|
|
189
|
+
calculationMaterialization: {},
|
|
190
|
+
evaluationOrder: {},
|
|
191
|
+
totals: {},
|
|
192
|
+
},
|
|
193
|
+
} as const);
|
|
194
|
+
|
|
195
|
+
export const { reportDiagnostic, createDiagnostic, stateKeys } = $lib;
|
package/src/linter.ts
ADDED
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import type { Model, ModelProperty, Scalar } from "@typespec/compiler";
|
|
2
|
+
import {
|
|
3
|
+
createRule,
|
|
4
|
+
defineCodeFix,
|
|
5
|
+
defineLinter,
|
|
6
|
+
getDoc,
|
|
7
|
+
getMaxItems,
|
|
8
|
+
getMaxLength,
|
|
9
|
+
getMinItems,
|
|
10
|
+
getMinLength,
|
|
11
|
+
getSourceLocation,
|
|
12
|
+
paramMessage,
|
|
13
|
+
serializeValueAsJson,
|
|
14
|
+
} from "@typespec/compiler";
|
|
15
|
+
import {
|
|
16
|
+
allBlocks,
|
|
17
|
+
modelOrder,
|
|
18
|
+
orderedProps,
|
|
19
|
+
propEnabledWhen,
|
|
20
|
+
propOmit,
|
|
21
|
+
propReadOnlyWhen,
|
|
22
|
+
propRequiredWhen,
|
|
23
|
+
propSection,
|
|
24
|
+
propValidationConstraintsWhen,
|
|
25
|
+
propVisibleWhen,
|
|
26
|
+
} from "./model.js";
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Hygiene checks. A TypeSpec linter rule may only be a warning, so everything whose failure
|
|
30
|
+
* makes an artifact *wrong* lives in `$onValidate` instead; what is left here is a
|
|
31
|
+
* specification worth tidying rather than a broken one.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
const orphanQuestion = createRule({
|
|
35
|
+
name: "no-orphan-question",
|
|
36
|
+
severity: "warning",
|
|
37
|
+
description: "A bank question that no form and no other question composes.",
|
|
38
|
+
messages: {
|
|
39
|
+
default: paramMessage`Question "${"id"}" is composed by nothing. Either a form should ask it or it should be deleted; an unasked question is still maintained.`,
|
|
40
|
+
},
|
|
41
|
+
create(context) {
|
|
42
|
+
return {
|
|
43
|
+
root: (program) => {
|
|
44
|
+
const blocks = allBlocks(program);
|
|
45
|
+
const byType = new Map(blocks.map((block) => [block.model, block]));
|
|
46
|
+
const referenced = new Set<string>();
|
|
47
|
+
|
|
48
|
+
// Reachability, not property references. A question is composed by holding it in a
|
|
49
|
+
// property, by extending it, or by being reached through a model that is not itself
|
|
50
|
+
// a question -- and all three have to count, or the rule reports a question that a
|
|
51
|
+
// form does ask.
|
|
52
|
+
const seen = new Set<Model | Scalar>();
|
|
53
|
+
const reach = (type: Model | Scalar, viaComposition: boolean): void => {
|
|
54
|
+
const block = byType.get(type);
|
|
55
|
+
if (block && viaComposition) referenced.add(block.id);
|
|
56
|
+
if (seen.has(type)) return;
|
|
57
|
+
seen.add(type);
|
|
58
|
+
// Extending a question composes it, for a scalar as much as for a model: naming a
|
|
59
|
+
// shape does not make the shape unasked.
|
|
60
|
+
const base = type.kind === "Model" ? type.baseModel : type.baseScalar;
|
|
61
|
+
if (base) reach(base, true);
|
|
62
|
+
if (type.kind !== "Model") return;
|
|
63
|
+
for (const prop of type.properties.values()) {
|
|
64
|
+
for (const target of held(prop)) reach(target, true);
|
|
65
|
+
for (
|
|
66
|
+
let source = prop.sourceProperty;
|
|
67
|
+
source;
|
|
68
|
+
source = source.sourceProperty
|
|
69
|
+
) {
|
|
70
|
+
if (source.model) reach(source.model, true);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
for (const block of blocks) reach(block.model, false);
|
|
75
|
+
|
|
76
|
+
for (const block of blocks) {
|
|
77
|
+
if (block.kind !== "question") continue;
|
|
78
|
+
if (referenced.has(block.id)) continue;
|
|
79
|
+
context.reportDiagnostic({
|
|
80
|
+
target: block.model,
|
|
81
|
+
format: { id: block.id },
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* The declarations a property holds: its own type, and the entries of a list.
|
|
91
|
+
*
|
|
92
|
+
* Both, because a list can itself be a question -- `primary-org/applicant-type` is one to
|
|
93
|
+
* three codes, so the *list* is what the form asks -- and the entries can be a question too.
|
|
94
|
+
* Returning only the entries reports the list as composed by nothing.
|
|
95
|
+
*/
|
|
96
|
+
function held(prop: ModelProperty): (Model | Scalar)[] {
|
|
97
|
+
const type = prop.type;
|
|
98
|
+
if (type.kind === "Scalar") return [type];
|
|
99
|
+
if (type.kind !== "Model") return [];
|
|
100
|
+
const out: (Model | Scalar)[] = [type];
|
|
101
|
+
const item = type.indexer?.value;
|
|
102
|
+
if (item && (item.kind === "Model" || item.kind === "Scalar")) out.push(item);
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
const questionDocs = createRule({
|
|
107
|
+
name: "require-question-docs",
|
|
108
|
+
severity: "warning",
|
|
109
|
+
description:
|
|
110
|
+
"Every bank question needs a doc comment; it becomes its description.",
|
|
111
|
+
messages: {
|
|
112
|
+
default: paramMessage`Question "${"id"}" has no doc comment, so it has no description. Applicants read it, and so does anyone browsing the bank.`,
|
|
113
|
+
},
|
|
114
|
+
create(context) {
|
|
115
|
+
return {
|
|
116
|
+
root: (program) => {
|
|
117
|
+
for (const block of allBlocks(program)) {
|
|
118
|
+
if (block.kind !== "question") continue;
|
|
119
|
+
if (getDoc(program, block.model)) continue;
|
|
120
|
+
context.reportDiagnostic({
|
|
121
|
+
target: block.model,
|
|
122
|
+
format: { id: block.id },
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const sectionUnused = createRule({
|
|
131
|
+
name: "section-unused",
|
|
132
|
+
severity: "warning",
|
|
133
|
+
description: "A declared section that no field is placed in.",
|
|
134
|
+
messages: {
|
|
135
|
+
default: paramMessage`Section "${"section"}" on ${"form"} holds no fields, so it will not render. Usually this means a field was dropped.`,
|
|
136
|
+
},
|
|
137
|
+
create(context) {
|
|
138
|
+
return {
|
|
139
|
+
root: (program) => {
|
|
140
|
+
for (const block of allBlocks(program)) {
|
|
141
|
+
if (
|
|
142
|
+
block.kind !== "form" ||
|
|
143
|
+
!block.sections ||
|
|
144
|
+
block.model.kind !== "Model"
|
|
145
|
+
)
|
|
146
|
+
continue;
|
|
147
|
+
const used = new Set<string>();
|
|
148
|
+
for (const prop of orderedProps(program, block)) {
|
|
149
|
+
const section = propSection(program, prop);
|
|
150
|
+
if (section) used.add(section.name);
|
|
151
|
+
}
|
|
152
|
+
for (const member of block.sections.members.values()) {
|
|
153
|
+
if (used.has(member.name)) continue;
|
|
154
|
+
// A documented empty section is deliberate static presentation content.
|
|
155
|
+
if (getDoc(program, member)) continue;
|
|
156
|
+
context.reportDiagnostic({
|
|
157
|
+
target: member,
|
|
158
|
+
format: { section: member.name, form: block.id },
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
const orderIncomplete = createRule({
|
|
168
|
+
name: "order-incomplete",
|
|
169
|
+
severity: "warning",
|
|
170
|
+
description: "@UI.order that omits a property, which then falls to the end.",
|
|
171
|
+
messages: {
|
|
172
|
+
default: paramMessage`@UI.order on ${"model"} omits ${"missing"}. Omitted properties are appended in declaration order, which is rarely what was meant.`,
|
|
173
|
+
},
|
|
174
|
+
create(context) {
|
|
175
|
+
return {
|
|
176
|
+
model: (model) => {
|
|
177
|
+
const order = modelOrder(context.program, model);
|
|
178
|
+
if (!order) return;
|
|
179
|
+
const missing = [...properties(model)]
|
|
180
|
+
.filter(
|
|
181
|
+
(prop) =>
|
|
182
|
+
!propOmit(context.program, prop) && !order.includes(prop.name),
|
|
183
|
+
)
|
|
184
|
+
.map((prop) => prop.name);
|
|
185
|
+
if (!missing.length) return;
|
|
186
|
+
context.reportDiagnostic({
|
|
187
|
+
target: model,
|
|
188
|
+
format: {
|
|
189
|
+
model: model.name || "an anonymous model",
|
|
190
|
+
missing: missing.join(", "),
|
|
191
|
+
},
|
|
192
|
+
codefixes: [appendToOrder(model, missing)],
|
|
193
|
+
});
|
|
194
|
+
},
|
|
195
|
+
};
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* The fix is mechanical -- append the missing names in declaration order -- so it is
|
|
201
|
+
* offered rather than described.
|
|
202
|
+
*/
|
|
203
|
+
function appendToOrder(model: Model, missing: string[]) {
|
|
204
|
+
return defineCodeFix({
|
|
205
|
+
id: "append-to-ui-order",
|
|
206
|
+
label: `Append ${missing.join(", ")} to @UI.order`,
|
|
207
|
+
fix: (context) => {
|
|
208
|
+
const decorator = model.decorators.find(
|
|
209
|
+
(d) => d.definition?.name === "@order",
|
|
210
|
+
);
|
|
211
|
+
const location = getSourceLocation(decorator?.node ?? model.node);
|
|
212
|
+
if (!location) return;
|
|
213
|
+
const text = location.file.text.slice(location.pos, location.end);
|
|
214
|
+
const close = text.lastIndexOf(")");
|
|
215
|
+
if (close < 0) return;
|
|
216
|
+
const addition = missing.map((n) => `, ${model.name}.${n}`).join("");
|
|
217
|
+
return context.prependText(
|
|
218
|
+
{
|
|
219
|
+
file: location.file,
|
|
220
|
+
pos: location.pos + close,
|
|
221
|
+
end: location.pos + close,
|
|
222
|
+
},
|
|
223
|
+
addition,
|
|
224
|
+
);
|
|
225
|
+
},
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const redeclaredProperty = createRule({
|
|
230
|
+
name: "no-redeclared-property",
|
|
231
|
+
severity: "warning",
|
|
232
|
+
description: "A derived block re-declaring a property it already inherits.",
|
|
233
|
+
messages: {
|
|
234
|
+
default: paramMessage`${"model"} re-declares ${"names"} identically to ${"base"}. A redeclaration that narrows -- making an optional member required for this form -- says something; one that repeats says nothing, and leaves two copies to keep in step by hand. For presentation only, use @UI.overrides.`,
|
|
235
|
+
},
|
|
236
|
+
create(context) {
|
|
237
|
+
return {
|
|
238
|
+
model: (model) => {
|
|
239
|
+
if (!model.baseModel) return;
|
|
240
|
+
const inherited = new Map<string, ModelProperty>();
|
|
241
|
+
for (let m: Model | undefined = model.baseModel; m; m = m.baseModel) {
|
|
242
|
+
for (const [name, prop] of m.properties)
|
|
243
|
+
if (!inherited.has(name)) inherited.set(name, prop);
|
|
244
|
+
}
|
|
245
|
+
const clashes = [...model.properties.values()]
|
|
246
|
+
.filter((prop) => {
|
|
247
|
+
const base = inherited.get(prop.name);
|
|
248
|
+
if (
|
|
249
|
+
base &&
|
|
250
|
+
[
|
|
251
|
+
[
|
|
252
|
+
propEnabledWhen(context.program, base),
|
|
253
|
+
propEnabledWhen(context.program, prop),
|
|
254
|
+
],
|
|
255
|
+
[
|
|
256
|
+
propReadOnlyWhen(context.program, base),
|
|
257
|
+
propReadOnlyWhen(context.program, prop),
|
|
258
|
+
],
|
|
259
|
+
[
|
|
260
|
+
propRequiredWhen(context.program, base),
|
|
261
|
+
propRequiredWhen(context.program, prop),
|
|
262
|
+
],
|
|
263
|
+
[
|
|
264
|
+
propValidationConstraintsWhen(context.program, base),
|
|
265
|
+
propValidationConstraintsWhen(context.program, prop),
|
|
266
|
+
],
|
|
267
|
+
[
|
|
268
|
+
propVisibleWhen(context.program, base),
|
|
269
|
+
propVisibleWhen(context.program, prop),
|
|
270
|
+
],
|
|
271
|
+
].some(
|
|
272
|
+
([left, right]) =>
|
|
273
|
+
JSON.stringify(left) !== JSON.stringify(right),
|
|
274
|
+
)
|
|
275
|
+
) {
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
// Narrowing is the point of redeclaring: a form may require a member the
|
|
279
|
+
// question leaves optional. Only an identical repeat is worth a warning.
|
|
280
|
+
return (
|
|
281
|
+
base &&
|
|
282
|
+
base.optional === prop.optional &&
|
|
283
|
+
base.type === prop.type &&
|
|
284
|
+
getMinItems(context.program, base) ===
|
|
285
|
+
getMinItems(context.program, prop) &&
|
|
286
|
+
getMaxItems(context.program, base) ===
|
|
287
|
+
getMaxItems(context.program, prop) &&
|
|
288
|
+
getMinLength(context.program, base) ===
|
|
289
|
+
getMinLength(context.program, prop) &&
|
|
290
|
+
getMaxLength(context.program, base) ===
|
|
291
|
+
getMaxLength(context.program, prop) &&
|
|
292
|
+
JSON.stringify(
|
|
293
|
+
base.defaultValue
|
|
294
|
+
? serializeValueAsJson(
|
|
295
|
+
context.program,
|
|
296
|
+
base.defaultValue,
|
|
297
|
+
base.defaultValue.type,
|
|
298
|
+
)
|
|
299
|
+
: undefined,
|
|
300
|
+
) ===
|
|
301
|
+
JSON.stringify(
|
|
302
|
+
prop.defaultValue
|
|
303
|
+
? serializeValueAsJson(
|
|
304
|
+
context.program,
|
|
305
|
+
prop.defaultValue,
|
|
306
|
+
prop.defaultValue.type,
|
|
307
|
+
)
|
|
308
|
+
: undefined,
|
|
309
|
+
)
|
|
310
|
+
);
|
|
311
|
+
})
|
|
312
|
+
.map((prop) => prop.name);
|
|
313
|
+
if (!clashes.length) return;
|
|
314
|
+
context.reportDiagnostic({
|
|
315
|
+
target: model,
|
|
316
|
+
format: {
|
|
317
|
+
model: model.name || "an anonymous model",
|
|
318
|
+
names: clashes.join(", "),
|
|
319
|
+
base: model.baseModel.name || "its base",
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
},
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
const untaggedQuestion = createRule({
|
|
328
|
+
name: "require-question-tags",
|
|
329
|
+
severity: "warning",
|
|
330
|
+
description: "A bank question with no tags cannot be found by browsing.",
|
|
331
|
+
messages: {
|
|
332
|
+
default: paramMessage`Question "${"id"}" has no @Meta.tag, so it appears under no heading in the question browser.`,
|
|
333
|
+
},
|
|
334
|
+
create(context) {
|
|
335
|
+
return {
|
|
336
|
+
root: (program) => {
|
|
337
|
+
for (const block of allBlocks(program)) {
|
|
338
|
+
if (block.kind !== "question" || block.tags.length) continue;
|
|
339
|
+
context.reportDiagnostic({
|
|
340
|
+
target: block.model,
|
|
341
|
+
format: { id: block.id },
|
|
342
|
+
});
|
|
343
|
+
}
|
|
344
|
+
},
|
|
345
|
+
};
|
|
346
|
+
},
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
/** Own and inherited properties, the derived declaration winning. */
|
|
350
|
+
function properties(model: Model): ModelProperty[] {
|
|
351
|
+
const out = new Map<string, ModelProperty>();
|
|
352
|
+
const chain: Model[] = [];
|
|
353
|
+
for (let m: Model | undefined = model; m; m = m.baseModel) chain.unshift(m);
|
|
354
|
+
for (const m of chain)
|
|
355
|
+
for (const prop of m.properties.values()) out.set(prop.name, prop);
|
|
356
|
+
return [...out.values()];
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
export const $linter = defineLinter({
|
|
360
|
+
rules: [
|
|
361
|
+
orphanQuestion,
|
|
362
|
+
questionDocs,
|
|
363
|
+
sectionUnused,
|
|
364
|
+
orderIncomplete,
|
|
365
|
+
redeclaredProperty,
|
|
366
|
+
untaggedQuestion,
|
|
367
|
+
],
|
|
368
|
+
ruleSets: {
|
|
369
|
+
recommended: {
|
|
370
|
+
enable: {
|
|
371
|
+
"simpler-forms/no-orphan-question": true,
|
|
372
|
+
"simpler-forms/require-question-docs": true,
|
|
373
|
+
"simpler-forms/section-unused": true,
|
|
374
|
+
"simpler-forms/order-incomplete": true,
|
|
375
|
+
"simpler-forms/no-redeclared-property": true,
|
|
376
|
+
"simpler-forms/require-question-tags": true,
|
|
377
|
+
},
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
});
|