@agentshouse/mdmodel 0.1.0-c3208a08ee314 → 0.1.0-c36da0b08f3db

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.
Files changed (136) hide show
  1. package/API.md +282 -254
  2. package/README.md +21 -22
  3. package/dist/blocks.d.ts.map +1 -1
  4. package/dist/blocks.js +35 -20
  5. package/dist/definition.d.ts +17 -1
  6. package/dist/definition.d.ts.map +1 -1
  7. package/dist/definition.js +326 -46
  8. package/dist/divide.d.ts.map +1 -1
  9. package/dist/divide.js +27 -17
  10. package/dist/document.d.ts +7 -3
  11. package/dist/document.d.ts.map +1 -1
  12. package/dist/document.js +350 -176
  13. package/dist/event.d.ts +78 -0
  14. package/dist/event.d.ts.map +1 -0
  15. package/dist/event.js +230 -0
  16. package/dist/facts.d.ts +15 -0
  17. package/dist/facts.d.ts.map +1 -0
  18. package/dist/facts.js +38 -0
  19. package/dist/index.d.ts +26 -9
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js +15 -6
  22. package/dist/kinds.d.ts +19 -0
  23. package/dist/kinds.d.ts.map +1 -0
  24. package/dist/kinds.js +161 -0
  25. package/dist/links.d.ts +3 -0
  26. package/dist/links.d.ts.map +1 -1
  27. package/dist/links.js +108 -51
  28. package/dist/paths.d.ts +10 -1
  29. package/dist/paths.d.ts.map +1 -1
  30. package/dist/paths.js +118 -12
  31. package/dist/pattern.d.ts +10 -0
  32. package/dist/pattern.d.ts.map +1 -0
  33. package/dist/pattern.js +355 -0
  34. package/dist/project.d.ts +1 -0
  35. package/dist/project.d.ts.map +1 -1
  36. package/dist/project.js +38 -15
  37. package/dist/query/language.d.ts +254 -0
  38. package/dist/query/language.d.ts.map +1 -0
  39. package/dist/query/language.js +620 -0
  40. package/dist/query/match.d.ts +14 -0
  41. package/dist/query/match.d.ts.map +1 -0
  42. package/dist/query/match.js +147 -0
  43. package/dist/query/rows.d.ts +32 -0
  44. package/dist/query/rows.d.ts.map +1 -0
  45. package/dist/query/rows.js +161 -0
  46. package/dist/query/sql.d.ts +39 -0
  47. package/dist/query/sql.d.ts.map +1 -0
  48. package/dist/query/sql.js +284 -0
  49. package/dist/references.d.ts +2 -2
  50. package/dist/references.d.ts.map +1 -1
  51. package/dist/references.js +6 -1
  52. package/dist/reserved.d.ts +5 -0
  53. package/dist/reserved.d.ts.map +1 -0
  54. package/dist/reserved.js +20 -0
  55. package/dist/schema.d.ts +1 -0
  56. package/dist/schema.d.ts.map +1 -1
  57. package/dist/schema.js +55 -4
  58. package/dist/types.d.ts +61 -2
  59. package/dist/types.d.ts.map +1 -1
  60. package/dist/validate.d.ts +32 -2
  61. package/dist/validate.d.ts.map +1 -1
  62. package/dist/validate.js +438 -120
  63. package/dist/yaml.d.ts.map +1 -1
  64. package/dist/yaml.js +36 -3
  65. package/fixtures/README.md +26 -6
  66. package/fixtures/cases/conditional-section/context.json +1 -0
  67. package/fixtures/cases/conditional-section/expected.json +6 -0
  68. package/fixtures/cases/conditional-section/library/questions/hosting.md +9 -0
  69. package/fixtures/cases/conditional-section/library/questions/payments.md +12 -0
  70. package/fixtures/cases/conditional-section/schema.yaml +14 -0
  71. package/fixtures/cases/cross-room-link/context.json +1 -0
  72. package/fixtures/cases/cross-room-link/expected.json +1 -0
  73. package/fixtures/cases/cross-room-link/library/clients/acme.md +6 -0
  74. package/fixtures/cases/cross-room-link/schema.yaml +3 -0
  75. package/fixtures/cases/cross-room-reference/context.json +1 -0
  76. package/fixtures/cases/cross-room-reference/expected.json +1 -0
  77. package/fixtures/cases/cross-room-reference/library/clients/acme.md +9 -0
  78. package/fixtures/cases/cross-room-reference/schema.yaml +9 -0
  79. package/fixtures/cases/link-into-a-reserved-name/context.json +1 -0
  80. package/fixtures/cases/link-into-a-reserved-name/expected.json +5 -0
  81. package/fixtures/cases/link-into-a-reserved-name/library/notes/a.md +6 -0
  82. package/fixtures/cases/link-into-a-reserved-name/schema.yaml +3 -0
  83. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/context.json +1 -0
  84. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/expected.json +5 -0
  85. package/fixtures/cases/link-into-a-reserved-name-from-an-untyped-document/library/notes/b.md +5 -0
  86. package/fixtures/cases/member-on-an-untyped-document/context.json +1 -0
  87. package/fixtures/cases/member-on-an-untyped-document/expected.json +6 -0
  88. package/fixtures/cases/member-on-an-untyped-document/library/note.md +4 -0
  89. package/fixtures/cases/member-on-another-type/context.json +1 -0
  90. package/fixtures/cases/member-on-another-type/expected.json +6 -0
  91. package/fixtures/cases/member-on-another-type/library/note.md +4 -0
  92. package/fixtures/cases/member-on-another-type/schema.yaml +7 -0
  93. package/fixtures/cases/reference-into-a-reserved-name/context.json +1 -0
  94. package/fixtures/cases/reference-into-a-reserved-name/expected.json +6 -0
  95. package/fixtures/cases/reference-into-a-reserved-name/library/notes/a.md +4 -0
  96. package/fixtures/cases/reference-into-a-reserved-name/library/settings/brief.md +1 -0
  97. package/fixtures/cases/reference-into-a-reserved-name/schema.yaml +3 -0
  98. package/fixtures/cases/room-md-declares-a-type/context.json +1 -0
  99. package/fixtures/cases/room-md-declares-a-type/expected.json +6 -0
  100. package/fixtures/cases/room-md-declares-a-type/library/ROOM.md +5 -0
  101. package/fixtures/cases/typed-path-names-another-type/context.json +11 -0
  102. package/fixtures/cases/typed-path-names-another-type/expected.json +6 -0
  103. package/fixtures/cases/typed-path-names-another-type/library/settings/prefs/one.md +3 -0
  104. package/fixtures/cases/typed-path-names-another-type/schema.yaml +6 -0
  105. package/fixtures/cases/typed-path-names-its-type/context.json +11 -0
  106. package/fixtures/cases/typed-path-names-its-type/expected.json +3 -0
  107. package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/deeper/two.md +1 -0
  108. package/fixtures/cases/typed-path-names-its-type/library/settings/prefs/one.md +4 -0
  109. package/fixtures/cases/typed-path-names-its-type/schema.yaml +6 -0
  110. package/fixtures/cases/typed-path-names-no-type/context.json +11 -0
  111. package/fixtures/cases/typed-path-names-no-type/expected.json +6 -0
  112. package/fixtures/cases/typed-path-names-no-type/library/settings/prefs/one.md +1 -0
  113. package/fixtures/cases/typed-path-names-no-type/schema.yaml +6 -0
  114. package/fixtures/cases/unnamed-typed-path-checks-its-fields/context.json +11 -0
  115. package/fixtures/cases/unnamed-typed-path-checks-its-fields/expected.json +6 -0
  116. package/fixtures/cases/unnamed-typed-path-checks-its-fields/library/rooms/team/tasks/plan.md +4 -0
  117. package/fixtures/cases/unnamed-typed-path-checks-its-fields/schema.yaml +5 -0
  118. package/fixtures/cases/unnamed-typed-path-names-a-type/context.json +11 -0
  119. package/fixtures/cases/unnamed-typed-path-names-a-type/expected.json +6 -0
  120. package/fixtures/cases/unnamed-typed-path-names-a-type/library/rooms/team/tasks/plan.md +6 -0
  121. package/fixtures/cases/unnamed-typed-path-names-a-type/schema.yaml +5 -0
  122. package/fixtures/cases/unnamed-typed-path-reads-its-type/context.json +11 -0
  123. package/fixtures/cases/unnamed-typed-path-reads-its-type/expected.json +3 -0
  124. package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/plan.md +1 -0
  125. package/fixtures/cases/unnamed-typed-path-reads-its-type/library/rooms/team/tasks/plan.md +5 -0
  126. package/fixtures/cases/unnamed-typed-path-reads-its-type/schema.yaml +5 -0
  127. package/fixtures/cases/unnamed-typed-path-without-a-body/context.json +11 -0
  128. package/fixtures/cases/unnamed-typed-path-without-a-body/expected.json +5 -0
  129. package/fixtures/cases/unnamed-typed-path-without-a-body/library/rooms/team/tasks/plan.md +5 -0
  130. package/fixtures/cases/unnamed-typed-path-without-a-body/schema.yaml +5 -0
  131. package/fixtures/division.json +26 -26
  132. package/fixtures/queries.json +964 -0
  133. package/package.json +10 -3
  134. package/dist/condition.d.ts +0 -150
  135. package/dist/condition.d.ts.map +0 -1
  136. package/dist/condition.js +0 -497
package/dist/validate.js CHANGED
@@ -1,15 +1,62 @@
1
- import { ATTACHMENT, BUILT_IN_TYPE, readSchema } from './definition.js';
2
- import { parse } from './document.js';
1
+ import { ATTACHMENT, BUILT_IN_TYPE, readSchema, } from './definition.js';
2
+ import { links, parse } from './document.js';
3
+ import { MEMBER_FIELD } from './fields.js';
3
4
  import { hrefOccurrences, linkInventory } from './links.js';
4
- import { classifyHref } from './paths.js';
5
+ import { classifyHref, roomTarget } from './paths.js';
5
6
  import { references } from './references.js';
7
+ import { isDocumentPath, ROOM_DESCRIPTION } from './reserved.js';
6
8
  import { refusal } from './refuse.js';
7
- import { fitsItem, frontmatterRefusal } from './schema.js';
9
+ import { conditionedSections, fitsItem, frontmatterRefusal } from './schema.js';
10
+ import { openPatternBudget, PatternTooCostly } from './pattern.js';
8
11
  import { headingSpans, listItems } from './sections.js';
9
12
  import { bodyAfterFrontmatter, bodyStartOf } from './yaml.js';
10
13
  function refuse(file, rule, extra) {
11
14
  return { verdict: 'refuse', ...refusal(file, rule, extra) };
12
15
  }
16
+ function unmarkedMember(file, document, definition) {
17
+ return Object.hasOwn(document.fields, MEMBER_FIELD) && definition?.memberDocument !== true
18
+ ? refuse(file, 'undeclared-field', { location: `/${MEMBER_FIELD}` })
19
+ : null;
20
+ }
21
+ function reservedLink(file, document, changed) {
22
+ return (document.type !== undefined &&
23
+ isDocumentPath(file) &&
24
+ links(document, file).some((link) => !isDocumentPath(link.target) && (changed?.has(link.target) ?? true)));
25
+ }
26
+ function describedRoom(file, document) {
27
+ return file === ROOM_DESCRIPTION && document.type !== undefined
28
+ ? refuse(file, 'room-md-type', { location: '/type' })
29
+ : null;
30
+ }
31
+ function nameFits(pattern, name) {
32
+ const at = pattern.indexOf('*');
33
+ if (at === -1)
34
+ return pattern === name;
35
+ const head = pattern.slice(0, at);
36
+ const tail = pattern.slice(at + 1);
37
+ return name.length > head.length + tail.length && name.startsWith(head) && name.endsWith(tail);
38
+ }
39
+ function typedPathOf(file, typedPaths = []) {
40
+ const names = file.split('/');
41
+ return typedPaths.find(({ path }) => {
42
+ const patterns = path.split('/');
43
+ return (patterns.length === names.length &&
44
+ patterns.every((pattern, at) => nameFits(pattern, names[at])));
45
+ });
46
+ }
47
+ const HELD_TEXT = /\S/u;
48
+ function typedPathRefusal(file, content, document, typedPath) {
49
+ if (typedPath === undefined)
50
+ return null;
51
+ if (document.type !== (typedPath.named ? typedPath.type : undefined))
52
+ return refuse(file, 'unknown-type', { location: '/type' });
53
+ return typedPath.body && !HELD_TEXT.test(bodyAfterFrontmatter(content))
54
+ ? refuse(file, 'required-body')
55
+ : null;
56
+ }
57
+ function kindOf(document, typedPath) {
58
+ return typedPath !== undefined && !typedPath.named ? typedPath.type : instanceType(document);
59
+ }
13
60
  function instanceType(document) {
14
61
  const raw = document.fields.type;
15
62
  if (raw === undefined || raw === null)
@@ -18,33 +65,76 @@ function instanceType(document) {
18
65
  return null;
19
66
  return raw;
20
67
  }
21
- function reaches(file, href, to, types, binaryTargets) {
22
- const resolved = classifyHref(file, href);
23
- if (resolved.kind !== 'path')
68
+ function reaches(reference, typeOf, binary) {
69
+ if (reference.target === null)
24
70
  return false;
25
- if (to.includes(ATTACHMENT))
71
+ if (reference.to.includes(ATTACHMENT))
26
72
  return true;
27
- if (binaryTargets.has(resolved.path))
73
+ if (binary(reference.target))
74
+ return false;
75
+ const held = typeOf(reference.target);
76
+ return held === undefined || reference.to.includes(held);
77
+ }
78
+ function missing(reference, typeOf, binary) {
79
+ if (reference.to.includes(ATTACHMENT) || reference.target === null)
28
80
  return false;
29
- const held = types.get(resolved.path);
30
- return held === undefined || to.includes(held);
81
+ return typeOf(reference.target) === undefined && !binary(reference.target);
82
+ }
83
+ export function resolves(reference, typeOf, binary) {
84
+ return reaches(reference, typeOf, binary) && !missing(reference, typeOf, binary);
31
85
  }
32
- function itemHrefs(content, items) {
33
- const bodyStart = bodyStartOf(content);
34
- const { uses } = linkInventory(content.slice(bodyStart));
86
+ function usesIn(content) {
87
+ let held;
88
+ return () => {
89
+ if (held !== undefined)
90
+ return held;
91
+ const bodyStart = bodyStartOf(content);
92
+ held = linkInventory(content.slice(bodyStart)).uses.map((use) => ({
93
+ ...use,
94
+ start: bodyStart + use.start,
95
+ end: bodyStart + use.end,
96
+ }));
97
+ return held;
98
+ };
99
+ }
100
+ function itemHrefs(content, items, uses) {
35
101
  const hrefs = [];
36
102
  for (const item of items) {
37
- const link = uses.find((use) => bodyStart + use.start === item.start &&
38
- bodyStart + use.end === item.end &&
39
- content[item.start] === '[');
103
+ const link = uses().find((use) => use.start === item.start && use.end === item.end && content[item.start] === '[');
40
104
  if (link === undefined)
41
105
  return null;
42
106
  hrefs.push(link.destination.href);
43
107
  }
44
108
  return hrefs;
45
109
  }
46
- export function verify(schema) {
47
- const read = readSchema(schema);
110
+ function listReads(content, definition, spans = headingSpans(content, bodyStartOf(content))) {
111
+ const uses = usesIn(content);
112
+ const reads = [];
113
+ for (const span of spans) {
114
+ const spec = Object.hasOwn(definition.sections, span.name)
115
+ ? definition.sections[span.name]
116
+ : undefined;
117
+ if (spec?.kind !== 'list')
118
+ continue;
119
+ const listed = listItems(content, span.bodyStart, span.bodyEnd);
120
+ reads.push({
121
+ section: span.name,
122
+ items: spec.items,
123
+ listed,
124
+ hrefs: listed === null || spec.items.reference === undefined
125
+ ? null
126
+ : itemHrefs(content, listed, uses),
127
+ });
128
+ }
129
+ return reads;
130
+ }
131
+ function listedReferences(reads) {
132
+ return reads.flatMap(({ section, items, hrefs }) => items.reference === undefined || hrefs === null
133
+ ? []
134
+ : [{ section, to: items.reference, crossRoom: items.crossRoom === true, hrefs }]);
135
+ }
136
+ export function verify(schema, provided) {
137
+ const read = readSchema(schema, provided);
48
138
  if ('types' in read)
49
139
  return { verdict: 'accept', model: read };
50
140
  return {
@@ -53,114 +143,342 @@ export function verify(schema) {
53
143
  ...(read.location === undefined ? {} : { location: read.location }),
54
144
  };
55
145
  }
56
- export function validate(library, context = { binaryTargets: [] }, model = { types: {} }) {
57
- const binaryTargets = new Set(context.binaryTargets);
58
- const files = Object.keys(library).sort();
59
- const types = new Map();
60
- const documents = new Map();
61
- for (const file of files) {
62
- if (binaryTargets.has(file))
63
- return refuse(file, 'binary-target-document');
64
- const parsed = parse(library[file], file);
65
- if (!parsed.ok)
66
- return { verdict: 'refuse', ...parsed.refusal };
67
- types.set(file, parsed.document.type ?? BUILT_IN_TYPE);
68
- if (instanceType(parsed.document) !== BUILT_IN_TYPE)
69
- documents.set(file, parsed.document);
146
+ export function readTypes(schema, provided) {
147
+ const read = readSchema(schema, provided, false);
148
+ if ('types' in read)
149
+ return read;
150
+ throw new Error(`the schema reads as no types: ${read.rule} ${read.location ?? ''}`.trim());
151
+ }
152
+ function structure(file, content, document, definition) {
153
+ if (definition === undefined)
154
+ return { refusal: refuse(file, 'unknown-type', { location: '/type' }) };
155
+ const fieldFailure = frontmatterRefusal(definition, document.fields);
156
+ if (fieldFailure !== undefined) {
157
+ return { refusal: refuse(file, fieldFailure.rule, { location: fieldFailure.location }) };
70
158
  }
71
- const definitions = new Map(Object.entries(model.types));
72
- const sectionReferences = new Map();
73
- for (const [file, document] of documents) {
74
- const kind = instanceType(document);
75
- if (kind === BUILT_IN_TYPE)
76
- continue;
77
- const definition = kind === null ? undefined : definitions.get(kind);
78
- if (definition === undefined)
79
- return refuse(file, 'unknown-type', { location: '/type' });
80
- const fieldFailure = frontmatterRefusal(definition, document.fields);
81
- if (fieldFailure !== undefined) {
82
- return refuse(file, fieldFailure.rule, { location: fieldFailure.location });
83
- }
84
- const content = library[file];
85
- const spans = headingSpans(content, bodyStartOf(content));
86
- const references = [];
87
- for (const [name, spec] of Object.entries(definition.sections)) {
88
- const occurrences = spans.filter((one) => one.name === name);
89
- if (occurrences.length === 0 && spec.required)
90
- return refuse(file, 'required-section', { location: name });
91
- if (spec.kind !== 'list')
92
- continue;
93
- for (const span of occurrences) {
94
- const items = listItems(content, span.bodyStart, span.bodyEnd);
95
- if (items === null ||
96
- items.some((item) => !fitsItem(definition, name, content.slice(item.start, item.end))))
97
- return refuse(file, 'structured-section', { location: name });
98
- if (spec.items.reference === undefined)
99
- continue;
100
- const hrefs = itemHrefs(content, items);
101
- if (hrefs === null)
102
- return refuse(file, 'structured-section', { location: name });
103
- references.push({ section: name, to: spec.items.reference, hrefs });
104
- }
159
+ const spans = headingSpans(content, bodyStartOf(content));
160
+ const reads = listReads(content, definition, spans);
161
+ const conditioned = conditionedSections(definition, document.fields);
162
+ for (const [name, spec] of Object.entries(definition.sections)) {
163
+ const occurrences = spans.filter((one) => one.name === name);
164
+ if (occurrences.length === 0 && spec.required)
165
+ return { refusal: refuse(file, 'required-section', { location: name }) };
166
+ if (conditioned.includes(name) &&
167
+ occurrences.every((span) => content.slice(span.bodyStart, span.bodyEnd).trim() === ''))
168
+ return { refusal: refuse(file, 'conditional-section', { location: name }) };
169
+ for (const { items, listed, hrefs } of reads.filter((read) => read.section === name)) {
170
+ if (listed === null ||
171
+ listed.some((item) => !fitsItem(definition, name, content.slice(item.start, item.end))) ||
172
+ (items.reference !== undefined && hrefs === null))
173
+ return { refusal: refuse(file, 'structured-section', { location: name }) };
105
174
  }
106
- sectionReferences.set(file, references);
107
175
  }
108
- for (const [file, document] of documents) {
109
- const kind = instanceType(document);
110
- if (kind === null || kind === BUILT_IN_TYPE)
111
- continue;
112
- const definition = definitions.get(kind);
113
- if (definition === undefined)
114
- continue;
115
- for (const { pointer, to, value } of references(document, definition)) {
116
- if (!reaches(file, value, to, types, binaryTargets))
117
- return refuse(file, 'unresolved-reference', { location: pointer });
118
- }
119
- const body = bodyAfterFrontmatter(library[file]);
120
- for (const occurrence of hrefOccurrences(body)) {
121
- const resolved = classifyHref(file, occurrence.href);
122
- if (resolved.kind === 'invalid')
123
- return refuse(file, 'unresolved-link');
176
+ const member = unmarkedMember(file, document, definition);
177
+ if (member !== null)
178
+ return { refusal: member };
179
+ return {
180
+ sections: Object.keys(definition.sections).flatMap((name) => listedReferences(reads.filter((read) => read.section === name))),
181
+ };
182
+ }
183
+ function linked(file, content) {
184
+ return hrefOccurrences(bodyAfterFrontmatter(content)).every((occurrence) => classifyHref(file, occurrence.href).kind !== 'invalid');
185
+ }
186
+ function reached(file, href, to) {
187
+ const resolved = classifyHref(file, href);
188
+ return { target: resolved.kind === 'path' ? resolved.path : null, to };
189
+ }
190
+ function referred(file, href, to) {
191
+ const reference = reached(file, href, to);
192
+ return reference.target !== null &&
193
+ isDocumentPath(file) &&
194
+ !isDocumentPath(reference.target) &&
195
+ !to.includes(ATTACHMENT)
196
+ ? { target: null, to }
197
+ : reference;
198
+ }
199
+ function crossing(file, href, crossRoom) {
200
+ return crossRoom === true && classifyHref(file, href).kind === 'room';
201
+ }
202
+ function definitionOf(kind, model) {
203
+ return kind === null || !Object.hasOwn(model.types, kind) ? undefined : model.types[kind];
204
+ }
205
+ export function checkDocument(file, content, model, context = {}) {
206
+ const anyRoom = context.anyRoom === true;
207
+ const parsed = parse(content, file);
208
+ if (!parsed.ok)
209
+ return { ok: false };
210
+ const document = parsed.document;
211
+ const type = document.type ?? BUILT_IN_TYPE;
212
+ const typedPath = typedPathOf(file, context.typedPaths);
213
+ if (describedRoom(file, document) !== null ||
214
+ typedPathRefusal(file, content, document, typedPath) !== null ||
215
+ reservedLink(file, document))
216
+ return { ok: false };
217
+ const kind = kindOf(document, typedPath);
218
+ if (kind === BUILT_IN_TYPE)
219
+ return unmarkedMember(file, document, undefined) === null
220
+ ? { ok: true, type, typed: false, references: [] }
221
+ : { ok: false };
222
+ const definition = definitionOf(kind, model);
223
+ const read = structure(file, content, document, definition);
224
+ if ('refusal' in read || definition === undefined || !linked(file, content))
225
+ return { ok: false };
226
+ return {
227
+ ok: true,
228
+ type,
229
+ typed: true,
230
+ references: [
231
+ ...references(document, definition).flatMap(({ value, to, crossRoom }) => crossing(file, value, anyRoom || crossRoom) ? [] : [referred(file, value, to)]),
232
+ ...read.sections.flatMap(({ to, crossRoom, hrefs }) => hrefs.flatMap((href) => crossing(file, href, anyRoom || crossRoom) ? [] : [referred(file, href, to)])),
233
+ ],
234
+ };
235
+ }
236
+ export function bodyRoomLinks(body) {
237
+ const found = [];
238
+ for (const href of new Set(hrefOccurrences(body).map((occurrence) => occurrence.href))) {
239
+ const room = roomTarget(href);
240
+ if (room !== null)
241
+ found.push({ kind: 'link', href, ...room });
242
+ }
243
+ return found;
244
+ }
245
+ export function roomReferences(document, definition) {
246
+ const { content } = document;
247
+ const found = bodyRoomLinks(bodyAfterFrontmatter(content));
248
+ if (definition === undefined)
249
+ return found;
250
+ for (const { pointer, value, to, crossRoom } of references(document, definition)) {
251
+ const room = roomTarget(value);
252
+ if (room !== null)
253
+ found.push({
254
+ kind: 'field',
255
+ pointer,
256
+ value,
257
+ to,
258
+ ...room,
259
+ ...(crossRoom ? { crossRoom } : {}),
260
+ });
261
+ }
262
+ for (const { section, to, crossRoom, hrefs } of listedReferences(listReads(content, definition))) {
263
+ for (const href of hrefs) {
264
+ const room = roomTarget(href);
265
+ if (room !== null)
266
+ found.push({
267
+ kind: 'item',
268
+ section,
269
+ href,
270
+ to,
271
+ ...room,
272
+ ...(crossRoom ? { crossRoom: true } : {}),
273
+ });
124
274
  }
125
- for (const { section, to, hrefs } of sectionReferences.get(file)) {
126
- for (const href of hrefs) {
127
- if (!reaches(file, href, to, types, binaryTargets))
128
- return refuse(file, 'unresolved-reference', { location: section });
275
+ }
276
+ return found;
277
+ }
278
+ function least(refused, file, verdict) {
279
+ return refused === undefined || file < refused.file ? { file, verdict } : refused;
280
+ }
281
+ export function assess(file, parsed, binary, model = { types: {} }, context = {}) {
282
+ const unread = { file, type: undefined, targets: [], structured: null, fields: [], sections: [] };
283
+ if (binary)
284
+ return { ...unread, read: refuse(file, 'binary-target-document'), linked: true };
285
+ if (!parsed.ok)
286
+ return { ...unread, read: { verdict: 'refuse', ...parsed.refusal }, linked: true };
287
+ const document = parsed.document;
288
+ const content = document.content;
289
+ const type = document.type ?? BUILT_IN_TYPE;
290
+ const typed = { file, type, read: null, structured: null };
291
+ const typedPath = typedPathOf(file, context.typedPaths);
292
+ const described = describedRoom(file, document) ?? typedPathRefusal(file, content, document, typedPath);
293
+ if (described !== null)
294
+ return { ...typed, targets: [], structured: described, fields: [], linked: true, sections: [] };
295
+ const kind = kindOf(document, typedPath);
296
+ if (kind === BUILT_IN_TYPE) {
297
+ const structured = unmarkedMember(file, document, undefined);
298
+ return {
299
+ ...typed,
300
+ targets: [],
301
+ structured,
302
+ fields: [],
303
+ linked: !reservedLink(file, document),
304
+ sections: [],
305
+ };
306
+ }
307
+ const definition = definitionOf(kind, model);
308
+ try {
309
+ return structured(file, content, typed, document, definition, context);
310
+ }
311
+ catch (error) {
312
+ if (!(error instanceof PatternTooCostly))
313
+ throw error;
314
+ return {
315
+ ...typed,
316
+ targets: [],
317
+ structured: refuse(file, 'pattern', { location: '' }),
318
+ fields: [],
319
+ linked: true,
320
+ sections: [],
321
+ };
322
+ }
323
+ }
324
+ function structured(file, content, typed, document, definition, context) {
325
+ const read = structure(file, content, document, definition);
326
+ if ('refusal' in read || definition === undefined) {
327
+ return {
328
+ ...typed,
329
+ targets: [],
330
+ structured: 'refusal' in read ? read.refusal : null,
331
+ fields: [],
332
+ linked: true,
333
+ sections: [],
334
+ };
335
+ }
336
+ const anyRoom = context.anyRoom === true;
337
+ const fields = references(document, definition).flatMap(({ pointer, value, to, crossRoom }) => crossing(file, value, anyRoom || crossRoom)
338
+ ? []
339
+ : [{ reference: referred(file, value, to), location: pointer }]);
340
+ const sections = read.sections.flatMap(({ section, to, crossRoom, hrefs }) => hrefs.flatMap((href) => crossing(file, href, anyRoom || crossRoom)
341
+ ? []
342
+ : [{ reference: referred(file, href, to), location: section }]));
343
+ const targets = new Set();
344
+ for (const { reference } of [...fields, ...sections]) {
345
+ if (reference.target !== null)
346
+ targets.add(reference.target);
347
+ }
348
+ return {
349
+ ...typed,
350
+ targets: [...targets],
351
+ fields,
352
+ linked: linked(file, content) && !reservedLink(file, document),
353
+ sections,
354
+ };
355
+ }
356
+ export function assessReferences(file, parsed, changed, model = { types: {} }, context = {}) {
357
+ const unread = {
358
+ file,
359
+ type: undefined,
360
+ targets: [],
361
+ read: null,
362
+ structured: null,
363
+ fields: [],
364
+ linked: true,
365
+ sections: [],
366
+ };
367
+ if (!parsed.ok)
368
+ return unread;
369
+ const { document } = parsed;
370
+ const definition = definitionOf(kindOf(document, typedPathOf(file, context.typedPaths)), model);
371
+ const placed = (href, to, location) => {
372
+ const resolved = classifyHref(file, href);
373
+ return resolved.kind === 'path' && changed.has(resolved.path)
374
+ ? [{ reference: referred(file, href, to), location }]
375
+ : [];
376
+ };
377
+ const fields = definition === undefined
378
+ ? []
379
+ : references(document, definition).flatMap(({ pointer, value, to }) => placed(value, to, pointer));
380
+ const sections = definition === undefined
381
+ ? []
382
+ : listedReferences(listReads(document.content, definition)).flatMap(({ section, to, hrefs }) => hrefs.flatMap((href) => placed(href, to, section)));
383
+ const targets = new Set();
384
+ for (const { reference } of [...fields, ...sections]) {
385
+ if (reference.target !== null)
386
+ targets.add(reference.target);
387
+ }
388
+ return {
389
+ ...unread,
390
+ type: document.type ?? BUILT_IN_TYPE,
391
+ targets: [...targets],
392
+ fields,
393
+ linked: !reservedLink(file, document, changed),
394
+ sections,
395
+ };
396
+ }
397
+ export function verdicts() {
398
+ let read;
399
+ let structured;
400
+ let referred;
401
+ let missed;
402
+ const unreached = (placed, typeOf, binary) => placed.find(({ reference }) => !reaches(reference, typeOf, binary));
403
+ return {
404
+ add(assessment, typeOf, binary) {
405
+ const { file } = assessment;
406
+ if (assessment.read !== null) {
407
+ read = least(read, file, assessment.read);
408
+ return 0;
409
+ }
410
+ if (assessment.structured !== null) {
411
+ structured = least(structured, file, assessment.structured);
412
+ return 0;
413
+ }
414
+ const field = unreached(assessment.fields, typeOf, binary);
415
+ if (field !== undefined) {
416
+ referred = least(referred, file, refuse(file, 'unresolved-reference', { location: field.location }));
417
+ return 0;
418
+ }
419
+ if (!assessment.linked) {
420
+ referred = least(referred, file, refuse(file, 'unresolved-link'));
421
+ return 0;
422
+ }
423
+ const section = unreached(assessment.sections, typeOf, binary);
424
+ if (section !== undefined) {
425
+ referred = least(referred, file, refuse(file, 'unresolved-reference', { location: section.location }));
426
+ return 0;
427
+ }
428
+ const all = [...assessment.fields, ...assessment.sections];
429
+ for (const { reference } of all) {
430
+ if (!missing(reference, typeOf, binary))
431
+ continue;
432
+ const path = reference.target;
433
+ if (missed === undefined || path < missed.path)
434
+ missed = { path, referrers: [] };
435
+ if (path === missed.path && missed.referrers.at(-1) !== file)
436
+ missed.referrers.push(file);
129
437
  }
438
+ return all.length;
439
+ },
440
+ verdict() {
441
+ const refused = read ?? structured ?? referred;
442
+ if (refused !== undefined)
443
+ return refused.verdict;
444
+ if (missed !== undefined) {
445
+ return refuse(missed.path, 'referenced-missing-file', {
446
+ referrers: [...missed.referrers].sort(),
447
+ });
448
+ }
449
+ return { verdict: 'accept' };
450
+ },
451
+ };
452
+ }
453
+ export function* validating(library, context = { binaryTargets: [] }, model = { types: {} }) {
454
+ const binaryTargets = new Set(context.binaryTargets);
455
+ const binary = (path) => binaryTargets.has(path);
456
+ const types = new Map();
457
+ const assessments = [];
458
+ const closeBudget = openPatternBudget();
459
+ try {
460
+ for (const [file, content] of library) {
461
+ const assessment = assess(file, parse(content, file), binary(file), model, context);
462
+ if (assessment.type !== undefined)
463
+ types.set(file, assessment.type);
464
+ assessments.push(assessment);
465
+ yield { path: file, resolved: 0 };
130
466
  }
131
- }
132
- const missing = new Map();
133
- for (const [file, document] of documents) {
134
- const kind = instanceType(document);
135
- if (kind === null || kind === BUILT_IN_TYPE)
136
- continue;
137
- const definition = definitions.get(kind);
138
- if (definition === undefined)
139
- continue;
140
- const named = [
141
- ...references(document, definition),
142
- ...sectionReferences
143
- .get(file)
144
- .flatMap(({ to, hrefs }) => hrefs.map((value) => ({ value, to }))),
145
- ];
146
- for (const { value, to } of named) {
147
- if (to.includes(ATTACHMENT))
148
- continue;
149
- const resolved = classifyHref(file, value);
150
- if (resolved.kind !== 'path')
151
- continue;
152
- if (types.has(resolved.path) || binaryTargets.has(resolved.path))
153
- continue;
154
- const referrers = missing.get(resolved.path) ?? [];
155
- referrers.push(file);
156
- missing.set(resolved.path, referrers);
467
+ const judged = verdicts();
468
+ for (const assessment of assessments) {
469
+ const resolved = judged.add(assessment, (path) => types.get(path), binary);
470
+ yield { path: assessment.file, resolved };
157
471
  }
472
+ return judged.verdict();
158
473
  }
159
- const missingPaths = [...missing.keys()].sort();
160
- if (missingPaths.length > 0) {
161
- const path = missingPaths[0];
162
- const referrers = [...new Set(missing.get(path) ?? [])].sort();
163
- return refuse(path, 'referenced-missing-file', { referrers });
474
+ finally {
475
+ closeBudget();
164
476
  }
165
- return { verdict: 'accept' };
477
+ }
478
+ export function validate(library, context = { binaryTargets: [] }, model = { types: {} }) {
479
+ const steps = validating(new Map(Object.entries(library)), context, model);
480
+ let step = steps.next();
481
+ while (step.done !== true)
482
+ step = steps.next();
483
+ return step.value;
166
484
  }
@@ -1 +1 @@
1
- {"version":3,"file":"yaml.d.ts","sourceRoot":"","sources":["../src/yaml.ts"],"names":[],"mappings":"AAAA,qBAAa,SAAU,SAAQ,KAAK;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM;IAAnC,YAAqB,MAAM,EAAE,MAAM,EAGlC;CACF;AAED,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,IAAI,EAAE,QAAQ,CAAC;IACxB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;IACjD,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG;IACpB,QAAQ,CAAC,IAAI,EAAE,KAAK,CAAC;IACrB,QAAQ,CAAC,KAAK,EAAE,SAAS,QAAQ,EAAE,CAAC;IACpC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,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