@rse/specbook 1.2.2 → 1.2.3

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.
@@ -134,16 +134,16 @@ export const resolveSet = (index, reference) => {
134
134
  subtree, which beats one in another artifact) */
135
135
  const nearest = (index, matches, from) => {
136
136
  const chain = chainOf(index, from);
137
- const distance = (object) => {
137
+ const proximity = (object) => {
138
138
  const other = chainOf(index, object);
139
139
  let i = 0;
140
140
  while (i < chain.length && i < other.length && chain[i] === other[i])
141
141
  i++;
142
142
  return i;
143
143
  };
144
- const distances = matches.map((object) => distance(object));
145
- const best = Math.max(...distances);
146
- return matches.filter((_, i) => distances[i] === best);
144
+ const proximities = matches.map((object) => proximity(object));
145
+ const best = Math.max(...proximities);
146
+ return matches.filter((_, i) => proximities[i] === best);
147
147
  };
148
148
  /* resolve a reference into a unique target: a single segment tries the
149
149
  ordered variants (1) id/anchor, (2) name, (3) "KIND:name-or-id", where
@@ -24,6 +24,7 @@ export declare class ParseContext {
24
24
  parenProps: WeakMap<SpecObject, SpecProperty>;
25
25
  assets: Set<string>;
26
26
  diagnose(file: string, line: number, message: string, severity?: DiagnosticSeverity): void;
27
+ metaOf(object: SpecObject): ObjectMeta;
27
28
  }
28
29
  export declare const becauseRegex: RegExp;
29
30
  export declare const embeddingRegex: RegExp;
@@ -20,6 +20,11 @@ export class ParseContext {
20
20
  diagnose(file, line, message, severity = "error") {
21
21
  this.diagnostics.push({ file, line, column: 1, severity, message });
22
22
  }
23
+ /* the source location of an object (defensively falling back onto
24
+ line 1 of no file for an object unknown to the syntactic phase) */
25
+ metaOf(object) {
26
+ return this.objectMeta.get(object) ?? { file: "", line: 1 };
27
+ }
23
28
  }
24
29
  /* the marker separating a statement from its rationale */
25
30
  export const becauseRegex = /,\s*(?:\*\*BECAUSE\*\*|BECAUSE)\s+/;
@@ -154,7 +154,7 @@ const checkSiblingFlags = (ctx, object, childs, meta) => {
154
154
  if (first === undefined)
155
155
  seen.set(value, sibling);
156
156
  else {
157
- const siblingMeta = ctx.objectMeta.get(sibling) ?? { file: "", line: 1 };
157
+ const siblingMeta = ctx.metaOf(sibling);
158
158
  ctx.diagnose(siblingMeta.file, ctx.propMeta.get(property)?.line ?? siblingMeta.line, `value "${value}" of unique property "${prop.name}" on ${sibling.kind} "${sibling.name}" ` +
159
159
  `already used by preceding ${sibling.kind} "${first.name}"`);
160
160
  }
@@ -167,7 +167,7 @@ const checkSiblingFlags = (ctx, object, childs, meta) => {
167
167
  };
168
168
  /* validate a single object (and recursively its childs) against its schema */
169
169
  const validateObject = (ctx, object, schema, level) => {
170
- const meta = ctx.objectMeta.get(object) ?? { file: "", line: 1 };
170
+ const meta = ctx.metaOf(object);
171
171
  const props = schema.props ?? [];
172
172
  /* check the name convention (the configured name of non-artifact
173
173
  objects is a regular expression pattern) */
@@ -201,7 +201,7 @@ const validateObject = (ctx, object, schema, level) => {
201
201
  /* check the configured child objects */
202
202
  const childs = schema.childs ?? [];
203
203
  for (const child of object.childs) {
204
- const childMeta = ctx.objectMeta.get(child) ?? { file: "", line: 1 };
204
+ const childMeta = ctx.metaOf(child);
205
205
  const childSchema = childs.find((c) => c.kind === child.kind);
206
206
  if (childSchema === undefined) {
207
207
  ctx.diagnose(childMeta.file, childMeta.line, `unknown object kind "${child.kind}" below ${object.kind} "${object.name}"`);
@@ -281,7 +281,7 @@ const checkIds = (ctx, objects) => {
281
281
  if (first === undefined)
282
282
  seen.set(key, object);
283
283
  else {
284
- const meta = ctx.objectMeta.get(object) ?? { file: "", line: 1 };
284
+ const meta = ctx.metaOf(object);
285
285
  ctx.diagnose(meta.file, meta.line, `id "${object.id}" of ${object.kind} "${object.name}" collides with preceding ${object.kind} "${first.name}"`);
286
286
  }
287
287
  checkIds(ctx, object.childs);
@@ -352,7 +352,7 @@ const checkAutomata = (ctx, schemas) => {
352
352
  const reachable = closure(initials, succs);
353
353
  const coreachable = closure(finals, preds);
354
354
  for (const node of nodes) {
355
- const meta = ctx.objectMeta.get(node) ?? { file: "", line: 1 };
355
+ const meta = ctx.metaOf(node);
356
356
  const name = `${node.kind} "${node.name}" of ${object.kind} "${object.name}"`;
357
357
  if (initials.length > 0 && !reachable.has(node))
358
358
  ctx.diagnose(meta.file, meta.line, `${name} is not reachable from the initial ${node.kind} through any ${automaton.edges}`);
@@ -383,7 +383,7 @@ const checkRelations = (ctx, schemas) => {
383
383
  for (const [prop, objects] of groups) {
384
384
  const members = new Set(objects);
385
385
  const locate = (object) => {
386
- const meta = ctx.objectMeta.get(object) ?? { file: "", line: 1 };
386
+ const meta = ctx.metaOf(object);
387
387
  const property = findProp(ctx, object, prop.name);
388
388
  const line = property !== undefined ? ctx.propMeta.get(property)?.line : undefined;
389
389
  return { file: meta.file, line: line ?? meta.line };
@@ -470,7 +470,7 @@ const checkReferenced = (ctx, specification, schemas) => {
470
470
  const covered = Array.from(referrers.get(object) ?? [])
471
471
  .some((referrer) => chainOf(ctx.linkIndex, referrer).some((o) => admissible.has(o)));
472
472
  if (!covered) {
473
- const meta = ctx.objectMeta.get(object) ?? { file: "", line: 1 };
473
+ const meta = ctx.metaOf(object);
474
474
  ctx.diagnose(meta.file, meta.line, `${object.kind} "${object.name}" is not referenced from any object matching ` +
475
475
  schema.referenced.map((entry) => `"${entry}"`).join(" or "), "warning");
476
476
  }
@@ -504,7 +504,7 @@ export const validate = (ctx, specification, config) => {
504
504
  const position = new Map();
505
505
  for (const artifact of specification.artifacts) {
506
506
  for (const object of artifact.objects) {
507
- const meta = ctx.objectMeta.get(object) ?? { file: "", line: 1 };
507
+ const meta = ctx.metaOf(object);
508
508
  const schema = schemas.get(object);
509
509
  if (schema === undefined) {
510
510
  ctx.diagnose(meta.file, meta.line, `unknown artifact "${object.kind}: ${object.name}" (id "${object.id}")`);
@@ -545,7 +545,7 @@ export const validateReferences = (ctx, specification) => {
545
545
  }
546
546
  };
547
547
  const walk = (object) => {
548
- const meta = ctx.objectMeta.get(object) ?? { file: "", line: 1 };
548
+ const meta = ctx.metaOf(object);
549
549
  check(object, object.name, meta.file, meta.line);
550
550
  for (const property of object.properties)
551
551
  check(object, property.value, meta.file, ctx.propMeta.get(property)?.line ?? meta.line);
@@ -280,17 +280,17 @@ const parseHeading = (ctx, token, state, stamps, file, line) => {
280
280
  state.group = null;
281
281
  if (heading.malformed !== undefined)
282
282
  ctx.diagnose(file, line, `malformed anchor "${heading.malformed}" in heading`);
283
- if (heading.name === "" && depth > 1) {
284
- const parent = state.stack[depth - 2];
285
- if (parent === undefined) {
286
- ctx.diagnose(file, line, `heading level ${depth} without parent object`);
287
- state.current = null;
288
- }
289
- else {
290
- state.group = { parent, kind: heading.kind };
291
- state.current = parent;
292
- state.stack.length = depth - 1;
293
- }
283
+ /* a nested heading requires the enclosing object one level above */
284
+ const parent = depth > 1 ? state.stack[depth - 2] : undefined;
285
+ if (depth > 1 && parent === undefined) {
286
+ ctx.diagnose(file, line, `heading level ${depth} without parent object`);
287
+ state.current = null;
288
+ return;
289
+ }
290
+ if (parent !== undefined && heading.name === "") {
291
+ state.group = { parent, kind: heading.kind };
292
+ state.current = parent;
293
+ state.stack.length = depth - 1;
294
294
  return;
295
295
  }
296
296
  const object = {
@@ -307,17 +307,10 @@ const parseHeading = (ctx, token, state, stamps, file, line) => {
307
307
  if (heading.primary)
308
308
  object.primary = true;
309
309
  ctx.objectMeta.set(object, { file, line });
310
- if (depth === 1)
310
+ if (parent === undefined)
311
311
  state.artifacts.push({ ...stamps, objects: [object] });
312
- else {
313
- const parent = state.stack[depth - 2];
314
- if (parent === undefined) {
315
- ctx.diagnose(file, line, `heading level ${depth} without parent object`);
316
- state.current = null;
317
- return;
318
- }
312
+ else
319
313
  parent.childs.push(object);
320
- }
321
314
  state.stack.length = depth - 1;
322
315
  state.stack[depth - 1] = object;
323
316
  state.current = object;
@@ -362,7 +355,9 @@ export const parseFile = (ctx, source) => {
362
355
  const flush = () => {
363
356
  const current = state.current;
364
357
  if (current !== null && parts.length > 0) {
365
- if (current.description === undefined)
358
+ if (state.group !== null)
359
+ ctx.diagnose(source.file, partsLine, `content ignored below ${state.group.kind} container of ${current.kind} "${current.name}"`);
360
+ else if (current.description === undefined)
366
361
  current.description = splitDescription(parts.join("\n\n").trim());
367
362
  else
368
363
  ctx.diagnose(source.file, partsLine, `content ignored, as ${current.kind} "${current.name}" already carries a description`);
@@ -1,5 +1,10 @@
1
+ import { Spec } from "./specbook-format-spec.js";
1
2
  import { type Schema } from "./specbook-format-schema.js";
2
3
  import { type SourceFile, type ParseResult } from "./specbook-parse-common.js";
3
4
  export { type SourceFile, type ParseResult } from "./specbook-parse-common.js";
4
5
  export { resolveArtifact } from "./specbook-parse-semantic.js";
5
6
  export declare const parseSpecification: (sources: SourceFile[], config?: Schema) => ParseResult;
7
+ export declare const specStatistics: (specification: Spec) => {
8
+ objects: number;
9
+ links: number;
10
+ };
@@ -5,7 +5,7 @@
5
5
  */
6
6
  import * as v from "valibot";
7
7
  import { Spec } from "./specbook-format-spec.js";
8
- import { buildLinkIndex } from "./specbook-link.js";
8
+ import { buildLinkIndex, referenceRegex, plainText } from "./specbook-link.js";
9
9
  import { ParseContext } from "./specbook-parse-common.js";
10
10
  import { parseFile } from "./specbook-parse-syntax.js";
11
11
  import { validate, validateReferences } from "./specbook-parse-semantic.js";
@@ -38,3 +38,22 @@ export const parseSpecification = (sources, config) => {
38
38
  }
39
39
  return { specification, diagnostics: ctx.diagnostics, assets: Array.from(ctx.assets) };
40
40
  };
41
+ /* the statistics of a specification: the number of defined objects
42
+ (recursively) and of relationships, i.e. the Wiki-style references
43
+ in the names, property values, descriptions, and rationales */
44
+ export const specStatistics = (specification) => {
45
+ let objects = 0;
46
+ let links = 0;
47
+ const walk = (object) => {
48
+ objects++;
49
+ const texts = [object.name, ...object.properties.map((p) => p.value)];
50
+ if (object.description !== undefined)
51
+ texts.push(object.description.description, object.description.rationale ?? "");
52
+ for (const text of texts)
53
+ links += (plainText(text).match(referenceRegex) ?? []).length;
54
+ object.childs.forEach(walk);
55
+ };
56
+ for (const artifact of specification.artifacts)
57
+ artifact.objects.forEach(walk);
58
+ return { objects, links };
59
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rse/specbook",
3
- "version": "1.2.2",
3
+ "version": "1.2.3",
4
4
  "publishConfig": { "access": "public" },
5
5
  "description": "Markdown-based Specification Format (API, CLI and MCP service)",
6
6
  "license": "Apache-2.0",
@@ -24,7 +24,7 @@
24
24
  "pdf-lib": "1.17.1",
25
25
  "subset-font": "2.7.0",
26
26
  "@rse/mrcs": "0.9.2",
27
- "@rse/gradia": "1.1.7",
27
+ "@rse/gradia": "1.1.9",
28
28
  "@toon-format/toon": "4.1.1",
29
29
  "valibot": "1.4.2",
30
30
  "nunjucks": "3.2.4",
@@ -39,7 +39,7 @@
39
39
  "chokidar": "5.0.0",
40
40
  "glob": "13.0.6",
41
41
  "es-toolkit": "1.52.0",
42
- "fastify": "5.12.1",
42
+ "fastify": "5.12.3",
43
43
  "@fastify/websocket": "11.3.0",
44
44
  "@modelcontextprotocol/sdk": "1.30.0",
45
45
  "zod": "4.5.4"