@astryxdesign/cli 0.7.2-canary.6fddf4f → 0.7.2-canary.72db6f9

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/README.md +62 -62
  2. package/api/component/_adapter.d.mts +31 -0
  3. package/api/component/_adapter.mjs +116 -0
  4. package/api/component/component.mjs +60 -10
  5. package/api/component/doc-links.test.mjs +386 -0
  6. package/api/discover/_adapter.d.mts +119 -12
  7. package/api/discover/_adapter.mjs +449 -10
  8. package/api/discover/_adapter.test.mjs +599 -17
  9. package/api/discover/_catalog-view.d.mts +16 -5
  10. package/api/discover/_catalog-view.mjs +32 -8
  11. package/api/discover/detail/doc/doc.mjs +9 -1
  12. package/api/discover/discover.d.mts +11 -0
  13. package/api/discover/discover.doc.mjs +7 -6
  14. package/api/discover/discover.mjs +107 -19
  15. package/api/discover/discover.type.d.mts +18 -7
  16. package/api/discover/discover.type.mjs +15 -7
  17. package/api/discover/list/list.d.mts +4 -3
  18. package/api/discover/list/list.mjs +17 -10
  19. package/api/docs/_adapter.d.mts +63 -0
  20. package/api/docs/_adapter.mjs +309 -16
  21. package/api/docs/integration-tree.test.mjs +22 -0
  22. package/api/docs/list/list.mjs +12 -2
  23. package/api/doctor/theme-checks.test.mjs +136 -0
  24. package/api/gap-report/gap-report.d.mts +3 -0
  25. package/api/gap-report/gap-report.doc.mjs +11 -3
  26. package/api/gap-report/gap-report.mjs +152 -48
  27. package/api/gap-report/gap-report.test.mjs +233 -0
  28. package/api/gap-report/gap-report.type.d.mts +41 -0
  29. package/api/gap-report/gap-report.type.mjs +18 -0
  30. package/api/integration/_adapter.d.mts +33 -0
  31. package/api/integration/_adapter.mjs +58 -0
  32. package/api/integration/authoring-checks.mjs +98 -3
  33. package/api/integration/authoring-checks.test.mjs +769 -0
  34. package/api/integration/authoring-checks.type.d.mts +134 -0
  35. package/api/integration/authoring-checks.type.mjs +60 -1
  36. package/api/integration/integrationComponentConflicts.doc.mjs +3 -1
  37. package/api/integration/integrationDocConflicts.doc.mjs +22 -5
  38. package/api/integration/pack-check.mjs +296 -7
  39. package/api/integration/pack-check.test.mjs +367 -0
  40. package/api/integration/validate-integration-fixes.test.mjs +67 -0
  41. package/api/integration/validate-integration.mjs +28 -3
  42. package/api/integration/validate-integration.test.mjs +77 -8
  43. package/api/integration/validateIntegration.doc.mjs +1 -1
  44. package/api/search/search.mjs +2 -1
  45. package/api/upgrade/_adapter.d.mts +40 -2
  46. package/api/upgrade/_adapter.mjs +117 -4
  47. package/api/upgrade/run/cli-behind-core.test.mjs +438 -0
  48. package/api/upgrade/run/run.mjs +30 -1
  49. package/api/upgrade/status/status.d.mts +21 -5
  50. package/api/upgrade/status/status.mjs +45 -8
  51. package/api/upgrade/upgrade.doc.mjs +5 -3
  52. package/api/upgrade/upgrade.type.d.mts +23 -0
  53. package/api/upgrade/upgrade.type.mjs +16 -1
  54. package/assets/codemods/runner.mjs +7 -1
  55. package/assets/discover/official-packages.json +367 -0
  56. package/assets/docs/author-a-theme.doc.mjs +2 -2
  57. package/assets/docs/browser-support.doc.mjs +1 -1
  58. package/assets/docs/styling-overview.doc.mjs +1 -1
  59. package/assets/docs/theme.doc.mjs +1 -1
  60. package/assets/docs/tree/add-a-topic.doc.mjs +3 -3
  61. package/assets/docs/tree/check-the-props.doc.mjs +7 -2
  62. package/assets/docs/tree/check-your-docs.doc.mjs +15 -3
  63. package/assets/docs/tree/checks.doc.mjs +5 -5
  64. package/assets/docs/tree/debug-and-gap-reports.doc.mjs +25 -5
  65. package/assets/docs/tree/document-the-theme.doc.mjs +2 -2
  66. package/assets/docs/tree/layout-spacing.doc.mjs +2 -3
  67. package/assets/docs/tree/layout.doc.mjs +6 -6
  68. package/assets/docs/tree/migration-setup.doc.mjs +2 -2
  69. package/assets/docs/tree/publishing.doc.mjs +3 -3
  70. package/assets/docs/tree/quick-start.doc.mjs +2 -2
  71. package/assets/docs/tree/sections-and-placement.doc.mjs +2 -2
  72. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  73. package/assets/docs/tree/troubleshooting.doc.mjs +5 -5
  74. package/assets/docs/tree/upgrading.doc.mjs +1 -1
  75. package/authoring/discover/discover.doc.mjs +1 -1
  76. package/authoring/doctypes/component/component.doc.mjs +1 -1
  77. package/authoring/doctypes/component/type.ts +8 -0
  78. package/authoring/doctypes/hook/type.ts +4 -0
  79. package/clients/cli/commands/discover.broken-integration.test.mjs +4 -1
  80. package/clients/cli/commands/discover.doc.mjs +1 -1
  81. package/clients/cli/commands/discover.mjs +144 -93
  82. package/clients/cli/commands/discover.no-source.test.mjs +447 -33
  83. package/clients/cli/commands/discover.sources.test.mjs +20 -1
  84. package/clients/cli/commands/doctor-integration-components.doc.mjs +3 -2
  85. package/clients/cli/commands/doctor-integration-docs.doc.mjs +19 -5
  86. package/clients/cli/commands/doctor-integration.test.mjs +48 -1
  87. package/clients/cli/commands/doctor.mjs +91 -5
  88. package/clients/cli/commands/gap-report.doc.mjs +14 -2
  89. package/clients/cli/commands/gap-report.mjs +24 -2
  90. package/clients/cli/commands/gap-report.test.mjs +79 -0
  91. package/clients/cli/commands/integration-authoring.test.mjs +46 -0
  92. package/clients/cli/commands/integration-check.test.mjs +144 -0
  93. package/clients/cli/commands/integration.mjs +12 -7
  94. package/clients/cli/commands/json-contract.test.mjs +5 -1
  95. package/clients/cli/commands/upgrade.doc.mjs +14 -0
  96. package/clients/cli/commands/upgrade.older-cli.test.mjs +160 -0
  97. package/foundation/discovery/copied-passages.d.mts +216 -0
  98. package/foundation/discovery/copied-passages.mjs +720 -0
  99. package/foundation/discovery/copied-passages.test.mjs +124 -0
  100. package/foundation/discovery/doc-consistency.d.mts +34 -0
  101. package/foundation/discovery/doc-consistency.mjs +513 -0
  102. package/foundation/discovery/doc-consistency.test.mjs +347 -0
  103. package/foundation/discovery/doc-fluff.d.mts +112 -0
  104. package/foundation/discovery/doc-fluff.mjs +328 -0
  105. package/foundation/discovery/doc-fluff.test.mjs +142 -0
  106. package/foundation/discovery/doc-types.mjs +12 -5
  107. package/foundation/discovery/doc-types.test.mjs +46 -2
  108. package/foundation/discovery/docs-discovery.d.mts +7 -0
  109. package/foundation/discovery/docs-discovery.mjs +26 -2
  110. package/foundation/discovery/prop-drift.d.mts +25 -6
  111. package/foundation/discovery/prop-drift.mjs +171 -6
  112. package/foundation/discovery/prop-drift.test.mjs +65 -0
  113. package/foundation/discovery/stray-pointers.d.mts +52 -0
  114. package/foundation/discovery/stray-pointers.mjs +293 -0
  115. package/foundation/discovery/stray-pointers.test.mjs +86 -0
  116. package/foundation/doc-compiler/doc-loads.test.mjs +16 -0
  117. package/foundation/doc-compiler/links.d.mts +32 -0
  118. package/foundation/doc-compiler/links.mjs +163 -26
  119. package/foundation/doc-compiler/links.test.mjs +97 -4
  120. package/foundation/doc-compiler/tree.mjs +30 -1
  121. package/foundation/doc-compiler/tree.test.mjs +7 -0
  122. package/foundation/env/package-manager.d.mts +32 -0
  123. package/foundation/env/package-manager.mjs +47 -0
  124. package/foundation/env/package-manager.test.mjs +194 -7
  125. package/foundation/fs/module-loader.d.mts +2 -1
  126. package/foundation/fs/module-loader.mjs +60 -4
  127. package/foundation/fs/module-loader.test.mjs +194 -0
  128. package/foundation/integrations/contribution-inventory.d.mts +11 -0
  129. package/foundation/integrations/contribution-inventory.mjs +50 -2
  130. package/foundation/integrations/issue.ts +6 -0
  131. package/foundation/integrations/validate-contributions.mjs +30 -1
  132. package/foundation/response/response-types.doc.mjs +10 -9
  133. package/package.json +11 -11
@@ -0,0 +1,293 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Stray doc pointers (architecture:cli-surface DEC-8): a code span in
5
+ * prose that is exactly `astryx docs <name>` for a doc that exists. The doc's
6
+ * `{@link}` prints the command that opens the doc and is checked by doctor's
7
+ * link check, so a written command is a pointer nothing checks.
8
+ *
9
+ * @input the docs a package ships, as discoverIntegrationDocs returns them
10
+ * @output each stray pointer, with the link that replaces it
11
+ * @position foundation/discovery, called through api/integration/_adapter.mjs
12
+ */
13
+
14
+ import fs from 'node:fs';
15
+
16
+ import {textFields} from '../doc-compiler/links.mjs';
17
+ import {builtinGuideFiles} from './docs-discovery.mjs';
18
+ import {
19
+ fileWords,
20
+ lineAtOffset,
21
+ loadQuietly,
22
+ locateWords,
23
+ textWords,
24
+ } from './copied-passages.mjs';
25
+
26
+ /**
27
+ * A code span that is exactly the command opening one doc. A command with
28
+ * more words, a flag, or a route is a walkthrough, not a pointer. A name holds
29
+ * what a topic name may (letters, digits, `_` and `-`), never opening with `-`.
30
+ */
31
+ const POINTER = /`astryx docs ([A-Za-z0-9_][A-Za-z0-9_-]*)`/g;
32
+
33
+ /** @type {Promise<Map<string, string>> | undefined} */
34
+ let coreKinds;
35
+
36
+ /**
37
+ * The CLI's own docs by name, each with the kind a link names it by: a
38
+ * namespace doc as `namespace`, a topic or guide as `generic`.
39
+ * @returns {Promise<Map<string, string>>}
40
+ */
41
+ function loadCoreKinds() {
42
+ coreKinds ??= (async () => {
43
+ const kinds = new Map();
44
+ for (const file of builtinGuideFiles()) {
45
+ const doc = await loadQuietly(file);
46
+ if (typeof doc?.name === 'string') {
47
+ kinds.set(doc.name, doc.type === 'namespace' ? 'namespace' : 'generic');
48
+ }
49
+ }
50
+ return kinds;
51
+ })();
52
+ return coreKinds;
53
+ }
54
+
55
+ /**
56
+ * The strings of a doc the CLI resolves links in (spec:AST-047 FR9). In a
57
+ * topic: its description and summary, then the text of prose and heading
58
+ * blocks, list items, and table cells in each section's content, with the
59
+ * section's title. In a component doc (any doc with no sections): every text
60
+ * field, such as its description, usage, best practices, and each prop's and
61
+ * example's description, with the field's path. Code is never read.
62
+ *
63
+ * @param {unknown} doc an authored doc object
64
+ * @returns {{text: string, section: string | null, field: string | null}[]}
65
+ */
66
+ export function linkableStrings(doc) {
67
+ /** @type {{text: string, section: string | null, field: string | null}[]} */
68
+ const strings = [];
69
+ if (doc == null || typeof doc !== 'object') return strings;
70
+ const top = /** @type {Record<string, any>} */ (doc);
71
+ if (!Array.isArray(top.sections)) {
72
+ for (const {field, text} of textFields(doc)) {
73
+ strings.push({text, section: null, field});
74
+ }
75
+ return strings;
76
+ }
77
+ for (const field of ['description', 'summary']) {
78
+ if (typeof top[field] === 'string') {
79
+ strings.push({text: top[field], section: null, field});
80
+ }
81
+ }
82
+ /**
83
+ * @param {unknown} value
84
+ * @param {string | null} section
85
+ */
86
+ const walk = (value, section) => {
87
+ if (Array.isArray(value)) {
88
+ for (const item of value) walk(item, section);
89
+ return;
90
+ }
91
+ if (value == null || typeof value !== 'object') return;
92
+ const record = /** @type {Record<string, any>} */ (value);
93
+ const title = typeof record.title === 'string' ? record.title : section;
94
+ if (Array.isArray(record.sections)) walk(record.sections, section);
95
+ if (!Array.isArray(record.content)) return;
96
+ for (const block of record.content) {
97
+ const add = (/** @type {unknown} */ text) => {
98
+ if (typeof text === 'string') {
99
+ strings.push({text, section: title, field: null});
100
+ }
101
+ };
102
+ switch (block?.type) {
103
+ case 'prose':
104
+ case 'heading':
105
+ add(block.text);
106
+ break;
107
+ case 'list':
108
+ (block.items ?? []).forEach(add);
109
+ break;
110
+ case 'table':
111
+ (block.headers ?? []).forEach(add);
112
+ for (const row of block.rows ?? []) row.forEach(add);
113
+ break;
114
+ default:
115
+ walk(block, title);
116
+ }
117
+ }
118
+ };
119
+ walk(doc, null);
120
+ return strings;
121
+ }
122
+
123
+ /**
124
+ * Every pointer span in one prose string, in order.
125
+ * @param {string} text
126
+ * @returns {string[]} the doc name each one points at
127
+ */
128
+ export function pointersIn(text) {
129
+ return [...text.matchAll(POINTER)].map(match => match[1]);
130
+ }
131
+
132
+ /**
133
+ * Where a string's words start among a file's words, from `from` on. A
134
+ * one-line string the file holds as a quoted literal is found at that
135
+ * literal, so a list item that is only `astryx docs theme` is not placed at a
136
+ * summary or code block that writes the same words.
137
+ *
138
+ * @param {string[]} lines the file's lines
139
+ * @param {{word: string, line: number}[]} file the file's words
140
+ * @param {string} text
141
+ * @param {{word: string}[]} words the string's words
142
+ * @param {number} from
143
+ * @returns {number}
144
+ */
145
+ function locateString(lines, file, text, words, from) {
146
+ /** @type {number[]} */
147
+ const starts = [];
148
+ for (let at = locateWords(file, words); at >= 0;) {
149
+ starts.push(at);
150
+ at = locateWords(file, words, at + 1);
151
+ }
152
+ if (starts.length === 0) return -1;
153
+ const literals = text.includes('\n')
154
+ ? []
155
+ : [`'${text}'`, `"${text}"`, `'${text.replace(/'/g, "\\'")}'`];
156
+ const quoted = starts.filter(at =>
157
+ literals.some(literal => lines[file[at].line - 1]?.includes(literal)),
158
+ );
159
+ const pool = quoted.length > 0 ? quoted : starts;
160
+ return pool.find(at => at >= from) ?? pool[0];
161
+ }
162
+
163
+ /**
164
+ * Each stray pointer in a package's topics and component docs.
165
+ *
166
+ * @param {{records: {name: string, path: string}[], guides?: {name: string, ref?: {topicFile?: string}}[], namespaces?: {doc?: {name?: string}}[], components?: {name: string, file: string}[]}} discovered
167
+ * @returns {Promise<import('../../api/integration/authoring-checks.type.mjs').StrayPointer[]>}
168
+ */
169
+ export async function findStrayPointers(discovered) {
170
+ /** @type {{name: string, file: string | undefined, kind: 'topic' | 'component'}[]} */
171
+ const topics = [
172
+ ...discovered.records.map(record => ({
173
+ name: record.name,
174
+ file: record.path,
175
+ kind: /** @type {const} */ ('topic'),
176
+ })),
177
+ ...(discovered.guides ?? []).map(guide => ({
178
+ name: guide.name,
179
+ file: guide.ref?.topicFile,
180
+ kind: /** @type {const} */ ('topic'),
181
+ })),
182
+ ];
183
+ /** @type {Map<string, string>} */
184
+ const own = new Map(topics.map(topic => [topic.name, 'generic']));
185
+ for (const namespace of discovered.namespaces ?? []) {
186
+ const name = namespace?.doc?.name;
187
+ if (typeof name === 'string' && !own.has(name)) own.set(name, 'namespace');
188
+ }
189
+ for (const component of discovered.components ?? []) {
190
+ topics.push({
191
+ name: component.name,
192
+ file: component.file,
193
+ kind: 'component',
194
+ });
195
+ }
196
+ const core = await loadCoreKinds();
197
+ // A topic that extends another has no route of its own: a link to it names
198
+ // the page it shows on, the topic it extends.
199
+ /** @type {Map<string, string>} */
200
+ const extendsOf = new Map();
201
+ for (const topic of topics) {
202
+ if (!topic.file) continue;
203
+ const doc = /** @type {any} */ (await loadQuietly(topic.file));
204
+ if (typeof doc?.extends === 'string')
205
+ extendsOf.set(topic.name, doc.extends);
206
+ }
207
+ /** @param {string} target */
208
+ const linkFor = target => {
209
+ const page = extendsOf.get(target);
210
+ if (page != null) {
211
+ if (own.has(page) && !extendsOf.has(page)) {
212
+ return `{@link ${own.get(page)}:${page}}`;
213
+ }
214
+ return core.has(page)
215
+ ? `{@link @astryxdesign/cli:${core.get(page)}:${page}}`
216
+ : null;
217
+ }
218
+ return own.has(target)
219
+ ? `{@link ${own.get(target)}:${target}}`
220
+ : core.has(target)
221
+ ? `{@link @astryxdesign/cli:${core.get(target)}:${target}}`
222
+ : null;
223
+ };
224
+ /** @type {import('../../api/integration/authoring-checks.type.mjs').StrayPointer[]} */
225
+ const found = [];
226
+ for (const topic of topics) {
227
+ if (!topic.file) continue;
228
+ const doc = await loadQuietly(topic.file);
229
+ if (!doc) continue;
230
+ /** @type {{word: string, line: number}[]} */
231
+ let source = [];
232
+ /** @type {string[]} */
233
+ let lines = [];
234
+ try {
235
+ const raw = fs.readFileSync(topic.file, 'utf8');
236
+ source = fileWords(raw);
237
+ lines = raw.split('\n');
238
+ } catch {
239
+ // No line numbers; the finding still names the section.
240
+ }
241
+ // A component doc is named by its own name: a family doc by the family.
242
+ const name =
243
+ topic.kind === 'component' && typeof doc.name === 'string'
244
+ ? doc.name
245
+ : topic.name;
246
+ const label =
247
+ topic.kind === 'component' ? `component "${name}"` : `"${name}"`;
248
+ // Each string is found by its whole run of words, from where the last
249
+ // one was found, so two strings that open alike keep their own lines.
250
+ let cursor = 0;
251
+ for (const {text, section, field} of linkableStrings(doc)) {
252
+ const words = textWords(text);
253
+ const index = locateString(lines, source, text, words, cursor);
254
+ if (index >= 0) cursor = index + words.length;
255
+ for (const match of text.matchAll(POINTER)) {
256
+ const target = match[1];
257
+ const link = linkFor(target);
258
+ if (!link) continue;
259
+ const command = `astryx docs ${target}`;
260
+ const line =
261
+ index < 0
262
+ ? null
263
+ : lineAtOffset(
264
+ source,
265
+ index,
266
+ words,
267
+ /** @type {number} */ (match.index) + 1,
268
+ );
269
+ const where = [
270
+ section ? `section "${section}"` : null,
271
+ field ? `field ${field}` : null,
272
+ line == null ? null : `line ${line}`,
273
+ ]
274
+ .filter(Boolean)
275
+ .join(', ');
276
+ found.push({
277
+ topic: name,
278
+ kind: topic.kind,
279
+ section,
280
+ field,
281
+ file: topic.file,
282
+ line,
283
+ pointer: command,
284
+ link,
285
+ message:
286
+ `${label}${where ? ` (${where})` : ''} points at a doc with the written command \`${command}\`. ` +
287
+ `Replace \`${command}\`, code ticks included, with ${link}: the CLI prints the command that opens the doc, and doctor checks the link.`,
288
+ });
289
+ }
290
+ }
291
+ }
292
+ return found;
293
+ }
@@ -0,0 +1,86 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import {describe, expect, it} from 'vitest';
4
+
5
+ import {linkableStrings, pointersIn} from './stray-pointers.mjs';
6
+
7
+ describe('pointersIn', () => {
8
+ it('finds a code span that is exactly the command opening one doc', () => {
9
+ expect(
10
+ pointersIn(
11
+ 'Read `astryx docs theme`, then `astryx docs layout-spacing`.',
12
+ ),
13
+ ).toEqual(['theme', 'layout-spacing']);
14
+ });
15
+
16
+ it('leaves a walkthrough alone: more words, a flag, or a route', () => {
17
+ expect(
18
+ pointersIn(
19
+ 'Run `astryx docs theme overview`, `astryx docs --json`, `astryx docs cli/integrations`, or `astryx docs`.',
20
+ ),
21
+ ).toEqual([]);
22
+ });
23
+
24
+ it('leaves the bare name and a runner-written command alone', () => {
25
+ expect(
26
+ pointersIn('See astryx docs theme, or `pnpm astryx docs theme`.'),
27
+ ).toEqual([]);
28
+ });
29
+ });
30
+
31
+ describe('linkableStrings', () => {
32
+ it("reads a topic's description and summary, then prose, headings, list items and table cells, not code", () => {
33
+ const strings = linkableStrings({
34
+ name: 'acme',
35
+ description: 'Read `astryx docs theme`.',
36
+ summary: 'In short.',
37
+ sections: [
38
+ {
39
+ title: 'Use it',
40
+ content: [
41
+ {type: 'prose', text: 'prose'},
42
+ {type: 'heading', level: 3, text: 'heading'},
43
+ {type: 'list', style: 'unordered', items: ['item']},
44
+ {type: 'table', headers: ['head'], rows: [['cell']]},
45
+ {type: 'code', lang: 'bash', code: 'astryx docs theme'},
46
+ ],
47
+ },
48
+ ],
49
+ });
50
+ expect(strings).toEqual([
51
+ {text: 'Read `astryx docs theme`.', section: null, field: 'description'},
52
+ {text: 'In short.', section: null, field: 'summary'},
53
+ {text: 'prose', section: 'Use it', field: null},
54
+ {text: 'heading', section: 'Use it', field: null},
55
+ {text: 'item', section: 'Use it', field: null},
56
+ {text: 'head', section: 'Use it', field: null},
57
+ {text: 'cell', section: 'Use it', field: null},
58
+ ]);
59
+ });
60
+
61
+ it("reads a component doc's text fields, not its names, types, defaults, or code", () => {
62
+ const strings = linkableStrings({
63
+ type: 'component',
64
+ name: 'AcmeButton',
65
+ displayName: 'Acme Button',
66
+ usage: {
67
+ description: 'usage',
68
+ bestPractices: [{guidance: true, description: 'practice'}],
69
+ },
70
+ props: [
71
+ {name: 'tone', type: 'string', default: "'a'", description: 'prop'},
72
+ ],
73
+ examples: [{label: 'example', code: 'astryx docs theme'}],
74
+ });
75
+ expect(strings).toEqual([
76
+ {text: 'usage', section: null, field: 'usage.description'},
77
+ {
78
+ text: 'practice',
79
+ section: null,
80
+ field: 'usage.bestPractices[0].description',
81
+ },
82
+ {text: 'prop', section: null, field: 'props[0].description'},
83
+ {text: 'example', section: null, field: 'examples[0].label'},
84
+ ]);
85
+ });
86
+ });
@@ -252,10 +252,26 @@ const RUNNERS = {
252
252
  runs: '@babel/parser, on first use, to read the members a family doc lists in its components without running the doc',
253
253
  sites: ['require(@babel/parser)'],
254
254
  },
255
+ 'foundation/discovery/copied-passages.mjs': {
256
+ runs: "the CLI's own guides and the package's topics, to compare their prose for passages two topics repeat",
257
+ sites: ['builtinGuideFiles', 'loadTopicModule'],
258
+ },
259
+ 'foundation/discovery/doc-consistency.mjs': {
260
+ runs: "a package's component docs and topics, to read the props, overlays, sections, and replaces/extends they declare",
261
+ sites: ['importDocModule ×2'],
262
+ },
263
+ 'foundation/discovery/doc-fluff.mjs': {
264
+ runs: "the CLI's own guides, to measure the median share of content words a topic's density is compared with",
265
+ sites: ['builtinGuideFiles'],
266
+ },
255
267
  'foundation/discovery/docs-discovery.mjs': {
256
268
  runs: 'a contributed topic, for its catalog fields: name, title, description, category, and relationships',
257
269
  sites: ['importDocModule'],
258
270
  },
271
+ 'foundation/discovery/stray-pointers.mjs': {
272
+ runs: "the CLI's own guides, for their names, to tell which written `astryx docs <name>` points at a doc that exists",
273
+ sites: ['builtinGuideFiles'],
274
+ },
259
275
  'foundation/discovery/project-typescript.mjs': {
260
276
  runs: "the project's own typescript, for the author checks that read source and docs with it, and its package.json for the version of one they cannot use; beside TypeScript 7, which has no compiler API, the TypeScript 6 API package @typescript/typescript6",
261
277
  sites: [
@@ -29,6 +29,8 @@
29
29
  * @property {string} target the target as written
30
30
  * @property {string} message
31
31
  * @property {string} [section] the key of the section the link sits in
32
+ * @property {string} [field] the field the link sits in, outside any section:
33
+ * `description`, or a path such as `props[2].description`
32
34
  * @property {true} [include] the problem is in a `reference` block, which
33
35
  * includes content rather than linking to it: a reader loses that content,
34
36
  * so the authoring check fails on it
@@ -55,6 +57,31 @@ export function parseLinkTarget(target: unknown): LinkTarget | {
55
57
  * @returns {string}
56
58
  */
57
59
  export function unlinkText(text: string): string;
60
+ /**
61
+ * Every text field of a doc object, in authored order, by the same rules
62
+ * {@link linkFields} resolves links by: the strings a reader reads, never a
63
+ * name, type, default, or code.
64
+ * @param {unknown} doc
65
+ * @returns {{field: string, text: string}[]}
66
+ */
67
+ export function textFields(doc: unknown): {
68
+ field: string;
69
+ text: string;
70
+ }[];
71
+ /**
72
+ * Resolve every inline link in the text fields of a doc object, such as a
73
+ * component doc or a topic's summary fields (spec:AST-047 FR9): its
74
+ * description, its usage and best practices, each prop's and example's
75
+ * description, at any depth. Code and code ticks show link syntax as written.
76
+ * @template T
77
+ * @param {T} doc
78
+ * @param {LinkResolver} resolve
79
+ * @returns {Promise<{doc: T, problems: LinkProblem[]}>}
80
+ */
81
+ export function linkFields<T>(doc: T, resolve: LinkResolver): Promise<{
82
+ doc: T;
83
+ problems: LinkProblem[];
84
+ }>;
58
85
  /**
59
86
  * Resolve every link in a list of content blocks, in order.
60
87
  * @param {any[]} blocks
@@ -148,6 +175,11 @@ export type LinkProblem = {
148
175
  * the key of the section the link sits in
149
176
  */
150
177
  section?: string | undefined;
178
+ /**
179
+ * the field the link sits in, outside any section:
180
+ * `description`, or a path such as `props[2].description`
181
+ */
182
+ field?: string | undefined;
151
183
  /**
152
184
  * the problem is in a `reference` block, which
153
185
  * includes content rather than linking to it: a reader loses that content,
@@ -65,6 +65,8 @@ const CODE_SPAN = /(`[^`]*`)/;
65
65
  * @property {string} target the target as written
66
66
  * @property {string} message
67
67
  * @property {string} [section] the key of the section the link sits in
68
+ * @property {string} [field] the field the link sits in, outside any section:
69
+ * `description`, or a path such as `props[2].description`
68
70
  * @property {true} [include] the problem is in a `reference` block, which
69
71
  * includes content rather than linking to it: a reader loses that content,
70
72
  * so the authoring check fails on it
@@ -91,7 +93,11 @@ export function parseLinkTarget(target) {
91
93
  };
92
94
  }
93
95
  const parts = target.trim().split(':');
94
- if (parts.length < 2 || parts.length > 3 || parts.some(p => p.trim() === '')) {
96
+ if (
97
+ parts.length < 2 ||
98
+ parts.length > 3 ||
99
+ parts.some(p => p.trim() === '')
100
+ ) {
95
101
  return {error: `"${target}" is not \`[<provider>:]<kind>:<name>\``};
96
102
  }
97
103
  const provider = parts.length === 3 ? parts[0].trim() : null;
@@ -127,6 +133,159 @@ export function unlinkText(text) {
127
133
  .join('');
128
134
  }
129
135
 
136
+ /**
137
+ * Text with each inline link replaced by the command that opens its doc. A
138
+ * link written inside code ticks is shown as written: it documents the syntax,
139
+ * it does not link. A link that names no doc prints as written: text that was
140
+ * never meant as a link reads the way it did, and doctor names the problem.
141
+ * @param {unknown} text
142
+ * @param {(target: string) => Promise<DocLink | null>} find
143
+ * @returns {Promise<any>}
144
+ */
145
+ async function resolveInline(text, find) {
146
+ if (typeof text !== 'string' || !text.includes('{@link')) return text;
147
+ let out = '';
148
+ for (const part of text.split(CODE_SPAN)) {
149
+ if (part.startsWith('`')) {
150
+ out += part;
151
+ continue;
152
+ }
153
+ let last = 0;
154
+ for (const match of part.matchAll(INLINE_LINK)) {
155
+ const link = await find(match[1]);
156
+ const at = /** @type {number} */ (match.index);
157
+ out += part.slice(last, at);
158
+ out += link ? `\`${link.command}\`` : match[0];
159
+ last = at + match[0].length;
160
+ }
161
+ out += part.slice(last);
162
+ }
163
+ return out;
164
+ }
165
+
166
+ /**
167
+ * The fields of a doc that hold text a reader reads, where a link resolves:
168
+ * a description or summary, an example's label, and each value of a
169
+ * `propDescriptions` overlay. Every other string is a name, a type, a
170
+ * default, or code, and is shown as written.
171
+ */
172
+ const TEXT_FIELDS = new Set(['description', 'summary', 'label']);
173
+
174
+ /** Fields that hold code or data, never text: nothing below them links. */
175
+ const CODE_FIELDS = new Set([
176
+ 'code',
177
+ 'playground',
178
+ 'slotElements',
179
+ 'registry',
180
+ 'keywords',
181
+ ]);
182
+
183
+ /**
184
+ * Every text field of a doc object, in authored order, by the same rules
185
+ * {@link linkFields} resolves links by: the strings a reader reads, never a
186
+ * name, type, default, or code.
187
+ * @param {unknown} doc
188
+ * @returns {{field: string, text: string}[]}
189
+ */
190
+ export function textFields(doc) {
191
+ /** @type {{field: string, text: string}[]} */
192
+ const out = [];
193
+ /**
194
+ * @param {unknown} value
195
+ * @param {string} at
196
+ * @param {boolean} text
197
+ */
198
+ const walk = (value, at, text) => {
199
+ if (typeof value === 'string') {
200
+ if (text) out.push({field: at, text: value});
201
+ return;
202
+ }
203
+ if (Array.isArray(value)) {
204
+ value.forEach((item, i) => walk(item, `${at}[${i}]`, false));
205
+ return;
206
+ }
207
+ if (value == null || typeof value !== 'object') return;
208
+ for (const [key, field] of Object.entries(value)) {
209
+ const path = at === '' ? key : `${at}.${key}`;
210
+ if (CODE_FIELDS.has(key)) continue;
211
+ if (key === 'propDescriptions' && field && typeof field === 'object') {
212
+ for (const [prop, description] of Object.entries(field)) {
213
+ walk(description, `${path}.${prop}`, true);
214
+ }
215
+ } else {
216
+ walk(field, path, TEXT_FIELDS.has(key));
217
+ }
218
+ }
219
+ };
220
+ walk(doc, '', false);
221
+ return out;
222
+ }
223
+
224
+ /**
225
+ * Resolve every inline link in the text fields of a doc object, such as a
226
+ * component doc or a topic's summary fields (spec:AST-047 FR9): its
227
+ * description, its usage and best practices, each prop's and example's
228
+ * description, at any depth. Code and code ticks show link syntax as written.
229
+ * @template T
230
+ * @param {T} doc
231
+ * @param {LinkResolver} resolve
232
+ * @returns {Promise<{doc: T, problems: LinkProblem[]}>}
233
+ */
234
+ export async function linkFields(doc, resolve) {
235
+ /** @type {LinkProblem[]} */
236
+ const problems = [];
237
+ /**
238
+ * @param {unknown} value
239
+ * @param {string} at the field path, e.g. `usage.bestPractices[0].description`
240
+ * @param {boolean} text whether a string here is text
241
+ * @returns {Promise<any>}
242
+ */
243
+ const walk = async (value, at, text) => {
244
+ if (typeof value === 'string') {
245
+ if (!text) return value;
246
+ return resolveInline(value, async target => {
247
+ const found = await resolve(target);
248
+ if ('problem' in found) {
249
+ problems.push({target, message: found.problem, field: at});
250
+ return null;
251
+ }
252
+ return found;
253
+ });
254
+ }
255
+ if (Array.isArray(value)) {
256
+ const out = [];
257
+ for (let i = 0; i < value.length; i++) {
258
+ out.push(await walk(value[i], `${at}[${i}]`, false));
259
+ }
260
+ return out;
261
+ }
262
+ if (value == null || typeof value !== 'object') return value;
263
+ /** @type {Record<string, unknown>} */
264
+ const out = {};
265
+ for (const [key, field] of Object.entries(value)) {
266
+ const path = at === '' ? key : `${at}.${key}`;
267
+ if (CODE_FIELDS.has(key)) {
268
+ out[key] = field;
269
+ } else if (
270
+ key === 'propDescriptions' &&
271
+ field &&
272
+ typeof field === 'object'
273
+ ) {
274
+ /** @type {Record<string, unknown>} */
275
+ const described = {};
276
+ for (const [prop, description] of Object.entries(field)) {
277
+ described[prop] = await walk(description, `${path}.${prop}`, true);
278
+ }
279
+ out[key] = described;
280
+ } else {
281
+ out[key] = await walk(field, path, TEXT_FIELDS.has(key));
282
+ }
283
+ }
284
+ return out;
285
+ };
286
+ return {doc: await walk(doc, '', false), problems};
287
+ }
288
+
130
289
  /**
131
290
  * Resolve every link in a list of content blocks, in order.
132
291
  * @param {any[]} blocks
@@ -156,30 +315,7 @@ export async function linkBlocks(blocks, resolve, at = {}, include) {
156
315
  * @param {unknown} text
157
316
  * @returns {Promise<any>}
158
317
  */
159
- const linkText = async text => {
160
- if (typeof text !== 'string' || !text.includes('{@link')) return text;
161
- let out = '';
162
- // A link written inside code ticks is shown as written: it documents the
163
- // syntax, it does not link.
164
- for (const part of text.split(CODE_SPAN)) {
165
- if (part.startsWith('`')) {
166
- out += part;
167
- continue;
168
- }
169
- let last = 0;
170
- for (const match of part.matchAll(INLINE_LINK)) {
171
- const link = await find(match[1]);
172
- const at = /** @type {number} */ (match.index);
173
- out += part.slice(last, at);
174
- // A link that names no doc prints as written: text that was never
175
- // meant as a link reads the way it did, and doctor names the problem.
176
- out += link ? `\`${link.command}\`` : match[0];
177
- last = at + match[0].length;
178
- }
179
- out += part.slice(last);
180
- }
181
- return out;
182
- };
318
+ const linkText = text => resolveInline(text, find);
183
319
  /**
184
320
  * @param {unknown[]} values
185
321
  * @returns {Promise<any[]>}
@@ -251,7 +387,8 @@ export async function linkBlocks(blocks, resolve, at = {}, include) {
251
387
  }
252
388
  if (Array.isArray(step.references)) {
253
389
  const links = [];
254
- for (const target of step.references) links.push(await find(target));
390
+ for (const target of step.references)
391
+ links.push(await find(target));
255
392
  linked.links = links;
256
393
  }
257
394
  steps.push(linked);