@delurio/cli 0.10.0 → 0.12.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/CHANGELOG.md +15 -0
- package/NOTICE +7 -0
- package/README.md +38 -22
- package/SPEC.md +8 -1
- package/js/changes.d.ts +17 -0
- package/js/cli.js +6 -0
- package/{dist → js}/edges.d.ts +2 -2
- package/js/edges.js +40 -0
- package/js/expr.js +25 -0
- package/{dist → js}/graph.d.ts +3 -0
- package/js/graph.js +73 -0
- package/{dist → js}/html.d.ts +6 -6
- package/js/html.js +13 -0
- package/js/internal.js +408 -0
- package/js/kit.js +54 -0
- package/{dist → js}/model.d.ts +2 -2
- package/js/model.js +136 -0
- package/js/native.cjs +46 -0
- package/js/native.d.cts +328 -0
- package/{dist → js}/nav.d.ts +1 -1
- package/js/nav.js +70 -0
- package/js/needs.js +30 -0
- package/{dist → js}/spine.d.ts +3 -3
- package/js/spine.js +22 -0
- package/{dist → js}/starts.d.ts +1 -3
- package/{dist → js}/story.d.ts +2 -2
- package/js/story.js +22 -0
- package/{dist → js}/wireframe.d.ts +2 -2
- package/js/wireframe.js +29 -0
- package/js/yaml.d.ts +20 -0
- package/js/yaml.js +40 -0
- package/package.json +40 -24
- package/schemas/binding.schema.json +2211 -0
- package/schemas/ui.schema.json +6 -0
- package/dist/anchors.d.ts +0 -29
- package/dist/anchors.js +0 -160
- package/dist/changes.d.ts +0 -25
- package/dist/changes.js +0 -101
- package/dist/check.d.ts +0 -23
- package/dist/check.js +0 -649
- package/dist/cli.d.ts +0 -2
- package/dist/cli.js +0 -194
- package/dist/compare.d.ts +0 -34
- package/dist/compare.js +0 -143
- package/dist/diff-flow.d.ts +0 -33
- package/dist/diff-flow.js +0 -547
- package/dist/diff-nav.d.ts +0 -20
- package/dist/diff-nav.js +0 -401
- package/dist/diff.d.ts +0 -25
- package/dist/diff.js +0 -331
- package/dist/domain-names.d.ts +0 -18
- package/dist/domain-names.js +0 -124
- package/dist/domain.d.ts +0 -25
- package/dist/domain.js +0 -110
- package/dist/edges.js +0 -120
- package/dist/expr.js +0 -157
- package/dist/graph.js +0 -236
- package/dist/html.js +0 -311
- package/dist/index-page.d.ts +0 -12
- package/dist/index-page.js +0 -50
- package/dist/invokes.d.ts +0 -10
- package/dist/invokes.js +0 -67
- package/dist/kit.js +0 -711
- package/dist/mermaid.d.ts +0 -3
- package/dist/mermaid.js +0 -50
- package/dist/model.js +0 -608
- package/dist/nav-page.d.ts +0 -15
- package/dist/nav-page.js +0 -283
- package/dist/nav.js +0 -578
- package/dist/needs.js +0 -335
- package/dist/pan.d.ts +0 -2
- package/dist/pan.js +0 -252
- package/dist/schemas.d.ts +0 -15
- package/dist/schemas.js +0 -316
- package/dist/spine.js +0 -156
- package/dist/starts.js +0 -88
- package/dist/story.js +0 -109
- package/dist/tutorial-check.d.ts +0 -9
- package/dist/tutorial-check.js +0 -297
- package/dist/ui-check.d.ts +0 -8
- package/dist/ui-check.js +0 -142
- package/dist/watch.d.ts +0 -5
- package/dist/watch.js +0 -114
- package/dist/wireframe.js +0 -276
- package/dist/yaml.d.ts +0 -19
- package/dist/yaml.js +0 -134
- /package/{dist → js}/expr.d.ts +0 -0
- /package/{dist → js}/kit.d.ts +0 -0
- /package/{dist → js}/needs.d.ts +0 -0
package/dist/wireframe.js
DELETED
|
@@ -1,276 +0,0 @@
|
|
|
1
|
-
import { shownIn, titleOf } from './graph.js';
|
|
2
|
-
import { drawWith, mark } from './kit.js';
|
|
3
|
-
const esc = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
4
|
-
function fill(g, key, warn) {
|
|
5
|
-
const raw = g.catalog[key];
|
|
6
|
-
if (raw === undefined) {
|
|
7
|
-
warn(`catalog key ${key} is missing`);
|
|
8
|
-
return `⟨missing ${key}⟩`;
|
|
9
|
-
}
|
|
10
|
-
return raw.replace(/\{(\w+)\}/g, (_m, p) => {
|
|
11
|
-
const sample = g.ui.samples[p];
|
|
12
|
-
if (sample === undefined) {
|
|
13
|
-
warn(`no sample value for {${p}} in ${key}`);
|
|
14
|
-
return `⟨${p}⟩`;
|
|
15
|
-
}
|
|
16
|
-
return sample;
|
|
17
|
-
});
|
|
18
|
-
}
|
|
19
|
-
function texts(g, n, warn) {
|
|
20
|
-
const t = n.text;
|
|
21
|
-
if (t === undefined)
|
|
22
|
-
return [];
|
|
23
|
-
if (t.key !== undefined)
|
|
24
|
-
return [fill(g, t.key, warn)];
|
|
25
|
-
if (t.oneOf !== undefined)
|
|
26
|
-
return t.oneOf.map((k) => fill(g, k, warn));
|
|
27
|
-
if (t.literal !== undefined)
|
|
28
|
-
return [t.literal];
|
|
29
|
-
return [];
|
|
30
|
-
}
|
|
31
|
-
function accessibleName(g, n, warn) {
|
|
32
|
-
const nm = n.name;
|
|
33
|
-
if (nm?.key !== undefined)
|
|
34
|
-
return fill(g, nm.key, warn);
|
|
35
|
-
if (nm?.literal !== undefined)
|
|
36
|
-
return nm.literal;
|
|
37
|
-
if (nm?.labelledBy !== undefined) {
|
|
38
|
-
const ref = g.ui.nodes[nm.labelledBy];
|
|
39
|
-
return ref === undefined ? undefined : texts(g, ref, warn).join(' ');
|
|
40
|
-
}
|
|
41
|
-
if (n.role === 'button' || n.role === 'heading' || n.role === 'status' || n.role === 'alert')
|
|
42
|
-
return texts(g, n, warn)[0];
|
|
43
|
-
// a checkbox, a radio and a select are named by their label, the words beside them (SPEC.md §4)
|
|
44
|
-
if (LABELLED.has(n.component ?? ''))
|
|
45
|
-
return texts(g, n, warn)[0];
|
|
46
|
-
return undefined;
|
|
47
|
-
}
|
|
48
|
-
const LABELLED = new Set(['Checkbox', 'Radio', 'Select']);
|
|
49
|
-
// the words a field holds when it is shown (`value`), for a select the option it shows chosen
|
|
50
|
-
function valueWords(g, n, warn) {
|
|
51
|
-
const v = n.value;
|
|
52
|
-
if (v === undefined)
|
|
53
|
-
return '';
|
|
54
|
-
if (v.literal !== undefined)
|
|
55
|
-
return v.literal;
|
|
56
|
-
const key = v.key ?? v.oneOf?.[0];
|
|
57
|
-
return key === undefined ? '' : fill(g, key, warn);
|
|
58
|
-
}
|
|
59
|
-
function chips(it) {
|
|
60
|
-
const n = it.node;
|
|
61
|
-
const parts = [n.role ?? n.type];
|
|
62
|
-
if (n.emits !== undefined)
|
|
63
|
-
parts.push(`emits ${n.emits}`);
|
|
64
|
-
if (it.disabled)
|
|
65
|
-
parts.push('disabled');
|
|
66
|
-
else if (n.disabledWhen !== undefined)
|
|
67
|
-
parts.push(`disabled when ${n.disabledWhen}`);
|
|
68
|
-
if (n.when !== undefined)
|
|
69
|
-
parts.push(`when ${n.when}`);
|
|
70
|
-
if (it.focus)
|
|
71
|
-
parts.push('first focus');
|
|
72
|
-
return `<span class="chip">${esc(parts.join(' · '))}</span>`;
|
|
73
|
-
}
|
|
74
|
-
function renderWireframe(g, state, items, warn, clean = false, kit = null) {
|
|
75
|
-
// clean: no annotation chips and no dashed or focus outlines, for the tutorial stage
|
|
76
|
-
const item = (it, inner) => {
|
|
77
|
-
const cls = clean ? 'wf-item' : ['wf-item', it.node.when !== undefined ? 'wf-cond' : '', it.focus ? 'wf-focus' : ''].filter((c) => c !== '').join(' ');
|
|
78
|
-
return `<div class="${cls}" data-node="${esc(it.id)}">${inner}${clean ? '' : chips(it)}</div>`;
|
|
79
|
-
};
|
|
80
|
-
const out = [];
|
|
81
|
-
let inList = false;
|
|
82
|
-
let inRow = false;
|
|
83
|
-
let kitList = null; // a List drawn with the kit waits for its items
|
|
84
|
-
const closeList = () => {
|
|
85
|
-
if (kitList !== null)
|
|
86
|
-
out.push(drawKitList(kitList.it, kitList.items));
|
|
87
|
-
else if (inList)
|
|
88
|
-
out.push('</ul>');
|
|
89
|
-
kitList = null;
|
|
90
|
-
inList = false;
|
|
91
|
-
};
|
|
92
|
-
const fill = (it, text) => {
|
|
93
|
-
const n = it.node;
|
|
94
|
-
const name = accessibleName(g, n, warn);
|
|
95
|
-
// the role the expected tree gives the node, which picks a template from the component's `roles`
|
|
96
|
-
const role = n.role ?? (n.type === 'text' ? undefined : n.type);
|
|
97
|
-
return { id: it.id, text, name: name ?? text, intent: n.intent, role, disabled: it.disabled, named: name !== undefined && name !== text, conditional: n.when !== undefined, checked: n.checked === true };
|
|
98
|
-
};
|
|
99
|
-
const drawKitList = (it, drawn) => {
|
|
100
|
-
const f = { ...fill(it, texts(g, it.node, warn)[0] ?? ''), children: drawn.map((d) => `\n${d}`).join('') + '\n' };
|
|
101
|
-
return mark(drawWith(kit, 'List', f, warn) ?? '', it.id, '');
|
|
102
|
-
};
|
|
103
|
-
const closeRow = () => {
|
|
104
|
-
if (inRow)
|
|
105
|
-
out.push('</div>');
|
|
106
|
-
inRow = false;
|
|
107
|
-
};
|
|
108
|
-
for (const it of items) {
|
|
109
|
-
const n = it.node;
|
|
110
|
-
const t = texts(g, n, warn);
|
|
111
|
-
const text = esc(t[0] ?? '');
|
|
112
|
-
if (n.component !== 'ListItem')
|
|
113
|
-
closeList();
|
|
114
|
-
if (n.component !== 'Button')
|
|
115
|
-
closeRow();
|
|
116
|
-
if (kit !== null && n.component !== undefined && n.component !== 'Page') {
|
|
117
|
-
if (kit.components[n.component] !== undefined) {
|
|
118
|
-
if (n.component === 'List') {
|
|
119
|
-
kitList = { it, items: [] };
|
|
120
|
-
inList = true;
|
|
121
|
-
}
|
|
122
|
-
else if (n.component === 'ListItem') {
|
|
123
|
-
if (!inList)
|
|
124
|
-
warn(`${it.id}: ListItem outside a List in ${state}`);
|
|
125
|
-
for (const line of t)
|
|
126
|
-
(kitList?.items ?? out).push(mark(drawWith(kit, 'ListItem', fill(it, line), warn) ?? '', it.id, clean ? '' : chips(it)));
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
if (n.component === 'Button' && !inRow)
|
|
130
|
-
out.push('<div class="wf-row">');
|
|
131
|
-
if (n.component === 'Button')
|
|
132
|
-
inRow = true;
|
|
133
|
-
out.push(item(it, drawWith(kit, n.component, fill(it, t[0] ?? ''), warn) ?? ''));
|
|
134
|
-
}
|
|
135
|
-
continue;
|
|
136
|
-
}
|
|
137
|
-
warn(`kit ${kit.id} has no template for ${n.component} (node ${it.id}); drawn with the fixed component`);
|
|
138
|
-
}
|
|
139
|
-
switch (n.component) {
|
|
140
|
-
case 'Page':
|
|
141
|
-
break;
|
|
142
|
-
case 'Heading':
|
|
143
|
-
out.push(item(it, `<h4 class="wf-h">${text}</h4>`));
|
|
144
|
-
break;
|
|
145
|
-
case 'Text':
|
|
146
|
-
out.push(item(it, `<p class="wf-t">${text}</p>`));
|
|
147
|
-
break;
|
|
148
|
-
case 'Button':
|
|
149
|
-
if (!inRow)
|
|
150
|
-
out.push('<div class="wf-row">');
|
|
151
|
-
inRow = true;
|
|
152
|
-
out.push(item(it, `<button type="button" class="wf-btn wf-${esc(n.intent ?? 'secondary')}"${it.disabled ? ' disabled' : ''}>${text}</button>`));
|
|
153
|
-
break;
|
|
154
|
-
case 'LinkButton':
|
|
155
|
-
out.push(item(it, `<button type="button" class="wf-link"${it.disabled ? ' disabled' : ''}>${text}</button>`));
|
|
156
|
-
break;
|
|
157
|
-
case 'TextField':
|
|
158
|
-
// the visible text is the label; the input is named by it, as a <label> around it does
|
|
159
|
-
out.push(item(it, `<label class="wf-field"><span>${text}</span><input type="text" class="wf-input" tabindex="-1"${it.disabled ? ' disabled' : ''}></label>`));
|
|
160
|
-
break;
|
|
161
|
-
case 'Checkbox':
|
|
162
|
-
case 'Radio':
|
|
163
|
-
// the box beside its label, as a <label> around an <input> draws it
|
|
164
|
-
out.push(item(it, `<label class="wf-check"><input type="${n.component === 'Radio' ? 'radio' : 'checkbox'}" tabindex="-1"${n.checked === true ? ' checked' : ''}${it.disabled ? ' disabled' : ''}><span>${text}</span></label>`));
|
|
165
|
-
break;
|
|
166
|
-
case 'Select':
|
|
167
|
-
out.push(item(it, `<label class="wf-field"><span>${text}</span><select class="wf-input" tabindex="-1"${it.disabled ? ' disabled' : ''}><option>${esc(valueWords(g, n, warn))}</option></select></label>`));
|
|
168
|
-
break;
|
|
169
|
-
case 'List':
|
|
170
|
-
out.push(`<ul class="wf-list" data-node="${esc(it.id)}">`);
|
|
171
|
-
inList = true;
|
|
172
|
-
break;
|
|
173
|
-
case 'ListItem':
|
|
174
|
-
if (!inList)
|
|
175
|
-
warn(`${it.id}: ListItem outside a List in ${state}`);
|
|
176
|
-
for (const line of t)
|
|
177
|
-
(kitList?.items ?? out).push(`<li data-node="${esc(it.id)}">${esc(line)}${clean ? '' : chips(it)}</li>`);
|
|
178
|
-
break;
|
|
179
|
-
case 'Status':
|
|
180
|
-
out.push(item(it, `<div class="wf-status" role="status">${text}</div>`));
|
|
181
|
-
break;
|
|
182
|
-
case 'Alert':
|
|
183
|
-
out.push(item(it, `<div class="wf-alert" role="alert">${text}</div>`));
|
|
184
|
-
break;
|
|
185
|
-
default:
|
|
186
|
-
warn(`${it.id}: component ${n.component ?? '(none)'} has no wireframe renderer`);
|
|
187
|
-
out.push(item(it, `<div class="wf-unmapped">${esc(n.component ?? n.type)} ${esc(it.id)}</div>`));
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
closeList();
|
|
191
|
-
closeRow();
|
|
192
|
-
return out.join('\n');
|
|
193
|
-
}
|
|
194
|
-
function renderTree(g, items, warn) {
|
|
195
|
-
const lines = [];
|
|
196
|
-
for (const it of items) {
|
|
197
|
-
const n = it.node;
|
|
198
|
-
const depth = n.component === 'Page' ? 0 : n.component === 'ListItem' ? 2 : 1;
|
|
199
|
-
const flags = [it.disabled ? 'disabled' : n.disabledWhen !== undefined ? `disabled when ${n.disabledWhen}` : '', n.when !== undefined ? `when ${n.when}` : '', it.focus ? 'first focus' : '', n.emits !== undefined ? `→ ${n.emits}` : '']
|
|
200
|
-
.filter((f) => f !== '')
|
|
201
|
-
.join(', ');
|
|
202
|
-
const suffix = flags === '' ? '' : ` [${flags}]`;
|
|
203
|
-
const pad = ' '.repeat(depth);
|
|
204
|
-
if (n.component === 'ListItem') {
|
|
205
|
-
for (const line of texts(g, n, warn))
|
|
206
|
-
lines.push(`${pad}listitem "${line}"${suffix}`);
|
|
207
|
-
continue;
|
|
208
|
-
}
|
|
209
|
-
const role = n.role ?? (n.type === 'text' ? 'text' : n.type);
|
|
210
|
-
const name = role === 'text' ? texts(g, n, warn)[0] : accessibleName(g, n, warn);
|
|
211
|
-
lines.push(`${pad}${role}${name === undefined ? '' : ` "${name}"`}${suffix}`);
|
|
212
|
-
}
|
|
213
|
-
return lines.join('\n');
|
|
214
|
-
}
|
|
215
|
-
export function renderScreens(g, warn, kit = null) {
|
|
216
|
-
const out = [];
|
|
217
|
-
for (const [state, st] of Object.entries(g.flow.states)) {
|
|
218
|
-
const shown = Object.entries(g.ui.nodes).filter(([, n]) => shownIn(n, state));
|
|
219
|
-
if (shown.length === 0)
|
|
220
|
-
continue;
|
|
221
|
-
const focusTargets = new Set(shown.map(([, n]) => n.initialFocus).filter((f) => f !== undefined));
|
|
222
|
-
const items = shown.map(([id, node]) => ({
|
|
223
|
-
id,
|
|
224
|
-
node,
|
|
225
|
-
disabled: (node.disabledIn ?? []).includes(state),
|
|
226
|
-
focus: focusTargets.has(id),
|
|
227
|
-
}));
|
|
228
|
-
out.push(`<article class="screen" id="screen-${esc(state)}"><header><h3>${esc(titleOf(g.flow, state))}</h3><span class="tag">${st.type === 'final' ? 'ends here, page shown' : 'screen'}</span><span class="mono muted">${esc(state)}</span></header>`);
|
|
229
|
-
if (st.description !== undefined)
|
|
230
|
-
out.push(`<p class="muted">${esc(st.description)}</p>`);
|
|
231
|
-
out.push(`<div class="wf${kit === null ? '' : ' kit'}" role="group" aria-label="Wireframe of ${esc(titleOf(g.flow, state))}">`);
|
|
232
|
-
out.push(renderWireframe(g, state, items, warn, false, kit));
|
|
233
|
-
out.push('</div>');
|
|
234
|
-
out.push(`<details class="ax-wrap"><summary>Expected accessibility tree</summary><pre class="ax">${esc(renderTree(g, items, warn))}</pre></details>`);
|
|
235
|
-
out.push('</article>');
|
|
236
|
-
}
|
|
237
|
-
return out.join('\n');
|
|
238
|
-
}
|
|
239
|
-
// One state's screen for the tutorial stage. Nodes that depend on a flag (`when`) appear only when named in `reveal`.
|
|
240
|
-
export function renderStageScreen(g, state, reveal, warn, kit = null) {
|
|
241
|
-
const shown = Object.entries(g.ui.nodes).filter(([id, n]) => shownIn(n, state) && (n.when === undefined || reveal.includes(id)));
|
|
242
|
-
const items = shown.map(([id, node]) => ({ id, node, disabled: (node.disabledIn ?? []).includes(state), focus: false }));
|
|
243
|
-
return `<div class="wf${kit === null ? '' : ' kit'}" role="group" aria-label="Screen: ${esc(titleOf(g.flow, state))}">${renderWireframe(g, state, items, warn, true, kit)}</div>`;
|
|
244
|
-
}
|
|
245
|
-
// a node's type that is no WAI-ARIA role: a field or an action with no `role` says nothing a screen reader can find
|
|
246
|
-
const NO_ARIA_TYPES = new Set(['field', 'action']);
|
|
247
|
-
// For `kit check`: every screen with all its nodes (flags revealed, no annotations), and what each node must expose.
|
|
248
|
-
export function renderCheckScreens(g, kit, warn) {
|
|
249
|
-
const html = [];
|
|
250
|
-
const expected = [];
|
|
251
|
-
for (const state of Object.keys(g.flow.states)) {
|
|
252
|
-
const shown = Object.entries(g.ui.nodes).filter(([, n]) => shownIn(n, state));
|
|
253
|
-
if (shown.length === 0)
|
|
254
|
-
continue;
|
|
255
|
-
const items = shown.map(([id, node]) => ({ id, node, disabled: (node.disabledIn ?? []).includes(state), focus: false }));
|
|
256
|
-
html.push(`<section class="screen" data-state="${esc(state)}"><div class="wf${kit === null ? '' : ' kit'}">${renderWireframe(g, state, items, warn, true, kit)}</div></section>`);
|
|
257
|
-
for (const it of items) {
|
|
258
|
-
const n = it.node;
|
|
259
|
-
if (n.component === 'Page')
|
|
260
|
-
continue; // the screen itself: nothing is drawn for it
|
|
261
|
-
if (n.component === 'ListItem') {
|
|
262
|
-
texts(g, n, warn).forEach((line, index) => expected.push({ state, id: it.id, index, role: n.role ?? 'listitem', roleGiven: true, name: line, disabled: it.disabled }));
|
|
263
|
-
continue;
|
|
264
|
-
}
|
|
265
|
-
const role = n.role ?? (n.type === 'text' ? 'text' : n.type);
|
|
266
|
-
const roleGiven = n.role !== undefined || !NO_ARIA_TYPES.has(n.type);
|
|
267
|
-
expected.push({ state, id: it.id, index: 0, role, roleGiven, name: role === 'text' ? texts(g, n, warn)[0] : accessibleName(g, n, warn), disabled: it.disabled });
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return { html: html.join('\n'), expected };
|
|
271
|
-
}
|
|
272
|
-
// the visible words of a node, for narration: a node with several texts gives several entries
|
|
273
|
-
export function nodeTexts(g, id, warn) {
|
|
274
|
-
const n = g.ui.nodes[id];
|
|
275
|
-
return n === undefined ? [] : texts(g, n, warn);
|
|
276
|
-
}
|
package/dist/yaml.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { type ErrorCode } from 'yaml';
|
|
2
|
-
export interface Unsupported {
|
|
3
|
-
offset: number;
|
|
4
|
-
line: number;
|
|
5
|
-
column: number;
|
|
6
|
-
code: ErrorCode;
|
|
7
|
-
message: string;
|
|
8
|
-
}
|
|
9
|
-
export declare function lineAndColumn(text: string, offset: number): {
|
|
10
|
-
line: number;
|
|
11
|
-
column: number;
|
|
12
|
-
};
|
|
13
|
-
export declare function unsupported(text: string): Unsupported | null;
|
|
14
|
-
export declare function parsingOnce<T>(run: () => T): T;
|
|
15
|
-
export declare function readYaml(text: string): unknown;
|
|
16
|
-
export declare function keyAt(text: string, path: string[]): {
|
|
17
|
-
line: number;
|
|
18
|
-
column: number;
|
|
19
|
-
};
|
package/dist/yaml.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
// YAML as delurio reads it (SPEC.md §1): YAML 1.2 without anchors, aliases, tags, or keys that are lists or maps.
|
|
2
|
-
// No graph needs them, and two of them keep the yaml library busy far beyond a file's size: on a 40 KB file, maps
|
|
3
|
-
// used as keys, or aliases inside keys, held `parse` for more than 30 seconds, which stops a server that reads the
|
|
4
|
-
// file. So the text is first read into the library's concrete syntax tree, which resolves nothing and takes time in
|
|
5
|
-
// proportion to the text, and walked once; a file that holds one of them is refused at its place, as a file that is not
|
|
6
|
-
// YAML is. Every reader of a graph file goes through `readYaml`.
|
|
7
|
-
import { isMap, isSeq, LineCounter, parse, parseDocument, Parser, YAMLParseError } from 'yaml';
|
|
8
|
-
const WHY = 'delurio reads YAML without anchors, aliases, tags or keys that are lists or maps (SPEC.md §1)';
|
|
9
|
-
const PROPS = {
|
|
10
|
-
anchor: ['BAD_ALIAS', 'an anchor', 'write the value where it is used'],
|
|
11
|
-
alias: ['BAD_ALIAS', 'an alias', 'write the value where it is used'],
|
|
12
|
-
tag: ['TAG_RESOLVE_FAILED', 'a tag', 'write the value without it'],
|
|
13
|
-
};
|
|
14
|
-
const COLLECTIONS = new Set(['block-map', 'block-seq', 'flow-collection']);
|
|
15
|
-
// the 1-based line and column of an offset into the text
|
|
16
|
-
export function lineAndColumn(text, offset) {
|
|
17
|
-
let line = 1;
|
|
18
|
-
let from = 0;
|
|
19
|
-
for (let i = text.indexOf('\n'); i >= 0 && i < offset; i = text.indexOf('\n', i + 1)) {
|
|
20
|
-
line++;
|
|
21
|
-
from = i + 1;
|
|
22
|
-
}
|
|
23
|
-
return { line, column: offset - from + 1 };
|
|
24
|
-
}
|
|
25
|
-
// the first place in the text, by offset, that delurio does not read; null when there is none. A text that is not
|
|
26
|
-
// YAML at all is left to the parser, which says why.
|
|
27
|
-
export function unsupported(text) {
|
|
28
|
-
let first = null;
|
|
29
|
-
const found = (offset, code, what) => {
|
|
30
|
-
if (first === null || offset < first.offset)
|
|
31
|
-
first = { offset, code, what };
|
|
32
|
-
};
|
|
33
|
-
// walked with a stack of its own: a deeply nested text must not overflow the call stack here
|
|
34
|
-
const stack = [...new Parser().parse(text)];
|
|
35
|
-
while (stack.length > 0) {
|
|
36
|
-
const v = stack.pop();
|
|
37
|
-
if (Array.isArray(v)) {
|
|
38
|
-
for (const x of v)
|
|
39
|
-
if (x !== null && typeof x === 'object')
|
|
40
|
-
stack.push(x);
|
|
41
|
-
continue;
|
|
42
|
-
}
|
|
43
|
-
if (v === null || typeof v !== 'object')
|
|
44
|
-
continue;
|
|
45
|
-
const t = v;
|
|
46
|
-
const offset = typeof t.offset === 'number' ? t.offset : 0;
|
|
47
|
-
const prop = typeof t.type === 'string' ? PROPS[t.type] : undefined;
|
|
48
|
-
if (prop !== undefined)
|
|
49
|
-
found(offset, prop[0], `${prop[1]} (${String(t.source)}): ${prop[2]}`);
|
|
50
|
-
const key = t.key;
|
|
51
|
-
if (key !== null && key !== undefined && typeof key.type === 'string' && COLLECTIONS.has(key.type)) {
|
|
52
|
-
const list = key.type === 'block-seq' || key.start?.source === '[';
|
|
53
|
-
found(typeof key.offset === 'number' ? key.offset : offset, 'NON_STRING_KEY', `a key that is a ${list ? 'list' : 'map'}: name it with a string`);
|
|
54
|
-
}
|
|
55
|
-
for (const x of Object.values(v))
|
|
56
|
-
if (x !== null && typeof x === 'object')
|
|
57
|
-
stack.push(x);
|
|
58
|
-
}
|
|
59
|
-
if (first === null)
|
|
60
|
-
return null;
|
|
61
|
-
const { offset, code, what } = first;
|
|
62
|
-
return { offset, ...lineAndColumn(text, offset), code, message: `unsupported YAML: ${what}; ${WHY}` };
|
|
63
|
-
}
|
|
64
|
-
// the refusal as the error the yaml library throws for a text that is not YAML, so that every reader that already
|
|
65
|
-
// says "cannot be loaded" for one says it for this too, at the same place
|
|
66
|
-
function refusal(u) {
|
|
67
|
-
const err = new YAMLParseError([u.offset, u.offset + 1], u.code, `${u.message} at line ${u.line}, column ${u.column}`);
|
|
68
|
-
err.linePos = [{ line: u.line, col: u.column }];
|
|
69
|
-
return err;
|
|
70
|
-
}
|
|
71
|
-
let read = null;
|
|
72
|
-
// `run`, with every text it reads through `readYaml` or `keyAt` parsed once; the same as `run` alone otherwise
|
|
73
|
-
export function parsingOnce(run) {
|
|
74
|
-
if (read !== null)
|
|
75
|
-
return run();
|
|
76
|
-
read = { values: new Map(), documents: new Map() };
|
|
77
|
-
try {
|
|
78
|
-
return run();
|
|
79
|
-
}
|
|
80
|
-
finally {
|
|
81
|
-
read = null;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
// the value of a graph file's text: the yaml library's `parse`, once nothing unsupported is found. Inside
|
|
85
|
-
// `parsingOnce` each call gets its own copy, so a reader that changes the value changes no other reader's.
|
|
86
|
-
export function readYaml(text) {
|
|
87
|
-
const kept = read?.values.get(text);
|
|
88
|
-
if (kept !== undefined) {
|
|
89
|
-
if ('refused' in kept)
|
|
90
|
-
throw refusal(kept.refused);
|
|
91
|
-
return structuredClone(kept.value);
|
|
92
|
-
}
|
|
93
|
-
const u = unsupported(text);
|
|
94
|
-
if (u !== null) {
|
|
95
|
-
read?.values.set(text, { refused: u });
|
|
96
|
-
throw refusal(u);
|
|
97
|
-
}
|
|
98
|
-
const value = parse(text);
|
|
99
|
-
if (read === null)
|
|
100
|
-
return value;
|
|
101
|
-
read.values.set(text, { value });
|
|
102
|
-
return structuredClone(value);
|
|
103
|
-
}
|
|
104
|
-
// the 1-based line and column of the key at `path` in a YAML text (of the item, for a list index), else the file's
|
|
105
|
-
// start; for a text `readYaml` has read
|
|
106
|
-
export function keyAt(text, path) {
|
|
107
|
-
let parsed = read?.documents.get(text);
|
|
108
|
-
if (parsed === undefined) {
|
|
109
|
-
const counter = new LineCounter();
|
|
110
|
-
parsed = { doc: parseDocument(text, { lineCounter: counter, prettyErrors: false }), lineCounter: counter };
|
|
111
|
-
read?.documents.set(text, parsed);
|
|
112
|
-
}
|
|
113
|
-
const { doc, lineCounter } = parsed;
|
|
114
|
-
const keyOf = (pair) => String(pair.key?.value ?? pair.key);
|
|
115
|
-
let node = doc.contents;
|
|
116
|
-
let at;
|
|
117
|
-
for (const step of path) {
|
|
118
|
-
if (isMap(node)) {
|
|
119
|
-
const pair = node.items.find((p) => keyOf(p) === step);
|
|
120
|
-
if (pair === undefined)
|
|
121
|
-
break;
|
|
122
|
-
at = pair.key?.range?.[0] ?? at;
|
|
123
|
-
node = pair.value;
|
|
124
|
-
}
|
|
125
|
-
else if (isSeq(node)) {
|
|
126
|
-
node = node.items[Number(step)];
|
|
127
|
-
at = node?.range?.[0] ?? at;
|
|
128
|
-
}
|
|
129
|
-
else
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
const { line, col } = lineCounter.linePos(at ?? 0);
|
|
133
|
-
return { line, column: col };
|
|
134
|
-
}
|
/package/{dist → js}/expr.d.ts
RENAMED
|
File without changes
|
/package/{dist → js}/kit.d.ts
RENAMED
|
File without changes
|
/package/{dist → js}/needs.d.ts
RENAMED
|
File without changes
|