@agentshouse/mdmodel 0.1.0-c04c0d681d2d4 → 0.1.0-c08b2adadd2fe

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