@agentshouse/mdmodel 0.1.0-c375119e5613a → 0.1.0-c3c0e116a89f8

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