-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi-docs-plugin.ts
More file actions
578 lines (524 loc) · 17.7 KB
/
Copy pathapi-docs-plugin.ts
File metadata and controls
578 lines (524 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
import fs from 'node:fs'
import path from 'node:path'
import type { Plugin } from 'rolldown'
interface MemberProp {
name: string
type?: string
jsdoc: string
optional: boolean
defaultValue?: string
}
interface ApiSymbol {
name: string
kind: 'function' | 'class' | 'interface' | 'type' | 'const'
jsdoc: string
signature: string
paramTypes?: Record<string, string>
members?: MemberProp[]
extendsTypes?: string[]
}
interface ModuleExports {
[moduleName: string]: ApiSymbol[]
}
const ROOT = path.resolve(import.meta.dirname!)
const ENTRY_POINTS: Record<string, string> = {
entry: path.join(ROOT, 'src/entry/index.ts'),
provider: path.join(ROOT, 'src/provider/index.ts'),
utils: path.join(ROOT, 'src/utils/index.ts'),
vite: path.join(ROOT, 'src/vite/index.ts'),
}
function resolveRelativeImport(importPath: string, fromDir: string): string | null {
const extensions = ['.ts', '/index.ts']
for (const ext of extensions) {
const full = path.resolve(fromDir, importPath + ext)
if (fs.existsSync(full)) {
return full
}
}
return null
}
function followReExports(entryFile: string): string[] {
const visited = new Set<string>()
const result: string[] = []
const queue = [entryFile]
while (queue.length > 0) {
const file = queue.shift()!
const resolved = path.resolve(file)
if (visited.has(resolved)) {
continue
}
visited.add(resolved)
result.push(resolved)
const content = fs.readFileSync(resolved, 'utf-8')
// Match `export * from`, `export { } from`, `export type * from`, `export type { } from`
const reExportRegex =
/export\s+(?:type\s+)?(?:\*\s+from\s+['"]([^'"]+)['"]|\{[^}]*\}\s*from\s+['"]([^'"]+)['"])/g
let match: RegExpExecArray | null
while ((match = reExportRegex.exec(content)) !== null) {
const importPath = match[1] || match[2]
if (importPath?.startsWith('.')) {
const resolvedImport = resolveRelativeImport(importPath, path.dirname(resolved))
if (resolvedImport && !visited.has(resolvedImport)) {
queue.push(resolvedImport)
}
}
}
}
return result
}
function findJSDocBefore(code: string, position: number): string | null {
// Scan backwards from position, skipping whitespace
let i = position - 1
while (i >= 0 && (code[i] === ' ' || code[i] === '\n' || code[i] === '\r' || code[i] === '\t')) {
i--
}
// Check for closing */
if (i < 1 || code[i] !== '/' || code[i - 1] !== '*') {
return null
}
// Find opening /**
const end = i + 1
let start = code.lastIndexOf('/**', end)
if (start === -1) {
return null
}
// Make sure only whitespace between the comment and the previous non-comment content
const beforeComment = code.slice(0, start).trimEnd()
if (beforeComment.endsWith('*/')) {
return null
} // Not directly before: there's another comment in between
const raw = code.slice(start + 3, end - 2)
// Clean up JSDoc text, preserve line breaks for @example blocks
const text = raw
.split('\n')
.map((line) => line.replace(/^\s*\*\s?/, ''))
.filter((line) => !line.startsWith('@type'))
.join('\n')
.trim()
return text || null
}
interface FormattedJSDoc {
description: string
example: string | null
}
function formatInlineTags(text: string): string {
// Convert {@link Target} to [\`Target\`](#target)
return text.replace(/\{@link\s+([^}]+)\}/g, (_, target) => {
const slug = target.trim().toLowerCase()
return `[\`${target.trim()}\`](#${slug})`
})
}
function formatJSDoc(raw: string, paramTypes?: Record<string, string>): FormattedJSDoc {
const parts: string[] = []
const params: { name: string; type?: string; desc: string }[] = []
let example: string | null = null
let remaining = raw
// Extract the main description (everything before the first @tag), keep line breaks
const firstTag = remaining.search(/@\w+/)
if (firstTag > 0) {
parts.push(
formatInlineTags(
remaining
.slice(0, firstTag)
.replace(/\n{2,}/g, '\n')
.trim(),
),
)
remaining = remaining.slice(firstTag)
} else if (firstTag === -1) {
return {
description: formatInlineTags(remaining.replace(/\n{2,}/g, '\n').trim()),
example: null,
}
}
// Extract @param, @example, @returns, @default, etc. (not @link — inline tag)
const tags = remaining.split(/(?=\s*@(?!link)\w+)/g)
for (const tag of tags) {
const trimmed = tag.trim()
if (!trimmed) {
continue
}
if (trimmed.startsWith('@example')) {
example = trimmed.replace(/^@example\s*/, '').trim()
} else if (trimmed.startsWith('@param')) {
const content = trimmed.replace(/^@param\s*/, '')
const spaceIdx = content.indexOf(' ')
const name = spaceIdx > 0 ? content.slice(0, spaceIdx) : content
const desc =
spaceIdx > 0
? formatInlineTags(
content
.slice(spaceIdx + 1)
.replace(/\n/g, ' ')
.replace(/^-\s*/, '')
.trim(),
)
: ''
params.push({ name, type: paramTypes?.[name], desc })
} else if (trimmed.startsWith('@returns') || trimmed.startsWith('@return')) {
parts.push(`- ${formatInlineTags(trimmed.replace(/\n/g, ' ').trim())}`)
} else if (trimmed.startsWith('@default')) {
parts.push(`- ${formatInlineTags(trimmed.replace(/\n/g, ' ').trim())}`)
} else if (trimmed.startsWith('@see')) {
const see = trimmed.replace(/^@see\s*/, '').trim()
parts.push(`*See also:* ${formatInlineTags(see)}`)
} else {
// Handle {@link ...} inline tags in orphan text, and strip stray @link prefixes
parts.push(formatInlineTags(trimmed.replace(/^@link\s*/, '')))
}
}
// Render params as table
if (params.length > 0) {
parts.push('')
const hasTypes = params.some((p) => p.type)
if (hasTypes) {
parts.push('| Parameter | Type | Description |')
parts.push('|-----------|------|-------------|')
for (const p of params) {
parts.push(`| \`${p.name}\` | \`${p.type || ''}\` | ${p.desc} |`)
}
} else {
parts.push('| Parameter | Description |')
parts.push('|-----------|-------------|')
for (const p of params) {
parts.push(`| \`${p.name}\` | ${p.desc} |`)
}
}
}
return { description: parts.join('\n'), example }
}
function getSignatureSource(code: string, exportNode: any): string {
const decl = exportNode.declaration
if (!decl) {
return code.slice(exportNode.start, exportNode.end)
}
// For FunctionDeclaration and ClassDeclaration, truncate at body
if (decl.type === 'FunctionDeclaration' || decl.type === 'ClassDeclaration') {
if (decl.body && decl.body.start !== undefined) {
return `${code.slice(exportNode.start, decl.body.start).trimEnd()} { /* ... */ }`
}
}
return code.slice(exportNode.start, exportNode.end)
}
function extractExports(code: string, ast: any): ApiSymbol[] {
const symbols: ApiSymbol[] = []
function processExport(exportNode: any): void {
const jsdoc = findJSDocBefore(code, exportNode.start)
if (jsdoc && /(?:^|\s)@deprecated(?:\s|$)/.test(jsdoc)) {
return
}
const decl = exportNode.declaration
if (!decl) {
return
}
let name: string | undefined
let kind: ApiSymbol['kind'] = 'function'
const signature = getSignatureSource(code, exportNode)
const extendsTypes = extractExtends(code, decl)
if (decl.type === 'FunctionDeclaration' && decl.id) {
name = decl.id.name
} else if (decl.type === 'ClassDeclaration' && decl.id) {
name = decl.id.name
kind = 'class'
} else if (decl.type === 'TSInterfaceDeclaration' && decl.id) {
name = decl.id.name
kind = 'interface'
if (!jsdoc) {
const members = extractMembers(code, decl)
if (members.length > 0) {
symbols.push({ name, kind, jsdoc: '', signature, members, extendsTypes })
}
return
}
} else if (decl.type === 'TSTypeAliasDeclaration' && decl.id) {
name = decl.id.name
kind = 'type'
if (!jsdoc && decl.typeAnnotation?.type === 'TSTypeLiteral') {
const members = extractMembersFromTypeLiteral(code, decl.typeAnnotation)
if (members.length > 0) {
symbols.push({ name, kind: 'interface', jsdoc: '', signature, members, extendsTypes })
}
return
}
} else if (decl.type === 'VariableDeclaration') {
for (const varDecl of decl.declarations) {
if (varDecl.id?.type === 'Identifier') {
const varSignature = varDecl.init
? `export const ${varDecl.id.name} = ${code.slice(varDecl.init.start, varDecl.init.end)}`
: `export const ${varDecl.id.name}: ${code.slice(varDecl.id.typeAnnotation?.start ?? varDecl.id.end, varDecl.id.typeAnnotation?.end ?? varDecl.id.end)}`
if (jsdoc) {
symbols.push({
name: varDecl.id.name,
kind: 'const',
jsdoc,
signature: varSignature,
})
}
}
}
return
}
if (name && jsdoc) {
symbols.push({
name,
kind,
jsdoc,
signature,
paramTypes: extractParamTypes(code, decl),
extendsTypes,
})
} else if (name && kind === 'type') {
symbols.push({ name, kind, jsdoc: jsdoc || '', signature, extendsTypes })
}
}
function extractExtends(code: string, decl: any): string[] | undefined {
const result: string[] = []
if (decl.superClass?.id) {
result.push(code.slice(decl.superClass.id.start, decl.superClass.id.end))
}
if (decl.superTypeArguments?.params) {
for (const p of decl.superTypeArguments.params) {
const typeName = code.slice(p.start, p.end)
if (typeName && !result.includes(typeName)) {
result.push(typeName)
}
}
}
if (decl.extends && Array.isArray(decl.extends)) {
for (const ext of decl.extends) {
if (ext.expression) {
const typeName = code.slice(ext.expression.start, ext.expression.end)
if (typeName) {
result.push(typeName)
}
} else if (ext.id) {
result.push(code.slice(ext.id.start, ext.id.end))
}
}
}
return result.length > 0 ? result : undefined
}
function extractParamTypes(code: string, decl: any): Record<string, string> | undefined {
if (!decl.params || !Array.isArray(decl.params)) {
return undefined
}
const types: Record<string, string> = {}
for (const param of decl.params) {
let paramName: string | undefined
let typeSource: string | undefined
if (param.type === 'Identifier') {
paramName = param.name
typeSource = param.typeAnnotation ? getTypeSource(code, param.typeAnnotation) : undefined
} else if (param.type === 'AssignmentPattern' && param.left?.type === 'Identifier') {
paramName = param.left.name
typeSource = param.left.typeAnnotation
? getTypeSource(code, param.left.typeAnnotation)
: undefined
}
if (paramName && typeSource) {
types[paramName] = typeSource
}
}
return Object.keys(types).length > 0 ? types : undefined
}
function getTypeSource(code: string, typeAnnotation: any): string {
const raw = typeAnnotation.typeAnnotation
? code.slice(typeAnnotation.typeAnnotation.start, typeAnnotation.typeAnnotation.end)
: code.slice(typeAnnotation.start, typeAnnotation.end)
return raw
.replace(/\n/g, ' ')
.replace(/\s+/g, ' ')
.replace(/\(\s+/g, '(')
.replace(/\s+\)/g, ')')
.trim()
}
function extractMembers(code: string, decl: any): MemberProp[] {
if (!decl.body?.body || !Array.isArray(decl.body.body)) {
return []
}
return extractMembersList(code, decl.body.body)
}
function extractMembersFromTypeLiteral(code: string, typeLiteral: any): MemberProp[] {
if (!typeLiteral.members || !Array.isArray(typeLiteral.members)) {
return []
}
return extractMembersList(code, typeLiteral.members)
}
function extractMembersList(code: string, memberList: any[]): MemberProp[] {
const members: MemberProp[] = []
for (const member of memberList) {
const propJsdoc = findJSDocBefore(code, member.start)
if (!propJsdoc) {
continue
}
const key = member.key
if (!key) {
continue
}
let propName: string | undefined
if (key.type === 'Identifier') {
propName = key.name
} else if (key.type === 'Literal') {
propName = String(key.value)
}
if (!propName) {
continue
}
const optional = !!member.optional
const typeSource = member.typeAnnotation
? getTypeSource(code, member.typeAnnotation)
: undefined
// Extract @default value and remove from jsdoc
let defaultValue: string | undefined
let cleanJsdoc = propJsdoc
const defaultMatch = propJsdoc.match(/@default\s+(.+?)(?:\n|$)/)
if (defaultMatch) {
defaultValue = defaultMatch[1].trim()
cleanJsdoc = propJsdoc.replace(/\s*@default\s+.+?(?:\n|$)/, '')
}
members.push({
name: propName,
type: typeSource,
jsdoc: formatInlineTags(cleanJsdoc.replace(/\n/g, ' ').trim()),
optional,
defaultValue,
})
}
return members
}
function walkNode(node: any): void {
if (!node || typeof node !== 'object') {
return
}
if (node.type === 'ExportNamedDeclaration' && node.declaration) {
processExport(node)
} else if (node.type === 'ExportDefaultDeclaration' && node.declaration) {
processExport(node)
}
for (const key of Object.keys(node)) {
const child = node[key]
if (Array.isArray(child)) {
for (const item of child) {
walkNode(item)
}
} else if (child && typeof child === 'object' && child.type) {
walkNode(child)
}
}
}
if (ast?.body && Array.isArray(ast.body)) {
for (const stmt of ast.body) {
walkNode(stmt)
}
}
return symbols
}
function generateMarkdownDocs(symbols: ApiSymbol[]): string {
if (symbols.length === 0) {
return '_No JSDoc comments found._'
}
const lines: string[] = []
for (const sym of symbols) {
const { description, example } = formatJSDoc(sym.jsdoc, sym.paramTypes)
lines.push(`#### \`${sym.name}\``)
lines.push('')
if (description) {
lines.push(description)
lines.push('')
}
if (sym.extendsTypes && sym.extendsTypes.length > 0) {
const links = sym.extendsTypes.map((t) => `[\`${t}\`](#${t.toLowerCase()})`).join(', ')
lines.push(`*Extends:* ${links}`)
lines.push('')
}
if (sym.members && sym.members.length > 0) {
const hasTypes = sym.members.some((m) => m.type)
const hasDefaults = sym.members.some((m) => m.defaultValue !== undefined)
const headers = ['Property']
if (hasTypes) {
headers.push('Type')
}
if (hasDefaults) {
headers.push('Default')
}
headers.push('Description')
const headerRow = `| ${headers.join(' | ')} |`
const sepRow = `|${headers.map((h) => '-'.repeat(h.length + 2)).join('|')}|`
lines.push(headerRow)
lines.push(sepRow)
for (const m of sym.members) {
const cells = [`\`${m.name}${m.optional ? '?' : ''}\``]
if (hasTypes) {
cells.push(`\`${m.type || ''}\``)
}
if (hasDefaults) {
cells.push(m.defaultValue ? `\`${m.defaultValue}\`` : '—')
}
cells.push(m.jsdoc)
lines.push(`| ${cells.join(' | ')} |`)
}
lines.push('')
}
lines.push('```ts')
lines.push(sym.signature)
lines.push('```')
if (example) {
// Strip existing code fences to avoid nesting
const cleanExample = example.replace(/^```\w*\n?/, '').replace(/\n?```\s*$/, '')
lines.push('')
lines.push('**Example:**')
lines.push('')
lines.push('```ts')
lines.push(cleanExample)
lines.push('```')
}
lines.push('')
lines.push('---')
lines.push('')
}
return lines.join('\n')
}
function updateApiMd(allExports: ModuleExports, apiMdPath: string): void {
let content = fs.readFileSync(apiMdPath, 'utf-8')
for (const [module, symbols] of Object.entries(allExports)) {
const markerStart = `<!-- AUTO-GENERATED:${module.toUpperCase()}:START -->`
const markerEnd = `<!-- AUTO-GENERATED:${module.toUpperCase()}:END -->`
const generated = generateMarkdownDocs(symbols)
const regex = new RegExp(`${escapeRegex(markerStart)}[\\s\\S]*?${escapeRegex(markerEnd)}`, 'g')
content = content.replace(regex, `${markerStart}\n${generated}\n${markerEnd}`)
}
fs.writeFileSync(apiMdPath, content)
}
function escapeRegex(str: string): string {
return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
export function apiDocsPlugin(): Plugin {
return {
name: 'api-docs',
async buildStart() {
const allExports: ModuleExports = {}
for (const [module, entryFile] of Object.entries(ENTRY_POINTS)) {
const files = followReExports(entryFile)
const symbols: ApiSymbol[] = []
for (const file of files) {
const code = fs.readFileSync(file, 'utf-8')
try {
// this.parse is provided by tsdown/rolldown runtime (oxc)
const ast = this.parse(code, {
lang: 'ts',
})
const fileSymbols = extractExports(code, ast)
symbols.push(...fileSymbols)
} catch {
// Silently skip files that can't be parsed
}
}
allExports[module] = symbols
}
const apiMdPath = path.join(ROOT, 'API.md')
if (fs.existsSync(apiMdPath)) {
updateApiMd(allExports, apiMdPath)
}
},
}
}