scripted-yaml 0.0.0-stage → 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/dist/cli.js ADDED
@@ -0,0 +1,2285 @@
1
+ #!/usr/bin/env node
2
+
3
+ // src/document.ts
4
+ import { isDeepStrictEqual } from "node:util";
5
+
6
+ // src/node.ts
7
+ class Key {
8
+ raw;
9
+ name;
10
+ marker;
11
+ constructor(raw) {
12
+ this.raw = raw;
13
+ const at = raw.lastIndexOf("!");
14
+ this.name = at < 0 ? raw : raw.slice(0, at);
15
+ this.marker = at < 0 ? undefined : raw.slice(at + 1);
16
+ }
17
+ get kind() {
18
+ if (this.marker === undefined)
19
+ return "plain";
20
+ return this.marker === "" ? "formula" : "marked";
21
+ }
22
+ get keyword() {
23
+ return this.name.startsWith("$");
24
+ }
25
+ }
26
+
27
+ class Signature {
28
+ name;
29
+ parameters;
30
+ constructor(key) {
31
+ const open = key.indexOf("(");
32
+ const short = open > 0 && key.endsWith(")");
33
+ this.name = short ? key.slice(0, open).trim() : key;
34
+ this.parameters = short ? key.slice(open + 1, -1).split(",").map((parameter) => parameter.trim()).filter((parameter) => parameter !== "") : undefined;
35
+ }
36
+ define(body) {
37
+ return `function(${this.parameters.join(", ")}) ${body}`;
38
+ }
39
+ }
40
+
41
+ class Node {
42
+ file;
43
+ line;
44
+ column;
45
+ parent;
46
+ entry;
47
+ expression;
48
+ constructor(file, line, column) {
49
+ this.file = file;
50
+ this.line = line;
51
+ this.column = column;
52
+ }
53
+ path() {
54
+ if (this.entry)
55
+ return this.entry.path();
56
+ if (this.parent instanceof ListNode)
57
+ return `${this.parent.path()}${this.parent.label(this)}`;
58
+ return "";
59
+ }
60
+ within(ancestor) {
61
+ for (let node = this;node; node = node.parent)
62
+ if (node === ancestor)
63
+ return true;
64
+ return false;
65
+ }
66
+ ancestors() {
67
+ const list = [];
68
+ for (let node = this.parent;node; node = node.parent)
69
+ list.push(node);
70
+ return list;
71
+ }
72
+ }
73
+
74
+ class ScalarNode extends Node {
75
+ value;
76
+ source;
77
+ constructor(file, line, column, value, source) {
78
+ super(file, line, column);
79
+ this.value = value;
80
+ this.source = source;
81
+ }
82
+ toData() {
83
+ return this.value;
84
+ }
85
+ clone() {
86
+ return new ScalarNode(this.file, this.line, this.column, this.value, this.source);
87
+ }
88
+ }
89
+
90
+ class Entry {
91
+ value;
92
+ map;
93
+ file;
94
+ line;
95
+ column;
96
+ key;
97
+ base;
98
+ marked;
99
+ formula;
100
+ constructor(raw, value, map, file, line, column) {
101
+ this.value = value;
102
+ this.map = map;
103
+ this.file = file;
104
+ this.line = line;
105
+ this.column = column;
106
+ this.key = new Key(raw);
107
+ value.entry = this;
108
+ value.parent = map;
109
+ }
110
+ get name() {
111
+ return this.key.name;
112
+ }
113
+ path() {
114
+ const before = this.map.path();
115
+ return before === "" ? this.key.raw : `${before}.${this.key.raw}`;
116
+ }
117
+ }
118
+
119
+ class MapNode extends Node {
120
+ entries = [];
121
+ shape;
122
+ get(name) {
123
+ return this.entries.find((entry) => entry.name === name);
124
+ }
125
+ find(name) {
126
+ const own = this.get(name);
127
+ if (own)
128
+ return own;
129
+ for (const item of this.cases()) {
130
+ const added = item.get(name);
131
+ if (added)
132
+ return added;
133
+ }
134
+ return;
135
+ }
136
+ cases() {
137
+ const when = this.get("$when")?.value;
138
+ if (!(when instanceof ListNode))
139
+ return [];
140
+ return when.items.filter((item) => item instanceof MapNode);
141
+ }
142
+ scalar(name) {
143
+ const entry = this.get(name);
144
+ return entry?.key.kind === "plain" && entry.value instanceof ScalarNode ? entry.value.value : undefined;
145
+ }
146
+ set(raw, value, from, at) {
147
+ const entry = new Entry(raw, value, this, from.file, from.line, from.column);
148
+ if (at === undefined)
149
+ this.entries.push(entry);
150
+ else
151
+ this.entries.splice(at, 0, entry);
152
+ return entry;
153
+ }
154
+ remove(entry) {
155
+ this.entries.splice(this.entries.indexOf(entry), 1);
156
+ }
157
+ toData() {
158
+ const data = {};
159
+ for (const entry of this.entries)
160
+ data[entry.key.raw] = entry.value.toData();
161
+ return data;
162
+ }
163
+ clone() {
164
+ const copy = new MapNode(this.file, this.line, this.column);
165
+ for (const entry of this.entries)
166
+ copy.set(entry.key.raw, entry.value.clone(), entry);
167
+ return copy;
168
+ }
169
+ }
170
+
171
+ class ListNode extends Node {
172
+ items = [];
173
+ keyField = "id";
174
+ add(item, at) {
175
+ item.parent = this;
176
+ if (at === undefined)
177
+ this.items.push(item);
178
+ else
179
+ this.items.splice(at, 0, item);
180
+ }
181
+ byId(id) {
182
+ return this.items.find((item) => item instanceof MapNode && String(item.scalar(this.keyField)) === id && item.scalar(this.keyField) !== undefined);
183
+ }
184
+ label(item) {
185
+ const id = item instanceof MapNode ? item.scalar(this.keyField) : undefined;
186
+ return id === undefined ? `[${this.items.indexOf(item) + 1}]` : `.${String(id)}`;
187
+ }
188
+ toData() {
189
+ return this.items.map((item) => item.toData());
190
+ }
191
+ clone() {
192
+ const copy = new ListNode(this.file, this.line, this.column);
193
+ copy.keyField = this.keyField;
194
+ for (const item of this.items)
195
+ copy.add(item.clone());
196
+ return copy;
197
+ }
198
+ }
199
+
200
+ // src/problem.ts
201
+ class Problem {
202
+ file;
203
+ line;
204
+ column;
205
+ key;
206
+ message;
207
+ severity;
208
+ constructor(file, line, column, key, message, severity = "error") {
209
+ this.file = file;
210
+ this.line = line;
211
+ this.column = column;
212
+ this.key = key;
213
+ this.message = message;
214
+ this.severity = severity;
215
+ }
216
+ toString() {
217
+ const where = this.line > 0 ? `${this.file}:${this.line}` : this.file;
218
+ return [where, this.key, this.message].filter((part) => part !== "").join(" ");
219
+ }
220
+ }
221
+
222
+ class Problems {
223
+ list = [];
224
+ error(at, message) {
225
+ this.add(at, message, "error");
226
+ }
227
+ warning(at, message) {
228
+ this.add(at, message, "warning");
229
+ }
230
+ add(at, message, severity) {
231
+ this.list.push(new Problem(at.file, at.line, at.column, at.path(), message, severity));
232
+ }
233
+ sort() {
234
+ const files = [...new Set(this.list.map((problem) => problem.file))];
235
+ this.list.sort((a, b) => files.indexOf(a.file) - files.indexOf(b.file) || a.line - b.line);
236
+ }
237
+ get errors() {
238
+ return this.list.filter((problem) => problem.severity === "error");
239
+ }
240
+ get failed() {
241
+ return this.list.some((problem) => problem.severity === "error");
242
+ }
243
+ }
244
+
245
+ class ScriptedYamlError extends Error {
246
+ problems;
247
+ constructor(problems, options) {
248
+ super(problems.filter((problem) => problem.severity === "error").join(`
249
+ `), options);
250
+ this.name = new.target.name;
251
+ this.problems = problems;
252
+ }
253
+ }
254
+
255
+ class ValidationFailed extends ScriptedYamlError {
256
+ }
257
+
258
+ class ResolveFailed extends ScriptedYamlError {
259
+ }
260
+
261
+ // src/resolution.ts
262
+ import { evaluate as evaluate2 } from "feelin";
263
+
264
+ // src/expression.ts
265
+ import { parse as parseTemplate } from "feelers";
266
+
267
+ // src/lazy-formula.ts
268
+ import { parseExpression } from "feelin";
269
+ var FeelNode = {
270
+ VariableName: "VariableName",
271
+ PathExpression: "PathExpression",
272
+ FunctionInvocation: "FunctionInvocation",
273
+ FilterExpression: "FilterExpression",
274
+ ForExpression: "ForExpression",
275
+ Type: "Type",
276
+ Name: "Name",
277
+ NamedParameter: "NamedParameter"
278
+ };
279
+
280
+ class Ref {
281
+ head;
282
+ path;
283
+ call;
284
+ inFilter;
285
+ constructor(head, path, call, inFilter) {
286
+ this.head = head;
287
+ this.path = path;
288
+ this.call = call;
289
+ this.inFilter = inFilter;
290
+ }
291
+ get name() {
292
+ return [this.head, ...this.path].join(".");
293
+ }
294
+ }
295
+
296
+ class LazyFormula {
297
+ source;
298
+ static cache = new Map;
299
+ refs = [];
300
+ errors = [];
301
+ own = new Set;
302
+ static of(source) {
303
+ let formula = LazyFormula.cache.get(source);
304
+ if (!formula)
305
+ LazyFormula.cache.set(source, formula = new LazyFormula(source));
306
+ return formula;
307
+ }
308
+ constructor(source) {
309
+ this.source = source;
310
+ if (source.trim() === "") {
311
+ this.errors.push("the formula is empty");
312
+ return;
313
+ }
314
+ let tree;
315
+ try {
316
+ tree = parseExpression(source, {}, undefined);
317
+ } catch (error) {
318
+ this.errors.push(`not a valid formula: ${error.message}`);
319
+ return;
320
+ }
321
+ const used = [];
322
+ tree.iterate({
323
+ enter: (at) => {
324
+ if (this.errors.length > 0)
325
+ return false;
326
+ if (!at.type.isError)
327
+ return this.enter(at.node, used);
328
+ const near = source.slice(Math.max(0, at.from - 20), at.to + 10).trim();
329
+ this.errors.push(`not a valid formula (FEEL) at position ${at.from + 1}, near "${near}"`);
330
+ return false;
331
+ }
332
+ });
333
+ if (this.errors.length === 0)
334
+ this.refs.push(...used.filter((ref) => !this.own.has(ref.head) && !(ref.inFilter && ref.head === "item")));
335
+ }
336
+ static isName(text) {
337
+ const { refs } = LazyFormula.of(text);
338
+ return refs.length === 1 && refs[0].head === text && refs[0].path.length === 0 && !text.includes(" ");
339
+ }
340
+ text(node) {
341
+ return this.source.slice(node.from, node.to);
342
+ }
343
+ enter(node, used) {
344
+ if (node.name === FeelNode.Type)
345
+ return false;
346
+ if (node.name === FeelNode.ForExpression)
347
+ this.own.add("partial");
348
+ if (node.name === FeelNode.Name && node.parent?.parent?.name !== FeelNode.NamedParameter)
349
+ this.own.add(this.text(node));
350
+ if (node.name !== FeelNode.VariableName)
351
+ return;
352
+ const path = [];
353
+ let whole = node;
354
+ while (whole.parent?.name === FeelNode.PathExpression && !whole.prevSibling) {
355
+ whole = whole.parent;
356
+ path.push(this.text(whole.lastChild));
357
+ }
358
+ const call = whole.parent?.name === FeelNode.FunctionInvocation && !whole.prevSibling && path.length === 0;
359
+ used.push(new Ref(this.text(node), path, call, this.inFilter(node)));
360
+ return false;
361
+ }
362
+ inFilter(node) {
363
+ for (let inner = node, outer = node.parent;outer; inner = outer, outer = outer.parent) {
364
+ if (outer.name === FeelNode.FilterExpression && inner.prevSibling)
365
+ return true;
366
+ }
367
+ return false;
368
+ }
369
+ }
370
+
371
+ // src/expression.ts
372
+ class Expression {
373
+ source;
374
+ reads = [];
375
+ constructor(source) {
376
+ this.source = source;
377
+ }
378
+ toString() {
379
+ return this.source;
380
+ }
381
+ toJSON() {
382
+ return { source: this.source, reads: this.reads };
383
+ }
384
+ }
385
+
386
+ class Formula extends Expression {
387
+ get errors() {
388
+ return LazyFormula.of(this.source).errors;
389
+ }
390
+ get formulas() {
391
+ return this.errors.length > 0 ? [] : [LazyFormula.of(this.source)];
392
+ }
393
+ }
394
+
395
+ class TextBlock {
396
+ from;
397
+ to;
398
+ formula;
399
+ constructor(from, to, formula) {
400
+ this.from = from;
401
+ this.to = to;
402
+ this.formula = formula;
403
+ }
404
+ }
405
+
406
+ class Text extends Expression {
407
+ blocks = [];
408
+ problems = [];
409
+ constructor(source) {
410
+ super(source);
411
+ this.collect(parseTemplate(source).topNode);
412
+ }
413
+ static in(value) {
414
+ return typeof value === "string" && value.includes("{{");
415
+ }
416
+ get errors() {
417
+ return this.problems;
418
+ }
419
+ get formulas() {
420
+ return this.errors.length > 0 ? [] : this.blocks.map((block) => block.formula);
421
+ }
422
+ render(compute) {
423
+ let out = "";
424
+ let at = 0;
425
+ for (const block of this.blocks) {
426
+ out += this.source.slice(at, block.from) + Text.show(compute(block));
427
+ at = block.to;
428
+ }
429
+ return out + this.source.slice(at);
430
+ }
431
+ static show(value) {
432
+ if (value === null || value === undefined)
433
+ return "";
434
+ return typeof value === "object" ? JSON.stringify(value) : String(value);
435
+ }
436
+ fail(message) {
437
+ if (this.problems.length === 0)
438
+ this.problems.push(message);
439
+ }
440
+ collect(node) {
441
+ if (node.type.isError)
442
+ return this.fail('"{{" is not closed with "}}"');
443
+ if (node.name === "ConditionalSpanner" || node.name === "LoopSpanner")
444
+ return this.fail("{{#if}} and {{#loop}} are not supported in text: use $if! and $each!");
445
+ if (node.name === "Insert") {
446
+ const formula = node.getChild("FeelBlock");
447
+ if (node.lastChild?.type.isError || !formula)
448
+ return this.fail('"{{" is not closed with "}}"');
449
+ const block = LazyFormula.of(this.source.slice(formula.from, formula.to));
450
+ for (const error of block.errors)
451
+ this.fail(`in {{${block.source}}}: ${error}`);
452
+ this.blocks.push(new TextBlock(node.from, node.to, block));
453
+ return;
454
+ }
455
+ for (let child = node.firstChild;child; child = child.nextSibling)
456
+ this.collect(child);
457
+ }
458
+ }
459
+
460
+ // src/builtins.ts
461
+ import { evaluate } from "feelin";
462
+
463
+ class Builtins {
464
+ static known = new Map;
465
+ static has(name) {
466
+ let known = Builtins.known.get(name);
467
+ if (known === undefined)
468
+ Builtins.known.set(name, known = Builtins.ask(name));
469
+ return known;
470
+ }
471
+ static ask(name) {
472
+ try {
473
+ return !evaluate(`${name}()`, {}).warnings.some((warning) => warning.type === "NO_FUNCTION_FOUND");
474
+ } catch {
475
+ return false;
476
+ }
477
+ }
478
+ }
479
+
480
+ // src/source.ts
481
+ import { readFileSync as readFileSync2 } from "node:fs";
482
+ import { dirname, extname as extname2, join, resolve } from "node:path";
483
+
484
+ // src/importers.ts
485
+ import { existsSync, readFileSync } from "node:fs";
486
+ import { extname } from "node:path";
487
+ import { pathToFileURL } from "node:url";
488
+ import { mdq } from "mdq-cli";
489
+
490
+ class ImportSpec {
491
+ path;
492
+ absolute;
493
+ wanted;
494
+ entry;
495
+ constructor(path, absolute, wanted, entry) {
496
+ this.path = path;
497
+ this.absolute = absolute;
498
+ this.wanted = wanted;
499
+ this.entry = entry;
500
+ }
501
+ }
502
+
503
+ class Imported {
504
+ name;
505
+ value;
506
+ document;
507
+ constructor(name, value, document = false) {
508
+ this.name = name;
509
+ this.value = value;
510
+ this.document = document;
511
+ }
512
+ }
513
+
514
+ class ImportFailed extends Error {
515
+ }
516
+
517
+ class Importer {
518
+ static all = [];
519
+ static for(path) {
520
+ if (Importer.all.length === 0)
521
+ Importer.all.push(new ModuleImporter, new YamlImporter, new MarkdownImporter);
522
+ return Importer.all.find((importer) => importer.extensions.includes(extname(path)));
523
+ }
524
+ static get supported() {
525
+ Importer.for("");
526
+ return Importer.all.flatMap((importer) => importer.extensions);
527
+ }
528
+ exists(spec) {
529
+ try {
530
+ return existsSync(spec.absolute);
531
+ } catch {
532
+ return false;
533
+ }
534
+ }
535
+ oneName(spec) {
536
+ if (typeof spec.wanted !== "string")
537
+ throw new ImportFailed(`"${spec.path}" is imported under one name: ${spec.path}: name`);
538
+ return [spec.wanted];
539
+ }
540
+ }
541
+
542
+ class ModuleImporter extends Importer {
543
+ extensions = [".ts", ".js"];
544
+ names(spec) {
545
+ const list = typeof spec.wanted === "string" ? [spec.wanted] : spec.wanted;
546
+ if (!Array.isArray(list) || list.some((name) => typeof name !== "string"))
547
+ throw new ImportFailed(`"${spec.path}" needs the list of names to import: [first, second]`);
548
+ return list;
549
+ }
550
+ async load(spec) {
551
+ let module;
552
+ try {
553
+ module = await import(pathToFileURL(spec.absolute).href);
554
+ } catch (error) {
555
+ throw new ImportFailed(`cannot import "${spec.path}": ${error.message}`);
556
+ }
557
+ return this.names(spec).map((name) => {
558
+ if (!(name in module))
559
+ throw new ImportFailed(`"${spec.path}" does not export "${name}"`);
560
+ const value = module[name];
561
+ const kind = typeof value === "function" ? value.constructor.name : "";
562
+ if (kind === "AsyncFunction" || kind === "AsyncGeneratorFunction")
563
+ throw new ImportFailed(`"${name}" from "${spec.path}" is an async function; a formula cannot wait, so imported functions must be synchronous`);
564
+ return new Imported(name, value);
565
+ });
566
+ }
567
+ }
568
+
569
+ class YamlImporter extends Importer {
570
+ extensions = [".yml", ".yaml"];
571
+ names(spec) {
572
+ return this.oneName(spec);
573
+ }
574
+ async load(spec, host) {
575
+ const [name] = this.names(spec);
576
+ return [new Imported(name, await host.document(spec.absolute), true)];
577
+ }
578
+ }
579
+
580
+ class MarkdownImporter extends Importer {
581
+ extensions = [".md"];
582
+ names(spec) {
583
+ return this.oneName(spec);
584
+ }
585
+ async load(spec) {
586
+ const [name] = this.names(spec);
587
+ let text;
588
+ try {
589
+ text = readFileSync(spec.absolute, "utf8");
590
+ } catch (error) {
591
+ throw new ImportFailed(`cannot read "${spec.path}": ${error.message}`);
592
+ }
593
+ const markdown = mdq(text);
594
+ return [new Imported(name, (selector) => markdown.query(selector).text())];
595
+ }
596
+ }
597
+
598
+ // src/patch.ts
599
+ class Patch {
600
+ patch;
601
+ base;
602
+ problems;
603
+ static merged = ["$vars", "$functions", "$import", "$tests"];
604
+ static replaced = ["$dsl", "$schema"];
605
+ constructor(patch, base, problems) {
606
+ this.patch = patch;
607
+ this.base = base;
608
+ this.problems = problems;
609
+ }
610
+ apply() {
611
+ const changes = new MapNode(this.patch.file, this.patch.line, this.patch.column);
612
+ for (const entry of this.patch.entries) {
613
+ if (entry.name === "$extends")
614
+ continue;
615
+ if (Patch.merged.includes(entry.name))
616
+ this.merge(entry);
617
+ else if (Patch.replaced.includes(entry.name))
618
+ this.put(this.base, entry);
619
+ else
620
+ changes.entries.push(entry);
621
+ }
622
+ this.change(changes, this.base);
623
+ }
624
+ put(map, entry, base) {
625
+ const old = map.get(entry.name);
626
+ const at = old ? map.entries.indexOf(old) : undefined;
627
+ if (old)
628
+ map.remove(old);
629
+ const added = map.set(entry.key.raw, entry.value, entry, at);
630
+ added.base = base;
631
+ return added;
632
+ }
633
+ merge(entry) {
634
+ const target = this.base.get(entry.name)?.value;
635
+ if (target instanceof MapNode && entry.value instanceof MapNode)
636
+ for (const item of entry.value.entries)
637
+ this.put(target, item);
638
+ else if (target instanceof ListNode && entry.value instanceof ListNode)
639
+ for (const item of entry.value.items)
640
+ target.add(item);
641
+ else
642
+ this.put(this.base, entry);
643
+ }
644
+ change(changes, target) {
645
+ for (const entry of changes.entries) {
646
+ switch (entry.key.raw) {
647
+ case "$replace":
648
+ this.replace(entry, target);
649
+ break;
650
+ case "$add":
651
+ this.insert(entry, target, "end", true);
652
+ break;
653
+ case "$before":
654
+ this.insert(entry, target, "start", false);
655
+ break;
656
+ case "$after":
657
+ this.insert(entry, target, "end", false);
658
+ break;
659
+ case "$remove":
660
+ this.remove(entry, target);
661
+ break;
662
+ default:
663
+ this.descend(entry, target);
664
+ }
665
+ }
666
+ }
667
+ child(target, name) {
668
+ if (target instanceof MapNode)
669
+ return target.get(name)?.value;
670
+ if (target instanceof ListNode)
671
+ return target.byId(name);
672
+ return;
673
+ }
674
+ descend(entry, target) {
675
+ if (entry.key.keyword)
676
+ return this.problems.error(entry, `"${entry.key.raw}" cannot be used in a patch here`);
677
+ const child = this.child(target, entry.name);
678
+ if (!child)
679
+ return this.problems.error(entry, `"${entry.name}" matches nothing in the base; a patch adds with $add, $before or $after`);
680
+ if (entry.key.kind !== "plain" || !(entry.value instanceof MapNode)) {
681
+ return this.problems.error(entry, `a plain field in a patch does not overwrite; write $replace: { ${entry.key.raw}: … }`);
682
+ }
683
+ if (child.entry && child.entry.key.kind !== "plain")
684
+ return this.problems.error(entry, `"${entry.name}" is a formula or a marked key in the base; change it with $replace`);
685
+ this.change(entry.value, child);
686
+ }
687
+ replace(entry, target) {
688
+ if (!(target instanceof MapNode) || !(entry.value instanceof MapNode))
689
+ return this.problems.error(entry, "$replace lists fields of a map: $replace: { field: value }");
690
+ for (const field of entry.value.entries) {
691
+ const old = target.get(field.name);
692
+ if (old)
693
+ this.put(target, field, old);
694
+ else
695
+ this.problems.error(field, `"${field.name}" does not exist in the base, so it cannot be replaced; add it with $add`);
696
+ }
697
+ }
698
+ remove(entry, target) {
699
+ const names = entry.value instanceof ListNode ? entry.value.toData().map(String) : [String(entry.value.toData())];
700
+ for (const name of names) {
701
+ const child = this.child(target, name);
702
+ if (!child)
703
+ this.problems.error(entry, `"${name}" does not exist in the base, so it cannot be removed`);
704
+ else if (target instanceof MapNode)
705
+ target.remove(child.entry);
706
+ else if (target instanceof ListNode)
707
+ target.items.splice(target.items.indexOf(child), 1);
708
+ }
709
+ }
710
+ insert(entry, target, where, mustBeNew) {
711
+ const added = entry.value;
712
+ if (target instanceof ScalarNode)
713
+ return this.text(entry, target, where);
714
+ if (target instanceof ListNode && !(added instanceof MapNode)) {
715
+ const values = added instanceof ListNode ? added.items : [added];
716
+ values.forEach((value, index) => target.add(value, where === "start" ? index : undefined));
717
+ return;
718
+ }
719
+ if (!(added instanceof MapNode))
720
+ return this.problems.error(entry, `${entry.key.raw} takes a map of new items, each under its name`);
721
+ const beside = !mustBeNew && target instanceof MapNode && target.parent instanceof ListNode ? target.parent : undefined;
722
+ const into = beside ?? target;
723
+ added.entries.forEach((item, index) => {
724
+ if (this.child(into, item.name))
725
+ return this.problems.error(item, `"${item.name}" already exists in the base`);
726
+ if (into instanceof MapNode) {
727
+ into.set(item.key.raw, item.value, item, where === "start" ? index : undefined);
728
+ } else if (into instanceof ListNode) {
729
+ const start = beside ? into.items.indexOf(target) + (where === "start" ? 0 : 1 + index) : where === "start" ? index : into.items.length;
730
+ into.add(this.named(item, into.keyField), start);
731
+ }
732
+ });
733
+ }
734
+ named(item, keyField) {
735
+ if (!(item.value instanceof MapNode)) {
736
+ this.problems.error(item, `the new item "${item.name}" must be a map of its fields`);
737
+ return item.value;
738
+ }
739
+ const node = new MapNode(item.file, item.line, item.column);
740
+ node.set(keyField, new ScalarNode(item.file, item.line, item.column, item.name, item.name), item);
741
+ for (const field of item.value.entries)
742
+ node.set(field.key.raw, field.value, field);
743
+ return node;
744
+ }
745
+ text(entry, target, where) {
746
+ const holder = target.entry?.map.get(target.entry.name);
747
+ const now = holder?.value;
748
+ const added = entry.value;
749
+ if (!holder || holder.key.kind !== "plain" || !(now instanceof ScalarNode) || typeof now.value !== "string" || !(added instanceof ScalarNode)) {
750
+ return this.problems.error(entry, `${entry.key.raw} adds text to a text field; this field is not text`);
751
+ }
752
+ const joined = where === "start" ? `${added.source}
753
+ ${now.value}` : `${now.value.trimEnd()}
754
+ ${added.source}`;
755
+ const at = holder.map.entries.indexOf(holder);
756
+ holder.map.remove(holder);
757
+ holder.map.set(holder.key.raw, new ScalarNode(entry.file, entry.line, entry.column, joined, joined), holder, at);
758
+ }
759
+ }
760
+
761
+ // src/reader.ts
762
+ import { isAlias, isMap, isScalar, isSeq, LineCounter, parseDocument } from "yaml";
763
+ class SourceReader {
764
+ file;
765
+ problems;
766
+ constructor(file, problems) {
767
+ this.file = file;
768
+ this.problems = problems;
769
+ }
770
+ static for(input, file, problems) {
771
+ return typeof input === "string" ? new YamlTextReader(input, file, problems) : new DataReader(input, file, problems);
772
+ }
773
+ notAMap() {
774
+ this.problems.error({ file: this.file, line: 1, column: 1, path: () => "" }, "the root of a file must be a map of keys");
775
+ return;
776
+ }
777
+ }
778
+
779
+ class YamlTextReader extends SourceReader {
780
+ text;
781
+ lines = new LineCounter;
782
+ document;
783
+ constructor(text, file, problems) {
784
+ super(file, problems);
785
+ this.text = text;
786
+ }
787
+ read() {
788
+ this.document = parseDocument(this.text, { lineCounter: this.lines, prettyErrors: false, uniqueKeys: true });
789
+ for (const error of this.document.errors)
790
+ this.report(error.pos[0], error.message, "error");
791
+ for (const warning of this.document.warnings)
792
+ this.report(warning.pos[0], warning.message, "warning");
793
+ if (this.document.errors.length > 0)
794
+ return;
795
+ const contents = this.document.contents;
796
+ if (contents === null)
797
+ return new MapNode(this.file, 1, 1);
798
+ const root = this.convert(contents);
799
+ return root instanceof MapNode ? root : this.notAMap();
800
+ }
801
+ report(offset, message, severity) {
802
+ const { line, col } = this.lines.linePos(offset);
803
+ this.problems.add({ file: this.file, line, column: col, path: () => "" }, message, severity);
804
+ }
805
+ at(node) {
806
+ const { line, col } = this.lines.linePos(node?.range?.[0] ?? 0);
807
+ return [this.file, line, col];
808
+ }
809
+ convert(node) {
810
+ if (isAlias(node))
811
+ return this.convert(node.resolve(this.document) ?? null);
812
+ if (isMap(node)) {
813
+ const map = new MapNode(...this.at(node));
814
+ for (const pair of node.items) {
815
+ const key = pair.key;
816
+ const [file, line, column] = this.at(key);
817
+ const name = isScalar(key) ? typeof key.value === "string" ? key.value : key.source ?? String(key.value) : undefined;
818
+ if (name === undefined) {
819
+ this.report(key?.range?.[0] ?? 0, "a key must be text", "error");
820
+ continue;
821
+ }
822
+ map.set(name, this.convert(pair.value), { file, line, column, path: () => name });
823
+ }
824
+ return map;
825
+ }
826
+ if (isSeq(node)) {
827
+ const list = new ListNode(...this.at(node));
828
+ for (const item of node.items)
829
+ list.add(this.convert(item));
830
+ return list;
831
+ }
832
+ const value = isScalar(node) ? node.value : null;
833
+ const source = typeof value === "string" ? value : (isScalar(node) ? node.source : undefined) ?? String(value);
834
+ return new ScalarNode(...this.at(node), value, source);
835
+ }
836
+ }
837
+
838
+ class DataReader extends SourceReader {
839
+ data;
840
+ constructor(data, file, problems) {
841
+ super(file, problems);
842
+ this.data = data;
843
+ }
844
+ read() {
845
+ if (this.data === null || this.data === undefined)
846
+ return new MapNode(this.file, 0, 0);
847
+ const root = this.convert(this.data);
848
+ return root instanceof MapNode ? root : this.notAMap();
849
+ }
850
+ convert(value) {
851
+ if (Array.isArray(value)) {
852
+ const list = new ListNode(this.file, 0, 0);
853
+ for (const item of value)
854
+ list.add(this.convert(item));
855
+ return list;
856
+ }
857
+ if (value !== null && typeof value === "object" && Object.getPrototypeOf(value) === Object.prototype) {
858
+ const map = new MapNode(this.file, 0, 0);
859
+ for (const [key, item] of Object.entries(value))
860
+ map.set(key, this.convert(item), map);
861
+ return map;
862
+ }
863
+ return new ScalarNode(this.file, 0, 0, value ?? null, String(value ?? ""));
864
+ }
865
+ }
866
+
867
+ // src/schema.ts
868
+ class Field {
869
+ required;
870
+ name;
871
+ many;
872
+ type;
873
+ constructor(word, required) {
874
+ this.required = required;
875
+ const colon = word.indexOf(":");
876
+ const name = colon < 0 ? word : word.slice(0, colon);
877
+ this.type = colon < 0 ? undefined : word.slice(colon + 1);
878
+ this.many = name.endsWith("*");
879
+ this.name = this.many ? name.slice(0, -1) : name;
880
+ }
881
+ }
882
+
883
+ class Rule {
884
+ message;
885
+ formula;
886
+ constructor(source, message) {
887
+ this.message = message;
888
+ this.formula = new Formula(source);
889
+ }
890
+ }
891
+
892
+ class Shape {
893
+ name;
894
+ fields = new Map;
895
+ rules = [];
896
+ key;
897
+ with;
898
+ constructor(name, data, at, problems) {
899
+ this.name = name;
900
+ for (const word of Shape.words(data.required))
901
+ this.add(new Field(word, true));
902
+ for (const word of Shape.words(data.optional))
903
+ this.add(new Field(word, false));
904
+ this.key = typeof data.key === "string" ? data.key : "id";
905
+ this.with = data.with ?? {};
906
+ for (const rule of Array.isArray(data.rules) ? data.rules : [])
907
+ this.rule(rule, at, problems);
908
+ }
909
+ static words(list) {
910
+ return Array.isArray(list) ? list.map(String) : [];
911
+ }
912
+ add(field) {
913
+ this.fields.set(field.name, field);
914
+ }
915
+ rule(data, at, problems) {
916
+ const rule = new Rule(String(data?.["valid!"] ?? ""), String(data?.message ?? "a rule of the schema is broken"));
917
+ const label = this.name === "" ? "the schema" : this.name;
918
+ for (const error of rule.formula.errors)
919
+ problems.error(at, `a rule of ${label}: ${error}`);
920
+ for (const formula of rule.formula.formulas) {
921
+ for (const ref of formula.refs) {
922
+ const known = ref.inFilter || this.fields.has(ref.head) || ref.call && Builtins.has(ref.head);
923
+ if (!known)
924
+ problems.error(at, `a rule of ${label} reads "${ref.head}", which is not one of its fields`);
925
+ }
926
+ }
927
+ this.rules.push(rule);
928
+ }
929
+ }
930
+
931
+ class Schema {
932
+ root;
933
+ shapes = new Map;
934
+ words = new Map;
935
+ constructor(node, problems) {
936
+ const data = node instanceof MapNode ? node.toData() : {};
937
+ if (!(node instanceof MapNode))
938
+ problems.error(node, '$schema must be a map with "required" and "optional"');
939
+ this.root = new Shape("", data, node, problems);
940
+ for (const [name, value] of Object.entries(data)) {
941
+ const first = name.charAt(0);
942
+ if (first === first.toLowerCase())
943
+ continue;
944
+ if (Array.isArray(value))
945
+ this.words.set(name, value.map(String));
946
+ else
947
+ this.shapes.set(name, new Shape(name, value ?? {}, node, problems));
948
+ }
949
+ for (const shape of [this.root, ...this.shapes.values()]) {
950
+ for (const field of shape.fields.values()) {
951
+ if (field.type !== undefined && !this.shapes.has(field.type) && !this.words.has(field.type))
952
+ problems.error(node, `the schema names the type "${field.type}" and does not define it`);
953
+ }
954
+ }
955
+ }
956
+ check(root, problems) {
957
+ this.fit(root, this.root, problems);
958
+ }
959
+ fit(map, shape, problems, named) {
960
+ map.shape = shape;
961
+ for (const field of shape.fields.values()) {
962
+ if (field.required && !map.get(field.name) && field.name !== named)
963
+ problems.error(map, `missing required field "${field.name}"`);
964
+ }
965
+ for (const entry of map.entries) {
966
+ const field = shape.fields.get(entry.name);
967
+ if (!field && !entry.key.keyword)
968
+ problems.warning(entry, `"${entry.name}" is not a field of the schema`);
969
+ if (field && entry.key.kind === "plain")
970
+ this.value(entry.value, field, problems);
971
+ }
972
+ }
973
+ value(node, field, problems) {
974
+ if (!field.many)
975
+ return this.one(node, field, problems);
976
+ const shape = field.type === undefined ? undefined : this.shapes.get(field.type);
977
+ if (node instanceof ListNode) {
978
+ if (shape)
979
+ node.keyField = shape.key;
980
+ for (const item of node.items)
981
+ this.one(item, field, problems);
982
+ } else if (node instanceof MapNode) {
983
+ for (const entry of node.entries)
984
+ if (!entry.key.keyword && entry.key.kind === "plain")
985
+ this.one(entry.value, field, problems, shape?.key);
986
+ } else {
987
+ problems.error(node, `"${field.name}" takes many values: write a list`);
988
+ }
989
+ }
990
+ one(node, field, problems, named) {
991
+ if (field.type === undefined)
992
+ return;
993
+ const shape = this.shapes.get(field.type);
994
+ const words = this.words.get(field.type);
995
+ if (shape) {
996
+ if (node instanceof MapNode)
997
+ this.fit(node, shape, problems, named);
998
+ else
999
+ problems.error(node, `"${field.name}" must be a map with the fields of ${field.type}`);
1000
+ } else if (words) {
1001
+ if (node.expression)
1002
+ return;
1003
+ const word = node instanceof ScalarNode ? String(node.value) : undefined;
1004
+ if (word === undefined || !words.includes(word))
1005
+ problems.error(node, `"${word ?? field.name}" is not one of: ${words.join(", ")}`);
1006
+ }
1007
+ }
1008
+ }
1009
+
1010
+ // src/variables.ts
1011
+ class Variable {
1012
+ name;
1013
+ optional;
1014
+ secret;
1015
+ host;
1016
+ type;
1017
+ about;
1018
+ default;
1019
+ constructor(name, settings) {
1020
+ this.name = name;
1021
+ this.optional = settings.optional === true;
1022
+ this.secret = settings.secret === true;
1023
+ this.host = settings.host === true;
1024
+ this.type = typeof settings.type === "string" ? settings.type : undefined;
1025
+ this.about = typeof settings.about === "string" ? settings.about : undefined;
1026
+ this.default = settings.default;
1027
+ }
1028
+ get required() {
1029
+ return !this.optional && this.default === undefined;
1030
+ }
1031
+ mismatch(value) {
1032
+ const fits = {
1033
+ text: typeof value === "string",
1034
+ number: typeof value === "number",
1035
+ boolean: typeof value === "boolean",
1036
+ list: Array.isArray(value)
1037
+ };
1038
+ if (value === null || this.type === undefined || fits[this.type] !== false)
1039
+ return;
1040
+ return `variable "${this.name}" must be a ${this.type}; variables are not converted`;
1041
+ }
1042
+ }
1043
+
1044
+ class Variables {
1045
+ declared = new Map;
1046
+ patterns = [];
1047
+ known = new Set;
1048
+ add(node, problems, forChecking = false) {
1049
+ if (!node)
1050
+ return this;
1051
+ if (!(node instanceof MapNode)) {
1052
+ problems.error(node, "$vars must be a map of variable names");
1053
+ return this;
1054
+ }
1055
+ for (const entry of node.entries) {
1056
+ const name = entry.key.raw;
1057
+ if (name.length > 2 && name.startsWith("/") && name.endsWith("/"))
1058
+ this.pattern(name.slice(1, -1), entry, problems);
1059
+ else if (forChecking)
1060
+ this.known.add(name);
1061
+ else
1062
+ this.declare(name, entry.value, problems);
1063
+ }
1064
+ return this;
1065
+ }
1066
+ knows(name) {
1067
+ return this.declared.has(name) || this.known.has(name) || this.patterns.some((pattern) => pattern.test(name));
1068
+ }
1069
+ values(input) {
1070
+ const values = {};
1071
+ for (const [name, value] of Object.entries(input))
1072
+ values[name] = value === "" || value === undefined ? null : value;
1073
+ const missing = [];
1074
+ const mismatches = [];
1075
+ for (const variable of this.declared.values()) {
1076
+ values[variable.name] ??= variable.default ?? null;
1077
+ if (values[variable.name] === null && variable.required)
1078
+ missing.push(variable.name);
1079
+ const mismatch = variable.mismatch(values[variable.name]);
1080
+ if (mismatch)
1081
+ mismatches.push(mismatch);
1082
+ }
1083
+ return { values, missing, mismatches };
1084
+ }
1085
+ declare(name, node, problems) {
1086
+ const settings = node.toData();
1087
+ if (settings !== null && !(node instanceof MapNode)) {
1088
+ problems.error(node, `variable "${name}" is declared with a map: { default: …, optional: true }`);
1089
+ return;
1090
+ }
1091
+ this.declared.set(name, new Variable(name, settings ?? {}));
1092
+ }
1093
+ pattern(body, at, problems) {
1094
+ try {
1095
+ this.patterns.push(new RegExp(`^(?:${body})$`));
1096
+ } catch (error) {
1097
+ problems.error(at, `not a regular expression: ${error.message}`);
1098
+ }
1099
+ }
1100
+ }
1101
+
1102
+ // src/source.ts
1103
+ class Source {
1104
+ file;
1105
+ root;
1106
+ loader;
1107
+ problems;
1108
+ static rootKeywords = ["$vars", "$functions", "$import", "$schema", "$dsl", "$extends", "$tests"];
1109
+ variables = new Variables;
1110
+ functions = new Map;
1111
+ imports = [];
1112
+ imported = new Map;
1113
+ schema;
1114
+ constructor(file, root, loader, problems) {
1115
+ this.file = file;
1116
+ this.root = root;
1117
+ this.loader = loader;
1118
+ this.problems = problems;
1119
+ this.variables.add(root.get("$vars")?.value, problems);
1120
+ this.readFunctions(root.get("$functions")?.value);
1121
+ this.readImports(root.get("$import")?.value);
1122
+ const dsl = this.readDsl(root.get("$dsl"));
1123
+ const schema = root.get("$schema")?.value ?? dsl?.get("$schema")?.value;
1124
+ if (schema)
1125
+ this.schema = new Schema(schema, problems);
1126
+ }
1127
+ get data() {
1128
+ return this.root.entries.filter((entry) => !Source.rootKeywords.includes(entry.name));
1129
+ }
1130
+ get tests() {
1131
+ const tests = this.root.get("$tests")?.value.toData();
1132
+ return Array.isArray(tests) ? tests : [];
1133
+ }
1134
+ readFunctions(node) {
1135
+ if (!node)
1136
+ return;
1137
+ if (!(node instanceof MapNode))
1138
+ return this.problems.error(node, "$functions must be a map: name(parameter)!: formula");
1139
+ for (const entry of node.entries) {
1140
+ const signature = new Signature(entry.name);
1141
+ if (entry.key.kind !== "formula") {
1142
+ this.problems.error(entry, `a function is a formula: write "${signature.name}(…)!: …"`);
1143
+ continue;
1144
+ }
1145
+ if (signature.parameters && entry.value instanceof ScalarNode)
1146
+ entry.formula = signature.define(entry.value.source);
1147
+ this.functions.set(signature.name, entry);
1148
+ }
1149
+ }
1150
+ readImports(node) {
1151
+ if (!node)
1152
+ return;
1153
+ if (!(node instanceof MapNode))
1154
+ return this.problems.error(node, "$import must be a map: path: names");
1155
+ for (const entry of node.entries) {
1156
+ const path = entry.key.raw;
1157
+ const importer = Importer.for(path);
1158
+ if (!importer) {
1159
+ this.problems.error(entry, `cannot import "${path}": there is no importer for ${extname2(path) || "this kind of"} files (only ${Importer.supported.join(", ")})`);
1160
+ continue;
1161
+ }
1162
+ const spec = new ImportSpec(path, this.loader.absolute(entry.file, path), entry.value.toData(), entry);
1163
+ try {
1164
+ for (const name of importer.names(spec))
1165
+ this.imported.set(name, spec);
1166
+ if (!importer.exists(spec))
1167
+ throw new ImportFailed(`cannot import "${path}": the file does not exist`);
1168
+ this.imports.push(spec);
1169
+ } catch (error) {
1170
+ this.problems.error(entry, error.message);
1171
+ }
1172
+ }
1173
+ }
1174
+ readDsl(entry) {
1175
+ if (!entry)
1176
+ return;
1177
+ if (!(entry.value instanceof ScalarNode) || typeof entry.value.value !== "string") {
1178
+ this.problems.error(entry, "$dsl names one file by its path");
1179
+ return;
1180
+ }
1181
+ const dsl = this.loader.read(entry, entry.value.value);
1182
+ this.variables.add(dsl?.get("$vars")?.value, this.problems, true);
1183
+ return dsl;
1184
+ }
1185
+ }
1186
+
1187
+ class SourceLoader {
1188
+ problems;
1189
+ constructor(problems) {
1190
+ this.problems = problems;
1191
+ }
1192
+ load(input, file) {
1193
+ const root = this.tree(input, file, [resolve(file)]);
1194
+ return root && new Source(file, root, this, this.problems);
1195
+ }
1196
+ absolute(file, path) {
1197
+ return resolve(dirname(file), path);
1198
+ }
1199
+ read(from, path, chain = []) {
1200
+ const absolute = this.absolute(from.file, path);
1201
+ if (chain.includes(absolute)) {
1202
+ this.problems.error(from, `"${path}" extends itself, directly or through other files`);
1203
+ return;
1204
+ }
1205
+ let text;
1206
+ try {
1207
+ text = readFileSync2(absolute, "utf8");
1208
+ } catch (error) {
1209
+ const code = error.code;
1210
+ this.problems.error(from, `cannot read "${path}": ${code === "ENOENT" ? "the file does not exist" : error.message}`);
1211
+ return;
1212
+ }
1213
+ return this.tree(text, join(dirname(from.file), path), [...chain, absolute]);
1214
+ }
1215
+ tree(input, file, chain) {
1216
+ const root = SourceReader.for(input, file, this.problems).read();
1217
+ const base = root?.get("$extends");
1218
+ if (!root || !base)
1219
+ return root;
1220
+ if (!(base.value instanceof ScalarNode) || typeof base.value.value !== "string") {
1221
+ this.problems.error(base, "$extends names one file by its path");
1222
+ return;
1223
+ }
1224
+ const target = this.read(base, base.value.value, chain);
1225
+ if (!target)
1226
+ return;
1227
+ new Patch(root, target, this.problems).apply();
1228
+ return target;
1229
+ }
1230
+ }
1231
+
1232
+ // src/scope.ts
1233
+ class Reach {
1234
+ entry;
1235
+ node;
1236
+ path;
1237
+ steps = [];
1238
+ nodes = [];
1239
+ missing;
1240
+ constructor(entry, node, path) {
1241
+ this.entry = entry;
1242
+ this.node = node;
1243
+ this.path = path;
1244
+ this.nodes.push(node);
1245
+ }
1246
+ get rest() {
1247
+ return this.path.slice(this.steps.length);
1248
+ }
1249
+ }
1250
+
1251
+ class Scope {
1252
+ source;
1253
+ constructor(source) {
1254
+ this.source = source;
1255
+ }
1256
+ resolve(reader, ref) {
1257
+ const chain = [reader, ...reader.ancestors()];
1258
+ for (const node of chain) {
1259
+ if (node instanceof MapNode && node.get("$each") && reader.entry !== node.get("$each") && ref.head === Scope.eachName(node))
1260
+ return { kind: "each", owner: node };
1261
+ if (node.entry?.map.shape?.with[node.entry.name]?.includes(ref.head))
1262
+ return { kind: "given" };
1263
+ }
1264
+ if (ref.head === "vars")
1265
+ return { kind: "vars" };
1266
+ const replaced = chain.find((node) => node.entry?.base)?.entry?.base;
1267
+ if (ref.head === "base" && replaced)
1268
+ return { kind: "base", entry: replaced };
1269
+ const called = ref.call ? this.callable(ref.head) : undefined;
1270
+ if (called)
1271
+ return called;
1272
+ for (const map of this.maps(reader)) {
1273
+ const entry = map.find(ref.head);
1274
+ if (entry && !entry.key.keyword)
1275
+ return { kind: "key", entry };
1276
+ }
1277
+ return this.callable(ref.head, ref.call) ?? { kind: "unknown" };
1278
+ }
1279
+ walk(start, path, find = (map, name) => map.find(name)) {
1280
+ const reach = new Reach(start, start.value, path);
1281
+ for (const name of path) {
1282
+ if (reach.entry && reach.entry.key.kind !== "plain")
1283
+ break;
1284
+ const node = reach.node;
1285
+ if (node instanceof MapNode) {
1286
+ const next = find(node, name);
1287
+ if (!next || next.key.keyword) {
1288
+ if (!node.get("$value"))
1289
+ reach.missing = { name, map: node };
1290
+ break;
1291
+ }
1292
+ reach.entry = next;
1293
+ reach.node = next.value;
1294
+ } else if (node instanceof ListNode) {
1295
+ const item = node.byId(name);
1296
+ if (!item)
1297
+ break;
1298
+ reach.entry = undefined;
1299
+ reach.node = item;
1300
+ } else {
1301
+ break;
1302
+ }
1303
+ reach.steps.push(name);
1304
+ reach.nodes.push(reach.node);
1305
+ }
1306
+ return reach;
1307
+ }
1308
+ names(reader) {
1309
+ const keys = this.maps(reader).flatMap((map) => map.entries.filter((entry) => !entry.key.keyword).map((entry) => entry.name));
1310
+ return ["vars", ...keys, ...this.source.functions.keys(), ...this.source.imported.keys()];
1311
+ }
1312
+ static eachName(item) {
1313
+ return String(item.scalar("$as") ?? "item");
1314
+ }
1315
+ callable(name, builtins = true) {
1316
+ const defined = this.source.functions.get(name);
1317
+ if (defined)
1318
+ return { kind: "function", entry: defined };
1319
+ const spec = this.source.imported.get(name);
1320
+ if (spec)
1321
+ return { kind: "import", spec };
1322
+ return builtins && Builtins.has(name) ? { kind: "builtin" } : undefined;
1323
+ }
1324
+ maps(reader) {
1325
+ const top = [reader, ...reader.ancestors()].find((node) => node.parent === this.source.root);
1326
+ if (top?.entry && Source.rootKeywords.includes(top.entry.name))
1327
+ return [this.source.root];
1328
+ return reader.ancestors().filter((node) => node instanceof MapNode);
1329
+ }
1330
+ }
1331
+
1332
+ // src/resolution.ts
1333
+ class Env {
1334
+ owner;
1335
+ value;
1336
+ parent;
1337
+ values = new Map;
1338
+ open = new Set;
1339
+ lists = new Map;
1340
+ maps = new Map;
1341
+ constructor(owner, value, parent) {
1342
+ this.owner = owner;
1343
+ this.value = value;
1344
+ this.parent = parent;
1345
+ }
1346
+ for(node) {
1347
+ let env = this;
1348
+ while (env.owner && env.parent && !node.within(env.owner))
1349
+ env = env.parent;
1350
+ return env;
1351
+ }
1352
+ given(owner) {
1353
+ for (let env = this;env; env = env.parent)
1354
+ if (env.owner === owner)
1355
+ return env.value;
1356
+ return null;
1357
+ }
1358
+ }
1359
+
1360
+ class Resolution {
1361
+ source;
1362
+ imports;
1363
+ input;
1364
+ scope;
1365
+ top = new Env;
1366
+ vars;
1367
+ read = new Map;
1368
+ definitions = new Map;
1369
+ defining = new Set;
1370
+ guards = new Map;
1371
+ documents = new Map;
1372
+ promised = [];
1373
+ broken = [];
1374
+ afterwards = [];
1375
+ constructor(source, imports, input) {
1376
+ this.source = source;
1377
+ this.imports = imports;
1378
+ this.input = input;
1379
+ this.scope = new Scope(source);
1380
+ const { values, missing, mismatches } = source.variables.values(input);
1381
+ this.vars = values;
1382
+ const at = source.root.get("$vars") ?? source.root;
1383
+ if (missing.length > 0)
1384
+ this.fail(at, `missing required variable${missing.length > 1 ? "s" : ""}: ${missing.join(", ")}`);
1385
+ if (mismatches.length > 0)
1386
+ this.fail(at, mismatches.join("; "));
1387
+ }
1388
+ run() {
1389
+ const result = this.output(this.source.root, this.top);
1390
+ if (this.broken.length > 0)
1391
+ throw new ResolveFailed(this.broken);
1392
+ for (const settle of this.afterwards)
1393
+ settle();
1394
+ return result;
1395
+ }
1396
+ view() {
1397
+ return this.viewOf(this.source.root, this.top);
1398
+ }
1399
+ probe(source) {
1400
+ const formula = new Formula(source);
1401
+ const reader = new ScalarNode(this.source.file, 0, 0, source, source);
1402
+ reader.parent = this.source.root;
1403
+ reader.path = () => source;
1404
+ if (formula.errors.length > 0)
1405
+ this.fail(reader, formula.errors[0]);
1406
+ return this.evaluate(formula.formulas[0], reader, this.top);
1407
+ }
1408
+ output(node, env) {
1409
+ if (node.expression)
1410
+ return this.compute(node, env);
1411
+ if (node instanceof ListNode)
1412
+ return this.items(node, env).map(([item, own]) => this.output(item, own));
1413
+ if (!(node instanceof MapNode))
1414
+ return node.value;
1415
+ const out = {};
1416
+ for (const entry of this.effective(node, env)) {
1417
+ const name = entry.name;
1418
+ if (entry.key.keyword && name !== "$value")
1419
+ continue;
1420
+ if (entry.key.kind === "marked") {
1421
+ out[name] = entry.marked?.value;
1422
+ this.afterwards.push(() => {
1423
+ if (this.read.has(entry))
1424
+ out[name] = this.read.get(entry);
1425
+ });
1426
+ } else if (!(entry.value instanceof MapNode) || this.kept(entry.value, env)) {
1427
+ out[name] = this.output(entry.value, env);
1428
+ }
1429
+ }
1430
+ this.checkRules(node, env);
1431
+ return out;
1432
+ }
1433
+ viewOf(node, env) {
1434
+ if (node.expression)
1435
+ return this.compute(node, env);
1436
+ if (node instanceof ListNode)
1437
+ return this.items(node, env).map(([item, own]) => this.viewOf(item, own));
1438
+ if (!(node instanceof MapNode))
1439
+ return node.value;
1440
+ const value = node.get("$value");
1441
+ if (value)
1442
+ return this.entryView(value, env);
1443
+ const out = {};
1444
+ for (const entry of this.effective(node, env)) {
1445
+ if (entry.key.keyword)
1446
+ continue;
1447
+ if (!(entry.value instanceof MapNode) || entry.key.kind !== "plain" || this.kept(entry.value, env))
1448
+ out[entry.name] = this.entryView(entry, env);
1449
+ }
1450
+ return out;
1451
+ }
1452
+ entryView(entry, env) {
1453
+ return entry.key.kind === "marked" ? this.markerValue(entry) : this.viewOf(entry.value, env);
1454
+ }
1455
+ markerValue(entry) {
1456
+ if (this.read.has(entry))
1457
+ return this.read.get(entry);
1458
+ const raw = entry.marked?.value;
1459
+ let value = null;
1460
+ if (raw !== null && raw !== undefined) {
1461
+ let text;
1462
+ try {
1463
+ text = String(raw);
1464
+ } catch (error) {
1465
+ this.fail(entry, `toString() of the value of the marker "${entry.key.marker}" failed: ${error.message}`);
1466
+ }
1467
+ value = text === "1" ? true : text === "0" ? false : text;
1468
+ }
1469
+ this.read.set(entry, value);
1470
+ return value;
1471
+ }
1472
+ kept(node, env) {
1473
+ if (!(node instanceof MapNode))
1474
+ return true;
1475
+ const condition = node.get("$if");
1476
+ const exception = node.get("$unless");
1477
+ if (condition && !this.flag(condition, env))
1478
+ return false;
1479
+ return !(exception && this.flag(exception, env));
1480
+ }
1481
+ flag(entry, env) {
1482
+ const value = this.compute(entry.value, env);
1483
+ if (typeof value !== "boolean")
1484
+ this.fail(entry, `${entry.name} must be true or false; it is ${JSON.stringify(value) ?? typeof value}. FEEL has no truthiness: compare, for example "x != null"`);
1485
+ return value;
1486
+ }
1487
+ items(list, from) {
1488
+ const env = from.for(list);
1489
+ let items = env.lists.get(list);
1490
+ if (items)
1491
+ return items;
1492
+ env.lists.set(list, items = []);
1493
+ for (const item of list.items) {
1494
+ const each = item instanceof MapNode ? item.get("$each") : undefined;
1495
+ if (!each) {
1496
+ if (this.kept(item, env))
1497
+ items.push([item, env]);
1498
+ continue;
1499
+ }
1500
+ const values = this.compute(each.value, env);
1501
+ if (!Array.isArray(values))
1502
+ this.fail(each, `$each needs a list; it is ${JSON.stringify(values) ?? typeof values}`);
1503
+ for (const value of values) {
1504
+ const own = new Env(item, value, env);
1505
+ if (this.kept(item, own))
1506
+ items.push([item, own]);
1507
+ }
1508
+ }
1509
+ return items;
1510
+ }
1511
+ effective(map, from) {
1512
+ const top = map === this.source.root;
1513
+ if (!map.get("$when"))
1514
+ return top ? this.source.data : map.entries;
1515
+ const env = from.for(map);
1516
+ let entries = env.maps.get(map);
1517
+ if (entries)
1518
+ return entries;
1519
+ const byName = new Map;
1520
+ for (const entry of top ? this.source.data : map.entries)
1521
+ if (entry.name !== "$when")
1522
+ byName.set(entry.name, entry);
1523
+ let matched = false;
1524
+ for (const item of map.cases()) {
1525
+ const applies = item.get("$else") ? !matched : this.kept(item, env);
1526
+ if (!applies)
1527
+ continue;
1528
+ if (!item.get("$else"))
1529
+ matched = true;
1530
+ for (const entry of item.entries)
1531
+ if (!entry.key.keyword)
1532
+ byName.set(entry.name, entry);
1533
+ }
1534
+ env.maps.set(map, entries = [...byName.values()]);
1535
+ return entries;
1536
+ }
1537
+ checkRules(map, env) {
1538
+ if (!map.shape || map.shape.rules.length === 0)
1539
+ return;
1540
+ const context = {};
1541
+ const entries = this.effective(map, env);
1542
+ for (const name of map.shape.fields.keys()) {
1543
+ const entry = entries.find((candidate) => candidate.name === name);
1544
+ context[name] = !entry ? null : entry.key.kind === "marked" ? entry.value.toData() : this.entryView(entry, env);
1545
+ }
1546
+ for (const rule of map.shape.rules) {
1547
+ let valid;
1548
+ try {
1549
+ valid = evaluate2(rule.formula.source, context).value;
1550
+ } catch (error) {
1551
+ valid = error.message;
1552
+ }
1553
+ if (valid !== true)
1554
+ this.broken.push(this.problem(map, rule.message));
1555
+ }
1556
+ }
1557
+ compute(node, from) {
1558
+ const env = from.for(node);
1559
+ if (env.values.has(node))
1560
+ return env.values.get(node);
1561
+ if (env.open.has(node))
1562
+ this.fail(node.entry ?? node, "reads itself, directly or through other keys");
1563
+ env.open.add(node);
1564
+ try {
1565
+ const expression = node.expression;
1566
+ const value = expression instanceof Text ? expression.render((block) => this.evaluate(block.formula, node, env)) : this.evaluate(expression.formulas[0], node, env);
1567
+ env.values.set(node, value);
1568
+ return value;
1569
+ } finally {
1570
+ env.open.delete(node);
1571
+ }
1572
+ }
1573
+ evaluate(formula, reader, env) {
1574
+ const at = reader.entry ?? reader;
1575
+ const result = this.feel(formula, reader, env);
1576
+ const warnings = [...result.warnings];
1577
+ for (const [entry, definition] of this.definitions) {
1578
+ warnings.push(...definition.warnings.map((warning) => ({ ...warning, message: `in the function "${entry.name}": ${warning.message}` })));
1579
+ definition.warnings.length = 0;
1580
+ }
1581
+ if (this.promised.length > 0) {
1582
+ const names = this.promised.splice(0).join(", ");
1583
+ this.fail(at, `the function "${names}" returned a promise; a formula cannot wait, so imported functions must be synchronous`);
1584
+ }
1585
+ const real = warnings.filter((warning) => !(warning.type === "NO_PROPERTY_FOUND" && warning.details.values.target === null));
1586
+ if (real.length > 0)
1587
+ this.fail(at, [...new Set(real.map((warning) => warning.message))].join("; "));
1588
+ return result.value;
1589
+ }
1590
+ feel(formula, reader, env) {
1591
+ const context = this.context(formula, reader, env);
1592
+ try {
1593
+ return evaluate2(formula.source, context);
1594
+ } catch (error) {
1595
+ if (error instanceof ScriptedYamlError)
1596
+ throw error;
1597
+ this.fail(reader.entry ?? reader, error.message);
1598
+ }
1599
+ }
1600
+ context(formula, reader, env) {
1601
+ const context = {};
1602
+ const keys = [];
1603
+ for (const ref of formula.refs) {
1604
+ const target = this.scope.resolve(reader, ref);
1605
+ if (target.kind === "vars") {
1606
+ const bag = context.vars ??= {};
1607
+ if (ref.path[0] === undefined)
1608
+ Object.assign(bag, this.vars);
1609
+ else
1610
+ bag[ref.path[0]] = this.vars[ref.path[0]] ?? null;
1611
+ } else if (target.kind === "each") {
1612
+ context[ref.head] = env.given(target.owner);
1613
+ } else if (target.kind === "given") {
1614
+ if (!(ref.head in context))
1615
+ context[ref.head] = null;
1616
+ } else if (target.kind === "base") {
1617
+ context.base = this.entryView(target.entry, env);
1618
+ } else if (target.kind === "function") {
1619
+ context[ref.head] = this.definition(target.entry).value;
1620
+ } else if (target.kind === "import") {
1621
+ context[ref.head] = this.imported(ref.head, ref.call);
1622
+ } else if (target.kind === "key" && !(ref.inFilter && target.entry.value === reader)) {
1623
+ const reach = this.scope.walk(target.entry, ref.path, (map, name) => this.effective(map, env).find((entry) => entry.name === name));
1624
+ keys.push({ names: [ref.head, ...reach.steps], value: () => reach.entry ? this.entryView(reach.entry, env) : this.viewOf(reach.node, env) });
1625
+ }
1626
+ }
1627
+ keys.sort((a, b) => a.names.length - b.names.length);
1628
+ const shells = new WeakSet;
1629
+ for (const key of keys) {
1630
+ let holder = context;
1631
+ let covered = false;
1632
+ for (const name of key.names.slice(0, -1)) {
1633
+ if (!(name in holder))
1634
+ shells.add(holder[name] = {});
1635
+ const next = holder[name];
1636
+ if (next === null || typeof next !== "object" || !shells.has(next)) {
1637
+ covered = true;
1638
+ break;
1639
+ }
1640
+ holder = next;
1641
+ }
1642
+ const last = key.names.at(-1);
1643
+ if (!covered && !(last in holder))
1644
+ holder[last] = key.value();
1645
+ }
1646
+ return context;
1647
+ }
1648
+ definition(entry) {
1649
+ let definition = this.definitions.get(entry);
1650
+ if (definition)
1651
+ return definition;
1652
+ if (this.defining.has(entry))
1653
+ this.fail(entry, `the function "${entry.name}" calls itself, directly or through other functions`);
1654
+ this.defining.add(entry);
1655
+ try {
1656
+ definition = this.feel(entry.value.expression.formulas[0], entry.value, this.top);
1657
+ } finally {
1658
+ this.defining.delete(entry);
1659
+ }
1660
+ this.definitions.set(entry, definition);
1661
+ return definition;
1662
+ }
1663
+ imported(name, called) {
1664
+ const imported = this.imports.get(name);
1665
+ if (!imported)
1666
+ return null;
1667
+ if (imported.document) {
1668
+ if (!this.documents.has(name))
1669
+ this.documents.set(name, imported.value.view(this.input));
1670
+ return this.documents.get(name);
1671
+ }
1672
+ const fn = imported.value;
1673
+ if (!called || typeof fn !== "function")
1674
+ return fn;
1675
+ if (!this.guards.has(name)) {
1676
+ this.guards.set(name, (...args) => {
1677
+ const result = fn(...args);
1678
+ if (result !== null && typeof result === "object" && typeof result.then === "function")
1679
+ this.promised.push(name);
1680
+ return result;
1681
+ });
1682
+ }
1683
+ return this.guards.get(name);
1684
+ }
1685
+ problem(at, message) {
1686
+ return new Problem(at.file, at.line, at.column, at.path(), message);
1687
+ }
1688
+ fail(at, message) {
1689
+ throw new ResolveFailed([this.problem(at, message)]);
1690
+ }
1691
+ }
1692
+
1693
+ // src/document.ts
1694
+ class TestResult {
1695
+ about;
1696
+ failures;
1697
+ constructor(about, failures) {
1698
+ this.about = about;
1699
+ this.failures = failures;
1700
+ }
1701
+ get passed() {
1702
+ return this.failures.length === 0;
1703
+ }
1704
+ }
1705
+
1706
+ class Document {
1707
+ file;
1708
+ source;
1709
+ imports;
1710
+ data;
1711
+ constructor(file, source, imports, markers, problems) {
1712
+ this.file = file;
1713
+ this.source = source;
1714
+ this.imports = imports;
1715
+ const build = (node) => {
1716
+ if (node.expression)
1717
+ return node.expression;
1718
+ if (node instanceof ListNode)
1719
+ return node.items.map(build);
1720
+ if (!(node instanceof MapNode))
1721
+ return node.toData();
1722
+ const data = {};
1723
+ for (const entry of node === source.root ? source.data : node.entries) {
1724
+ data[entry.name] = entry.key.kind === "marked" ? this.mark(entry, markers, problems) : build(entry.value);
1725
+ }
1726
+ return data;
1727
+ };
1728
+ this.data = build(source.root);
1729
+ }
1730
+ get variables() {
1731
+ return [...this.source.variables.declared.values()];
1732
+ }
1733
+ async resolve(options = {}) {
1734
+ return new Resolution(this.source, this.imports, options.vars ?? {}).run();
1735
+ }
1736
+ async test() {
1737
+ return this.source.tests.map((test, index) => {
1738
+ const { about, vars, expect } = test ?? {};
1739
+ const failures = [];
1740
+ try {
1741
+ const resolution = new Resolution(this.source, this.imports, vars ?? {});
1742
+ resolution.run();
1743
+ for (const [path, expected] of Object.entries(expect ?? {})) {
1744
+ const actual = resolution.probe(path);
1745
+ if (!isDeepStrictEqual(actual, expected))
1746
+ failures.push(`${path}: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
1747
+ }
1748
+ } catch (error) {
1749
+ if (!(error instanceof ScriptedYamlError))
1750
+ throw error;
1751
+ failures.push(error.message);
1752
+ }
1753
+ return new TestResult(about ?? `test ${index + 1}`, failures);
1754
+ });
1755
+ }
1756
+ view(vars) {
1757
+ return new Resolution(this.source, this.imports, vars).view();
1758
+ }
1759
+ mark(entry, markers, problems) {
1760
+ const marker = markers[entry.key.marker];
1761
+ if (!marker) {
1762
+ problems.error(entry, `the marker "${entry.key.marker}" was registered by name only; loading a file needs its function`);
1763
+ return;
1764
+ }
1765
+ try {
1766
+ const value = marker(entry.value.toData());
1767
+ entry.marked = { value };
1768
+ return value;
1769
+ } catch (error) {
1770
+ problems.error(entry, `the function of the marker "${entry.key.marker}" failed: ${error.message}`);
1771
+ return;
1772
+ }
1773
+ }
1774
+ }
1775
+ // src/scripted-yaml.ts
1776
+ import { readFileSync as readFileSync3 } from "node:fs";
1777
+ import { resolve as resolve2 } from "node:path";
1778
+
1779
+ // src/checker.ts
1780
+ class Hint {
1781
+ static for(name, candidates, otherwise = "") {
1782
+ if (candidates.some((candidate) => candidate.startsWith(`${name}-`) || candidate.endsWith(`-${name}`))) {
1783
+ return "; a key with a dash cannot be read by a formula: name it with an underscore";
1784
+ }
1785
+ const close = candidates.find((candidate) => candidate !== name && Hint.distance(name, candidate) <= Math.max(1, Math.floor(name.length / 3)));
1786
+ return close ? `; did you mean "${close}"?` : otherwise;
1787
+ }
1788
+ static distance(a, b) {
1789
+ let row = Array.from({ length: b.length + 1 }, (_, index) => index);
1790
+ for (let i = 1;i <= a.length; i++) {
1791
+ const next = [i];
1792
+ for (let j = 1;j <= b.length; j++)
1793
+ next[j] = Math.min(row[j] + 1, next[j - 1] + 1, row[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
1794
+ row = next;
1795
+ }
1796
+ return row[b.length];
1797
+ }
1798
+ }
1799
+
1800
+ class Checker {
1801
+ source;
1802
+ markers;
1803
+ problems;
1804
+ static structure = ["$value", "$if", "$unless", "$each", "$as", "$when", "$else"];
1805
+ static conditions = ["$if", "$unless", "$each"];
1806
+ static patching = ["$replace", "$before", "$after", "$add", "$remove"];
1807
+ scope;
1808
+ computed = [];
1809
+ undeclared = new Set;
1810
+ constructor(source, markers, problems) {
1811
+ this.source = source;
1812
+ this.markers = markers;
1813
+ this.problems = problems;
1814
+ this.scope = new Scope(source);
1815
+ }
1816
+ run() {
1817
+ this.checkMap(this.source.root, true);
1818
+ for (const entry of this.source.functions.values())
1819
+ this.formula(entry);
1820
+ this.checkFunctionNames();
1821
+ if (this.source.schema && this.source.data.length > 0)
1822
+ this.source.schema.check(this.source.root, this.problems);
1823
+ for (const node of this.computed)
1824
+ this.checkNames(node);
1825
+ this.checkCycles();
1826
+ }
1827
+ checkMap(map, root = false) {
1828
+ const seen = new Map;
1829
+ for (const entry of map.entries) {
1830
+ const first = seen.get(entry.name);
1831
+ if (first)
1832
+ this.problems.error(entry, `"${first.key.raw}" and "${entry.key.raw}" are the same key "${entry.name}": keep one`);
1833
+ seen.set(entry.name, entry);
1834
+ if (root && Source.rootKeywords.includes(entry.name))
1835
+ continue;
1836
+ if (entry.key.keyword && !this.keyword(entry))
1837
+ continue;
1838
+ this.checkEntry(entry);
1839
+ }
1840
+ }
1841
+ keyword(entry) {
1842
+ const name = entry.name;
1843
+ if (Source.rootKeywords.includes(name))
1844
+ this.problems.error(entry, `${name} belongs at the top of the file`);
1845
+ else if (Checker.patching.includes(name))
1846
+ this.problems.error(entry, `${name} changes a base file: it needs $extends at the top of this file`);
1847
+ else if (!Checker.structure.includes(name))
1848
+ this.problems.error(entry, `unknown keyword "${name}"${Hint.for(name, [...Source.rootKeywords, ...Checker.structure])}`);
1849
+ else if (Checker.conditions.includes(name) && entry.key.kind !== "formula")
1850
+ this.problems.error(entry, `${name} is a formula: write "${name}!"`);
1851
+ else if (name === "$as" && !(entry.value instanceof ScalarNode && LazyFormula.isName(String(entry.value.value))))
1852
+ this.problems.error(entry, "$as gives one plain name");
1853
+ else if (name === "$when")
1854
+ return this.when(entry);
1855
+ else
1856
+ return true;
1857
+ return false;
1858
+ }
1859
+ when(entry) {
1860
+ const cases = entry.value instanceof ListNode ? entry.value.items : [];
1861
+ const fine = entry.key.kind === "plain" && cases.length > 0 && cases.every((item) => item instanceof MapNode && (item.get("$if") || item.get("$unless") || item.get("$else")));
1862
+ if (!fine)
1863
+ this.problems.error(entry, "$when is a list of cases; each case has $if!, $unless! or $else");
1864
+ return fine;
1865
+ }
1866
+ checkEntry(entry) {
1867
+ if (entry.base)
1868
+ this.checkEntry(entry.base);
1869
+ if (entry.key.kind === "marked") {
1870
+ if (!this.markers.includes(entry.key.marker))
1871
+ this.problems.error(entry, `no function is registered for the marker "${entry.key.marker}"`);
1872
+ } else if (entry.key.kind === "formula") {
1873
+ this.formula(entry);
1874
+ } else {
1875
+ this.checkValue(entry.value, entry);
1876
+ }
1877
+ }
1878
+ checkValue(node, at) {
1879
+ if (node instanceof MapNode)
1880
+ this.checkMap(node);
1881
+ else if (node instanceof ListNode)
1882
+ for (const item of node.items)
1883
+ this.checkValue(item, item);
1884
+ else if (node instanceof ScalarNode && Text.in(node.value))
1885
+ this.expression(node, new Text(node.value), at);
1886
+ }
1887
+ formula(entry) {
1888
+ if (entry.value instanceof ScalarNode)
1889
+ this.expression(entry.value, new Formula(entry.formula ?? entry.value.source), entry);
1890
+ else
1891
+ this.problems.error(entry, `"${entry.key.raw}" is a formula, so its value is text; for data, remove the "!"`);
1892
+ }
1893
+ expression(node, expression, at) {
1894
+ node.expression = expression;
1895
+ this.computed.push(node);
1896
+ for (const error of expression.errors)
1897
+ this.problems.error(at, error);
1898
+ }
1899
+ checkFunctionNames() {
1900
+ for (const [name, entry] of this.source.functions) {
1901
+ if (Builtins.has(name))
1902
+ this.problems.error(entry, `the function "${name}" has the name of a FEEL built-in; give it another name`);
1903
+ if (this.source.imported.has(name))
1904
+ this.problems.error(entry, `"${name}" is both defined in $functions and imported`);
1905
+ }
1906
+ for (const [name, spec] of this.source.imported) {
1907
+ if (Builtins.has(name))
1908
+ this.problems.error(spec.entry, `the imported name "${name}" is the name of a FEEL built-in; export it under another name`);
1909
+ }
1910
+ const taken = (entry) => !entry.key.keyword && (this.source.functions.has(entry.name) || this.source.imported.has(entry.name));
1911
+ const visit = (node) => {
1912
+ if (node instanceof ListNode)
1913
+ node.items.forEach(visit);
1914
+ if (!(node instanceof MapNode))
1915
+ return;
1916
+ for (const entry of node.entries) {
1917
+ if (taken(entry))
1918
+ this.problems.error(entry, `the key "${entry.name}" has the name of a function of this file; rename one of them`);
1919
+ if (entry.key.kind === "plain")
1920
+ visit(entry.value);
1921
+ }
1922
+ };
1923
+ for (const entry of this.source.data) {
1924
+ if (taken(entry))
1925
+ this.problems.error(entry, `the key "${entry.name}" has the name of a function of this file; rename one of them`);
1926
+ if (entry.key.kind === "plain")
1927
+ visit(entry.value);
1928
+ }
1929
+ }
1930
+ at(node) {
1931
+ return node.entry ?? node;
1932
+ }
1933
+ checkNames(node) {
1934
+ const expression = node.expression;
1935
+ const reads = new Set;
1936
+ const reaches = [];
1937
+ for (const formula of expression.formulas) {
1938
+ for (const ref of formula.refs) {
1939
+ const reach = this.checkRef(node, ref);
1940
+ if (reach === false)
1941
+ continue;
1942
+ if (!ref.call)
1943
+ reads.add(ref.name);
1944
+ if (reach !== true)
1945
+ reaches.push({ ref, reach });
1946
+ }
1947
+ }
1948
+ expression.reads = [...reads];
1949
+ this.checkOverlap(node, reaches);
1950
+ }
1951
+ checkRef(node, ref) {
1952
+ const target = this.scope.resolve(node, ref);
1953
+ if (target.kind === "unknown") {
1954
+ if (ref.inFilter)
1955
+ return false;
1956
+ const what = ref.call ? "unknown function" : "unknown name";
1957
+ this.problems.error(this.at(node), `${what} "${ref.head}"${Hint.for(ref.head, this.scope.names(node), ref.call ? "" : '; for text, remove the "!"')}`);
1958
+ return false;
1959
+ }
1960
+ if (target.kind === "vars") {
1961
+ const name = ref.path[0];
1962
+ if (name !== undefined && !this.source.variables.knows(name) && !this.undeclared.has(name)) {
1963
+ this.undeclared.add(name);
1964
+ this.problems.warning(this.at(node), `the variable "${name}" is not declared in $vars; it is null unless the application passes it`);
1965
+ }
1966
+ return true;
1967
+ }
1968
+ if (target.kind !== "key")
1969
+ return true;
1970
+ if (ref.inFilter && target.entry.value === node)
1971
+ return false;
1972
+ const reach = this.scope.walk(target.entry, ref.path);
1973
+ if (reach.missing) {
1974
+ const keys = reach.missing.map.entries.map((entry) => entry.name);
1975
+ this.problems.error(this.at(node), `"${ref.name}": there is no key "${reach.missing.name}" in "${reach.missing.map.path()}"${Hint.for(reach.missing.name, keys)}`);
1976
+ }
1977
+ return reach;
1978
+ }
1979
+ checkOverlap(node, reaches) {
1980
+ for (const short of reaches) {
1981
+ for (const long of reaches) {
1982
+ if (short.ref.head !== long.ref.head || short.reach.steps.length >= long.reach.steps.length)
1983
+ continue;
1984
+ if (short.reach.rest.length > 0 || !short.reach.steps.every((step, index) => long.reach.steps[index] === step))
1985
+ continue;
1986
+ const crossed = long.reach.nodes.slice(short.reach.steps.length, -1);
1987
+ if (!crossed.some((passed) => passed instanceof ListNode || passed instanceof MapNode && passed.get("$value")))
1988
+ continue;
1989
+ const shorter = [short.ref.head, ...short.reach.steps].join(".");
1990
+ this.problems.error(this.at(node), `one formula reads "${shorter}" as a value and "${long.ref.name}" inside it; read one of them through a key of its own`);
1991
+ return;
1992
+ }
1993
+ }
1994
+ }
1995
+ needs(node) {
1996
+ const needs = [];
1997
+ for (const formula of node.expression.formulas) {
1998
+ for (const ref of formula.refs) {
1999
+ const target = this.scope.resolve(node, ref);
2000
+ if (target.kind === "function")
2001
+ needs.push(target.entry.value);
2002
+ if (target.kind === "base")
2003
+ this.under(target.entry, target.entry.value, needs);
2004
+ if (target.kind !== "key" || ref.inFilter && target.entry.value === node)
2005
+ continue;
2006
+ const reach = this.scope.walk(target.entry, ref.path);
2007
+ this.under(reach.entry, reach.node, needs);
2008
+ }
2009
+ }
2010
+ return needs;
2011
+ }
2012
+ under(entry, node, into) {
2013
+ if (entry?.key.kind === "marked")
2014
+ return;
2015
+ if (node.expression)
2016
+ into.push(node);
2017
+ if (node instanceof ListNode)
2018
+ for (const item of node.items)
2019
+ this.under(undefined, item, into);
2020
+ if (!(node instanceof MapNode))
2021
+ return;
2022
+ const value = node.get("$value");
2023
+ for (const child of value ? [value] : node.entries)
2024
+ this.under(child, child.value, into);
2025
+ }
2026
+ checkCycles() {
2027
+ const state = new Map;
2028
+ const reported = new Set;
2029
+ const visit = (node) => {
2030
+ if (state.get(node) === "done" || !node.expression)
2031
+ return;
2032
+ if (state.get(node) === "open") {
2033
+ if (!reported.has(node))
2034
+ this.problems.error(this.at(node), "reads itself, directly or through other keys");
2035
+ reported.add(node);
2036
+ return;
2037
+ }
2038
+ state.set(node, "open");
2039
+ for (const next of this.needs(node))
2040
+ visit(next);
2041
+ state.set(node, "done");
2042
+ };
2043
+ for (const node of this.computed)
2044
+ visit(node);
2045
+ }
2046
+ }
2047
+
2048
+ // src/scripted-yaml.ts
2049
+ class ScriptedYaml {
2050
+ markers = {};
2051
+ constructor(options = {}) {
2052
+ if (Array.isArray(options.markers))
2053
+ for (const name of options.markers)
2054
+ this.markers[name] = undefined;
2055
+ else
2056
+ Object.assign(this.markers, options.markers);
2057
+ }
2058
+ async validate(path) {
2059
+ try {
2060
+ return this.validateText(this.readFile(path), { file: path });
2061
+ } catch (error) {
2062
+ if (error instanceof ValidationFailed)
2063
+ return error.problems;
2064
+ throw error;
2065
+ }
2066
+ }
2067
+ async validateText(input, options = {}) {
2068
+ return this.check(input, options.file ?? "(text)").problems.list;
2069
+ }
2070
+ async load(path) {
2071
+ return this.loadText(this.readFile(path), { file: path });
2072
+ }
2073
+ async loadText(input, options = {}) {
2074
+ return this.open(input, options.file ?? "(text)", []);
2075
+ }
2076
+ check(input, file) {
2077
+ const problems = new Problems;
2078
+ const source = new SourceLoader(problems).load(input, file);
2079
+ if (source)
2080
+ new Checker(source, Object.keys(this.markers), problems).run();
2081
+ problems.sort();
2082
+ return { source, problems };
2083
+ }
2084
+ readFile(path) {
2085
+ try {
2086
+ return readFileSync3(path, "utf8");
2087
+ } catch (error) {
2088
+ throw new ValidationFailed([new Problem(path, 0, 0, "", `cannot read the file: ${error.message}`)], { cause: error });
2089
+ }
2090
+ }
2091
+ async open(input, file, chain) {
2092
+ const { source, problems } = this.check(input, file);
2093
+ if (!source || problems.failed)
2094
+ throw new ValidationFailed(problems.list);
2095
+ const host = {
2096
+ document: (absolute) => {
2097
+ if (absolute === resolve2(file) || chain.includes(absolute))
2098
+ throw new ImportFailed("the import comes back to this file: an import cycle");
2099
+ return this.open(this.readFile(absolute), absolute, [...chain, resolve2(file)]);
2100
+ }
2101
+ };
2102
+ const imports = new Map;
2103
+ for (const spec of source.imports) {
2104
+ try {
2105
+ for (const imported of await Importer.for(spec.path).load(spec, host))
2106
+ imports.set(imported.name, imported);
2107
+ } catch (error) {
2108
+ if (error instanceof ImportFailed)
2109
+ problems.error(spec.entry, error.message);
2110
+ else if (error instanceof ValidationFailed)
2111
+ problems.error(spec.entry, `cannot import "${spec.path}": ${error.message}`);
2112
+ else
2113
+ throw error;
2114
+ }
2115
+ }
2116
+ const document = new Document(file, source, imports, this.markers, problems);
2117
+ if (problems.failed)
2118
+ throw new ValidationFailed(problems.list);
2119
+ return document;
2120
+ }
2121
+ }
2122
+ // src/cli.ts
2123
+ import { readFileSync as readFileSync4 } from "node:fs";
2124
+ import { extname as extname3 } from "node:path";
2125
+ import { parseArgs } from "node:util";
2126
+ import { mdq as mdq2 } from "mdq-cli";
2127
+ class UsageError extends Error {
2128
+ }
2129
+
2130
+ class MarkedValue {
2131
+ marker;
2132
+ source;
2133
+ constructor(marker, source) {
2134
+ this.marker = marker;
2135
+ this.source = source;
2136
+ }
2137
+ toString() {
2138
+ return typeof this.source === "string" ? this.source : JSON.stringify(this.source);
2139
+ }
2140
+ }
2141
+
2142
+ class Arguments {
2143
+ files;
2144
+ markers;
2145
+ pairs;
2146
+ varsFile;
2147
+ constructor(args) {
2148
+ let parsed;
2149
+ try {
2150
+ parsed = this.read(args);
2151
+ } catch (error) {
2152
+ throw new UsageError(error.message);
2153
+ }
2154
+ this.files = parsed.positionals;
2155
+ this.markers = (parsed.values.markers ?? "").split(",").filter((name) => name !== "");
2156
+ this.pairs = parsed.values.var ?? [];
2157
+ this.varsFile = parsed.values.vars;
2158
+ }
2159
+ get vars() {
2160
+ const vars = {};
2161
+ if (this.varsFile !== undefined) {
2162
+ try {
2163
+ Object.assign(vars, JSON.parse(readFileSync4(this.varsFile, "utf8")));
2164
+ } catch (error) {
2165
+ throw new UsageError(`cannot read --vars ${this.varsFile}: ${error.message}`);
2166
+ }
2167
+ }
2168
+ for (const pair of this.pairs) {
2169
+ const at = pair.indexOf("=");
2170
+ if (at < 1)
2171
+ throw new UsageError(`--var takes NAME=value, not "${pair}"`);
2172
+ vars[pair.slice(0, at)] = pair.slice(at + 1);
2173
+ }
2174
+ return vars;
2175
+ }
2176
+ read(args) {
2177
+ return parseArgs({
2178
+ args,
2179
+ allowPositionals: true,
2180
+ options: {
2181
+ var: { type: "string", multiple: true },
2182
+ vars: { type: "string" },
2183
+ markers: { type: "string" }
2184
+ }
2185
+ });
2186
+ }
2187
+ }
2188
+
2189
+ class Command {
2190
+ read(file) {
2191
+ const text = readFileSync4(file, "utf8");
2192
+ return extname3(file) === ".md" ? mdq2(text).frontmatter() : text;
2193
+ }
2194
+ line(problem) {
2195
+ return `${problem.severity.padEnd(7)} ${problem}`;
2196
+ }
2197
+ }
2198
+
2199
+ class ValidateCommand extends Command {
2200
+ usage = "scripted-yaml validate <file>... [--markers sh,mdq]";
2201
+ async run(args) {
2202
+ if (args.files.length === 0)
2203
+ throw new UsageError("validate needs at least one file");
2204
+ const scripted = new ScriptedYaml({ markers: args.markers });
2205
+ let failed = false;
2206
+ for (const file of args.files) {
2207
+ let lines;
2208
+ try {
2209
+ const problems = await scripted.validateText(this.read(file), { file });
2210
+ failed ||= problems.some((problem) => problem.severity === "error");
2211
+ lines = problems.length > 0 ? problems.map((problem) => this.line(problem)) : [`ok ${file}`];
2212
+ } catch (error) {
2213
+ failed = true;
2214
+ lines = [`error ${file} cannot read the file: ${error.message}`];
2215
+ }
2216
+ console.log(lines.join(`
2217
+ `));
2218
+ }
2219
+ return failed ? 1 : 0;
2220
+ }
2221
+ }
2222
+
2223
+ class ParseCommand extends Command {
2224
+ usage = "scripted-yaml parse <file> [--var NAME=value]... [--vars file.json] [--markers sh,mdq]";
2225
+ async run(args) {
2226
+ const [file, ...more] = args.files;
2227
+ if (file === undefined || more.length > 0)
2228
+ throw new UsageError("parse takes one file");
2229
+ const vars = args.vars;
2230
+ const markers = {};
2231
+ for (const name of args.markers)
2232
+ markers[name] = (source) => new MarkedValue(name, source);
2233
+ try {
2234
+ const document = await new ScriptedYaml({ markers }).loadText(this.read(file), { file });
2235
+ console.log(JSON.stringify(await document.resolve({ vars }), null, 2));
2236
+ return 0;
2237
+ } catch (error) {
2238
+ if (error instanceof ScriptedYamlError)
2239
+ console.error(error.problems.map((problem) => this.line(problem)).join(`
2240
+ `));
2241
+ else
2242
+ console.error(`error ${file} ${error.message}`);
2243
+ return 1;
2244
+ }
2245
+ }
2246
+ }
2247
+
2248
+ class Cli {
2249
+ commands = { validate: new ValidateCommand, parse: new ParseCommand };
2250
+ get usage() {
2251
+ return [
2252
+ "Usage:",
2253
+ ...Object.values(this.commands).map((command) => ` ${command.usage}`),
2254
+ "",
2255
+ " validate prints the problems of each file; the exit code is 1 when there is an error",
2256
+ " parse prints the file as JSON, every formula computed from the variables given",
2257
+ "",
2258
+ ' --markers the marker names the files use (run!sh: is the marker "sh")',
2259
+ ' parse shows a marked value as { "marker": "sh", "source": "…" }',
2260
+ " --var one variable, as text; --vars reads variables from a JSON file"
2261
+ ].join(`
2262
+ `);
2263
+ }
2264
+ async run(argv) {
2265
+ const [name, ...rest] = argv;
2266
+ if (name === "--help" || name === "-h") {
2267
+ console.log(this.usage);
2268
+ return 0;
2269
+ }
2270
+ try {
2271
+ const command = name === undefined ? undefined : this.commands[name];
2272
+ if (!command)
2273
+ throw new UsageError(name === undefined ? "no command given" : `unknown command "${name}"`);
2274
+ return await command.run(new Arguments(rest));
2275
+ } catch (error) {
2276
+ if (!(error instanceof UsageError))
2277
+ throw error;
2278
+ console.error(`${error.message}
2279
+
2280
+ ${this.usage}`);
2281
+ return 2;
2282
+ }
2283
+ }
2284
+ }
2285
+ process.exitCode = await new Cli().run(process.argv.slice(2));