-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
280 lines (241 loc) · 8.49 KB
/
index.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
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
'use strict'
const json = require('json-pointer')
function getDefaultPropertyType ({
propertyNameAsType, capitalizeProperty, defaultPropertyType
}, property) {
const fallbackPropertyType = '*'
if (property !== undefined && propertyNameAsType) {
return capitalizeProperty ? upperFirst(property) : property
}
if (defaultPropertyType === null || defaultPropertyType === '') {
return defaultPropertyType
}
return defaultPropertyType || fallbackPropertyType
}
function wrapDescription (config, description) {
const { maxLength } = config
if (!maxLength) {
return [description]
}
const result = []
while (true) {
const excess = description.length - config.indentMaxDelta
if (excess <= 0) {
if (description) {
result.push(description)
}
break
}
const maxLine = description.slice(0, config.indentMaxDelta)
const wsIndex = maxLine.search(/\s\S*$/)
let safeString
if (wsIndex === -1) {
// With this being all non-whitespace, e.g., a long link, we
// let it go on without wrapping until whitespace is reached
const remainder = description.slice(config.indentMaxDelta).match(/^\S+/)
safeString = maxLine + (remainder || '')
} else {
safeString = maxLine.slice(0, wsIndex)
}
result.push(safeString)
description = description.slice(safeString.length + 1)
}
return result
}
module.exports = generate
function generate (schema, options = {}) {
const jsdoc = []
if (!schema || Object.keys(schema).length === 0) {
return ''
}
const config = parseOptions(options)
jsdoc.push(...writeDescription(schema, config))
if (json.has(schema, '/properties')) {
jsdoc.push(...processProperties(schema, schema, null, config))
}
if (json.has(schema, '/items')) {
jsdoc.push(...processItems(schema, schema, null, config))
}
return format(config.outerIndent, jsdoc)
}
function parseOptions (options) {
const asteriskAndWhitespaceLength = 3 // ' * '
const outerIndent = (options.indentChar || ' ').repeat(options.indent || 0)
const indentMaxDelta = options.maxLength - outerIndent.length -
asteriskAndWhitespaceLength
return {
...options,
outerIndent,
indentMaxDelta
}
}
function processItems (schema, rootSchema, base, config) {
const items = json.get(schema, '/items')
if (!Array.isArray(items)) {
return []
}
const result = []
items.forEach((item, i) => {
const root = base ? `${base}.` : ''
const prefixedProperty = root + i
const defaultValue = item.default
const optional = !schema.minItems || i >= schema.minItems
if (item.type === 'array' && item.items) {
result.push(...writeProperty('array', prefixedProperty, item.description, optional, defaultValue, schema, config))
result.push(...processItems(item, rootSchema, prefixedProperty, config))
} else if (item.type === 'object' && item.properties) {
result.push(...writeProperty('object', prefixedProperty, item.description, optional, defaultValue, schema, config))
result.push(...processProperties(item, rootSchema, prefixedProperty, config))
} else {
const type = getSchemaType(item, rootSchema) || getDefaultPropertyType(config)
result.push(...writeProperty(type, prefixedProperty, item.description, optional, defaultValue, item, config))
}
})
return result
}
function processProperties (schema, rootSchema, base, config) {
const props = json.get(schema, '/properties')
const required = json.has(schema, '/required') ? json.get(schema, '/required') : []
const result = []
for (const property in props) {
if (Array.isArray(config.ignore) && config.ignore.includes(property)) {
continue
} else {
const prop = props[property]
const root = base ? `${base}.` : ''
const prefixedProperty = root + property
const defaultValue = prop.default
const optional = !required.includes(property)
if (prop.type === 'object' && prop.properties) {
result.push(...writeProperty('object', prefixedProperty, prop.description, optional, defaultValue, schema, config))
result.push(...processProperties(prop, rootSchema, prefixedProperty, config))
} else if (prop.type === 'array' && prop.items) {
result.push(...writeProperty('array', prefixedProperty, prop.description, optional, defaultValue, schema, config))
result.push(...processItems(prop, rootSchema, prefixedProperty, config))
} else {
const type = getSchemaType(prop, rootSchema) || getDefaultPropertyType(config, property)
result.push(...writeProperty(type, prefixedProperty, prop.description, optional, defaultValue, prop, config))
}
}
}
return result
}
function getSchemaType (schema, rootSchema) {
if (schema.$ref) {
const ref = json.get(rootSchema, schema.$ref.slice(1))
return getSchemaType(ref, rootSchema)
}
if (schema.enum) {
if (schema.type === 'string') {
return `"${schema.enum.join('"|"')}"`
}
if (
schema.type === 'number' || schema.type === 'integer' ||
schema.type === 'boolean'
) {
return `${schema.enum.join('|')}`
}
// Enum can represent more complex types such as array or object
// It can also include a mixture of different types
// Currently, these scenarios are not handled
return schema.type === 'null' ? 'null' : 'enum'
}
if (schema.const !== undefined) {
if (schema.type === 'string') {
return `"${schema.const}"`
}
if (
schema.type === 'number' || schema.type === 'integer' ||
schema.type === 'boolean'
) {
return `${schema.const}`
}
// Const can also be of more complex types like arrays or objects
// As of now, these cases are not addressed
return schema.type === 'null' ? 'null' : 'const'
}
if (Array.isArray(schema.type)) {
if (schema.type.includes('null')) {
return `?${schema.type[0]}`
} else {
return schema.type.join('|')
}
}
return schema.type
}
function getType (schema, config, type) {
const typeCheck = type || schema.type
let typeMatch
if (schema.format) {
typeMatch = config.formats && config.formats[schema.format] &&
config.formats[schema.format][typeCheck]
}
if (typeMatch === undefined || typeMatch === null) {
typeMatch = config.types && config.types[typeCheck]
}
let typeStr
if (config.types === null || config.formats === null ||
(config.formats && (
(config.formats[schema.format] === null) ||
(config.formats[schema.format] &&
config.formats[schema.format][typeCheck] === null)
)) ||
(typeMatch !== '' && !typeMatch && (type === null || type === ''))
) {
typeStr = ''
} else {
typeStr = ` {${
typeMatch === ''
? ''
: typeMatch || type || getSchemaType(schema, schema)
}}`
}
return typeStr
}
function writeDescription (schema, config) {
const result = []
const { objectTagName = 'typedef' } = config
let { description } = schema
if (description === undefined) {
description = config.autoDescribe ? generateDescription(schema.title, schema.type) : ''
}
const type = getType(schema, config)
if (description || config.addDescriptionLineBreak) {
result.push(...wrapDescription(config, description))
}
const namepath = schema.title ? ` ${config.capitalizeTitle ? upperFirst(schema.title) : schema.title}` : ''
result.push(`@${objectTagName}${type}${namepath}`)
return result
}
function writeProperty (type, field, description = '', optional, defaultValue, schema, config) {
const typeExpression = getType(schema, config, type)
let fieldTemplate = ' '
if (optional) {
fieldTemplate += `[${field}${defaultValue === undefined ? '' : `=${JSON.stringify(defaultValue)}`}]`
} else {
fieldTemplate += field
}
let desc
if (!description && !config.descriptionPlaceholder) {
desc = ''
} else if (config.hyphenatedDescriptions) {
desc = ` - ${description}`
} else {
desc = ` ${description}`
}
return wrapDescription(config, `@property${typeExpression}${fieldTemplate}${desc}`)
}
function upperFirst (str) {
return str.slice(0, 1).toUpperCase() + str.slice(1)
}
function generateDescription (title, type) {
const noun = title ? `${title} ${type}` : type
const article = `a${'aeiou'.split('').includes(noun.charAt()) ? 'n' : ''}`
return `Represents ${article} ${noun}`
}
function format (outerIndent, lines) {
const result = [`${outerIndent}/**`]
result.push(...lines.map(line => line ? `${outerIndent} * ${line}` : ' *'))
result.push(`${outerIndent} */\n`)
return result.join('\n')
}