@agentshouse/mdmodel 0.1.0-cabd61587647f → 0.1.0-cae8e240cefab

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