@agentshouse/mdmodel 0.1.0-c4fe1bc82bc55 → 0.1.0-c520180b7a33f

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