@driftsys/markspec 0.1.0 → 0.3.0

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 (242) hide show
  1. package/README.md +32 -8
  2. package/esm/_dnt.polyfills.d.ts +101 -0
  3. package/esm/_dnt.polyfills.d.ts.map +1 -0
  4. package/esm/_dnt.polyfills.js +127 -0
  5. package/esm/compiler/inverses.d.ts +22 -0
  6. package/esm/compiler/inverses.d.ts.map +1 -0
  7. package/esm/compiler/inverses.js +147 -0
  8. package/esm/compiler/link_target.d.ts +19 -0
  9. package/esm/compiler/link_target.d.ts.map +1 -0
  10. package/esm/compiler/link_target.js +66 -0
  11. package/esm/compiler/mod.d.ts +43 -0
  12. package/esm/compiler/mod.d.ts.map +1 -0
  13. package/esm/compiler/mod.js +163 -0
  14. package/esm/compiler/schema.d.ts +38 -0
  15. package/esm/compiler/schema.d.ts.map +1 -0
  16. package/esm/compiler/schema.js +44 -0
  17. package/esm/config/markspec.d.ts +51 -0
  18. package/esm/config/markspec.d.ts.map +1 -0
  19. package/esm/config/markspec.js +216 -0
  20. package/esm/config/mod.d.ts +48 -0
  21. package/esm/config/mod.d.ts.map +1 -0
  22. package/esm/config/mod.js +243 -0
  23. package/esm/formatter/mod.d.ts +65 -0
  24. package/esm/formatter/mod.d.ts.map +1 -0
  25. package/esm/formatter/mod.js +306 -0
  26. package/esm/mod.d.ts +19 -0
  27. package/esm/mod.d.ts.map +1 -1
  28. package/esm/mod.js +18 -0
  29. package/esm/model/attributes.d.ts +47 -0
  30. package/esm/model/attributes.d.ts.map +1 -0
  31. package/esm/model/attributes.js +97 -0
  32. package/esm/model/mod.d.ts +381 -0
  33. package/esm/model/mod.d.ts.map +1 -0
  34. package/esm/model/mod.js +73 -0
  35. package/esm/model/profile.d.ts +189 -0
  36. package/esm/model/profile.d.ts.map +1 -0
  37. package/esm/model/profile.js +31 -0
  38. package/esm/parser/attributes.d.ts +48 -0
  39. package/esm/parser/attributes.d.ts.map +1 -0
  40. package/esm/parser/attributes.js +131 -0
  41. package/esm/parser/captions.d.ts +25 -0
  42. package/esm/parser/captions.d.ts.map +1 -0
  43. package/esm/parser/captions.js +148 -0
  44. package/esm/parser/directives.d.ts +29 -0
  45. package/esm/parser/directives.d.ts.map +1 -0
  46. package/esm/parser/directives.js +88 -0
  47. package/esm/parser/frontmatter.d.ts +53 -0
  48. package/esm/parser/frontmatter.d.ts.map +1 -0
  49. package/esm/parser/frontmatter.js +185 -0
  50. package/esm/parser/grammars.d.ts +22 -0
  51. package/esm/parser/grammars.d.ts.map +1 -0
  52. package/esm/parser/grammars.js +70 -0
  53. package/esm/parser/markdown.d.ts +32 -0
  54. package/esm/parser/markdown.d.ts.map +1 -0
  55. package/esm/parser/markdown.js +225 -0
  56. package/esm/parser/mod.d.ts +73 -0
  57. package/esm/parser/mod.d.ts.map +1 -0
  58. package/esm/parser/mod.js +89 -0
  59. package/esm/parser/references.d.ts +24 -0
  60. package/esm/parser/references.d.ts.map +1 -0
  61. package/esm/parser/references.js +71 -0
  62. package/esm/parser/remark.d.ts +9 -0
  63. package/esm/parser/remark.d.ts.map +1 -0
  64. package/esm/parser/remark.js +11 -0
  65. package/esm/parser/source.d.ts +33 -0
  66. package/esm/parser/source.d.ts.map +1 -0
  67. package/esm/parser/source.js +192 -0
  68. package/esm/profile/cache.d.ts +29 -0
  69. package/esm/profile/cache.d.ts.map +1 -0
  70. package/esm/profile/cache.js +45 -0
  71. package/esm/profile/chain.d.ts +42 -0
  72. package/esm/profile/chain.d.ts.map +1 -0
  73. package/esm/profile/chain.js +193 -0
  74. package/esm/profile/git-cache.d.ts +73 -0
  75. package/esm/profile/git-cache.d.ts.map +1 -0
  76. package/esm/profile/git-cache.js +96 -0
  77. package/esm/profile/load.d.ts +28 -0
  78. package/esm/profile/load.d.ts.map +1 -0
  79. package/esm/profile/load.js +50 -0
  80. package/esm/profile/manifest.d.ts +21 -0
  81. package/esm/profile/manifest.d.ts.map +1 -0
  82. package/esm/profile/manifest.js +830 -0
  83. package/esm/profile/merge.d.ts +26 -0
  84. package/esm/profile/merge.d.ts.map +1 -0
  85. package/esm/profile/merge.js +518 -0
  86. package/esm/profile/mod.d.ts +23 -0
  87. package/esm/profile/mod.d.ts.map +1 -0
  88. package/esm/profile/mod.js +14 -0
  89. package/esm/profile/npm.d.ts +51 -0
  90. package/esm/profile/npm.d.ts.map +1 -0
  91. package/esm/profile/npm.js +186 -0
  92. package/esm/profile/resolver.d.ts +60 -0
  93. package/esm/profile/resolver.d.ts.map +1 -0
  94. package/esm/profile/resolver.js +132 -0
  95. package/esm/reporter/mod.d.ts +31 -0
  96. package/esm/reporter/mod.d.ts.map +1 -0
  97. package/esm/reporter/mod.js +173 -0
  98. package/esm/validator/attributes.d.ts +43 -0
  99. package/esm/validator/attributes.d.ts.map +1 -0
  100. package/esm/validator/attributes.js +169 -0
  101. package/esm/validator/mod.d.ts +44 -0
  102. package/esm/validator/mod.d.ts.map +1 -0
  103. package/esm/validator/mod.js +214 -0
  104. package/esm/validator/normalize.d.ts +21 -0
  105. package/esm/validator/normalize.d.ts.map +1 -0
  106. package/esm/validator/normalize.js +58 -0
  107. package/esm/validator/pattern.d.ts +23 -0
  108. package/esm/validator/pattern.d.ts.map +1 -0
  109. package/esm/validator/pattern.js +44 -0
  110. package/esm/validator/pipeline.d.ts +30 -0
  111. package/esm/validator/pipeline.d.ts.map +1 -0
  112. package/esm/validator/pipeline.js +115 -0
  113. package/esm/validator/traceability.d.ts +48 -0
  114. package/esm/validator/traceability.d.ts.map +1 -0
  115. package/esm/validator/traceability.js +141 -0
  116. package/esm/validator/types.d.ts +48 -0
  117. package/esm/validator/types.d.ts.map +1 -0
  118. package/esm/validator/types.js +140 -0
  119. package/esm/validator/value_types.d.ts +20 -0
  120. package/esm/validator/value_types.d.ts.map +1 -0
  121. package/esm/validator/value_types.js +126 -0
  122. package/package.json +8 -1
  123. package/script/_dnt.polyfills.d.ts +101 -0
  124. package/script/_dnt.polyfills.d.ts.map +1 -0
  125. package/script/_dnt.polyfills.js +130 -0
  126. package/script/compiler/inverses.d.ts +22 -0
  127. package/script/compiler/inverses.d.ts.map +1 -0
  128. package/script/compiler/inverses.js +150 -0
  129. package/script/compiler/link_target.d.ts +19 -0
  130. package/script/compiler/link_target.d.ts.map +1 -0
  131. package/script/compiler/link_target.js +69 -0
  132. package/script/compiler/mod.d.ts +43 -0
  133. package/script/compiler/mod.d.ts.map +1 -0
  134. package/script/compiler/mod.js +169 -0
  135. package/script/compiler/schema.d.ts +38 -0
  136. package/script/compiler/schema.d.ts.map +1 -0
  137. package/script/compiler/schema.js +47 -0
  138. package/script/config/markspec.d.ts +51 -0
  139. package/script/config/markspec.d.ts.map +1 -0
  140. package/script/config/markspec.js +222 -0
  141. package/script/config/mod.d.ts +48 -0
  142. package/script/config/mod.d.ts.map +1 -0
  143. package/script/config/mod.js +250 -0
  144. package/script/formatter/mod.d.ts +65 -0
  145. package/script/formatter/mod.d.ts.map +1 -0
  146. package/script/formatter/mod.js +313 -0
  147. package/script/mod.d.ts +19 -0
  148. package/script/mod.d.ts.map +1 -1
  149. package/script/mod.js +65 -2
  150. package/script/model/attributes.d.ts +47 -0
  151. package/script/model/attributes.d.ts.map +1 -0
  152. package/script/model/attributes.js +101 -0
  153. package/script/model/mod.d.ts +381 -0
  154. package/script/model/mod.d.ts.map +1 -0
  155. package/script/model/mod.js +83 -0
  156. package/script/model/profile.d.ts +189 -0
  157. package/script/model/profile.d.ts.map +1 -0
  158. package/script/model/profile.js +34 -0
  159. package/script/parser/attributes.d.ts +48 -0
  160. package/script/parser/attributes.d.ts.map +1 -0
  161. package/script/parser/attributes.js +137 -0
  162. package/script/parser/captions.d.ts +25 -0
  163. package/script/parser/captions.d.ts.map +1 -0
  164. package/script/parser/captions.js +151 -0
  165. package/script/parser/directives.d.ts +29 -0
  166. package/script/parser/directives.d.ts.map +1 -0
  167. package/script/parser/directives.js +91 -0
  168. package/script/parser/frontmatter.d.ts +53 -0
  169. package/script/parser/frontmatter.d.ts.map +1 -0
  170. package/script/parser/frontmatter.js +188 -0
  171. package/script/parser/grammars.d.ts +22 -0
  172. package/script/parser/grammars.d.ts.map +1 -0
  173. package/script/parser/grammars.js +77 -0
  174. package/script/parser/markdown.d.ts +32 -0
  175. package/script/parser/markdown.d.ts.map +1 -0
  176. package/script/parser/markdown.js +228 -0
  177. package/script/parser/mod.d.ts +73 -0
  178. package/script/parser/mod.d.ts.map +1 -0
  179. package/script/parser/mod.js +101 -0
  180. package/script/parser/references.d.ts +24 -0
  181. package/script/parser/references.d.ts.map +1 -0
  182. package/script/parser/references.js +74 -0
  183. package/script/parser/remark.d.ts +9 -0
  184. package/script/parser/remark.d.ts.map +1 -0
  185. package/script/parser/remark.js +17 -0
  186. package/script/parser/source.d.ts +33 -0
  187. package/script/parser/source.d.ts.map +1 -0
  188. package/script/parser/source.js +198 -0
  189. package/script/profile/cache.d.ts +29 -0
  190. package/script/profile/cache.d.ts.map +1 -0
  191. package/script/profile/cache.js +48 -0
  192. package/script/profile/chain.d.ts +42 -0
  193. package/script/profile/chain.d.ts.map +1 -0
  194. package/script/profile/chain.js +229 -0
  195. package/script/profile/git-cache.d.ts +73 -0
  196. package/script/profile/git-cache.d.ts.map +1 -0
  197. package/script/profile/git-cache.js +104 -0
  198. package/script/profile/load.d.ts +28 -0
  199. package/script/profile/load.d.ts.map +1 -0
  200. package/script/profile/load.js +53 -0
  201. package/script/profile/manifest.d.ts +21 -0
  202. package/script/profile/manifest.d.ts.map +1 -0
  203. package/script/profile/manifest.js +833 -0
  204. package/script/profile/merge.d.ts +26 -0
  205. package/script/profile/merge.d.ts.map +1 -0
  206. package/script/profile/merge.js +521 -0
  207. package/script/profile/mod.d.ts +23 -0
  208. package/script/profile/mod.d.ts.map +1 -0
  209. package/script/profile/mod.js +31 -0
  210. package/script/profile/npm.d.ts +51 -0
  211. package/script/profile/npm.d.ts.map +1 -0
  212. package/script/profile/npm.js +191 -0
  213. package/script/profile/resolver.d.ts +60 -0
  214. package/script/profile/resolver.d.ts.map +1 -0
  215. package/script/profile/resolver.js +136 -0
  216. package/script/reporter/mod.d.ts +31 -0
  217. package/script/reporter/mod.d.ts.map +1 -0
  218. package/script/reporter/mod.js +176 -0
  219. package/script/validator/attributes.d.ts +43 -0
  220. package/script/validator/attributes.d.ts.map +1 -0
  221. package/script/validator/attributes.js +173 -0
  222. package/script/validator/mod.d.ts +44 -0
  223. package/script/validator/mod.d.ts.map +1 -0
  224. package/script/validator/mod.js +231 -0
  225. package/script/validator/normalize.d.ts +21 -0
  226. package/script/validator/normalize.d.ts.map +1 -0
  227. package/script/validator/normalize.js +61 -0
  228. package/script/validator/pattern.d.ts +23 -0
  229. package/script/validator/pattern.d.ts.map +1 -0
  230. package/script/validator/pattern.js +47 -0
  231. package/script/validator/pipeline.d.ts +30 -0
  232. package/script/validator/pipeline.d.ts.map +1 -0
  233. package/script/validator/pipeline.js +118 -0
  234. package/script/validator/traceability.d.ts +48 -0
  235. package/script/validator/traceability.d.ts.map +1 -0
  236. package/script/validator/traceability.js +146 -0
  237. package/script/validator/types.d.ts +48 -0
  238. package/script/validator/types.d.ts.map +1 -0
  239. package/script/validator/types.js +144 -0
  240. package/script/validator/value_types.d.ts +20 -0
  241. package/script/validator/value_types.d.ts.map +1 -0
  242. package/script/validator/value_types.js +129 -0
@@ -0,0 +1,115 @@
1
+ /**
2
+ * @module core/validator/pipeline
3
+ *
4
+ * Validator pipeline. Composes Stage 1 (core hygiene — existing `validate`),
5
+ * Stage 2 (type classification — {@linkcode classifyEntriesStage}),
6
+ * Stage 2.5 (list-value normalization — {@linkcode normalizeListValues}),
7
+ * Stage 3 (typed attribute validation — {@linkcode validateAttributesForEntry}),
8
+ * and Stage 4 (traceability rules — {@linkcode validateTraceabilityForEntry}).
9
+ */
10
+ import { validate } from "./mod.js";
11
+ import { classifyEntriesStage } from "./types.js";
12
+ import { effectiveScope, validateAttributesForEntry } from "./attributes.js";
13
+ import { normalizeListValues } from "./normalize.js";
14
+ import { validateTraceabilityForEntry } from "./traceability.js";
15
+ /**
16
+ * Run the validator pipeline.
17
+ *
18
+ * - Stage 1 (always): core hygiene via {@linkcode validate}.
19
+ * - Stage 2 (when `profile` is non-null): entry classification via
20
+ * {@linkcode classifyEntriesStage}.
21
+ *
22
+ * When `profile` is `null`, entries pass through unchanged (no `type`
23
+ * assignments).
24
+ */
25
+ export function runPipeline(entries, profile) {
26
+ const diagnostics = [];
27
+ // Stage 1 — core hygiene. When a profile is loaded, suppress MSL-R010
28
+ // warnings for attributes the profile actually declares (Stage 3's scope
29
+ // validates them directly — the core-only R010 check would be noise).
30
+ const stage1 = validate(entries);
31
+ const profileDeclaredAttrs = profile !== null
32
+ ? collectAllProfileAttributes(entries, profile)
33
+ : null;
34
+ const filteredStage1 = profileDeclaredAttrs === null
35
+ ? stage1.diagnostics
36
+ : stage1.diagnostics.filter((d) => {
37
+ if (d.code !== "MSL-R010")
38
+ return true;
39
+ const match = /attribute '([^']+)'/.exec(d.message);
40
+ if (!match)
41
+ return true;
42
+ return !profileDeclaredAttrs.has(match[1]);
43
+ });
44
+ diagnostics.push(...filteredStage1);
45
+ // Stage 2 — classification (only when a profile is loaded).
46
+ let finalEntries = entries;
47
+ if (profile !== null) {
48
+ const stage2 = classifyEntriesStage(entries, profile);
49
+ finalEntries = stage2.entries;
50
+ diagnostics.push(...stage2.diagnostics);
51
+ }
52
+ // Stage 2.5 — normalize profile-declared list-value attributes. Splits
53
+ // comma-separated values for `id-list`/`tag-list` attributes the core parser
54
+ // didn't see so that Stage 3 sees already-split values.
55
+ if (profile !== null) {
56
+ finalEntries = finalEntries.map((e) => normalizeListValues(e, profile));
57
+ }
58
+ // Stage 3 — typed attributes (only when a profile is loaded).
59
+ if (profile !== null) {
60
+ for (const entry of finalEntries) {
61
+ const stage3 = validateAttributesForEntry(entry, profile);
62
+ diagnostics.push(...stage3);
63
+ }
64
+ }
65
+ // Stage 4 — traceability rules (only when a profile is loaded). Builds a
66
+ // graph index keyed by `entry.id` from the post-classification entries so
67
+ // trace-rule target matchers can look up the classified type/shape.
68
+ if (profile !== null) {
69
+ const graph = new Map();
70
+ for (const e of finalEntries) {
71
+ if (e.id)
72
+ graph.set(e.id, e);
73
+ }
74
+ for (const entry of finalEntries) {
75
+ const stage4 = validateTraceabilityForEntry(entry, profile, graph);
76
+ diagnostics.push(...stage4);
77
+ }
78
+ }
79
+ const valid = !diagnostics.some((d) => d.severity === "error");
80
+ return { entries: finalEntries, diagnostics, valid };
81
+ }
82
+ /**
83
+ * Union of all attribute names declared by the profile's effective scope
84
+ * across all entries. Used to suppress core Stage 1 MSL-R010 warnings for
85
+ * attributes that Stage 3 already validates.
86
+ *
87
+ * Computed per-entry because the effective scope for an entry depends on
88
+ * its shape and (when classified) type. Stage 1 runs before classification,
89
+ * so we pre-classify by iterating; entries without `type` still contribute
90
+ * their universal+shape scope.
91
+ */
92
+ function collectAllProfileAttributes(entries, profile) {
93
+ const out = new Set();
94
+ for (const entry of entries) {
95
+ const scope = effectiveScope(entry, profile);
96
+ for (const name of scope.attributes.keys())
97
+ out.add(name);
98
+ }
99
+ // Also include every type-scoped attribute in the profile, since an entry
100
+ // may have declared attributes before Stage 2 classification runs.
101
+ for (const [, typeEntry] of profile.types) {
102
+ for (const name of typeEntry.value.attributes.keys())
103
+ out.add(name);
104
+ // Trace rule link names are implicitly id-list attributes — include them
105
+ // so Stage 1's MSL-R010 doesn't flag them before Stage 2 classifies.
106
+ for (const name of typeEntry.value.traceability.keys())
107
+ out.add(name);
108
+ }
109
+ // Shape-level trace rule link names too.
110
+ for (const name of profile.identified.traceability.keys())
111
+ out.add(name);
112
+ for (const name of profile.referenced.traceability.keys())
113
+ out.add(name);
114
+ return out;
115
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @module core/validator/traceability
3
+ *
4
+ * Validator Stage 4 — traceability rule enforcement.
5
+ *
6
+ * Runs after Stage 3. For each classified identified entry, checks the
7
+ * profile's declared trace rules against the entry's outgoing link
8
+ * attributes:
9
+ * - Required (MSL-L001)
10
+ * - Cardinality bounds (MSL-L002 upper / MSL-L003 lower)
11
+ * - Target match against the rule's target matchers (MSL-L004)
12
+ *
13
+ * Referenced entries are skipped entirely — the profile manifest parser
14
+ * rejects `referenced.traceability` at load time, so referenced entries
15
+ * never have declared outgoing links.
16
+ */
17
+ import type { Diagnostic, EffectiveProfile, Entry, TargetMatcher, TraceRule } from "../model/mod.js";
18
+ /**
19
+ * Effective trace rules for an entry: union of identified-shape-scope rules
20
+ * and (when classified) type-scope rules. Type-scope rules win on
21
+ * link-attribute-name collision.
22
+ *
23
+ * Referenced entries always return an empty map.
24
+ */
25
+ export declare function effectiveTraceRules(entry: Entry, profile: EffectiveProfile): ReadonlyMap<string, TraceRule>;
26
+ /**
27
+ * Return true if the target entry matches any of the rule's target matchers.
28
+ * OR semantics across the list.
29
+ *
30
+ * - Type-name matcher (string): target's classified type equals the name.
31
+ * An un-classified target never matches a type-name matcher.
32
+ * - Shape matcher ({shape: "identified"|"referenced"}): target's shape
33
+ * equals the matcher's shape.
34
+ */
35
+ export declare function matchesAnyTarget(target: Entry, matchers: readonly TargetMatcher[]): boolean;
36
+ /**
37
+ * Run Stage 4 traceability checks for one entry.
38
+ *
39
+ * Skips referenced entries entirely. For identified entries, iterates the
40
+ * effective trace rules and emits MSL-L001..L004 as appropriate.
41
+ *
42
+ * @param entry - The entry to validate (after Stage 2 classification +
43
+ * Stage 2.5 normalization)
44
+ * @param profile - The effective profile (null → never called)
45
+ * @param graph - Index keyed by entry.id for target lookup
46
+ */
47
+ export declare function validateTraceabilityForEntry(entry: Entry, profile: EffectiveProfile, graph: ReadonlyMap<string, Entry>): Diagnostic[];
48
+ //# sourceMappingURL=traceability.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"traceability.d.ts","sourceRoot":"","sources":["../../src/validator/traceability.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EACV,UAAU,EACV,gBAAgB,EAChB,KAAK,EACL,aAAa,EACb,SAAS,EACV,MAAM,iBAAiB,CAAC;AAEzB;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,gBAAgB,GACxB,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAoBhC;AAED;;;;;;;;GAQG;AACH,wBAAgB,gBAAgB,CAC9B,MAAM,EAAE,KAAK,EACb,QAAQ,EAAE,SAAS,aAAa,EAAE,GACjC,OAAO,CAST;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,4BAA4B,CAC1C,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,gBAAgB,EACzB,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,KAAK,CAAC,GAChC,UAAU,EAAE,CAuEd"}
@@ -0,0 +1,141 @@
1
+ /**
2
+ * @module core/validator/traceability
3
+ *
4
+ * Validator Stage 4 — traceability rule enforcement.
5
+ *
6
+ * Runs after Stage 3. For each classified identified entry, checks the
7
+ * profile's declared trace rules against the entry's outgoing link
8
+ * attributes:
9
+ * - Required (MSL-L001)
10
+ * - Cardinality bounds (MSL-L002 upper / MSL-L003 lower)
11
+ * - Target match against the rule's target matchers (MSL-L004)
12
+ *
13
+ * Referenced entries are skipped entirely — the profile manifest parser
14
+ * rejects `referenced.traceability` at load time, so referenced entries
15
+ * never have declared outgoing links.
16
+ */
17
+ /**
18
+ * Effective trace rules for an entry: union of identified-shape-scope rules
19
+ * and (when classified) type-scope rules. Type-scope rules win on
20
+ * link-attribute-name collision.
21
+ *
22
+ * Referenced entries always return an empty map.
23
+ */
24
+ export function effectiveTraceRules(entry, profile) {
25
+ const out = new Map();
26
+ if (entry.shape !== "identified")
27
+ return out;
28
+ // Shape scope.
29
+ for (const [name, ruleEntry] of profile.identified.traceability) {
30
+ out.set(name, ruleEntry.value);
31
+ }
32
+ // Type scope (only when classified AND type is declared in the profile).
33
+ if (entry.type !== undefined) {
34
+ const typeEntry = profile.types.get(entry.type);
35
+ if (typeEntry !== undefined) {
36
+ for (const [name, ruleEntry] of typeEntry.value.traceability) {
37
+ out.set(name, ruleEntry.value);
38
+ }
39
+ }
40
+ }
41
+ return out;
42
+ }
43
+ /**
44
+ * Return true if the target entry matches any of the rule's target matchers.
45
+ * OR semantics across the list.
46
+ *
47
+ * - Type-name matcher (string): target's classified type equals the name.
48
+ * An un-classified target never matches a type-name matcher.
49
+ * - Shape matcher ({shape: "identified"|"referenced"}): target's shape
50
+ * equals the matcher's shape.
51
+ */
52
+ export function matchesAnyTarget(target, matchers) {
53
+ for (const m of matchers) {
54
+ if (typeof m === "string") {
55
+ if (target.type === m)
56
+ return true;
57
+ }
58
+ else {
59
+ if (target.shape === m.shape)
60
+ return true;
61
+ }
62
+ }
63
+ return false;
64
+ }
65
+ /**
66
+ * Run Stage 4 traceability checks for one entry.
67
+ *
68
+ * Skips referenced entries entirely. For identified entries, iterates the
69
+ * effective trace rules and emits MSL-L001..L004 as appropriate.
70
+ *
71
+ * @param entry - The entry to validate (after Stage 2 classification +
72
+ * Stage 2.5 normalization)
73
+ * @param profile - The effective profile (null → never called)
74
+ * @param graph - Index keyed by entry.id for target lookup
75
+ */
76
+ export function validateTraceabilityForEntry(entry, profile, graph) {
77
+ const diagnostics = [];
78
+ if (entry.shape !== "identified")
79
+ return diagnostics;
80
+ const rules = effectiveTraceRules(entry, profile);
81
+ const present = entry.typedAttributes;
82
+ for (const [linkName, rule] of rules) {
83
+ const values = present.get(linkName);
84
+ const count = values?.length ?? 0;
85
+ const card = rule.cardinality ?? { lower: 0, upper: Infinity };
86
+ // MSL-L001: required link missing.
87
+ if (rule.required && count === 0) {
88
+ diagnostics.push({
89
+ code: "MSL-L001",
90
+ severity: "error",
91
+ message: `${entry.displayId}: required link attribute '${linkName}' is missing`,
92
+ location: entry.location,
93
+ });
94
+ continue;
95
+ }
96
+ if (count === 0)
97
+ continue;
98
+ // MSL-L002: upper cardinality.
99
+ if (count > card.upper) {
100
+ diagnostics.push({
101
+ code: "MSL-L002",
102
+ severity: "error",
103
+ message: `${entry.displayId}: link '${linkName}' has ${count} values but max is ${formatUpper(card.upper)}`,
104
+ location: entry.location,
105
+ });
106
+ }
107
+ // MSL-L003: lower cardinality.
108
+ if (count < card.lower) {
109
+ diagnostics.push({
110
+ code: "MSL-L003",
111
+ severity: "error",
112
+ message: `${entry.displayId}: link '${linkName}' has ${count} values but min is ${card.lower}`,
113
+ location: entry.location,
114
+ });
115
+ }
116
+ // MSL-L004: target match for each resolved value.
117
+ for (const v of values) {
118
+ const target = graph.get(v);
119
+ if (!target)
120
+ continue;
121
+ if (!matchesAnyTarget(target, rule.target)) {
122
+ diagnostics.push({
123
+ code: "MSL-L004",
124
+ severity: "error",
125
+ message: `${entry.displayId}: link '${linkName}' targets ${target.displayId} ` +
126
+ `whose type '${target.type ?? "<unclassified>"}' / shape '${target.shape}' ` +
127
+ `is not accepted by rule target ${stringifyMatchers(rule.target)}`,
128
+ location: entry.location,
129
+ });
130
+ }
131
+ }
132
+ }
133
+ return diagnostics;
134
+ }
135
+ function formatUpper(u) {
136
+ return u === Infinity ? "N" : String(u);
137
+ }
138
+ function stringifyMatchers(matchers) {
139
+ const parts = matchers.map((m) => typeof m === "string" ? m : `{shape: ${m.shape}}`);
140
+ return `[${parts.join(", ")}]`;
141
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * @module core/validator/types
3
+ *
4
+ * Validator Stage 2 — entry classification.
5
+ *
6
+ * Each entry is assigned a profile-declared type either by an explicit
7
+ * `Type:` trailer attribute or by display-ID pattern matching. Un-classified
8
+ * entries in a strict profile (types declared) produce MSL-T003.
9
+ */
10
+ import type { Diagnostic, EffectiveProfile, Entry } from "../model/mod.js";
11
+ /** Result of classifying a single entry. */
12
+ export interface ClassifyResult {
13
+ /** The assigned type name, or `undefined` if un-classified. */
14
+ readonly type: string | undefined;
15
+ readonly diagnostics: readonly Diagnostic[];
16
+ }
17
+ /**
18
+ * Classify one {@linkcode Entry} against the profile's type vocabulary.
19
+ *
20
+ * Order of precedence:
21
+ * 1. Explicit `Type:` trailer attribute (if present).
22
+ * 2. Display-ID pattern match across types whose `shape` matches
23
+ * `entry.shape`.
24
+ *
25
+ * Emits:
26
+ * - `MSL-T001` when explicit `Type:` value is not in the profile's type
27
+ * vocabulary.
28
+ * - `MSL-T002` when the display ID matches more than one type's pattern.
29
+ * - `MSL-T003` when strict mode (types declared) and no classification
30
+ * could be assigned.
31
+ */
32
+ export declare function classifyEntry(entry: Entry, profile: EffectiveProfile): ClassifyResult;
33
+ /** Result of running the classification stage over a batch of entries. */
34
+ export interface ClassifyStageResult {
35
+ /** Entries with `type` set on successful classification. */
36
+ readonly entries: readonly Entry[];
37
+ readonly diagnostics: readonly Diagnostic[];
38
+ }
39
+ /**
40
+ * Run classification across all entries. Produces new {@linkcode Entry}
41
+ * objects with `type` set when classification succeeds; un-classified
42
+ * entries pass through with `type` unchanged.
43
+ *
44
+ * Also emits `MSL-T004` when a classified entry's display ID violates the
45
+ * type's `display-id-pattern-enforcement` level.
46
+ */
47
+ export declare function classifyEntriesStage(entries: readonly Entry[], profile: EffectiveProfile): ClassifyStageResult;
48
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/validator/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EACV,UAAU,EACV,gBAAgB,EAEhB,KAAK,EAEN,MAAM,iBAAiB,CAAC;AAGzB,4CAA4C;AAC5C,MAAM,WAAW,cAAc;IAC7B,+DAA+D;IAC/D,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,SAAS,CAAC;IAClC,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;CAC7C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,aAAa,CAC3B,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,gBAAgB,GACxB,cAAc,CA4DhB;AAWD,0EAA0E;AAC1E,MAAM,WAAW,mBAAmB;IAClC,4DAA4D;IAC5D,QAAQ,CAAC,OAAO,EAAE,SAAS,KAAK,EAAE,CAAC;IACnC,QAAQ,CAAC,WAAW,EAAE,SAAS,UAAU,EAAE,CAAC;CAC7C;AAED;;;;;;;GAOG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,SAAS,KAAK,EAAE,EACzB,OAAO,EAAE,gBAAgB,GACxB,mBAAmB,CAuBrB"}
@@ -0,0 +1,140 @@
1
+ /**
2
+ * @module core/validator/types
3
+ *
4
+ * Validator Stage 2 — entry classification.
5
+ *
6
+ * Each entry is assigned a profile-declared type either by an explicit
7
+ * `Type:` trailer attribute or by display-ID pattern matching. Un-classified
8
+ * entries in a strict profile (types declared) produce MSL-T003.
9
+ */
10
+ import { compileDisplayIdPattern } from "./pattern.js";
11
+ /**
12
+ * Classify one {@linkcode Entry} against the profile's type vocabulary.
13
+ *
14
+ * Order of precedence:
15
+ * 1. Explicit `Type:` trailer attribute (if present).
16
+ * 2. Display-ID pattern match across types whose `shape` matches
17
+ * `entry.shape`.
18
+ *
19
+ * Emits:
20
+ * - `MSL-T001` when explicit `Type:` value is not in the profile's type
21
+ * vocabulary.
22
+ * - `MSL-T002` when the display ID matches more than one type's pattern.
23
+ * - `MSL-T003` when strict mode (types declared) and no classification
24
+ * could be assigned.
25
+ */
26
+ export function classifyEntry(entry, profile) {
27
+ const diagnostics = [];
28
+ // 1. Explicit Type: trailer?
29
+ const explicitType = findExplicitTypeAttribute(entry);
30
+ if (explicitType !== undefined) {
31
+ if (profile.types.has(explicitType)) {
32
+ return { type: explicitType, diagnostics };
33
+ }
34
+ diagnostics.push({
35
+ code: "MSL-T001",
36
+ severity: "error",
37
+ message: `${entry.displayId}: explicit Type: '${explicitType}' is not a declared type`,
38
+ location: entry.location,
39
+ });
40
+ return { type: undefined, diagnostics };
41
+ }
42
+ // 2. Display-ID pattern match across same-shape types.
43
+ const matches = [];
44
+ for (const [typeName, typeEntry] of profile.types) {
45
+ if (typeEntry.value.shape !== entry.shape)
46
+ continue;
47
+ const pattern = typeEntry.value.displayIdPattern.value;
48
+ if (pattern === undefined)
49
+ continue;
50
+ const regex = compileDisplayIdPattern(pattern);
51
+ if (regex.test(entry.displayId)) {
52
+ matches.push(typeName);
53
+ }
54
+ }
55
+ if (matches.length === 1) {
56
+ return { type: matches[0], diagnostics };
57
+ }
58
+ if (matches.length > 1) {
59
+ diagnostics.push({
60
+ code: "MSL-T002",
61
+ severity: "error",
62
+ message: `${entry.displayId}: display ID matches multiple type patterns ` +
63
+ `(${matches.join(", ")}); add an explicit 'Type:' trailer to disambiguate`,
64
+ location: entry.location,
65
+ });
66
+ return { type: undefined, diagnostics };
67
+ }
68
+ // 0 matches. Strict mode → MSL-T003; permissive → OK.
69
+ if (profile.types.size > 0) {
70
+ diagnostics.push({
71
+ code: "MSL-T003",
72
+ severity: "error",
73
+ message: `${entry.displayId}: un-classified entry ` +
74
+ `(profile declares ${profile.types.size} types; display ID matched none)`,
75
+ location: entry.location,
76
+ });
77
+ }
78
+ return { type: undefined, diagnostics };
79
+ }
80
+ function findExplicitTypeAttribute(entry) {
81
+ for (const attr of entry.rawAttributes) {
82
+ if (attr.key === "Type") {
83
+ return attr.value.trim();
84
+ }
85
+ }
86
+ return undefined;
87
+ }
88
+ /**
89
+ * Run classification across all entries. Produces new {@linkcode Entry}
90
+ * objects with `type` set when classification succeeds; un-classified
91
+ * entries pass through with `type` unchanged.
92
+ *
93
+ * Also emits `MSL-T004` when a classified entry's display ID violates the
94
+ * type's `display-id-pattern-enforcement` level.
95
+ */
96
+ export function classifyEntriesStage(entries, profile) {
97
+ const diagnostics = [];
98
+ const out = [];
99
+ for (const entry of entries) {
100
+ const classified = classifyEntry(entry, profile);
101
+ diagnostics.push(...classified.diagnostics);
102
+ if (classified.type !== undefined) {
103
+ const typeEntry = profile.types.get(classified.type);
104
+ if (typeEntry !== undefined) {
105
+ const enforceDiag = checkEnforcement(entry, typeEntry);
106
+ if (enforceDiag !== undefined) {
107
+ diagnostics.push(enforceDiag);
108
+ }
109
+ }
110
+ out.push({ ...entry, type: classified.type });
111
+ }
112
+ else {
113
+ out.push(entry);
114
+ }
115
+ }
116
+ return { entries: out, diagnostics };
117
+ }
118
+ /**
119
+ * If the classified type declares a display-id-pattern and the entry's
120
+ * display ID doesn't match it, emit an MSL-T004 at the configured severity.
121
+ * Returns `undefined` when the display ID matches or enforcement is `off`.
122
+ */
123
+ function checkEnforcement(entry, typeEntry) {
124
+ const pattern = typeEntry.value.displayIdPattern.value;
125
+ if (pattern === undefined)
126
+ return undefined;
127
+ const level = typeEntry.value.displayIdPatternEnforcement.value;
128
+ if (level === "off")
129
+ return undefined;
130
+ const regex = compileDisplayIdPattern(pattern);
131
+ if (regex.test(entry.displayId))
132
+ return undefined;
133
+ return {
134
+ code: "MSL-T004",
135
+ severity: level === "error" ? "error" : "warning",
136
+ message: `${entry.displayId}: display ID doesn't match pattern ` +
137
+ `'${pattern}' for type '${typeEntry.value.name}'`,
138
+ location: entry.location,
139
+ };
140
+ }
@@ -0,0 +1,20 @@
1
+ /**
2
+ * @module core/validator/value_types
3
+ *
4
+ * Per-type value validators for profile-declared attributes. Each validator
5
+ * takes a string value and the attribute's declaration, returns `null` when
6
+ * valid or a short error message explaining why it's invalid.
7
+ *
8
+ * The top-level {@linkcode validateValue} dispatches on the declared type.
9
+ */
10
+ import type { AttrDecl } from "../model/mod.js";
11
+ /**
12
+ * Validate one string value against an attribute's declared value type.
13
+ * Returns `null` when the value is valid, or a short human-readable detail
14
+ * string (without the attribute name or display ID — the caller composes the
15
+ * full diagnostic message).
16
+ */
17
+ export declare function validateValue(value: string, decl: AttrDecl): string | null;
18
+ /** Signature for a single value-type validator. */
19
+ export type ValueValidator = (value: string, decl: AttrDecl) => string | null;
20
+ //# sourceMappingURL=value_types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"value_types.d.ts","sourceRoot":"","sources":["../../src/validator/value_types.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,KAAK,EAAE,QAAQ,EAAa,MAAM,iBAAiB,CAAC;AAG3D;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,GAAG,MAAM,GAAG,IAAI,CAG1E;AAED,mDAAmD;AACnD,MAAM,MAAM,cAAc,GAAG,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,QAAQ,KAAK,MAAM,GAAG,IAAI,CAAC"}
@@ -0,0 +1,126 @@
1
+ /**
2
+ * @module core/validator/value_types
3
+ *
4
+ * Per-type value validators for profile-declared attributes. Each validator
5
+ * takes a string value and the attribute's declaration, returns `null` when
6
+ * valid or a short error message explaining why it's invalid.
7
+ *
8
+ * The top-level {@linkcode validateValue} dispatches on the declared type.
9
+ */
10
+ import { ULID_RE, URI_SCHEME_RE } from "../model/mod.js";
11
+ /**
12
+ * Validate one string value against an attribute's declared value type.
13
+ * Returns `null` when the value is valid, or a short human-readable detail
14
+ * string (without the attribute name or display ID — the caller composes the
15
+ * full diagnostic message).
16
+ */
17
+ export function validateValue(value, decl) {
18
+ const fn = VALIDATORS[decl.type];
19
+ return fn(value, decl);
20
+ }
21
+ // ---------------------------------------------------------------------------
22
+ // Individual validators (Task 6.3 simple types)
23
+ // ---------------------------------------------------------------------------
24
+ const validateText = (_value, _decl) => {
25
+ return null;
26
+ };
27
+ const INTEGER_RE = /^-?\d+$/;
28
+ const validateInteger = (value, _decl) => {
29
+ return INTEGER_RE.test(value)
30
+ ? null
31
+ : `not a valid integer: '${value}' (expected digits optionally prefixed with '-')`;
32
+ };
33
+ const validateBoolean = (value, _decl) => {
34
+ return value === "true" || value === "false"
35
+ ? null
36
+ : `not a valid boolean: '${value}' (expected 'true' or 'false')`;
37
+ };
38
+ const DATE_RE = /^\d{4}-\d{2}-\d{2}$/;
39
+ const validateDate = (value, _decl) => {
40
+ return DATE_RE.test(value)
41
+ ? null
42
+ : `not a valid ISO 8601 date: '${value}' (expected YYYY-MM-DD)`;
43
+ };
44
+ const validateEnum = (value, decl) => {
45
+ const values = decl.values ?? [];
46
+ if (values.includes(value))
47
+ return null;
48
+ return `value '${value}' is not in declared enum [${values.join(", ")}]`;
49
+ };
50
+ // ---------------------------------------------------------------------------
51
+ // Individual validators (Task 6.4 ID/URI types)
52
+ // ---------------------------------------------------------------------------
53
+ const validateId = (value, _decl) => {
54
+ if (ULID_RE.test(value))
55
+ return null;
56
+ if (URI_SCHEME_RE.test(value))
57
+ return null;
58
+ return `not a valid id: '${value}' (expected 26-char ULID or scheme-qualified URI)`;
59
+ };
60
+ const HTTP_URL_RE = /^https?:\/\//;
61
+ const validateUrl = (value, _decl) => {
62
+ return HTTP_URL_RE.test(value) ? null : `not a valid http(s) URL: '${value}'`;
63
+ };
64
+ const validateUri = (value, _decl) => {
65
+ return URI_SCHEME_RE.test(value)
66
+ ? null
67
+ : `not a valid URI: '${value}' (expected scheme-qualified per RFC 3986)`;
68
+ };
69
+ const validateExternalId = (value, _decl) => {
70
+ return value.trim().length > 0
71
+ ? null
72
+ : `external-id cannot be empty or whitespace-only`;
73
+ };
74
+ // ---------------------------------------------------------------------------
75
+ // Individual validators (Task 6.5 path / list / citation types)
76
+ // ---------------------------------------------------------------------------
77
+ // `/absolute` (POSIX) or `C:` / `C:\` / `C:/` (Windows) → absolute.
78
+ const ABSOLUTE_RE = /^(\/|[A-Za-z]:[\\/]?)/;
79
+ const validatePath = (value, _decl) => {
80
+ if (value.length === 0)
81
+ return `path cannot be empty`;
82
+ if (ABSOLUTE_RE.test(value)) {
83
+ return `absolute paths are not allowed: '${value}' (use a relative path)`;
84
+ }
85
+ return null;
86
+ };
87
+ const validatePathOrId = (value, decl) => {
88
+ if (ULID_RE.test(value))
89
+ return null;
90
+ if (URI_SCHEME_RE.test(value))
91
+ return null;
92
+ return validatePath(value, decl);
93
+ };
94
+ const TAG_RE = /^[A-Za-z0-9_\-.]+$/;
95
+ const validateTagList = (value, _decl) => {
96
+ if (value.length === 0)
97
+ return `tag cannot be empty`;
98
+ return TAG_RE.test(value)
99
+ ? null
100
+ : `invalid tag '${value}' (expected bareword of letters, digits, '_', '-', '.')`;
101
+ };
102
+ const validateCitation = (value, _decl) => {
103
+ return value.trim().length > 0
104
+ ? null
105
+ : `citation cannot be empty or whitespace-only`;
106
+ };
107
+ // ---------------------------------------------------------------------------
108
+ // Registry — all 14 value types are now implemented.
109
+ // ---------------------------------------------------------------------------
110
+ /** Registry of per-type validators. */
111
+ const VALIDATORS = {
112
+ text: validateText,
113
+ integer: validateInteger,
114
+ boolean: validateBoolean,
115
+ date: validateDate,
116
+ enum: validateEnum,
117
+ id: validateId,
118
+ "id-list": validateId,
119
+ uri: validateUri,
120
+ url: validateUrl,
121
+ "external-id": validateExternalId,
122
+ path: validatePath,
123
+ "path-or-id": validatePathOrId,
124
+ "tag-list": validateTagList,
125
+ citation: validateCitation,
126
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@driftsys/markspec",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Markdown flavor and toolchain for traceable industrial documentation",
5
5
  "keywords": [
6
6
  "markdown",
@@ -28,5 +28,12 @@
28
28
  }
29
29
  },
30
30
  "scripts": {},
31
+ "dependencies": {
32
+ "@types/mdast": "^4",
33
+ "remark-gfm": "^4",
34
+ "remark-parse": "^11",
35
+ "unified": "^11",
36
+ "web-tree-sitter": "^0.24"
37
+ },
31
38
  "_generatedBy": "dnt@dev"
32
39
  }