simpler-forms 0.1.0-alpha.1 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +300 -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 +17 -0
- package/dist/src/decorators/mapping.js +122 -0
- package/dist/src/decorators/mapping.js.map +1 -0
- package/dist/src/decorators/meta.d.ts +15 -5
- package/dist/src/decorators/meta.js +31 -4
- package/dist/src/decorators/meta.js.map +1 -1
- package/dist/src/decorators/shared.d.ts +2 -2
- package/dist/src/decorators/shared.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 +55 -16
- 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 +10 -8
- package/dist/src/emitters/overlay.js +30 -105
- 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 +22 -8
- package/dist/src/index.js +17 -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 +585 -39
- package/dist/src/lib.js +258 -15
- package/dist/src/lib.js.map +1 -1
- package/dist/src/linter.js +8 -43
- package/dist/src/linter.js.map +1 -1
- package/dist/src/mapping-from.d.ts +21 -0
- package/dist/src/mapping-from.js +169 -0
- package/dist/src/mapping-from.js.map +1 -0
- package/dist/src/mapping.d.ts +65 -0
- package/dist/src/mapping.js +209 -0
- package/dist/src/mapping.js.map +1 -0
- package/dist/src/model.d.ts +143 -23
- package/dist/src/model.js +108 -21
- 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 +208 -58
- package/dist/src/validate.js.map +1 -1
- package/lib/main.tsp +1 -0
- package/lib/mapping.tsp +173 -0
- package/lib/meta.tsp +25 -6
- package/lib/types.tsp +24 -63
- package/lib/ui.tsp +34 -17
- package/package.json +17 -11
- package/src/casing.ts +172 -0
- package/src/decorators/index.ts +1 -0
- package/src/decorators/mapping.ts +200 -0
- package/src/decorators/meta.ts +56 -13
- package/src/decorators/shared.ts +6 -1
- package/src/decorators/ui.ts +43 -59
- package/src/emitter.ts +76 -16
- package/src/emitters/block-ui.ts +83 -135
- package/src/emitters/layout-ui.ts +280 -0
- package/src/emitters/overlay.ts +43 -116
- package/src/forms.ts +82 -0
- package/src/index.ts +17 -3
- package/src/layout.ts +592 -0
- package/src/lib.ts +264 -15
- package/src/linter.ts +17 -49
- package/src/mapping-from.ts +216 -0
- package/src/mapping.ts +358 -0
- package/src/model.ts +287 -46
- package/src/public.ts +60 -6
- package/src/validate.ts +221 -60
- 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/model.ts
CHANGED
|
@@ -14,6 +14,7 @@ import {
|
|
|
14
14
|
getMaxItems,
|
|
15
15
|
getMinItems,
|
|
16
16
|
} from "@typespec/compiler";
|
|
17
|
+
import type { Node } from "@typespec/compiler/ast";
|
|
17
18
|
import { stateKeys } from "./lib.js";
|
|
18
19
|
|
|
19
20
|
interface ConditionBase {
|
|
@@ -84,8 +85,11 @@ export type Condition = AtomicCondition | AnyCondition;
|
|
|
84
85
|
|
|
85
86
|
/** Everything the emitters need about one block, read out of decorator state. */
|
|
86
87
|
export interface Block {
|
|
87
|
-
/**
|
|
88
|
-
|
|
88
|
+
/**
|
|
89
|
+
* A Model for object-valued blocks, a Scalar for single-value questions, and an Enum for a
|
|
90
|
+
* code list published as a question of its own.
|
|
91
|
+
*/
|
|
92
|
+
model: Model | Scalar | Enum;
|
|
89
93
|
kind: "question" | "form";
|
|
90
94
|
/** True when the block is a single value rather than an object. */
|
|
91
95
|
scalar: boolean;
|
|
@@ -97,9 +101,7 @@ export interface Block {
|
|
|
97
101
|
responseRole?: ResponseRole;
|
|
98
102
|
label?: string;
|
|
99
103
|
doc?: string;
|
|
100
|
-
sections?: Enum;
|
|
101
104
|
order?: string[];
|
|
102
|
-
overrides: Record<string, Record<string, unknown>>;
|
|
103
105
|
}
|
|
104
106
|
|
|
105
107
|
export type ResponseRole =
|
|
@@ -125,18 +127,18 @@ function enumName(value: unknown): string | undefined {
|
|
|
125
127
|
|
|
126
128
|
export function readBlock(
|
|
127
129
|
program: Program,
|
|
128
|
-
model: Model | Scalar,
|
|
130
|
+
model: Model | Scalar | Enum,
|
|
129
131
|
): Block | undefined {
|
|
130
132
|
const q = g(program, stateKeys.questionMeta, model) as
|
|
131
133
|
Record<string, unknown> | undefined;
|
|
132
|
-
const
|
|
134
|
+
const s = g(program, stateKeys.formMeta, model) as
|
|
133
135
|
Record<string, unknown> | undefined;
|
|
134
|
-
if (!q && !
|
|
135
|
-
const meta = (q ??
|
|
136
|
+
if (!q && !s) return undefined;
|
|
137
|
+
const meta = (q ?? s)!;
|
|
136
138
|
return {
|
|
137
139
|
model,
|
|
138
140
|
kind: q ? "question" : "form",
|
|
139
|
-
scalar: model.kind
|
|
141
|
+
scalar: model.kind !== "Model",
|
|
140
142
|
id: String(meta.id),
|
|
141
143
|
meta,
|
|
142
144
|
classification: (enumName(meta.classification) ??
|
|
@@ -147,22 +149,51 @@ export function readBlock(
|
|
|
147
149
|
ResponseRole | undefined,
|
|
148
150
|
label: g(program, stateKeys.label, model) as string | undefined,
|
|
149
151
|
doc: getDoc(program, model),
|
|
150
|
-
sections: g(program, stateKeys.sections, model) as Enum | undefined,
|
|
151
152
|
order: g(program, stateKeys.order, model) as string[] | undefined,
|
|
152
|
-
overrides:
|
|
153
|
-
(g(program, stateKeys.overrides, model) as Record<
|
|
154
|
-
string,
|
|
155
|
-
Record<string, unknown>
|
|
156
|
-
>) ?? {},
|
|
157
153
|
};
|
|
158
154
|
}
|
|
159
155
|
|
|
156
|
+
/** A form's identity, from `@Meta.form`. */
|
|
157
|
+
export interface FormMeta {
|
|
158
|
+
id: string;
|
|
159
|
+
formName: string;
|
|
160
|
+
revision?: string;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** `@Meta.form` on a model, if it has one. */
|
|
164
|
+
export const modelFormMeta = (p: Program, model: Model) =>
|
|
165
|
+
g(p, stateKeys.formMeta, model) as FormMeta | undefined;
|
|
166
|
+
|
|
167
|
+
/** Every model with `@Meta.form`, in the order the decorators ran. */
|
|
168
|
+
export const allFormMetas = (p: Program) =>
|
|
169
|
+
p.stateMap(stateKeys.formMeta) as Map<Model, FormMeta>;
|
|
170
|
+
|
|
171
|
+
/** Every `@UI.layout` model, with the model it presents. */
|
|
172
|
+
export const allLayouts = (p: Program) =>
|
|
173
|
+
p.stateMap(stateKeys.layout) as Map<Model, Model>;
|
|
174
|
+
|
|
175
|
+
/** A `@UI.field` on a layout entry: the field it lays out, and the reference's syntax. */
|
|
176
|
+
export interface LayoutFieldRef {
|
|
177
|
+
field: ModelProperty;
|
|
178
|
+
node: Node;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export const entryField = (p: Program, prop: ModelProperty) =>
|
|
182
|
+
g(p, stateKeys.layoutField, prop) as LayoutFieldRef | undefined;
|
|
183
|
+
|
|
184
|
+
export const propFlatten = (p: Program, prop: ModelProperty) =>
|
|
185
|
+
g(p, stateKeys.flatten, prop) === true;
|
|
186
|
+
|
|
160
187
|
/** All blocks in the program, in declaration order. */
|
|
161
188
|
export function allBlocks(program: Program): Block[] {
|
|
162
189
|
const out: Block[] = [];
|
|
163
|
-
const seen = new Set<Model | Scalar>();
|
|
190
|
+
const seen = new Set<Model | Scalar | Enum>();
|
|
164
191
|
const walk = (ns: any): void => {
|
|
165
|
-
for (const m of [
|
|
192
|
+
for (const m of [
|
|
193
|
+
...ns.models.values(),
|
|
194
|
+
...ns.scalars.values(),
|
|
195
|
+
...ns.enums.values(),
|
|
196
|
+
]) {
|
|
166
197
|
if (seen.has(m)) continue;
|
|
167
198
|
seen.add(m);
|
|
168
199
|
const b = readBlock(program, m);
|
|
@@ -174,24 +205,69 @@ export function allBlocks(program: Program): Block[] {
|
|
|
174
205
|
return out;
|
|
175
206
|
}
|
|
176
207
|
|
|
208
|
+
/** A property's label: its own `@UI.label`, else the one its scalar, model or enum type declares. */
|
|
177
209
|
export const propLabel = (p: Program, prop: ModelProperty) =>
|
|
178
|
-
g(p, stateKeys.label, prop)
|
|
210
|
+
(g(p, stateKeys.label, prop) ??
|
|
211
|
+
(prop.type.kind === "Scalar" ||
|
|
212
|
+
prop.type.kind === "Model" ||
|
|
213
|
+
prop.type.kind === "Enum"
|
|
214
|
+
? g(p, stateKeys.label, prop.type)
|
|
215
|
+
: undefined)) as string | undefined;
|
|
179
216
|
export const propHelpText = (p: Program, prop: ModelProperty) =>
|
|
180
217
|
g(p, stateKeys.helpText, prop) as string | undefined;
|
|
181
|
-
|
|
182
|
-
|
|
218
|
+
/** A property's widget: its own `@UI.widget`, else its type's, or a base scalar's. */
|
|
219
|
+
export const propWidget = (p: Program, prop: ModelProperty) => {
|
|
220
|
+
const own = g(p, stateKeys.widget, prop) as string | undefined;
|
|
221
|
+
if (own) return own;
|
|
222
|
+
if (prop.type.kind === "Model")
|
|
223
|
+
return g(p, stateKeys.widget, prop.type) as string | undefined;
|
|
224
|
+
for (
|
|
225
|
+
let s = prop.type.kind === "Scalar" ? prop.type : undefined;
|
|
226
|
+
s;
|
|
227
|
+
s = s.baseScalar
|
|
228
|
+
) {
|
|
229
|
+
const widget = g(p, stateKeys.widget, s) as string | undefined;
|
|
230
|
+
if (widget) return widget;
|
|
231
|
+
}
|
|
232
|
+
return undefined;
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
/** Every enum a namespace declares with `@Meta.tags`: the tags the program catalogues by. */
|
|
236
|
+
export const declaredTagSets = (p: Program) =>
|
|
237
|
+
new Set(p.stateMap(stateKeys.tagSets).values() as Iterable<Enum>);
|
|
238
|
+
|
|
239
|
+
/** Each question or scalar given a `@Meta.tag`, with the enum members it named. */
|
|
240
|
+
export const tagMembers = (p: Program) =>
|
|
241
|
+
p.stateMap(stateKeys.tagMembers) as Map<Model | Scalar | Enum, EnumMember[]>;
|
|
242
|
+
|
|
243
|
+
/** Every enum a namespace declares with `@Meta.entities`: what questions are asked about. */
|
|
244
|
+
export const declaredEntitySets = (p: Program) =>
|
|
245
|
+
new Set(p.stateMap(stateKeys.entitySets).values() as Iterable<Enum>);
|
|
246
|
+
|
|
247
|
+
/** Each question that names an entity, with the enum member it named. */
|
|
248
|
+
export const entityMembers = (p: Program) =>
|
|
249
|
+
p.stateMap(stateKeys.entityMember) as Map<Model | Scalar | Enum, EnumMember>;
|
|
250
|
+
|
|
251
|
+
/** Every enum a namespace declares with `@UI.widgets`: the widgets the program provides. */
|
|
252
|
+
export const declaredWidgetSets = (p: Program) =>
|
|
253
|
+
new Set(p.stateMap(stateKeys.widgets).values() as Iterable<Enum>);
|
|
254
|
+
|
|
255
|
+
/** Each property given a `@UI.widget`, with the enum member it named. */
|
|
256
|
+
export const widgetMembers = (p: Program) =>
|
|
257
|
+
p.stateMap(stateKeys.widgetMember) as Map<
|
|
258
|
+
ModelProperty | Scalar | Model,
|
|
259
|
+
EnumMember
|
|
260
|
+
>;
|
|
183
261
|
export const propEncodedCheckboxGroup = (p: Program, prop: ModelProperty) =>
|
|
184
262
|
g(p, stateKeys.encodedCheckboxGroup, prop) as
|
|
185
263
|
Record<string, unknown> | undefined;
|
|
186
|
-
export const propSection = (p: Program, prop: ModelProperty) =>
|
|
187
|
-
g(p, stateKeys.section, prop) as { name: string; label?: string } | undefined;
|
|
188
264
|
export const propReadOnly = (p: Program, prop: ModelProperty) =>
|
|
189
265
|
g(p, stateKeys.readOnly, prop) === true;
|
|
190
266
|
export const propResponseRole = (p: Program, prop: ModelProperty) =>
|
|
191
267
|
g(p, stateKeys.responseRole, prop) as ResponseRole | undefined;
|
|
192
|
-
export const typeResponseRole = (p: Program, type: Model | Scalar) =>
|
|
268
|
+
export const typeResponseRole = (p: Program, type: Model | Scalar | Enum) =>
|
|
193
269
|
g(p, stateKeys.responseRole, type) as ResponseRole | undefined;
|
|
194
|
-
export const typeTags = (p: Program, type: Model | Scalar) =>
|
|
270
|
+
export const typeTags = (p: Program, type: Model | Scalar | Enum) =>
|
|
195
271
|
(g(p, stateKeys.tags, type) as string[]) ?? [];
|
|
196
272
|
export const propReadOnlyWhen = (p: Program, prop: ModelProperty) =>
|
|
197
273
|
(g(p, stateKeys.readOnlyWhen, prop) as Condition[]) ?? [];
|
|
@@ -205,6 +281,186 @@ export const propRequiredWhen = (p: Program, prop: ModelProperty) =>
|
|
|
205
281
|
(g(p, stateKeys.requiredWhen, prop) as Condition[]) ?? [];
|
|
206
282
|
export const propNotBefore = (p: Program, prop: ModelProperty) =>
|
|
207
283
|
g(p, stateKeys.notBefore, prop) as ModelProperty | undefined;
|
|
284
|
+
|
|
285
|
+
// --- mapping ----------------------------------------------------------------
|
|
286
|
+
|
|
287
|
+
/** One `@Mapping.to`: in `mapping`, the decorated type corresponds to `mappedTo`. */
|
|
288
|
+
export interface MappingLink {
|
|
289
|
+
mapping: EnumMember;
|
|
290
|
+
mappedTo: Model | ModelProperty;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** One `@Mapping.path`: in `mapping`, the dotted source path corresponds to `mappedTo`. */
|
|
294
|
+
export interface MappingPathLink {
|
|
295
|
+
mapping: EnumMember;
|
|
296
|
+
sourcePath: string;
|
|
297
|
+
mappedTo: ModelProperty;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** One `@Mapping.from`: in `mapping`, the decorated target takes its value from `source`. */
|
|
301
|
+
export interface MappingFromLink {
|
|
302
|
+
mapping: EnumMember;
|
|
303
|
+
source: Model | ModelProperty;
|
|
304
|
+
/** Which items of the list field `source` the link reads, when it names one with `Item` or `Slice`. */
|
|
305
|
+
select?: MappingSelection;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Which items of a list field a `@Mapping.from` link reads: one by `index` (`Mapping.Item`),
|
|
310
|
+
* or those from `start` to `stop`, excluded (`Mapping.Slice`). A negative number counts back
|
|
311
|
+
* from the end.
|
|
312
|
+
*/
|
|
313
|
+
export type MappingSelection =
|
|
314
|
+
{ index: number } | { start: number; stop?: number };
|
|
315
|
+
|
|
316
|
+
/** Every `@Mapping.from` on each target model and property, in every mapping. */
|
|
317
|
+
export const allMappingFromLinks = (p: Program) =>
|
|
318
|
+
p.stateMap(stateKeys.mappedFrom) as Map<
|
|
319
|
+
Model | ModelProperty,
|
|
320
|
+
MappingFromLink[]
|
|
321
|
+
>;
|
|
322
|
+
|
|
323
|
+
/** The fields a target property is filled from in `mapping`, one per form that fills it. */
|
|
324
|
+
export const propMappedFrom = (
|
|
325
|
+
p: Program,
|
|
326
|
+
prop: ModelProperty,
|
|
327
|
+
mapping: EnumMember,
|
|
328
|
+
) =>
|
|
329
|
+
((g(p, stateKeys.mappedFrom, prop) as MappingFromLink[] | undefined) ?? [])
|
|
330
|
+
.filter(
|
|
331
|
+
(link) =>
|
|
332
|
+
link.mapping === mapping && link.source.kind === "ModelProperty",
|
|
333
|
+
)
|
|
334
|
+
.map((link) => link.source as ModelProperty);
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* The links a target property is filled from in `mapping`, one per form that fills it: each
|
|
338
|
+
* field, and which of its items the link reads, if it names them with `Item` or `Slice`.
|
|
339
|
+
*/
|
|
340
|
+
export const propMappedFromLinks = (
|
|
341
|
+
p: Program,
|
|
342
|
+
prop: ModelProperty,
|
|
343
|
+
mapping: EnumMember,
|
|
344
|
+
) =>
|
|
345
|
+
(
|
|
346
|
+
(g(p, stateKeys.mappedFrom, prop) as MappingFromLink[] | undefined) ?? []
|
|
347
|
+
).filter(
|
|
348
|
+
(link) => link.mapping === mapping && link.source.kind === "ModelProperty",
|
|
349
|
+
) as (MappingFromLink & { source: ModelProperty })[];
|
|
350
|
+
|
|
351
|
+
/** Every enum a namespace declares with `@Mapping.targets`: the mappings the program produces. */
|
|
352
|
+
export const declaredMappingSets = (p: Program) =>
|
|
353
|
+
new Set(p.stateMap(stateKeys.mappingTargets).values() as Iterable<Enum>);
|
|
354
|
+
|
|
355
|
+
/** Every enum a namespace declares with `@Mapping.transforms`: the transformations emitters implement. */
|
|
356
|
+
export const declaredTransformSets = (p: Program) =>
|
|
357
|
+
new Set(
|
|
358
|
+
p.stateMap(stateKeys.mappingTransformSets).values() as Iterable<Enum>,
|
|
359
|
+
);
|
|
360
|
+
|
|
361
|
+
/** A target property's `@Mapping.transform`: the transformation, by the name an emitter implements it under. */
|
|
362
|
+
export interface MappingTransform {
|
|
363
|
+
/** The member's value, or its name when it has none. */
|
|
364
|
+
name: string;
|
|
365
|
+
member: EnumMember;
|
|
366
|
+
params?: Record<string, unknown>;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** Every target property with a `@Mapping.transform`. */
|
|
370
|
+
export const allTransformedProps = (p: Program) => [
|
|
371
|
+
...(p.stateMap(stateKeys.mappingTransform).keys() as Iterable<ModelProperty>),
|
|
372
|
+
];
|
|
373
|
+
|
|
374
|
+
/** The transformation `@Mapping.transform` gives a target property, if any. */
|
|
375
|
+
export function propTransform(
|
|
376
|
+
p: Program,
|
|
377
|
+
prop: ModelProperty,
|
|
378
|
+
): MappingTransform | undefined {
|
|
379
|
+
const stored = g(p, stateKeys.mappingTransform, prop) as
|
|
380
|
+
{ member: EnumMember; params?: Record<string, unknown> } | undefined;
|
|
381
|
+
if (!stored) return undefined;
|
|
382
|
+
const { member, params } = stored;
|
|
383
|
+
const name = typeof member.value === "string" ? member.value : member.name;
|
|
384
|
+
return { name, member, ...(params ? { params } : {}) };
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* A target property's `@Mapping.match`: the value each listed value is written as, and what
|
|
389
|
+
* an unlisted one is written as, a fixed `value` or a `field`'s, if anything.
|
|
390
|
+
*/
|
|
391
|
+
export interface MappingMatch {
|
|
392
|
+
cases: Record<string, string | number | boolean>;
|
|
393
|
+
default?: { value: string | number | boolean } | { field: ModelProperty };
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** The lookup `@Mapping.match` gives a target property, if any. */
|
|
397
|
+
export const propMatch = (p: Program, prop: ModelProperty) =>
|
|
398
|
+
g(p, stateKeys.mappingMatch, prop) as MappingMatch | undefined;
|
|
399
|
+
|
|
400
|
+
/** Whether `@Mapping.required` writes a target property even when its value is empty. */
|
|
401
|
+
export const propMappingRequired = (p: Program, prop: ModelProperty) =>
|
|
402
|
+
p.stateSet(stateKeys.mappingRequired).has(prop);
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* One of a target property's `@Mapping.when` conditions: the answer at `source` (or the
|
|
406
|
+
* item `select` picks of it) compared with `value` by `operator`, a member name of
|
|
407
|
+
* `SimplerForms.Operator`.
|
|
408
|
+
*/
|
|
409
|
+
export interface MappingCondition {
|
|
410
|
+
source: ModelProperty;
|
|
411
|
+
select?: MappingSelection;
|
|
412
|
+
operator: "equals" | "notEquals" | "in" | "contains" | "exists";
|
|
413
|
+
value?: unknown;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* The conditions `@Mapping.when` gives a target property, all of which must hold, in the
|
|
418
|
+
* order they're written. Decorators run from the bottom up, so they're stored reversed.
|
|
419
|
+
*/
|
|
420
|
+
export const propMappingConditions = (p: Program, prop: ModelProperty) =>
|
|
421
|
+
[
|
|
422
|
+
...((g(p, stateKeys.mappingWhen, prop) as MappingCondition[] | undefined) ??
|
|
423
|
+
[]),
|
|
424
|
+
].reverse();
|
|
425
|
+
|
|
426
|
+
/** Every member of a declared mapping set, in declaration order. */
|
|
427
|
+
export const mappingTargets = (p: Program): EnumMember[] =>
|
|
428
|
+
[...declaredMappingSets(p)].flatMap((e) => [...e.members.values()]);
|
|
429
|
+
|
|
430
|
+
/** Every `@Mapping.to` on each model and property, in every mapping. */
|
|
431
|
+
export const allMappingLinks = (p: Program) =>
|
|
432
|
+
p.stateMap(stateKeys.mappedTo) as Map<Model | ModelProperty, MappingLink[]>;
|
|
433
|
+
|
|
434
|
+
/** Every `@Mapping.path` on each model, in every mapping. */
|
|
435
|
+
export const allMappingPathLinks = (p: Program) =>
|
|
436
|
+
p.stateMap(stateKeys.mappingPaths) as Map<Model, MappingPathLink[]>;
|
|
437
|
+
|
|
438
|
+
/** The model a model corresponds to in `mapping`; the first, if it is linked twice. */
|
|
439
|
+
export const modelMappedTo = (p: Program, model: Model, mapping: EnumMember) =>
|
|
440
|
+
((g(p, stateKeys.mappedTo, model) as MappingLink[] | undefined) ?? []).find(
|
|
441
|
+
(link) => link.mapping === mapping && link.mappedTo.kind === "Model",
|
|
442
|
+
)?.mappedTo as Model | undefined;
|
|
443
|
+
|
|
444
|
+
/** The property a property corresponds to in `mapping`; the first, if it is linked twice. */
|
|
445
|
+
export const propMappedTo = (
|
|
446
|
+
p: Program,
|
|
447
|
+
prop: ModelProperty,
|
|
448
|
+
mapping: EnumMember,
|
|
449
|
+
) =>
|
|
450
|
+
((g(p, stateKeys.mappedTo, prop) as MappingLink[] | undefined) ?? []).find(
|
|
451
|
+
(link) =>
|
|
452
|
+
link.mapping === mapping && link.mappedTo.kind === "ModelProperty",
|
|
453
|
+
)?.mappedTo as ModelProperty | undefined;
|
|
454
|
+
|
|
455
|
+
/** A model's `@Mapping.path` links in `mapping`. */
|
|
456
|
+
export const modelMappingPaths = (
|
|
457
|
+
p: Program,
|
|
458
|
+
model: Model,
|
|
459
|
+
mapping: EnumMember,
|
|
460
|
+
) =>
|
|
461
|
+
(
|
|
462
|
+
(g(p, stateKeys.mappingPaths, model) as MappingPathLink[] | undefined) ?? []
|
|
463
|
+
).filter((link) => link.mapping === mapping);
|
|
208
464
|
export const propValidationConstraints = (p: Program, prop: ModelProperty) =>
|
|
209
465
|
(g(p, stateKeys.validationConstraints, prop) as
|
|
210
466
|
Record<string, unknown> | undefined) ?? {};
|
|
@@ -291,26 +547,6 @@ export const modelLabel = (p: Program, model: Model) =>
|
|
|
291
547
|
export const modelOrder = (p: Program, model: Model) =>
|
|
292
548
|
g(p, stateKeys.order, model) as string[] | undefined;
|
|
293
549
|
|
|
294
|
-
/**
|
|
295
|
-
* `@UI.overrides` for any model, block or not, keyed by dotted path relative to that model.
|
|
296
|
-
*
|
|
297
|
-
* A block's own table also reaches emitters through `Block.overrides`. This accessor exists
|
|
298
|
-
* for the other case: a form-local model composed by a form, whose overrides describe the
|
|
299
|
-
* thing being composed rather than one occurrence of it. A form asking for the same contact
|
|
300
|
-
* four times states such an override once, and an emitter rebases it onto each occurrence.
|
|
301
|
-
*/
|
|
302
|
-
export const modelOverrides = (p: Program, model: Model) =>
|
|
303
|
-
(g(p, stateKeys.overrides, model) as Record<
|
|
304
|
-
string,
|
|
305
|
-
Record<string, unknown>
|
|
306
|
-
>) ?? {};
|
|
307
|
-
|
|
308
|
-
export const propOverrides = (p: Program, prop: ModelProperty) =>
|
|
309
|
-
(g(p, stateKeys.overrides, prop) as Record<
|
|
310
|
-
string,
|
|
311
|
-
Record<string, unknown>
|
|
312
|
-
>) ?? {};
|
|
313
|
-
|
|
314
550
|
/** Own and inherited properties in declaration order, with the derived declaration winning. */
|
|
315
551
|
export function modelProperties(model: Model): ModelProperty[] {
|
|
316
552
|
const chain: Model[] = [];
|
|
@@ -333,7 +569,7 @@ export function modelProperties(model: Model): ModelProperty[] {
|
|
|
333
569
|
export function orderedProps(program: Program, block: Block): ModelProperty[] {
|
|
334
570
|
if (block.model.kind !== "Model") return [];
|
|
335
571
|
// A form may extend a question to preserve flat response fields with explicit semantic
|
|
336
|
-
// lineage. Its inherited fields remain
|
|
572
|
+
// lineage. Its inherited fields remain its members for presentation.
|
|
337
573
|
// Question artifacts themselves keep their local surface and compose their base through
|
|
338
574
|
// JSON Schema `allOf`, so walking inherited properties there would duplicate that surface.
|
|
339
575
|
const candidates =
|
|
@@ -360,8 +596,13 @@ export function orderedProps(program: Program, block: Block): ModelProperty[] {
|
|
|
360
596
|
*/
|
|
361
597
|
export function blockAncestry(
|
|
362
598
|
program: Program,
|
|
363
|
-
model: Model | Scalar,
|
|
599
|
+
model: Model | Scalar | Enum,
|
|
364
600
|
): string[] {
|
|
601
|
+
// An enum extends nothing: its ancestry is itself.
|
|
602
|
+
if (model.kind === "Enum") {
|
|
603
|
+
const block = readBlock(program, model);
|
|
604
|
+
return block ? [block.id] : [];
|
|
605
|
+
}
|
|
365
606
|
const out: string[] = [];
|
|
366
607
|
let current: Model | Scalar | undefined = model;
|
|
367
608
|
while (current) {
|
package/src/public.ts
CHANGED
|
@@ -24,6 +24,20 @@ export {
|
|
|
24
24
|
type Block,
|
|
25
25
|
} from "./model.js";
|
|
26
26
|
|
|
27
|
+
// --- forms ----------------------------------------------------------------
|
|
28
|
+
// Each `@Meta.form` is published as `forms/<id>/`, and a target emitter writing a form's
|
|
29
|
+
// artifacts beside the others writes them there.
|
|
30
|
+
export {
|
|
31
|
+
/** Every publishable `@Meta.form`, with the layout that presents it, if any. */
|
|
32
|
+
resolveForms,
|
|
33
|
+
type PublishedForm,
|
|
34
|
+
} from "./forms.js";
|
|
35
|
+
export {
|
|
36
|
+
type FormMeta,
|
|
37
|
+
/** Every `@UI.layout` model, with the model it presents. */
|
|
38
|
+
allLayouts,
|
|
39
|
+
} from "./model.js";
|
|
40
|
+
|
|
27
41
|
// --- conditions -----------------------------------------------------------
|
|
28
42
|
export {
|
|
29
43
|
/** The model a `countAtLeast` condition's source property belongs to. */
|
|
@@ -37,14 +51,10 @@ export {
|
|
|
37
51
|
type PresentCondition,
|
|
38
52
|
} from "./model.js";
|
|
39
53
|
|
|
40
|
-
/** An `enabledWhen` from `@UI.overrides`, normalized to the same shape as the decorator. */
|
|
41
|
-
export { normalizedOverrideEnabledWhen } from "./emitters/override-condition.js";
|
|
42
|
-
|
|
43
54
|
// --- model-level state ----------------------------------------------------
|
|
44
55
|
export {
|
|
45
56
|
modelLabel,
|
|
46
57
|
modelOrder,
|
|
47
|
-
modelOverrides,
|
|
48
58
|
modelAtLeastOneOf,
|
|
49
59
|
/** Own and inherited properties in declaration order, derived declaration winning. */
|
|
50
60
|
modelProperties,
|
|
@@ -63,10 +73,8 @@ export {
|
|
|
63
73
|
propHelpText,
|
|
64
74
|
propWidget,
|
|
65
75
|
propEncodedCheckboxGroup,
|
|
66
|
-
propSection,
|
|
67
76
|
propOmit,
|
|
68
77
|
propReadOnly,
|
|
69
|
-
propOverrides,
|
|
70
78
|
propResponseRole,
|
|
71
79
|
propVisibleWhen,
|
|
72
80
|
propEnabledWhen,
|
|
@@ -93,3 +101,49 @@ export {
|
|
|
93
101
|
cardinalityRequiredPathWhenPositiveDecimalString,
|
|
94
102
|
cardinalityPositiveDecimalStringWhenPathPresent,
|
|
95
103
|
} from "./model.js";
|
|
104
|
+
|
|
105
|
+
// --- mapping --------------------------------------------------------------
|
|
106
|
+
// A mapping emitter takes the members it emits as an option, checks them against
|
|
107
|
+
// `mappingTargets`, and resolves each form it emits once per member.
|
|
108
|
+
export {
|
|
109
|
+
/** Every member of every enum declared with `@Mapping.targets`. */
|
|
110
|
+
mappingTargets,
|
|
111
|
+
modelMappedTo,
|
|
112
|
+
propMappedTo,
|
|
113
|
+
modelMappingPaths,
|
|
114
|
+
propMappedFrom,
|
|
115
|
+
propMappedFromLinks,
|
|
116
|
+
propTransform,
|
|
117
|
+
propMatch,
|
|
118
|
+
propMappingRequired,
|
|
119
|
+
propMappingConditions,
|
|
120
|
+
declaredTransformSets,
|
|
121
|
+
type MappingLink,
|
|
122
|
+
type MappingPathLink,
|
|
123
|
+
type MappingFromLink,
|
|
124
|
+
type MappingSelection,
|
|
125
|
+
type MappingTransform,
|
|
126
|
+
type MappingMatch,
|
|
127
|
+
type MappingCondition,
|
|
128
|
+
} from "./model.js";
|
|
129
|
+
export {
|
|
130
|
+
/** A model's mapping in one target, resolved through every model it composes. */
|
|
131
|
+
resolveMapping,
|
|
132
|
+
type ResolvedMapping,
|
|
133
|
+
type MappingNode,
|
|
134
|
+
type MappingNodeKind,
|
|
135
|
+
} from "./mapping.js";
|
|
136
|
+
export {
|
|
137
|
+
/** A form's mapping read from the `@Mapping.from` links on its target: the same tree. */
|
|
138
|
+
resolveMappingFrom,
|
|
139
|
+
mappedFromRoot,
|
|
140
|
+
} from "./mapping-from.js";
|
|
141
|
+
|
|
142
|
+
// --- property casing ------------------------------------------------------
|
|
143
|
+
// A target emitter writing its own artifacts uses these to name properties as this one does.
|
|
144
|
+
export {
|
|
145
|
+
/** The name a property is emitted under, honoring `@encodedName("application/json")`. */
|
|
146
|
+
jsonPropertyName,
|
|
147
|
+
snakeCase,
|
|
148
|
+
type PropertyCasing,
|
|
149
|
+
} from "./casing.js";
|