simpler-forms 0.1.0-alpha.1 → 0.1.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/README.md +221 -76
- package/dist/src/casing.d.ts +43 -0
- package/dist/src/casing.js +146 -0
- package/dist/src/casing.js.map +1 -0
- package/dist/src/decorators/index.d.ts +1 -0
- package/dist/src/decorators/index.js +1 -0
- package/dist/src/decorators/index.js.map +1 -1
- package/dist/src/decorators/mapping.d.ts +11 -0
- package/dist/src/decorators/mapping.js +22 -0
- package/dist/src/decorators/mapping.js.map +1 -0
- package/dist/src/decorators/meta.d.ts +13 -3
- package/dist/src/decorators/meta.js +31 -4
- package/dist/src/decorators/meta.js.map +1 -1
- package/dist/src/decorators/ui.d.ts +14 -12
- package/dist/src/decorators/ui.js +20 -43
- package/dist/src/decorators/ui.js.map +1 -1
- package/dist/src/emitter.d.ts +8 -0
- package/dist/src/emitter.js +48 -13
- package/dist/src/emitter.js.map +1 -1
- package/dist/src/emitters/block-ui.d.ts +14 -3
- package/dist/src/emitters/block-ui.js +81 -119
- package/dist/src/emitters/block-ui.js.map +1 -1
- package/dist/src/emitters/layout-ui.d.ts +12 -0
- package/dist/src/emitters/layout-ui.js +225 -0
- package/dist/src/emitters/layout-ui.js.map +1 -0
- package/dist/src/emitters/overlay.d.ts +3 -6
- package/dist/src/emitters/overlay.js +3 -86
- package/dist/src/emitters/overlay.js.map +1 -1
- package/dist/src/forms.d.ts +23 -0
- package/dist/src/forms.js +54 -0
- package/dist/src/forms.js.map +1 -0
- package/dist/src/index.d.ts +15 -6
- package/dist/src/index.js +12 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/layout.d.ts +50 -0
- package/dist/src/layout.js +397 -0
- package/dist/src/layout.js.map +1 -0
- package/dist/src/lib.d.ts +485 -39
- package/dist/src/lib.js +215 -15
- package/dist/src/lib.js.map +1 -1
- package/dist/src/linter.js +2 -41
- package/dist/src/linter.js.map +1 -1
- package/dist/src/mapping-from.d.ts +20 -0
- package/dist/src/mapping-from.js +149 -0
- package/dist/src/mapping-from.js.map +1 -0
- package/dist/src/mapping.d.ts +58 -0
- package/dist/src/mapping.js +209 -0
- package/dist/src/mapping.js.map +1 -0
- package/dist/src/model.d.ts +69 -17
- package/dist/src/model.js +65 -19
- package/dist/src/model.js.map +1 -1
- package/dist/src/public.d.ts +20 -4
- package/dist/src/public.js +28 -4
- package/dist/src/public.js.map +1 -1
- package/dist/src/validate.d.ts +2 -1
- package/dist/src/validate.js +180 -58
- package/dist/src/validate.js.map +1 -1
- package/lib/main.tsp +1 -0
- package/lib/mapping.tsp +71 -0
- package/lib/meta.tsp +23 -4
- package/lib/types.tsp +13 -65
- package/lib/ui.tsp +34 -17
- package/package.json +7 -1
- package/src/casing.ts +172 -0
- package/src/decorators/index.ts +1 -0
- package/src/decorators/mapping.ts +57 -0
- package/src/decorators/meta.ts +50 -11
- package/src/decorators/ui.ts +43 -59
- package/src/emitter.ts +59 -13
- package/src/emitters/block-ui.ts +83 -135
- package/src/emitters/layout-ui.ts +280 -0
- package/src/emitters/overlay.ts +3 -103
- package/src/forms.ts +82 -0
- package/src/index.ts +12 -3
- package/src/layout.ts +592 -0
- package/src/lib.ts +221 -15
- package/src/linter.ts +1 -42
- package/src/mapping-from.ts +193 -0
- package/src/mapping.ts +351 -0
- package/src/model.ts +170 -37
- package/src/public.ts +50 -6
- package/src/validate.ts +188 -59
- package/dist/src/emitters/override-condition.d.ts +0 -12
- package/dist/src/emitters/override-condition.js +0 -25
- package/dist/src/emitters/override-condition.js.map +0 -1
- package/src/emitters/override-condition.ts +0 -37
package/src/lib.ts
CHANGED
|
@@ -1,4 +1,24 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
createTypeSpecLibrary,
|
|
3
|
+
paramMessage,
|
|
4
|
+
type JSONSchemaType,
|
|
5
|
+
} from "@typespec/compiler";
|
|
6
|
+
import type { FormSpecOptions } from "./emitter.js";
|
|
7
|
+
|
|
8
|
+
const EmitterOptionsSchema: JSONSchemaType<FormSpecOptions> = {
|
|
9
|
+
type: "object",
|
|
10
|
+
additionalProperties: false,
|
|
11
|
+
properties: {
|
|
12
|
+
"base-uri": { type: "string", nullable: true },
|
|
13
|
+
"property-casing": {
|
|
14
|
+
type: "string",
|
|
15
|
+
enum: ["preserve", "snake"],
|
|
16
|
+
nullable: true,
|
|
17
|
+
default: "preserve",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
required: [],
|
|
21
|
+
};
|
|
2
22
|
|
|
3
23
|
/**
|
|
4
24
|
* Named diagnostics for everything that makes an artifact wrong, and state keys for
|
|
@@ -18,7 +38,108 @@ export const $lib = createTypeSpecLibrary({
|
|
|
18
38
|
"form-scoped-question-id": {
|
|
19
39
|
severity: "error",
|
|
20
40
|
messages: {
|
|
21
|
-
default: paramMessage`Question id "${"id"}" names a form. Questions are named for what they mean;
|
|
41
|
+
default: paramMessage`Question id "${"id"}" names a form. Questions are named for what they mean; present a question differently in one form with that form's @UI.layout.`,
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
"layout-not-form": {
|
|
45
|
+
severity: "error",
|
|
46
|
+
messages: {
|
|
47
|
+
default: paramMessage`${"name"} presents ${"presented"}, which has no @Meta.form. A layout presents a form.`,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
"layout-duplicate": {
|
|
51
|
+
severity: "error",
|
|
52
|
+
messages: {
|
|
53
|
+
default: paramMessage`${"form"} is presented by both ${"first"} and ${"second"}. A form has one layout; remove one of them.`,
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
"layout-ref-root": {
|
|
57
|
+
severity: "error",
|
|
58
|
+
messages: {
|
|
59
|
+
default: paramMessage`${"ref"} starts from ${"root"}, but entries here lay out ${"here"}. Start it from ${"start"}, or from ${"presented"} for a path from the form.`,
|
|
60
|
+
list: paramMessage`${"ref"} is outside the list ${"list"}, whose entries are laid out here. Place it outside that list's @UI.field.`,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
"layout-entry-invalid": {
|
|
64
|
+
severity: "error",
|
|
65
|
+
messages: {
|
|
66
|
+
default: paramMessage`Layout entry ${"name"} is neither a field reference nor a model of entries. Give it a field as its type, e.g. \`${"name"}: Form.${"name"}\`, or a model of entries.`,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
"layout-unplaced": {
|
|
70
|
+
severity: "error",
|
|
71
|
+
messages: {
|
|
72
|
+
default: paramMessage`${"presented"}.${"field"} is not placed in any section of ${"layout"}. Place it, or @UI.omit it.`,
|
|
73
|
+
partial: paramMessage`Only ${"parts"} of ${"presented"}.${"field"} is placed in ${"layout"}. Place the field itself, or lay out the rest of it with @UI.field.`,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
"layout-placed-twice": {
|
|
77
|
+
severity: "error",
|
|
78
|
+
messages: {
|
|
79
|
+
default: paramMessage`${"path"} is placed twice in ${"layout"}.`,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
"layout-section-not-group": {
|
|
83
|
+
severity: "error",
|
|
84
|
+
messages: {
|
|
85
|
+
default: paramMessage`Top-level entries of ${"layout"} are sections. Give ${"name"} a model of entries, with @UI.field to lay out one field's contents.`,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
"layout-validation": {
|
|
89
|
+
severity: "error",
|
|
90
|
+
messages: {
|
|
91
|
+
default: paramMessage`@Validation.${"decorator"} on layout entry ${"name"} would make validity depend on the presentation. Declare it on ${"presented"}, which every layout of it shares.`,
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
"layout-condition-self": {
|
|
95
|
+
severity: "error",
|
|
96
|
+
messages: {
|
|
97
|
+
default: paramMessage`${"name"}'s condition reads ${"source"}, which is inside the field it controls.`,
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
"layout-condition-count": {
|
|
101
|
+
severity: "error",
|
|
102
|
+
messages: {
|
|
103
|
+
default: paramMessage`${"name"} has ${"count"} display conditions; an entry takes one.`,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
"layout-duplicate-label": {
|
|
107
|
+
severity: "warning",
|
|
108
|
+
messages: {
|
|
109
|
+
default: paramMessage`Two entries in ${"where"} render as "${"label"}" (${"first"} and ${"second"}).`,
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
"layout-entry-name": {
|
|
113
|
+
severity: "warning",
|
|
114
|
+
messages: {
|
|
115
|
+
default: paramMessage`${"name"} places ${"path"}. Name it for the field (${"field"}) or for a step on its path.`,
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
"layout-flatten-leaf": {
|
|
119
|
+
severity: "warning",
|
|
120
|
+
messages: {
|
|
121
|
+
default: paramMessage`${"name"} has no fields of its own to flatten.`,
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
"layout-condition-hides-required": {
|
|
125
|
+
severity: "warning",
|
|
126
|
+
messages: {
|
|
127
|
+
always: paramMessage`${"name"} is always required, but "${"condition"}" can hide or disable it.`,
|
|
128
|
+
narrower: paramMessage`${"name"} is required when ${"required"}, but this layout makes it usable only when ${"condition"}.`,
|
|
129
|
+
inverted: paramMessage`${"name"} is required when ${"condition"}, which is exactly when this layout hides or disables it.`,
|
|
130
|
+
default: paramMessage`${"name"} can be hidden while it is required.`,
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
"layout-condition-source-omitted": {
|
|
134
|
+
severity: "warning",
|
|
135
|
+
messages: {
|
|
136
|
+
default: paramMessage`${"name"} waits on ${"source"}, which this layout omits, so an applicant can never answer it.`,
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
"layout-condition-replaces": {
|
|
140
|
+
severity: "warning",
|
|
141
|
+
messages: {
|
|
142
|
+
default: paramMessage`${"name"}'s condition replaces the one ${"field"} declares itself. Restate that condition here if both should hold.`,
|
|
22
143
|
},
|
|
23
144
|
},
|
|
24
145
|
"duplicate-block-id": {
|
|
@@ -33,6 +154,84 @@ export const $lib = createTypeSpecLibrary({
|
|
|
33
154
|
default: paramMessage`"${"value"}" is not a member of ${"enumName"}, so this condition is constant -- never holding, or always holding if it is negated. Members: ${"members"}.`,
|
|
34
155
|
},
|
|
35
156
|
},
|
|
157
|
+
"mapping-target-undeclared": {
|
|
158
|
+
severity: "error",
|
|
159
|
+
messages: {
|
|
160
|
+
default: paramMessage`${"name"} is linked in mapping ${"mapping"} from ${"enumName"}, which no namespace declares with @Mapping.targets, so no emitter is known to produce it. Declare the program's mappings once, e.g. @Mapping.targets(${"enumName"}).`,
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
"mapping-duplicate": {
|
|
164
|
+
severity: "error",
|
|
165
|
+
messages: {
|
|
166
|
+
default: paramMessage`${"name"} has more than one @Mapping.to for mapping ${"mapping"}. A model or property corresponds to one thing per mapping.`,
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
"mapping-path-unresolved": {
|
|
170
|
+
severity: "error",
|
|
171
|
+
messages: {
|
|
172
|
+
default: paramMessage`@Mapping.path "${"path"}" on ${"name"} does not resolve: ${"reason"}.`,
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
"mapping-target-out-of-context": {
|
|
176
|
+
severity: "error",
|
|
177
|
+
messages: {
|
|
178
|
+
default: paramMessage`In mapping ${"mapping"}, ${"source"} is linked to ${"target"}, which is not a property of ${"context"}, the model its parent maps to. Link the parent to the model that holds it, or link this path from the composing model with @Mapping.path.`,
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
"mapping-type-mismatch": {
|
|
182
|
+
severity: "error",
|
|
183
|
+
messages: {
|
|
184
|
+
default: paramMessage`In mapping ${"mapping"}, ${"source"} is linked to ${"target"}, but ${"reason"}.`,
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
"mapping-cardinality-mismatch": {
|
|
188
|
+
severity: "error",
|
|
189
|
+
messages: {
|
|
190
|
+
default: paramMessage`In mapping ${"mapping"}, ${"source"} is ${"sourceShape"} but ${"target"} is ${"targetShape"}.`,
|
|
191
|
+
},
|
|
192
|
+
},
|
|
193
|
+
"mapping-target-duplicate": {
|
|
194
|
+
severity: "error",
|
|
195
|
+
messages: {
|
|
196
|
+
default: paramMessage`In mapping ${"mapping"}, both ${"first"} and ${"second"} are linked to ${"target"}. Each target property takes its value from one source.`,
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
"mapping-from-unresolved": {
|
|
200
|
+
severity: "error",
|
|
201
|
+
messages: {
|
|
202
|
+
default: paramMessage`In mapping ${"mapping"}, ${"target"} is filled from ${"source"}, which is not a field of ${"context"}, the field its parent is filled from.`,
|
|
203
|
+
},
|
|
204
|
+
},
|
|
205
|
+
"mapping-from-cardinality-mismatch": {
|
|
206
|
+
severity: "error",
|
|
207
|
+
messages: {
|
|
208
|
+
default: paramMessage`In mapping ${"mapping"}, ${"target"} is ${"targetShape"} but ${"source"}, which fills it, is ${"sourceShape"}.`,
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
"mapping-from-wrapper": {
|
|
212
|
+
severity: "warning",
|
|
213
|
+
messages: {
|
|
214
|
+
default: paramMessage`In mapping ${"mapping"}, ${"target"} has no field of its own: it wraps fields of ${"context"}. Rules keyed by the response's properties cannot write an element with no property behind it, so its contents are left out.`,
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
"tag-not-declared": {
|
|
218
|
+
severity: "error",
|
|
219
|
+
messages: {
|
|
220
|
+
default: paramMessage`${"name"} is tagged ${"tag"} from ${"enumName"}, which no namespace declares with @Meta.tags. Declare the program's tags once, e.g. @Meta.tags(${"enumName"}).`,
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
"entity-not-declared": {
|
|
224
|
+
severity: "error",
|
|
225
|
+
messages: {
|
|
226
|
+
default: paramMessage`${"name"} is asked about entity ${"entity"} from ${"enumName"}, which no namespace declares with @Meta.entities. Declare the program's entities once, e.g. @Meta.entities(${"enumName"}).`,
|
|
227
|
+
},
|
|
228
|
+
},
|
|
229
|
+
"widget-not-declared": {
|
|
230
|
+
severity: "error",
|
|
231
|
+
messages: {
|
|
232
|
+
default: paramMessage`${"name"} uses widget ${"widget"} from ${"enumName"}, which no namespace declares with @UI.widgets, so no renderer is known to provide it. Declare the program's widgets once, e.g. @UI.widgets(${"enumName"}).`,
|
|
233
|
+
},
|
|
234
|
+
},
|
|
36
235
|
"condition-path-unresolved": {
|
|
37
236
|
severity: "error",
|
|
38
237
|
messages: {
|
|
@@ -135,39 +334,42 @@ export const $lib = createTypeSpecLibrary({
|
|
|
135
334
|
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
335
|
},
|
|
137
336
|
},
|
|
138
|
-
"
|
|
337
|
+
"property-casing-collision": {
|
|
139
338
|
severity: "error",
|
|
140
339
|
messages: {
|
|
141
|
-
default: paramMessage
|
|
340
|
+
default: paramMessage`${"model"}.${"first"} and ${"model"}.${"second"} would both be emitted as "${"name"}". Rename one, or give one an @encodedName("application/json", ...).`,
|
|
142
341
|
},
|
|
143
342
|
},
|
|
144
|
-
"
|
|
343
|
+
"property-casing-inconsistent": {
|
|
145
344
|
severity: "error",
|
|
146
345
|
messages: {
|
|
147
|
-
default: paramMessage
|
|
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.`,
|
|
346
|
+
default: paramMessage`${"property"} would be emitted as "${"name"}", but ${"other"} as "${"otherName"}". A property name is emitted the same way everywhere, so give both the same @encodedName.`,
|
|
154
347
|
},
|
|
155
348
|
},
|
|
156
349
|
},
|
|
350
|
+
emitter: {
|
|
351
|
+
options: EmitterOptionsSchema,
|
|
352
|
+
},
|
|
157
353
|
state: {
|
|
158
354
|
questionMeta: {},
|
|
159
355
|
formMeta: {},
|
|
356
|
+
layout: {},
|
|
357
|
+
layoutField: {},
|
|
358
|
+
flatten: {},
|
|
359
|
+
tagSets: {},
|
|
160
360
|
tags: {},
|
|
361
|
+
tagMembers: {},
|
|
362
|
+
entitySets: {},
|
|
161
363
|
entity: {},
|
|
364
|
+
entityMember: {},
|
|
162
365
|
responseRole: {},
|
|
163
366
|
label: {},
|
|
164
367
|
helpText: {},
|
|
368
|
+
widgets: {},
|
|
165
369
|
widget: {},
|
|
370
|
+
widgetMember: {},
|
|
166
371
|
encodedCheckboxGroup: {},
|
|
167
|
-
sections: {},
|
|
168
|
-
section: {},
|
|
169
372
|
order: {},
|
|
170
|
-
overrides: {},
|
|
171
373
|
readOnly: {},
|
|
172
374
|
omit: {},
|
|
173
375
|
visibleWhen: {},
|
|
@@ -189,6 +391,10 @@ export const $lib = createTypeSpecLibrary({
|
|
|
189
391
|
calculationMaterialization: {},
|
|
190
392
|
evaluationOrder: {},
|
|
191
393
|
totals: {},
|
|
394
|
+
mappingTargets: {},
|
|
395
|
+
mappedTo: {},
|
|
396
|
+
mappingPaths: {},
|
|
397
|
+
mappedFrom: {},
|
|
192
398
|
},
|
|
193
399
|
} as const);
|
|
194
400
|
|
package/src/linter.ts
CHANGED
|
@@ -15,12 +15,10 @@ import {
|
|
|
15
15
|
import {
|
|
16
16
|
allBlocks,
|
|
17
17
|
modelOrder,
|
|
18
|
-
orderedProps,
|
|
19
18
|
propEnabledWhen,
|
|
20
19
|
propOmit,
|
|
21
20
|
propReadOnlyWhen,
|
|
22
21
|
propRequiredWhen,
|
|
23
|
-
propSection,
|
|
24
22
|
propValidationConstraintsWhen,
|
|
25
23
|
propVisibleWhen,
|
|
26
24
|
} from "./model.js";
|
|
@@ -127,43 +125,6 @@ const questionDocs = createRule({
|
|
|
127
125
|
},
|
|
128
126
|
});
|
|
129
127
|
|
|
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
128
|
const orderIncomplete = createRule({
|
|
168
129
|
name: "order-incomplete",
|
|
169
130
|
severity: "warning",
|
|
@@ -231,7 +192,7 @@ const redeclaredProperty = createRule({
|
|
|
231
192
|
severity: "warning",
|
|
232
193
|
description: "A derived block re-declaring a property it already inherits.",
|
|
233
194
|
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.
|
|
195
|
+
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. To present it differently in one form, use a @UI.layout.`,
|
|
235
196
|
},
|
|
236
197
|
create(context) {
|
|
237
198
|
return {
|
|
@@ -360,7 +321,6 @@ export const $linter = defineLinter({
|
|
|
360
321
|
rules: [
|
|
361
322
|
orphanQuestion,
|
|
362
323
|
questionDocs,
|
|
363
|
-
sectionUnused,
|
|
364
324
|
orderIncomplete,
|
|
365
325
|
redeclaredProperty,
|
|
366
326
|
untaggedQuestion,
|
|
@@ -370,7 +330,6 @@ export const $linter = defineLinter({
|
|
|
370
330
|
enable: {
|
|
371
331
|
"simpler-forms/no-orphan-question": true,
|
|
372
332
|
"simpler-forms/require-question-docs": true,
|
|
373
|
-
"simpler-forms/section-unused": true,
|
|
374
333
|
"simpler-forms/order-incomplete": true,
|
|
375
334
|
"simpler-forms/no-redeclared-property": true,
|
|
376
335
|
"simpler-forms/require-question-tags": true,
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
Diagnostic,
|
|
3
|
+
EnumMember,
|
|
4
|
+
Model,
|
|
5
|
+
ModelProperty,
|
|
6
|
+
Program,
|
|
7
|
+
} from "@typespec/compiler";
|
|
8
|
+
import { createDiagnostic } from "./lib.js";
|
|
9
|
+
import { type MappingNode, type ResolvedMapping, shapeOf } from "./mapping.js";
|
|
10
|
+
import {
|
|
11
|
+
allMappingFromLinks,
|
|
12
|
+
modelProperties,
|
|
13
|
+
propMappedFrom,
|
|
14
|
+
} from "./model.js";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `form`'s mapping in `mapping`, read from the `@Mapping.from` links on its target, or
|
|
18
|
+
* undefined if no target model names `form`.
|
|
19
|
+
*
|
|
20
|
+
* The result is the tree `resolveMapping` builds from `@Mapping.to`, keyed by the form's
|
|
21
|
+
* properties, so an emitter can read either. Each target property is filled from the field
|
|
22
|
+
* its `@Mapping.from` names, found among the fields of the one its parent is filled from; one
|
|
23
|
+
* found below a field with no element of its own lands in a group. A target property with
|
|
24
|
+
* no `@Mapping.from` whose own properties are filled is a wrapper element, which a tree keyed
|
|
25
|
+
* by the form's properties cannot hold: it is left out, with a `mapping-from-wrapper` problem
|
|
26
|
+
* for an emitter whose output cannot hold it either to report.
|
|
27
|
+
*/
|
|
28
|
+
export function resolveMappingFrom(
|
|
29
|
+
program: Program,
|
|
30
|
+
form: Model,
|
|
31
|
+
mapping: EnumMember,
|
|
32
|
+
): { resolved: ResolvedMapping; problems: Diagnostic[] } | undefined {
|
|
33
|
+
const target = mappedFromRoot(program, form, mapping);
|
|
34
|
+
if (!target) return undefined;
|
|
35
|
+
const problems: Diagnostic[] = [];
|
|
36
|
+
|
|
37
|
+
const walk = (
|
|
38
|
+
into: Model,
|
|
39
|
+
from: Model,
|
|
40
|
+
prefix: string[],
|
|
41
|
+
stack: Model[],
|
|
42
|
+
): MappingNode[] => {
|
|
43
|
+
const nodes: MappingNode[] = [];
|
|
44
|
+
for (const element of modelProperties(into)) {
|
|
45
|
+
const to = shapeOf(element.type);
|
|
46
|
+
const sources = propMappedFrom(program, element, mapping);
|
|
47
|
+
if (!sources.length) {
|
|
48
|
+
if (!to.object || to.array || stack.includes(to.object)) continue;
|
|
49
|
+
// Structure: its own elements are filled from the same field as its parent.
|
|
50
|
+
const inner = walk(to.object, from, prefix, [...stack, to.object]);
|
|
51
|
+
if (inner.length)
|
|
52
|
+
problems.push(
|
|
53
|
+
createDiagnostic({
|
|
54
|
+
code: "mapping-from-wrapper",
|
|
55
|
+
target: element,
|
|
56
|
+
format: {
|
|
57
|
+
mapping: mapping.name,
|
|
58
|
+
target: describe(element),
|
|
59
|
+
context: from.name || "an anonymous model",
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const path = sources
|
|
66
|
+
.map((source) => pathTo(from, source))
|
|
67
|
+
.find((found) => found !== undefined);
|
|
68
|
+
if (!path) {
|
|
69
|
+
problems.push(
|
|
70
|
+
createDiagnostic({
|
|
71
|
+
code: "mapping-from-unresolved",
|
|
72
|
+
target: element,
|
|
73
|
+
format: {
|
|
74
|
+
mapping: mapping.name,
|
|
75
|
+
target: describe(element),
|
|
76
|
+
source: sources.map(describe).join(" or "),
|
|
77
|
+
context: from.name || "an anonymous model",
|
|
78
|
+
},
|
|
79
|
+
}),
|
|
80
|
+
);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
const source = path.at(-1)!;
|
|
84
|
+
const sourcePath = [...prefix, ...path.map((p) => p.name)];
|
|
85
|
+
const shape = shapeOf(source.type);
|
|
86
|
+
if (shape.array !== to.array) {
|
|
87
|
+
problems.push(
|
|
88
|
+
createDiagnostic({
|
|
89
|
+
code: "mapping-from-cardinality-mismatch",
|
|
90
|
+
target: element,
|
|
91
|
+
format: {
|
|
92
|
+
mapping: mapping.name,
|
|
93
|
+
target: describe(element),
|
|
94
|
+
source: sourcePath.join("."),
|
|
95
|
+
targetShape: to.array ? "a list" : "a single value",
|
|
96
|
+
sourceShape: shape.array ? "a list" : "a single value",
|
|
97
|
+
},
|
|
98
|
+
}),
|
|
99
|
+
);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const children =
|
|
103
|
+
shape.object && to.object && !stack.includes(to.object)
|
|
104
|
+
? walk(to.object, shape.object, sourcePath, [...stack, to.object])
|
|
105
|
+
: [];
|
|
106
|
+
insert(nodes, path, prefix, {
|
|
107
|
+
sourcePath,
|
|
108
|
+
source,
|
|
109
|
+
target: element,
|
|
110
|
+
kind: shape.array ? "array" : shape.object ? "object" : "leaf",
|
|
111
|
+
children,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
return nodes;
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const children = walk(target, form, [], [target]);
|
|
118
|
+
return {
|
|
119
|
+
resolved: { mapping, source: form, target, children },
|
|
120
|
+
problems,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** The target model that names `form` with `@Mapping.from` in `mapping`. */
|
|
125
|
+
export function mappedFromRoot(
|
|
126
|
+
program: Program,
|
|
127
|
+
form: Model,
|
|
128
|
+
mapping: EnumMember,
|
|
129
|
+
): Model | undefined {
|
|
130
|
+
for (const [target, links] of allMappingFromLinks(program))
|
|
131
|
+
if (
|
|
132
|
+
target.kind === "Model" &&
|
|
133
|
+
links.some((link) => link.mapping === mapping && link.source === form)
|
|
134
|
+
)
|
|
135
|
+
return target;
|
|
136
|
+
return undefined;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* The properties from `model` to `field`, `field` last: a property of `model` itself, or one
|
|
141
|
+
* below an object property of it with no element of its own. A copy made by spread or `is`
|
|
142
|
+
* counts as the property it was copied from.
|
|
143
|
+
*/
|
|
144
|
+
function pathTo(
|
|
145
|
+
model: Model,
|
|
146
|
+
field: ModelProperty,
|
|
147
|
+
seen: Model[] = [],
|
|
148
|
+
): ModelProperty[] | undefined {
|
|
149
|
+
for (const prop of modelProperties(model))
|
|
150
|
+
if (copies(prop, field)) return [prop];
|
|
151
|
+
for (const prop of modelProperties(model)) {
|
|
152
|
+
const shape = shapeOf(prop.type);
|
|
153
|
+
if (!shape.object || shape.array || seen.includes(shape.object)) continue;
|
|
154
|
+
const below = pathTo(shape.object, field, [...seen, model]);
|
|
155
|
+
if (below) return [prop, ...below];
|
|
156
|
+
}
|
|
157
|
+
return undefined;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const copies = (prop: ModelProperty, field: ModelProperty) => {
|
|
161
|
+
for (let p: ModelProperty | undefined = prop; p; p = p.sourceProperty)
|
|
162
|
+
if (p === field) return true;
|
|
163
|
+
return false;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
/** Adds `node` under group nodes for each property `path` passes through on the way to it. */
|
|
167
|
+
function insert(
|
|
168
|
+
nodes: MappingNode[],
|
|
169
|
+
path: ModelProperty[],
|
|
170
|
+
prefix: string[],
|
|
171
|
+
node: MappingNode,
|
|
172
|
+
): void {
|
|
173
|
+
let level = nodes;
|
|
174
|
+
const steps = [...prefix];
|
|
175
|
+
for (const prop of path.slice(0, -1)) {
|
|
176
|
+
steps.push(prop.name);
|
|
177
|
+
let group = level.find((n) => n.kind === "group" && n.source === prop);
|
|
178
|
+
if (!group) {
|
|
179
|
+
group = {
|
|
180
|
+
sourcePath: [...steps],
|
|
181
|
+
source: prop,
|
|
182
|
+
kind: "group",
|
|
183
|
+
children: [],
|
|
184
|
+
};
|
|
185
|
+
level.push(group);
|
|
186
|
+
}
|
|
187
|
+
level = group.children;
|
|
188
|
+
}
|
|
189
|
+
level.push(node);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const describe = (prop: ModelProperty) =>
|
|
193
|
+
prop.model?.name ? `${prop.model.name}.${prop.name}` : prop.name;
|