Skip to content

Commit 86a4859

Browse files
committed
document types
1 parent 33471aa commit 86a4859

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

docs/generate.ts

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,18 @@ type DocumentedInterface = {
5858
methods: Method[] | undefined
5959
}
6060

61-
type DocumentedAPI = DocumentedFunction | DocumentedClass | DocumentedInterface
61+
// Documented interface API
62+
type DocumentedType = {
63+
type: 'type'
64+
path: string
65+
source: string | undefined
66+
name: string
67+
aliases: string[] | undefined
68+
description: string
69+
signature: string
70+
}
71+
72+
type DocumentedAPI = DocumentedFunction | DocumentedClass | DocumentedInterface | DocumentedType
6273

6374
type Maps = {
6475
comments: Map<string, typedoc.Reflection> // full name => TypeDoc Reflection
@@ -181,6 +192,7 @@ function createLookupMaps(reflection: typedoc.ProjectReflection): Maps {
181192
typedoc.ReflectionKind.CallSignature,
182193
typedoc.ReflectionKind.Class,
183194
typedoc.ReflectionKind.Interface,
195+
typedoc.ReflectionKind.TypeAlias,
184196
// TODO: Not implemented yet - used for interactions like arrowLeft etc. so
185197
// we eventually will probably want to support
186198
// typedoc.ReflectionKind.Variable,
@@ -325,6 +337,10 @@ function getDocumentedAPI(fullName: string, node: typedoc.Reflection): Documente
325337
return getDocumentedInterface(fullName, node)
326338
}
327339

340+
if (node.isDeclaration() && node.kind === typedoc.ReflectionKind.TypeAlias) {
341+
return getDocumentedType(fullName, node)
342+
}
343+
328344
throw new Error(`Unsupported documented API kind: ${typedoc.ReflectionKind[node.kind]}`)
329345
} catch (e) {
330346
throw new Error(
@@ -404,6 +420,22 @@ function getDocumentedInterface(
404420
}
405421
}
406422

423+
function getDocumentedType(fullName: string, node: typedoc.DeclarationReflection): DocumentedType {
424+
let name = getApiNameFromFullName(fullName)
425+
return {
426+
type: 'type',
427+
path: getApiFilePath(fullName),
428+
source: node.sources?.[0]?.url,
429+
name,
430+
aliases: getApiAliases(node.comment!),
431+
description: getApiDescription(node.comment!),
432+
signature: node
433+
.toString()
434+
.replace(/^TypeAlias/, 'type')
435+
.replace(new RegExp(`${name}: `), `${name} = `),
436+
}
437+
}
438+
407439
function getApiAliases(typedocComment: typedoc.Comment): string[] | undefined {
408440
let tags = typedocComment.getTags('@alias')
409441
if (!tags || tags.length === 0) {
@@ -622,6 +654,8 @@ async function writeMarkdownFiles(comments: DocumentedAPI[]) {
622654
await fs.writeFile(mdPath, await getClassMarkdown(comment))
623655
} else if (comment.type === 'interface') {
624656
await fs.writeFile(mdPath, await getInterfaceMarkdown(comment))
657+
} else if (comment.type === 'type') {
658+
await fs.writeFile(mdPath, await getTypeMarkdown(comment))
625659
}
626660
}
627661
}
@@ -735,6 +769,12 @@ async function getInterfaceMarkdown(comment: DocumentedInterface): Promise<strin
735769
.join('\n\n')
736770
}
737771

772+
async function getTypeMarkdown(comment: DocumentedType): Promise<string> {
773+
return [...getCommonMarkdown(comment), h2('Signature', await pre(comment.signature))]
774+
.filter(Boolean)
775+
.join('\n\n')
776+
}
777+
738778
function getCommonMarkdown(comment: DocumentedAPI): (string | undefined)[] {
739779
return [
740780
['---', `title: ${comment.name}`, `type: ${comment.type}`, '---'].join('\n'),

0 commit comments

Comments
 (0)