@cubicecho/agent-docs 0.0.0-stage → 0.1.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.
- package/LICENSE +21 -0
- package/README.md +52 -2
- package/_agents/docs/01-install.md +53 -0
- package/_agents/docs/checks.md +15 -0
- package/_agents/docs/ci.md +30 -0
- package/_agents/docs/cli.md +39 -0
- package/_agents/docs/common-mistakes.md +49 -0
- package/_agents/docs/config.md +25 -0
- package/dist/bin.d.ts +7 -0
- package/dist/bin.js +11 -0
- package/dist/check/check-agent-docs.d.ts +22 -0
- package/dist/check/check-agent-docs.js +254 -0
- package/dist/cli/run.d.ts +24 -0
- package/dist/cli/run.js +131 -0
- package/dist/consts.d.ts +16 -0
- package/dist/consts.js +16 -0
- package/dist/core/config.d.ts +31 -0
- package/dist/core/config.js +69 -0
- package/dist/core/errors.d.ts +17 -0
- package/dist/core/errors.js +21 -0
- package/dist/core/files.d.ts +28 -0
- package/dist/core/files.js +67 -0
- package/dist/core/json.d.ts +26 -0
- package/dist/core/json.js +44 -0
- package/dist/core/package-manifest.d.ts +36 -0
- package/dist/core/package-manifest.js +45 -0
- package/dist/defaults.d.ts +26 -0
- package/dist/defaults.js +14 -0
- package/dist/exports/entry-points.d.ts +30 -0
- package/dist/exports/entry-points.js +119 -0
- package/dist/exports/export-index.d.ts +44 -0
- package/dist/exports/export-index.js +233 -0
- package/dist/exports/module-source.d.ts +74 -0
- package/dist/exports/module-source.js +161 -0
- package/dist/exports/scan.d.ts +29 -0
- package/dist/exports/scan.js +156 -0
- package/dist/exports/summary.d.ts +36 -0
- package/dist/exports/summary.js +84 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +16 -0
- package/dist/llms/identity.d.ts +21 -0
- package/dist/llms/identity.js +67 -0
- package/dist/llms/plan.d.ts +53 -0
- package/dist/llms/plan.js +318 -0
- package/dist/llms/sync.d.ts +16 -0
- package/dist/llms/sync.js +36 -0
- package/dist/markdown/anchors.d.ts +35 -0
- package/dist/markdown/anchors.js +66 -0
- package/dist/markdown/page.d.ts +20 -0
- package/dist/markdown/page.js +57 -0
- package/llms.txt +42 -0
- package/package.json +65 -4
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { isJsonObject } from '../core/json.js';
|
|
4
|
+
import { LAYOUT_DEFAULTS } from '../defaults.js';
|
|
5
|
+
const { outDir, sourceDir } = LAYOUT_DEFAULTS;
|
|
6
|
+
/** Conditions read for an entry's file, most telling first. */
|
|
7
|
+
const TARGET_CONDITIONS = ['types', 'import', 'default', 'node', 'require'];
|
|
8
|
+
const BUILT_EXTENSION = /\.(d\.ts|d\.mts|js|mjs)$/;
|
|
9
|
+
const SOURCE_EXTENSION = /\.(ts|mts|tsx)$/;
|
|
10
|
+
const SOURCE_EXTENSIONS = ['.ts', '.mts', '.tsx'];
|
|
11
|
+
/**
|
|
12
|
+
* Maps each package.json `exports` entry back to its source file.
|
|
13
|
+
*
|
|
14
|
+
* @param root - Package root.
|
|
15
|
+
* @param packageName - The package's name, for the import paths.
|
|
16
|
+
* @param exportsField - package.json `exports` as written; undefined when absent.
|
|
17
|
+
* @returns The entries, and a problem for each that points into the build at a file `src/` lacks.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* A `types` or `import` target in `dist/` maps to the same path in `src/` with a `.ts` extension. Without
|
|
21
|
+
* `exports`, `src/index.ts` is the one entry when it exists. Wildcard subpaths and non-code targets (JSON, CSS)
|
|
22
|
+
* are left out, since they name no module to read.
|
|
23
|
+
*/
|
|
24
|
+
export function findEntryPoints(root, packageName, exportsField) {
|
|
25
|
+
if (exportsField === undefined) {
|
|
26
|
+
const fallback = `${sourceDir}/index.ts`;
|
|
27
|
+
const hasFallback = existsSync(join(root, fallback));
|
|
28
|
+
const entries = hasFallback ? [{ subpath: '.', importPath: packageName, source: fallback }] : [];
|
|
29
|
+
return { entries, problems: [] };
|
|
30
|
+
}
|
|
31
|
+
const entries = [];
|
|
32
|
+
const problems = [];
|
|
33
|
+
for (const [subpath, value] of subpathEntries(exportsField)) {
|
|
34
|
+
const target = pickTarget(value);
|
|
35
|
+
const isIndexable = target !== undefined && subpath.includes('*') === false && isCodeTarget(target);
|
|
36
|
+
if (isIndexable === false) {
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const importPath = subpath === '.' ? packageName : `${packageName}${subpath.slice(1)}`;
|
|
40
|
+
const source = sourceFor(root, target);
|
|
41
|
+
if (source === undefined) {
|
|
42
|
+
const expected = expectedSource(target);
|
|
43
|
+
problems.push(`package.json exports["${subpath}"] points at ${target}, but ${expected} does not exist: build it from ${sourceDir}/, or set "exports": false in _agents/llms.json.`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
entries.push({ subpath, importPath, source });
|
|
47
|
+
}
|
|
48
|
+
return { entries, problems };
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The subpath map of an `exports` field, whichever of its shapes it has.
|
|
52
|
+
*
|
|
53
|
+
* @param exportsField - package.json `exports`.
|
|
54
|
+
* @returns `[subpath, value]` pairs; a bare string or a conditions object is the `.` entry.
|
|
55
|
+
*/
|
|
56
|
+
function subpathEntries(exportsField) {
|
|
57
|
+
if (isJsonObject(exportsField) === false) {
|
|
58
|
+
return [['.', exportsField]];
|
|
59
|
+
}
|
|
60
|
+
const keys = Object.keys(exportsField);
|
|
61
|
+
const isSubpathMap = keys.length > 0 && keys.every((key) => key.startsWith('.'));
|
|
62
|
+
return isSubpathMap ? Object.entries(exportsField) : [['.', exportsField]];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The file an `exports` value resolves to, through nested conditions.
|
|
66
|
+
*
|
|
67
|
+
* @param value - One entry's value.
|
|
68
|
+
* @returns The first file path found under the preferred conditions; undefined for `null` or an empty object.
|
|
69
|
+
*/
|
|
70
|
+
function pickTarget(value) {
|
|
71
|
+
if (typeof value === 'string') {
|
|
72
|
+
return value;
|
|
73
|
+
}
|
|
74
|
+
if (Array.isArray(value)) {
|
|
75
|
+
return value.map(pickTarget).find((target) => target !== undefined);
|
|
76
|
+
}
|
|
77
|
+
if (isJsonObject(value) === false) {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
const preferred = TARGET_CONDITIONS.filter((condition) => condition in value);
|
|
81
|
+
const others = Object.keys(value).filter((condition) => TARGET_CONDITIONS.includes(condition) === false);
|
|
82
|
+
return [...preferred, ...others].map((condition) => pickTarget(value[condition])).find((t) => t !== undefined);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Whether a target names a module, built or source.
|
|
86
|
+
*
|
|
87
|
+
* @param target - A path from `exports`.
|
|
88
|
+
* @returns true for `.js`, `.mjs`, `.d.ts` and TypeScript files.
|
|
89
|
+
*/
|
|
90
|
+
function isCodeTarget(target) {
|
|
91
|
+
return BUILT_EXTENSION.test(target) || SOURCE_EXTENSION.test(target);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* The source path a target maps to before checking it exists.
|
|
95
|
+
*
|
|
96
|
+
* @param target - A path from `exports`.
|
|
97
|
+
* @returns The `src/…/x.ts` path, relative to the root.
|
|
98
|
+
*/
|
|
99
|
+
function expectedSource(target) {
|
|
100
|
+
const path = target.replace(/^\.\//, '');
|
|
101
|
+
if (SOURCE_EXTENSION.test(path) && BUILT_EXTENSION.test(path) === false) {
|
|
102
|
+
return path;
|
|
103
|
+
}
|
|
104
|
+
const inSource = path.startsWith(`${outDir}/`) ? `${sourceDir}/${path.slice(outDir.length + 1)}` : path;
|
|
105
|
+
return `${inSource.replace(BUILT_EXTENSION, '')}.ts`;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The existing source file a target maps to.
|
|
109
|
+
*
|
|
110
|
+
* @param root - Package root.
|
|
111
|
+
* @param target - A path from `exports`.
|
|
112
|
+
* @returns The path relative to the root, or undefined when no candidate exists.
|
|
113
|
+
*/
|
|
114
|
+
function sourceFor(root, target) {
|
|
115
|
+
const expected = expectedSource(target);
|
|
116
|
+
const stem = expected.replace(SOURCE_EXTENSION, '');
|
|
117
|
+
const candidates = [expected, ...SOURCE_EXTENSIONS.map((extension) => `${stem}${extension}`)];
|
|
118
|
+
return candidates.find((candidate) => existsSync(join(root, candidate)));
|
|
119
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { EntryPoint } from './entry-points.ts';
|
|
2
|
+
/** One name an entry point exports, as the index lists it. */
|
|
3
|
+
export interface IndexedExport {
|
|
4
|
+
/** The name a consumer imports. */
|
|
5
|
+
name: string;
|
|
6
|
+
/** Importable as a type only. */
|
|
7
|
+
isType: boolean;
|
|
8
|
+
/** The first sentence of its doc comment; empty when it has none. */
|
|
9
|
+
summary: string;
|
|
10
|
+
}
|
|
11
|
+
/** Names an entry re-exports from another package, which that package documents. */
|
|
12
|
+
export interface ForeignReExport {
|
|
13
|
+
/** The package specifier. */
|
|
14
|
+
specifier: string;
|
|
15
|
+
/** The names, or `*` for `export * from`. */
|
|
16
|
+
names: string[];
|
|
17
|
+
}
|
|
18
|
+
/** What one entry point exports, ready to render. */
|
|
19
|
+
export interface EntryIndex {
|
|
20
|
+
/** What a consumer imports. */
|
|
21
|
+
importPath: string;
|
|
22
|
+
/** The entry's source file, relative to the package root. */
|
|
23
|
+
source: string;
|
|
24
|
+
/** The first paragraph of the entry file's own doc comment; empty when it has none. */
|
|
25
|
+
summary: string;
|
|
26
|
+
/** Its exports from this package's source, in source order. */
|
|
27
|
+
exports: IndexedExport[];
|
|
28
|
+
/** Its re-exports from other packages, one entry per package. */
|
|
29
|
+
foreign: ForeignReExport[];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Lists what each entry point exports, each name with the first sentence of its doc comment.
|
|
33
|
+
*
|
|
34
|
+
* @param root - Package root.
|
|
35
|
+
* @param entries - The entry points, as {@link findEntryPoints} traces them.
|
|
36
|
+
* @returns One index per entry, in the same order.
|
|
37
|
+
* @throws {@link AgentDocsError} If a re-exported module is missing or an `export` statement can't be read.
|
|
38
|
+
*
|
|
39
|
+
* @remarks
|
|
40
|
+
* A re-exported name is followed through `export … from`, `export *` and imports to the declaration that
|
|
41
|
+
* documents it. Where a name is declared twice (a vocabulary's const and its union type), the first doc comment
|
|
42
|
+
* wins.
|
|
43
|
+
*/
|
|
44
|
+
export declare function buildExportIndex(root: string, entries: readonly EntryPoint[]): EntryIndex[];
|
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { join, posix } from 'node:path';
|
|
3
|
+
import { AgentDocsError } from '../core/errors.js';
|
|
4
|
+
import { ExportForm, parseModule } from './module-source.js';
|
|
5
|
+
import { plainText } from './summary.js';
|
|
6
|
+
const SOURCE_CANDIDATES = ['', '.ts', '.tsx', '.mts', '/index.ts'];
|
|
7
|
+
const BUILT_TO_SOURCE = { '.js': '.ts', '.mjs': '.mts', '.jsx': '.tsx' };
|
|
8
|
+
/** How many re-export hops a name is followed through before it is given up as a cycle. */
|
|
9
|
+
const MAX_HOPS = 32;
|
|
10
|
+
/**
|
|
11
|
+
* Lists what each entry point exports, each name with the first sentence of its doc comment.
|
|
12
|
+
*
|
|
13
|
+
* @param root - Package root.
|
|
14
|
+
* @param entries - The entry points, as {@link findEntryPoints} traces them.
|
|
15
|
+
* @returns One index per entry, in the same order.
|
|
16
|
+
* @throws {@link AgentDocsError} If a re-exported module is missing or an `export` statement can't be read.
|
|
17
|
+
*
|
|
18
|
+
* @remarks
|
|
19
|
+
* A re-exported name is followed through `export … from`, `export *` and imports to the declaration that
|
|
20
|
+
* documents it. Where a name is declared twice (a vocabulary's const and its union type), the first doc comment
|
|
21
|
+
* wins.
|
|
22
|
+
*/
|
|
23
|
+
export function buildExportIndex(root, entries) {
|
|
24
|
+
const reader = new ModuleReader(root);
|
|
25
|
+
const indexes = entries.map((entry) => {
|
|
26
|
+
const module = reader.read(entry.source);
|
|
27
|
+
const listed = reader.listExports(entry.source, 0);
|
|
28
|
+
return {
|
|
29
|
+
importPath: entry.importPath,
|
|
30
|
+
source: entry.source,
|
|
31
|
+
summary: plainText(module?.moduleDoc[0] ?? ''),
|
|
32
|
+
exports: dedupe(listed.exports),
|
|
33
|
+
foreign: [...listed.foreign].map(([specifier, names]) => ({ specifier, names: [...new Set(names)] })),
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
if (reader.problems.length > 0) {
|
|
37
|
+
throw new AgentDocsError([...new Set(reader.problems)]);
|
|
38
|
+
}
|
|
39
|
+
return indexes;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Keeps the first entry per name, merging what a later one adds.
|
|
43
|
+
*
|
|
44
|
+
* @param listed - Exports in source order, possibly repeating a name.
|
|
45
|
+
* @returns One entry per name; a value wins over a type, and the first summary found is kept.
|
|
46
|
+
*/
|
|
47
|
+
function dedupe(listed) {
|
|
48
|
+
const byName = new Map();
|
|
49
|
+
for (const entry of listed) {
|
|
50
|
+
const known = byName.get(entry.name);
|
|
51
|
+
if (known === undefined) {
|
|
52
|
+
byName.set(entry.name, { ...entry });
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
known.isType = known.isType && entry.isType;
|
|
56
|
+
known.summary = known.summary === '' ? entry.summary : known.summary;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return [...byName.values()];
|
|
60
|
+
}
|
|
61
|
+
/** Reads and caches the modules an entry reaches, collecting what it can't read. */
|
|
62
|
+
class ModuleReader {
|
|
63
|
+
/** One line per module or statement that could not be read. */
|
|
64
|
+
problems = [];
|
|
65
|
+
#root;
|
|
66
|
+
#cache = new Map();
|
|
67
|
+
/**
|
|
68
|
+
* Starts a reader for one package.
|
|
69
|
+
*
|
|
70
|
+
* @param root - Package root.
|
|
71
|
+
*/
|
|
72
|
+
constructor(root) {
|
|
73
|
+
this.#root = root;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Parses a module once.
|
|
77
|
+
*
|
|
78
|
+
* @param file - Path relative to the root.
|
|
79
|
+
* @returns The module, or undefined when the file is missing.
|
|
80
|
+
*/
|
|
81
|
+
read(file) {
|
|
82
|
+
if (this.#cache.has(file)) {
|
|
83
|
+
return this.#cache.get(file);
|
|
84
|
+
}
|
|
85
|
+
const full = join(this.#root, file);
|
|
86
|
+
const module = existsSync(full) ? parseModule(file, readFileSync(full, 'utf8')) : undefined;
|
|
87
|
+
this.#cache.set(file, module);
|
|
88
|
+
for (const line of module?.unreadLines ?? []) {
|
|
89
|
+
this.problems.push(`${file}:${line} has an export this index can't read: write it as a declaration or an "export { … } from" list.`);
|
|
90
|
+
}
|
|
91
|
+
return module;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Resolves a relative specifier to a source file.
|
|
95
|
+
*
|
|
96
|
+
* @param from - The importing file, relative to the root.
|
|
97
|
+
* @param specifier - The specifier as written.
|
|
98
|
+
* @returns The file relative to the root, or undefined when nothing matches (also reported as a problem).
|
|
99
|
+
*/
|
|
100
|
+
resolve(from, specifier) {
|
|
101
|
+
const base = posix.join(posix.dirname(from), specifier);
|
|
102
|
+
const builtExtension = Object.keys(BUILT_TO_SOURCE).find((extension) => base.endsWith(extension));
|
|
103
|
+
const stem = builtExtension ? base.slice(0, -builtExtension.length) : base;
|
|
104
|
+
const mapped = builtExtension ? [`${stem}${BUILT_TO_SOURCE[builtExtension]}`] : [];
|
|
105
|
+
const candidates = [...mapped, ...SOURCE_CANDIDATES.map((suffix) => `${stem}${suffix}`)];
|
|
106
|
+
const found = candidates.find((candidate) => this.#isFile(candidate));
|
|
107
|
+
if (found === undefined) {
|
|
108
|
+
this.problems.push(`${from} imports "${specifier}", which has no source file: fix the path.`);
|
|
109
|
+
}
|
|
110
|
+
return found;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Whether a path is an existing file with a source extension.
|
|
114
|
+
*
|
|
115
|
+
* @param path - Path relative to the root.
|
|
116
|
+
* @returns true for an existing `.ts`, `.tsx` or `.mts` file.
|
|
117
|
+
*/
|
|
118
|
+
#isFile(path) {
|
|
119
|
+
return /\.(ts|tsx|mts)$/.test(path) && existsSync(join(this.#root, path));
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Everything a module exports, in source order.
|
|
123
|
+
*
|
|
124
|
+
* @param file - Path relative to the root.
|
|
125
|
+
* @param hops - Re-export hops taken so far.
|
|
126
|
+
* @returns Its own and re-exported names, and the names it takes from other packages.
|
|
127
|
+
*/
|
|
128
|
+
listExports(file, hops) {
|
|
129
|
+
const exports = [];
|
|
130
|
+
const foreign = new Map();
|
|
131
|
+
const module = this.read(file);
|
|
132
|
+
if (module === undefined || hops > MAX_HOPS) {
|
|
133
|
+
return { exports, foreign };
|
|
134
|
+
}
|
|
135
|
+
const addForeign = (specifier, names) => {
|
|
136
|
+
foreign.set(specifier, [...(foreign.get(specifier) ?? []), ...names]);
|
|
137
|
+
};
|
|
138
|
+
const items = [
|
|
139
|
+
...module.declarations.filter((d) => d.isExported).map((d) => ({ offset: d.offset, declaration: d })),
|
|
140
|
+
...module.statements.map((s) => ({ offset: s.offset, statement: s })),
|
|
141
|
+
].sort((a, b) => a.offset - b.offset);
|
|
142
|
+
for (const item of items) {
|
|
143
|
+
if ('declaration' in item) {
|
|
144
|
+
const { name, isType, summary } = item.declaration;
|
|
145
|
+
exports.push({ name, isType, summary });
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
const { form, names, specifier } = item.statement;
|
|
149
|
+
const isForeign = specifier !== null && specifier.startsWith('.') === false;
|
|
150
|
+
if (isForeign) {
|
|
151
|
+
addForeign(specifier, form === ExportForm.All ? ['*'] : names.map((n) => n.exported));
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
const target = specifier === null ? file : this.resolve(file, specifier);
|
|
155
|
+
if (target === undefined) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (form === ExportForm.All) {
|
|
159
|
+
const inner = this.listExports(target, hops + 1);
|
|
160
|
+
exports.push(...inner.exports);
|
|
161
|
+
for (const [innerSpecifier, innerNames] of inner.foreign) {
|
|
162
|
+
addForeign(innerSpecifier, innerNames);
|
|
163
|
+
}
|
|
164
|
+
continue;
|
|
165
|
+
}
|
|
166
|
+
if (form === ExportForm.Namespace) {
|
|
167
|
+
const summary = plainText(this.read(target)?.moduleDoc[0] ?? '');
|
|
168
|
+
exports.push(...names.map((n) => ({ name: n.exported, isType: n.isType, summary })));
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
for (const name of names) {
|
|
172
|
+
const described = this.describe(target, name.local, hops + 1);
|
|
173
|
+
const isType = name.isType || (described?.isType ?? false);
|
|
174
|
+
exports.push({ name: name.exported, isType, summary: described?.summary ?? '' });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return { exports, foreign };
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Follows a name to the declaration that documents it.
|
|
181
|
+
*
|
|
182
|
+
* @param file - The module the name is looked up in.
|
|
183
|
+
* @param name - The name inside that module.
|
|
184
|
+
* @param hops - Re-export hops taken so far.
|
|
185
|
+
* @returns Whether it is a type and its summary; undefined when it leaves the package or can't be found.
|
|
186
|
+
*/
|
|
187
|
+
describe(file, name, hops) {
|
|
188
|
+
const module = this.read(file);
|
|
189
|
+
if (module === undefined || hops > MAX_HOPS) {
|
|
190
|
+
return undefined;
|
|
191
|
+
}
|
|
192
|
+
const declared = module.declarations.filter((d) => d.name === name);
|
|
193
|
+
if (declared.length > 0) {
|
|
194
|
+
const summary = declared.find((d) => d.summary !== '')?.summary ?? '';
|
|
195
|
+
return { isType: declared.every((d) => d.isType), summary };
|
|
196
|
+
}
|
|
197
|
+
const imported = module.imports.get(name);
|
|
198
|
+
if (imported !== undefined) {
|
|
199
|
+
return this.#follow(file, imported.specifier, imported.imported, hops);
|
|
200
|
+
}
|
|
201
|
+
for (const statement of module.statements) {
|
|
202
|
+
const listed = statement.names.find((n) => n.exported === name);
|
|
203
|
+
const isListed = listed !== undefined && statement.form === ExportForm.List && statement.specifier !== null;
|
|
204
|
+
if (isListed) {
|
|
205
|
+
return this.#follow(file, statement.specifier ?? '', listed.local, hops);
|
|
206
|
+
}
|
|
207
|
+
if (statement.form === ExportForm.All && statement.specifier?.startsWith('.')) {
|
|
208
|
+
const target = this.resolve(file, statement.specifier);
|
|
209
|
+
const found = target === undefined ? undefined : this.describe(target, name, hops + 1);
|
|
210
|
+
if (found !== undefined) {
|
|
211
|
+
return found;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return undefined;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Describes a name in the module a specifier names, when it is in this package.
|
|
219
|
+
*
|
|
220
|
+
* @param from - The file the specifier is written in.
|
|
221
|
+
* @param specifier - The specifier.
|
|
222
|
+
* @param name - The name inside the target module.
|
|
223
|
+
* @param hops - Re-export hops taken so far.
|
|
224
|
+
* @returns What {@link ModuleReader.describe} finds there; undefined for another package.
|
|
225
|
+
*/
|
|
226
|
+
#follow(from, specifier, name, hops) {
|
|
227
|
+
if (specifier.startsWith('.') === false) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
const target = this.resolve(from, specifier);
|
|
231
|
+
return target === undefined ? undefined : this.describe(target, name, hops + 1);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/** A top-level declaration and what its doc comment says. */
|
|
2
|
+
export interface Declaration {
|
|
3
|
+
/** The declared name; `default` for an `export default` with no name of its own. */
|
|
4
|
+
name: string;
|
|
5
|
+
/** An interface or type alias. */
|
|
6
|
+
isType: boolean;
|
|
7
|
+
/** Declared with `export`. */
|
|
8
|
+
isExported: boolean;
|
|
9
|
+
/** The first sentence of its doc comment; empty when it has none. */
|
|
10
|
+
summary: string;
|
|
11
|
+
/** Offset of the declaration in the file. */
|
|
12
|
+
offset: number;
|
|
13
|
+
}
|
|
14
|
+
/** One name in an `export { … }` or `import { … }` list. */
|
|
15
|
+
export interface ListedName {
|
|
16
|
+
/** The name inside the module it comes from. */
|
|
17
|
+
local: string;
|
|
18
|
+
/** The name it goes by after `as`, or `local`. */
|
|
19
|
+
exported: string;
|
|
20
|
+
/** Marked `type`, on the name or the whole list. */
|
|
21
|
+
isType: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** The shapes of export statement this reader understands, beside declarations. */
|
|
24
|
+
export declare const ExportForm: {
|
|
25
|
+
/** `export { a, type B } from '…'`, or the same list without `from`. */
|
|
26
|
+
readonly List: 'list';
|
|
27
|
+
/** `export * from '…'`. */
|
|
28
|
+
readonly All: 'all';
|
|
29
|
+
/** `export * as ns from '…'`. */
|
|
30
|
+
readonly Namespace: 'namespace';
|
|
31
|
+
};
|
|
32
|
+
export type ExportForm = (typeof ExportForm)[keyof typeof ExportForm];
|
|
33
|
+
/** An export statement that is not a declaration. */
|
|
34
|
+
export interface ExportStatement {
|
|
35
|
+
/** Which shape it has. */
|
|
36
|
+
form: ExportForm;
|
|
37
|
+
/** The names it exports: the list, or the one namespace name. Empty for `export *`. */
|
|
38
|
+
names: ListedName[];
|
|
39
|
+
/** The module it re-exports from; null for a list of the file's own names. */
|
|
40
|
+
specifier: string | null;
|
|
41
|
+
/** Offset of the statement in the file. */
|
|
42
|
+
offset: number;
|
|
43
|
+
}
|
|
44
|
+
/** What one TypeScript file declares, exports and imports. */
|
|
45
|
+
export interface ModuleSource {
|
|
46
|
+
/** The file's path relative to the package root. */
|
|
47
|
+
file: string;
|
|
48
|
+
/** Paragraphs of the file's own doc comment (a doc comment followed by a blank line); empty when absent. */
|
|
49
|
+
moduleDoc: string[];
|
|
50
|
+
/** Top-level declarations in order, overloads and merged declarations each listed. */
|
|
51
|
+
declarations: Declaration[];
|
|
52
|
+
/** Export statements in order. */
|
|
53
|
+
statements: ExportStatement[];
|
|
54
|
+
/** Named imports by local name. */
|
|
55
|
+
imports: Map<string, {
|
|
56
|
+
imported: string;
|
|
57
|
+
specifier: string;
|
|
58
|
+
}>;
|
|
59
|
+
/** Line numbers of `export` statements none of the patterns read. */
|
|
60
|
+
unreadLines: number[];
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Reads the shape of one TypeScript module.
|
|
64
|
+
*
|
|
65
|
+
* @param file - The file's path relative to the package root, for messages.
|
|
66
|
+
* @param source - The file's text.
|
|
67
|
+
* @returns Its declarations, exports, imports and own doc comment.
|
|
68
|
+
* @throws {@link AgentDocsError} If a block comment never closes.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* Only top-level code at the start of a line is read, as a formatter leaves it. An `export` statement that no
|
|
72
|
+
* pattern reads is reported in `unreadLines` rather than skipped, so the index never quietly drops an export.
|
|
73
|
+
*/
|
|
74
|
+
export declare function parseModule(file: string, source: string): ModuleSource;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { scanSource } from './scan.js';
|
|
2
|
+
import { paragraphs, summarize } from './summary.js';
|
|
3
|
+
/** The shapes of export statement this reader understands, beside declarations. */
|
|
4
|
+
export const ExportForm = {
|
|
5
|
+
/** `export { a, type B } from '…'`, or the same list without `from`. */
|
|
6
|
+
List: 'list',
|
|
7
|
+
/** `export * from '…'`. */
|
|
8
|
+
All: 'all',
|
|
9
|
+
/** `export * as ns from '…'`. */
|
|
10
|
+
Namespace: 'namespace',
|
|
11
|
+
};
|
|
12
|
+
const DECLARATION = /^(export[ \t]+)?(?:declare[ \t]+)?(default[ \t]+)?(?:abstract[ \t]+)?(?:async[ \t]+)?(function[ \t]*\*?|const|let|var|class|interface|type|enum|namespace)[ \t]+([A-Za-z_$][\w$]*)/gm;
|
|
13
|
+
const DEFAULT_EXPRESSION = /^export[ \t]+default\b/gm;
|
|
14
|
+
const LIST = /^export[ \t]+(type[ \t]+)?\{([^}]*)\}(?:\s*from\s*(['"])([^'"\n]+)\3)?/gm;
|
|
15
|
+
const ALL = /^export[ \t]+(?:type[ \t]+)?\*(?:\s+as\s+([\w$]+))?\s+from\s*(['"])([^'"\n]+)\2/gm;
|
|
16
|
+
const IMPORT = /^import[ \t]+(type[ \t]+)?\{([^}]*)\}\s*from\s*(['"])([^'"\n]+)\3/gm;
|
|
17
|
+
const EXPORT_KEYWORD = /^export\b/gm;
|
|
18
|
+
const TYPE_KINDS = new Set(['interface', 'type']);
|
|
19
|
+
/**
|
|
20
|
+
* Reads the shape of one TypeScript module.
|
|
21
|
+
*
|
|
22
|
+
* @param file - The file's path relative to the package root, for messages.
|
|
23
|
+
* @param source - The file's text.
|
|
24
|
+
* @returns Its declarations, exports, imports and own doc comment.
|
|
25
|
+
* @throws {@link AgentDocsError} If a block comment never closes.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* Only top-level code at the start of a line is read, as a formatter leaves it. An `export` statement that no
|
|
29
|
+
* pattern reads is reported in `unreadLines` rather than skipped, so the index never quietly drops an export.
|
|
30
|
+
*/
|
|
31
|
+
export function parseModule(file, source) {
|
|
32
|
+
const { code, docBlocks } = scanSource(file, source);
|
|
33
|
+
const declarations = [];
|
|
34
|
+
for (const match of code.matchAll(DECLARATION)) {
|
|
35
|
+
const [, exportKeyword, defaultKeyword, kind, name] = match;
|
|
36
|
+
const block = attachedBlock(source, docBlocks, match.index);
|
|
37
|
+
declarations.push({
|
|
38
|
+
name: defaultKeyword ? 'default' : name,
|
|
39
|
+
isType: TYPE_KINDS.has(kind),
|
|
40
|
+
isExported: Boolean(exportKeyword),
|
|
41
|
+
summary: block ? summarize(block.body) : '',
|
|
42
|
+
offset: match.index,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const statements = exportStatements(code);
|
|
46
|
+
const read = new Set([...declarations.map((d) => d.offset), ...statements.map((s) => s.offset)]);
|
|
47
|
+
for (const match of code.matchAll(DEFAULT_EXPRESSION)) {
|
|
48
|
+
const isUnread = read.has(match.index) === false;
|
|
49
|
+
if (isUnread) {
|
|
50
|
+
const block = attachedBlock(source, docBlocks, match.index);
|
|
51
|
+
const summary = block ? summarize(block.body) : '';
|
|
52
|
+
declarations.push({ name: 'default', isType: false, isExported: true, summary, offset: match.index });
|
|
53
|
+
read.add(match.index);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
const unreadLines = [...code.matchAll(EXPORT_KEYWORD)]
|
|
57
|
+
.filter((match) => read.has(match.index) === false)
|
|
58
|
+
.map((match) => lineNumber(code, match.index));
|
|
59
|
+
const moduleBlock = docBlocks.find((block) => isFollowedByBlankLine(source, block));
|
|
60
|
+
return {
|
|
61
|
+
file,
|
|
62
|
+
moduleDoc: moduleBlock ? paragraphs(moduleBlock.body) : [],
|
|
63
|
+
declarations: declarations.sort((a, b) => a.offset - b.offset),
|
|
64
|
+
statements,
|
|
65
|
+
imports: namedImports(code),
|
|
66
|
+
unreadLines,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The `export { … }` and `export *` statements of a file.
|
|
71
|
+
*
|
|
72
|
+
* @param code - The file with comments blanked.
|
|
73
|
+
* @returns The statements, in order.
|
|
74
|
+
*/
|
|
75
|
+
function exportStatements(code) {
|
|
76
|
+
const statements = [];
|
|
77
|
+
for (const match of code.matchAll(LIST)) {
|
|
78
|
+
const [, typeKeyword, inner, , specifier] = match;
|
|
79
|
+
const names = listedNames(inner, Boolean(typeKeyword));
|
|
80
|
+
statements.push({ form: ExportForm.List, names, specifier: specifier ?? null, offset: match.index });
|
|
81
|
+
}
|
|
82
|
+
for (const match of code.matchAll(ALL)) {
|
|
83
|
+
const [, namespace, , specifier] = match;
|
|
84
|
+
const names = namespace ? [{ local: namespace, exported: namespace, isType: false }] : [];
|
|
85
|
+
const form = namespace ? ExportForm.Namespace : ExportForm.All;
|
|
86
|
+
statements.push({ form, names, specifier, offset: match.index });
|
|
87
|
+
}
|
|
88
|
+
return statements.sort((a, b) => a.offset - b.offset);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The names in the braces of an import or export list.
|
|
92
|
+
*
|
|
93
|
+
* @param inner - The text between the braces.
|
|
94
|
+
* @param isTypeList - Whether the whole list is marked `type`.
|
|
95
|
+
* @returns One entry per name, with its `as` rename applied.
|
|
96
|
+
*/
|
|
97
|
+
function listedNames(inner, isTypeList) {
|
|
98
|
+
return inner
|
|
99
|
+
.split(',')
|
|
100
|
+
.map((entry) => entry.trim())
|
|
101
|
+
.filter((entry) => entry !== '')
|
|
102
|
+
.map((entry) => {
|
|
103
|
+
const isType = isTypeList || entry.startsWith('type ');
|
|
104
|
+
const [local, exported = local] = entry.replace(/^type\s+/, '').split(/\s+as\s+/);
|
|
105
|
+
return { local, exported, isType };
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The named imports of a file.
|
|
110
|
+
*
|
|
111
|
+
* @param code - The file with comments blanked.
|
|
112
|
+
* @returns The imported name and module per local name.
|
|
113
|
+
*/
|
|
114
|
+
function namedImports(code) {
|
|
115
|
+
const imports = new Map();
|
|
116
|
+
for (const [, typeKeyword, inner, , specifier] of code.matchAll(IMPORT)) {
|
|
117
|
+
for (const { local, exported } of listedNames(inner, Boolean(typeKeyword))) {
|
|
118
|
+
imports.set(exported, { imported: local, specifier });
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
return imports;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* The doc comment directly above a position, with no blank line between.
|
|
125
|
+
*
|
|
126
|
+
* @param source - The file's text.
|
|
127
|
+
* @param blocks - The file's doc comments.
|
|
128
|
+
* @param offset - Where the declaration starts.
|
|
129
|
+
* @returns The comment, or undefined when the nearest one is separated by a blank line or by code.
|
|
130
|
+
*/
|
|
131
|
+
function attachedBlock(source, blocks, offset) {
|
|
132
|
+
const block = blocks.findLast((candidate) => candidate.end <= offset);
|
|
133
|
+
if (block === undefined) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
const between = source.slice(block.end, offset).split('\n');
|
|
137
|
+
const isAttached = between.slice(1, -1).every((line) => line.trim().startsWith('//') || line.trim().startsWith('@'));
|
|
138
|
+
const isOnlySpace = between[0].trim() === '' && between[between.length - 1].trim() === '';
|
|
139
|
+
return isAttached && isOnlySpace ? block : undefined;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Whether a doc comment is the file's own: followed by a blank line, or by nothing.
|
|
143
|
+
*
|
|
144
|
+
* @param source - The file's text.
|
|
145
|
+
* @param block - One of its doc comments.
|
|
146
|
+
* @returns true when the line after the comment is blank or the file ends.
|
|
147
|
+
*/
|
|
148
|
+
function isFollowedByBlankLine(source, block) {
|
|
149
|
+
const [rest = '', next] = source.slice(block.end).split('\n', 2);
|
|
150
|
+
return rest.trim() === '' && (next === undefined || next.trim() === '');
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The 1-based line number of an offset.
|
|
154
|
+
*
|
|
155
|
+
* @param text - The file's text.
|
|
156
|
+
* @param offset - A position in it.
|
|
157
|
+
* @returns The line it is on.
|
|
158
|
+
*/
|
|
159
|
+
function lineNumber(text, offset) {
|
|
160
|
+
return text.slice(0, offset).split('\n').length;
|
|
161
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** One `/** … *\/` comment. */
|
|
2
|
+
export interface DocBlock {
|
|
3
|
+
/** Offset of the opening `/**`. */
|
|
4
|
+
start: number;
|
|
5
|
+
/** Offset just past the closing delimiter. */
|
|
6
|
+
end: number;
|
|
7
|
+
/** The comment's lines, `*` margin stripped. */
|
|
8
|
+
body: string[];
|
|
9
|
+
}
|
|
10
|
+
/** A source file with its comments found. */
|
|
11
|
+
export interface ScannedSource {
|
|
12
|
+
/** The source with every comment replaced by spaces, newlines and offsets kept, so patterns never match inside one. */
|
|
13
|
+
code: string;
|
|
14
|
+
/** Every doc comment, in order. */
|
|
15
|
+
docBlocks: DocBlock[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Finds the comments in a TypeScript file.
|
|
19
|
+
*
|
|
20
|
+
* @param file - The file's path, for messages.
|
|
21
|
+
* @param source - The file's text.
|
|
22
|
+
* @returns The source with comments blanked, and its doc comments.
|
|
23
|
+
* @throws {@link AgentDocsError} If a block comment is never closed.
|
|
24
|
+
*
|
|
25
|
+
* @remarks
|
|
26
|
+
* Strings, template literals and regex literals are stepped over, so a `//` in a URL or a `/*` in a pattern is
|
|
27
|
+
* not taken for a comment.
|
|
28
|
+
*/
|
|
29
|
+
export declare function scanSource(file: string, source: string): ScannedSource;
|