@orkestrel/guide 0.0.9 → 0.0.11

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.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @orkestrel/guide
2
2
 
3
3
  A guides-parity **test helper** for `@orkestrel` packages. Add it as a
4
- devDependency, drop one short test file into `tests/guides/src/parity.test.ts`,
4
+ devDependency, drop one short test file into `tests/guides.test.ts`,
5
5
  wire a vitest `guides` project, and thereafter every guide is proven —
6
6
  mechanically, in CI, as ordinary vitest assertions — to be in **bijection**
7
7
  with the code it documents: every documented export exists in source and
@@ -24,12 +24,12 @@ npm install -D @orkestrel/guide
24
24
  ## Usage
25
25
 
26
26
  The entire consumer-side footprint is one short test file. An excerpt of the
27
- drop-in pattern (`tests/guides/src/parity.test.ts`):
27
+ drop-in pattern (`tests/guides.test.ts`):
28
28
 
29
29
  The consumer supplies the file inventory `Source` reflects over. The inventory
30
30
  below is an illustrative, non-runnable placeholder until the consumer supplies
31
31
  real workspace text; the complete runnable implementation is
32
- [`tests/guides/src/parity.test.ts`](tests/guides/src/parity.test.ts).
32
+ [`tests/guides.test.ts`](tests/guides.test.ts).
33
33
 
34
34
  ```ts
35
35
  import { expect, it } from 'vitest'
@@ -160,7 +160,7 @@ declaration.
160
160
  ## Guide
161
161
 
162
162
  For the full surface — the guide anatomy, the manifest format, and the check
163
- catalog — see [`guides/src/guide.md`](guides/src/guide.md). For the design
163
+ catalog — see [`guides/guide.md`](guides/guide.md). For the design
164
164
  and guide index, see [`guides/`](./guides/).
165
165
 
166
166
  ## Package
@@ -30,6 +30,75 @@ var EXTERNAL_SCHEMES = Object.freeze([
30
30
  "tel:"
31
31
  ]);
32
32
  //#endregion
33
+ //#region src/core/validators.ts
34
+ /**
35
+ * Whether `value` is one of the five documented {@link ExportKind} literals — the
36
+ * guard behind extracting a Surface table's `Kind` cell into a typed symbol.
37
+ *
38
+ * @param value - The candidate value
39
+ * @returns `true` when `value` is a valid {@link ExportKind}
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * isExportKind('class') // true
44
+ * isExportKind('enum') // false
45
+ * ```
46
+ */
47
+ var isExportKind = (0, _orkestrel_contract.literalOf)("type", "interface", "const", "function", "class");
48
+ /**
49
+ * Whether `value` is a well-formed {@link SurfaceSymbol} — a `name` string
50
+ * paired with a valid {@link ExportKind}.
51
+ *
52
+ * @param value - The value to test
53
+ * @returns `true` when `value` is a well-formed {@link SurfaceSymbol}
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * isSurfaceSymbol({ name: 'Markdown', kind: 'class' }) // true
58
+ * isSurfaceSymbol({ name: 'Markdown', kind: 'enum' }) // false
59
+ * ```
60
+ */
61
+ var isSurfaceSymbol = (0, _orkestrel_contract.recordOf)({
62
+ name: _orkestrel_contract.isString,
63
+ kind: isExportKind
64
+ });
65
+ /**
66
+ * Whether `value` is a well-formed {@link MethodGroup} — a backticked
67
+ * `interface` name paired with its documented `methods`.
68
+ *
69
+ * @param value - The value to test
70
+ * @returns `true` when `value` is a well-formed {@link MethodGroup}
71
+ *
72
+ * @example
73
+ * ```ts
74
+ * isMethodGroup({ interface: 'MarkdownInterface', methods: ['walk'] }) // true
75
+ * isMethodGroup({ interface: 'MarkdownInterface', methods: [1] }) // false
76
+ * ```
77
+ */
78
+ var isMethodGroup = (0, _orkestrel_contract.recordOf)({
79
+ interface: _orkestrel_contract.isString,
80
+ methods: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString)
81
+ });
82
+ /**
83
+ * Whether `value` is a well-formed {@link ManifestEntry} — a `## By concept`
84
+ * manifest row, its `source` accepting either a single directory or several.
85
+ *
86
+ * @param value - The value to test
87
+ * @returns `true` when `value` is a well-formed {@link ManifestEntry}
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * isManifestEntry({ concept: 'Markdown', spec: 'guides/src/markdown.md', source: 'src/core', tests: 'tests/src/core' }) // true
92
+ * isManifestEntry({ concept: 1 }) // false
93
+ * ```
94
+ */
95
+ var isManifestEntry = (0, _orkestrel_contract.recordOf)({
96
+ concept: _orkestrel_contract.isString,
97
+ spec: _orkestrel_contract.isString,
98
+ source: (0, _orkestrel_contract.unionOf)(_orkestrel_contract.isString, (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString)),
99
+ tests: _orkestrel_contract.isString
100
+ });
101
+ //#endregion
33
102
  //#region src/core/helpers.ts
34
103
  /**
35
104
  * Extract aligned physical source-line records in one character traversal.
@@ -563,6 +632,23 @@ function normalizeDirectories(module) {
563
632
  return directories;
564
633
  }
565
634
  /**
635
+ * The stable cache key for a {@link GuideModule}. Directories normalize before
636
+ * joining, so equivalent module spellings share one key. The NUL separator
637
+ * cannot occur in filesystem-backed canonical-segment inventory keys, so no
638
+ * directory boundary can collide with directory text.
639
+ *
640
+ * @param module - The module scope to key
641
+ * @returns The normalized directories joined by NUL
642
+ *
643
+ * @example
644
+ * ```ts
645
+ * moduleKey(['src/core', 'src/browser']) // 'src/core\0src/browser'
646
+ * ```
647
+ */
648
+ function moduleKey(module) {
649
+ return normalizeDirectories(module).join("\0");
650
+ }
651
+ /**
566
652
  * The exact opaque file-inventory keys belonging under any canonical
567
653
  * {@link GuideModule} directory, sorted. `'.'` selects canonical root-relative
568
654
  * keys without accepting `/`, `./`, or `../` aliases. Every selected exact
@@ -623,6 +709,24 @@ function findMissing(names, source) {
623
709
  return names.filter((name) => !existing.has(name));
624
710
  }
625
711
  /**
712
+ * The fences whose language is absent from the caller's listed languages.
713
+ * Untagged fences are always returned because they have no language to list.
714
+ *
715
+ * @param fences - The guide fences to check
716
+ * @param languages - The listed language tags
717
+ * @returns The unlisted fences in input order
718
+ *
719
+ * @example
720
+ * ```ts
721
+ * findUnlisted([{ language: 'typescript', code: 'walk()' }], ['ts'])
722
+ * // [{ language: 'typescript', code: 'walk()' }]
723
+ * ```
724
+ */
725
+ function findUnlisted(fences, languages) {
726
+ const listed = new Set(languages);
727
+ return fences.filter((fence) => fence.language === void 0 || !listed.has(fence.language));
728
+ }
729
+ /**
626
730
  * The symbol-key set-difference between two symbol lists — `symbols` present but
627
731
  * absent from `source`, compared by {@link symbolKey} so a symbol can drift in
628
732
  * neither name nor kind.
@@ -845,77 +949,6 @@ function kindIndex(table) {
845
949
  }).trim() === "Kind") return index;
846
950
  }
847
951
  }
848
- //#endregion
849
- //#region src/core/validators.ts
850
- /**
851
- * Whether `value` is one of the five documented {@link ExportKind} literals — the
852
- * guard behind extracting a Surface table's `Kind` cell into a typed symbol.
853
- *
854
- * @param value - The candidate value
855
- * @returns `true` when `value` is a valid {@link ExportKind}
856
- *
857
- * @example
858
- * ```ts
859
- * isExportKind('class') // true
860
- * isExportKind('enum') // false
861
- * ```
862
- */
863
- var isExportKind = (0, _orkestrel_contract.literalOf)("type", "interface", "const", "function", "class");
864
- /**
865
- * Whether `value` is a well-formed {@link SurfaceSymbol} — a `name` string
866
- * paired with a valid {@link ExportKind}.
867
- *
868
- * @param value - The value to test
869
- * @returns `true` when `value` is a well-formed {@link SurfaceSymbol}
870
- *
871
- * @example
872
- * ```ts
873
- * isSurfaceSymbol({ name: 'Markdown', kind: 'class' }) // true
874
- * isSurfaceSymbol({ name: 'Markdown', kind: 'enum' }) // false
875
- * ```
876
- */
877
- var isSurfaceSymbol = (0, _orkestrel_contract.recordOf)({
878
- name: _orkestrel_contract.isString,
879
- kind: isExportKind
880
- });
881
- /**
882
- * Whether `value` is a well-formed {@link MethodGroup} — a backticked
883
- * `interface` name paired with its documented `methods`.
884
- *
885
- * @param value - The value to test
886
- * @returns `true` when `value` is a well-formed {@link MethodGroup}
887
- *
888
- * @example
889
- * ```ts
890
- * isMethodGroup({ interface: 'MarkdownInterface', methods: ['walk'] }) // true
891
- * isMethodGroup({ interface: 'MarkdownInterface', methods: [1] }) // false
892
- * ```
893
- */
894
- var isMethodGroup = (0, _orkestrel_contract.recordOf)({
895
- interface: _orkestrel_contract.isString,
896
- methods: (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString)
897
- });
898
- /**
899
- * Whether `value` is a well-formed {@link ManifestEntry} — a `## By concept`
900
- * manifest row, its `source` accepting either a single directory or several.
901
- *
902
- * @param value - The value to test
903
- * @returns `true` when `value` is a well-formed {@link ManifestEntry}
904
- *
905
- * @example
906
- * ```ts
907
- * isManifestEntry({ concept: 'Markdown', spec: 'guides/src/markdown.md', source: 'src/core', tests: 'tests/src/core' }) // true
908
- * isManifestEntry({ concept: 1 }) // false
909
- * ```
910
- */
911
- var isManifestEntry = (0, _orkestrel_contract.recordOf)({
912
- concept: _orkestrel_contract.isString,
913
- spec: _orkestrel_contract.isString,
914
- source: (0, _orkestrel_contract.unionOf)(_orkestrel_contract.isString, (0, _orkestrel_contract.arrayOf)(_orkestrel_contract.isString)),
915
- tests: _orkestrel_contract.isString
916
- });
917
- //#endregion
918
- //#region src/core/parsers.ts
919
952
  /**
920
953
  * The module-scope exports declared in one file's source text — matches
921
954
  * `export (async)? (function(\*)?|class|const|interface|type) Name`, deduped
@@ -1373,22 +1406,27 @@ function exampleMethods(lines) {
1373
1406
  return Array.from(new Set(methods)).sort();
1374
1407
  }
1375
1408
  /**
1376
- * Every ```ts fenced code block's body text anywhere in the guide document —
1377
- * a full AST walk so a fence nested inside a blockquote or list still counts.
1409
+ * Every fenced code block anywhere in the guide document. A full AST walk
1410
+ * includes fences nested inside blockquotes and lists.
1378
1411
  *
1379
1412
  * @param document - The parsed guide document
1380
- * @returns Every `ts`-lang fence's verbatim code, in walk order
1413
+ * @returns Every fence's language and verbatim code, in document order
1381
1414
  *
1382
1415
  * @example
1383
1416
  * ```ts
1384
- * extractPatterns(document) // ["import { X } from './x.js'\nX()"]
1417
+ * extractFences(document) // [{ language: 'ts', code: "import { X } from './x.js'\nX()" }]
1385
1418
  * ```
1386
1419
  */
1387
- function extractPatterns(document) {
1388
- const patterns = [];
1389
- for (const node of (0, _orkestrel_markdown.walkNodes)(document)) if ((0, _orkestrel_markdown.isCodeBlockNode)(node) && node.lang === "ts") patterns.push(node.code);
1390
- return patterns;
1420
+ function extractFences(document) {
1421
+ const fences = [];
1422
+ for (const node of (0, _orkestrel_markdown.walkNodes)(document)) if ((0, _orkestrel_markdown.isCodeBlockNode)(node)) fences.push({
1423
+ language: node.lang,
1424
+ code: node.code
1425
+ });
1426
+ return fences;
1391
1427
  }
1428
+ //#endregion
1429
+ //#region src/core/parsers.ts
1392
1430
  /**
1393
1431
  * Parse a `## By concept` manifest table into its {@link ManifestEntry} rows —
1394
1432
  * each row's Concept cell (flattened text), Spec / Tests cells (a single link
@@ -1506,7 +1544,7 @@ var manifestEntryShape = (0, _orkestrel_contract.objectShape)({
1506
1544
  /**
1507
1545
  * A stateful, structured view over one parsed guide — the six documented
1508
1546
  * projections (`sections` / `surface` / `methods` / `links` / `tests` /
1509
- * `patterns`) are extracted once at construction and cached.
1547
+ * `fences`) are extracted once at construction and cached.
1510
1548
  *
1511
1549
  * @remarks
1512
1550
  * Pure: parses `source` once via `@orkestrel/markdown` and never touches the
@@ -1528,7 +1566,7 @@ var Guide = class {
1528
1566
  #methods;
1529
1567
  #links;
1530
1568
  #tests;
1531
- #patterns;
1569
+ #fences;
1532
1570
  constructor(source) {
1533
1571
  const document = (0, _orkestrel_markdown.createMarkdown)(source).document;
1534
1572
  this.#sections = document.children.filter(_orkestrel_markdown.isHeadingNode).filter((heading) => heading.level === 2).map((heading) => (0, _orkestrel_markdown.flattenText)(heading).trim());
@@ -1536,7 +1574,7 @@ var Guide = class {
1536
1574
  this.#methods = extractMethods(document);
1537
1575
  this.#links = extractLinks(document);
1538
1576
  this.#tests = extractTests(document);
1539
- this.#patterns = extractPatterns(document);
1577
+ this.#fences = extractFences(document);
1540
1578
  }
1541
1579
  sections() {
1542
1580
  return this.#sections;
@@ -1553,12 +1591,12 @@ var Guide = class {
1553
1591
  tests() {
1554
1592
  return this.#tests;
1555
1593
  }
1556
- patterns() {
1557
- return this.#patterns;
1594
+ fences() {
1595
+ return this.#fences;
1558
1596
  }
1559
1597
  };
1560
1598
  //#endregion
1561
- //#region src/core/Source.ts
1599
+ //#region src/core/sources/Source.ts
1562
1600
  /**
1563
1601
  * A pure `SourceInterface` — reflects a module scope's intentional direct
1564
1602
  * declarations, conventional barrel-reachable surface, and member methods over
@@ -1717,11 +1755,52 @@ var Source = class {
1717
1755
  }
1718
1756
  };
1719
1757
  //#endregion
1758
+ //#region src/core/sources/SourceManager.ts
1759
+ /**
1760
+ * Resolves a consumer-owned import-specifier policy into pure source views and
1761
+ * caches those views by module, so aliases for one module share one entity.
1762
+ * Unmapped specifiers remain foreign to the consumer and return `undefined`.
1763
+ *
1764
+ * @example
1765
+ * ```ts
1766
+ * import { SourceManager } from '@orkestrel/guide'
1767
+ *
1768
+ * const sources = new SourceManager({
1769
+ * files: { 'src/core/index.ts': "export * from './types.js'" },
1770
+ * modules: { '@scope/package': 'src/core' },
1771
+ * })
1772
+ * sources.source('@scope/package')?.surface()
1773
+ * ```
1774
+ */
1775
+ var SourceManager = class {
1776
+ #files;
1777
+ #modules;
1778
+ #sources = /* @__PURE__ */ new Map();
1779
+ constructor(options) {
1780
+ this.#files = options.files;
1781
+ this.#modules = options.modules;
1782
+ }
1783
+ source(specifier) {
1784
+ if (!Object.hasOwn(this.#modules, specifier)) return void 0;
1785
+ const module = this.#modules[specifier];
1786
+ if (module === void 0) return void 0;
1787
+ const key = moduleKey(module);
1788
+ const existing = this.#sources.get(key);
1789
+ if (existing !== void 0) return existing;
1790
+ const source = new Source({
1791
+ files: this.#files,
1792
+ module
1793
+ });
1794
+ this.#sources.set(key, source);
1795
+ return source;
1796
+ }
1797
+ };
1798
+ //#endregion
1720
1799
  //#region src/core/factories.ts
1721
1800
  /**
1722
1801
  * Create a structured {@link GuideInterface} view over one guide's markdown
1723
1802
  * source — parses once and caches its `sections` / `surface` / `methods` /
1724
- * `links` / `tests` / `patterns` projections.
1803
+ * `links` / `tests` / `fences` projections.
1725
1804
  *
1726
1805
  * @param source - The guide's markdown source text
1727
1806
  * @returns A working {@link GuideInterface}
@@ -1759,6 +1838,27 @@ function createSource(options) {
1759
1838
  return new Source(options);
1760
1839
  }
1761
1840
  /**
1841
+ * Create a {@link SourceManagerInterface} that resolves the consumer's local
1842
+ * import specifiers and shares one source view per module.
1843
+ *
1844
+ * @param options - The shared file inventory and specifier-to-module policy
1845
+ * @returns A source manager over the supplied policy
1846
+ *
1847
+ * @example
1848
+ * ```ts
1849
+ * import { createSourceManager } from '@orkestrel/guide'
1850
+ *
1851
+ * const sources = createSourceManager({
1852
+ * files: { 'src/core/index.ts': "export * from './types.js'" },
1853
+ * modules: { '@scope/package': 'src/core' },
1854
+ * })
1855
+ * sources.source('@scope/package')?.surface()
1856
+ * ```
1857
+ */
1858
+ function createSourceManager(options) {
1859
+ return new SourceManager(options);
1860
+ }
1861
+ /**
1762
1862
  * Compile the {@link surfaceSymbolShape} into a {@link ContractInterface} for
1763
1863
  * {@link SurfaceSymbol} — a guard, coercing parser, JSON Schema, and seeded
1764
1864
  * generator from one shape declaration (AGENTS §14).
@@ -1819,27 +1919,30 @@ exports.MANIFEST = MANIFEST;
1819
1919
  exports.METHODS = METHODS;
1820
1920
  exports.SURFACE = SURFACE;
1821
1921
  exports.Source = Source;
1922
+ exports.SourceManager = SourceManager;
1822
1923
  exports.TESTS = TESTS;
1823
1924
  exports.cellLinks = cellLinks;
1824
1925
  exports.createGuide = createGuide;
1825
1926
  exports.createManifestEntryContract = createManifestEntryContract;
1826
1927
  exports.createMethodGroupContract = createMethodGroupContract;
1827
1928
  exports.createSource = createSource;
1929
+ exports.createSourceManager = createSourceManager;
1828
1930
  exports.createSurfaceSymbolContract = createSurfaceSymbolContract;
1829
1931
  exports.declarationBody = declarationBody;
1830
1932
  exports.exampleMethods = exampleMethods;
1831
1933
  exports.examplesFrom = examplesFrom;
1832
1934
  exports.exportsFrom = exportsFrom;
1833
1935
  exports.extractExampleLines = extractExampleLines;
1936
+ exports.extractFences = extractFences;
1834
1937
  exports.extractLinks = extractLinks;
1835
1938
  exports.extractMethods = extractMethods;
1836
- exports.extractPatterns = extractPatterns;
1837
1939
  exports.extractSourceLines = extractSourceLines;
1838
1940
  exports.extractSurface = extractSurface;
1839
1941
  exports.extractTests = extractTests;
1840
1942
  exports.fenceImports = fenceImports;
1841
1943
  exports.findMissing = findMissing;
1842
1944
  exports.findUnexampled = findUnexampled;
1945
+ exports.findUnlisted = findUnlisted;
1843
1946
  exports.firstCode = firstCode;
1844
1947
  exports.hasCanonicalSegments = hasCanonicalSegments;
1845
1948
  exports.hiddenFrom = hiddenFrom;
@@ -1855,6 +1958,7 @@ exports.manifestEntryShape = manifestEntryShape;
1855
1958
  exports.memberMethods = memberMethods;
1856
1959
  exports.methodGroupShape = methodGroupShape;
1857
1960
  exports.missingSymbols = missingSymbols;
1961
+ exports.moduleKey = moduleKey;
1858
1962
  exports.normalizeDirectories = normalizeDirectories;
1859
1963
  exports.parseManifest = parseManifest;
1860
1964
  exports.resolveLink = resolveLink;