-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate-docs.js
executable file
·207 lines (183 loc) · 6 KB
/
generate-docs.js
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
/* eslint-disable import/no-commonjs */
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const { children } = require('./.reflection.json')
const modules = glob.sync(path.resolve(__dirname, '*.d.ts')).map((module) => {
const name = path.basename(module, '.d.ts')
const child = children.find((c) => c.name === name)
return {
name,
comment: child.comment,
functions: child.groups.find((g) => g.title === 'Functions'),
children: child.children,
}
})
for (const module of modules) {
fs.writeFileSync(
path.resolve(__dirname, 'docs', `${module.name}.md`),
formatModule(module),
'utf-8'
)
}
function formatTableOfContents(module) {
const categories = module.functions.categories.map((category) => {
const entries = category.children
.map((i) => module.children.find((c) => c.id === i))
.map((child) => li(createLink(child.name), 2))
return li(createLink(category.title), 1) + '\n' + entries.join('\n')
})
return h('Table of contents', 2) + '\n\n' + categories.join('\n')
}
function formatModule(module) {
const categories = module.functions.categories.map((category) => {
const contents = category.children
.map((i) => module.children.find((c) => c.id === i))
.map((c) => {
const isVariadic = ['compose', 'pipe'].includes(c.name)
const signatures = isVariadic
? c.signatures.slice(0, 3)
: // Right now, the only normal functions that have multiple signatures
// are predicates that also support type guards. In those cases, we want
// to show the normal signature first, so we reverse the results.
c.signatures.filter((s) => s.comment != null).reverse()
const comment = signatures[0] && signatures[0].comment
return (
h(c.name, 4) +
'\n\n<!-- prettier-ignore-start -->\n```typescript\n' +
signatures.map(formatCallSignature).join('\n') +
'\n```\n<!-- prettier-ignore-end -->\n\n' +
(comment ? formatComment(comment) : '') +
'\n\n---'
)
})
return h(category.title, 3) + '\n\n' + contents.join('\n\n')
})
const moduleName = module.name === 'index' ? 'iiris' : `iiris/${module.name}`
return (
h(`Module \`${moduleName}\``, 1) +
'\n\n' +
(module.comment ? formatComment(module.comment) + '\n\n' : '') +
formatTableOfContents(module) +
'\n\n' +
categories.join('\n\n') +
'\n'
)
}
function createLink(linkText, url) {
let target = url
? url.includes(')')
? '<' + url + '>'
: url
: '#' +
linkText
.toLowerCase()
.replace(/\s+/g, '-')
.replace(/[^-\w]+/, '')
return `[${linkText}](${target})`
}
function formatCallSignature(signature) {
const { typeParameter, parameters, type } = signature
const typeParameters = typeParameter
? `<${typeParameter.map(formatTypeParameter).join(', ')}>`
: ''
const parameterList = (parameters || [])
.map(
(p) => `${p.name}: ${p.flags.isRest ? '...' : ''}${formatType(p.type)}`
)
.join(', ')
return `${typeParameters}(${parameterList}) => ${formatType(type)}`
}
function formatTypeParameter(typeParameter) {
const { name, type } = typeParameter
return `${name}${type ? ` extends ${formatType(type)}` : ''}`
}
function formatType(type) {
switch (type.type) {
case 'array': {
const formatted = formatType(type.elementType)
// Show complex types that include whitespace with Array<...> to avoid
// precedence issues.
return formatted.includes(' ') ? `Array<${formatted}>` : `${formatted}[]`
}
case 'indexedAccess':
return `${formatType(type.objectType)}[${formatType(type.indexType)}]`
case 'intersection':
return type.types.map(formatType).join(' & ')
case 'intrinsic':
return type.name
case 'literal':
return String(type.value)
case 'mapped':
return `Record<${formatType(type.parameterType)}, ${formatType(
type.templateType
)}>`
case 'named-tuple-member':
return `${type.name}: ${formatType(type.element)}`
case 'reflection': {
return formatCallSignature(
type.declaration.indexSignature || type.declaration.signatures[0]
)
}
case 'predicate':
return `${type.name} is ${formatType(type.targetType)}`
case 'reference':
return type.name === 'Widen'
? 'T'
: `${type.name}${formatTypeArguments(type.typeArguments)}`
case 'tuple':
return `[${type.elements.map(formatType).join(', ')}]`
case 'typeOperator':
return formatTypeOperator(type)
case 'union':
return type.types.map(formatType).sort().join(' | ')
default:
throw new Error(`Unknown type: ${type.type}`)
}
}
function formatTypeArguments(typeArguments) {
return typeArguments ? `<${typeArguments.map(formatType).join(', ')}>` : ''
}
function formatTypeOperator(type) {
const target = formatType(type.target)
switch (type.operator) {
case 'keyof':
return `keyof ${target}`
case 'readonly':
return target
default:
throw new Error(`Unknown type operator: ${type.operator}`)
}
}
function formatComment(comment) {
const render = (text) =>
text
.trim()
.replace(/{@link (\S+)\s*(\S+)?}/g, (_, target, text) =>
text ? createLink(text.trim(), target) : createLink(target)
)
return (
render(comment.shortText) +
(comment.text ? '\n\n' + render(comment.text) : '') +
(comment.tags ? '\n\n' + formatCommentTags(comment.tags) : '')
)
}
function li(content, level) {
return ' '.repeat((level - 1) * 2) + '- ' + content
}
function h(content, level) {
return '#'.repeat(level) + ' ' + content
}
function formatCommentTags(tags) {
const example = tags
.filter((t) => t.tag === 'example')
.map((t) => t.text.trim())
.join('')
const see = tags
.filter((t) => t.tag === 'see')
.map((t) => createLink(t.text.trim()))
.join(', ')
return (
h('Example', 5) + '\n\n' + example + (see ? '\n\n**See also:** ' + see : '')
)
}