-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjsdoc.js
346 lines (277 loc) · 8.61 KB
/
jsdoc.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
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
'use strict'
const path = require('path')
const fs = require('fs')
class ClassDef {
constructor(name) {
this.name = name
this.methods = {}
this.properties = {}
this.serviceInfo = {}
}
property(name) {
return this.properties[name] || (this.properties[name] = new PropertyDef())
}
method(name) {
return this.methods[name] || (this.methods[name] = new MethodDef())
}
}
class PropertyDef {
type(value) {
this.type = value
return this
}
}
class MethodDef {
constructor() {
this.params = []
this.tags = {}
this.metaInfo = {
args: {}
}
}
ensureMetaInfoArgument(name) {
this.metaInfo.args[name] = this.metaInfo.args[name] || {}
}
addParam({
name,
label,
type,
optional,
description,
dictionary,
dependsOn,
uiComponent,
defaultValue,
schemaLoader,
hidden,
}) {
this.params.push(new ParamDef(name, label, type, optional, description, defaultValue))
if (dictionary) {
this.ensureMetaInfoArgument(name)
this.metaInfo.args[name].dictionary = dictionary
this.metaInfo.args[name].dependsOn = dependsOn
}
if (schemaLoader) {
this.ensureMetaInfoArgument(name)
this.metaInfo.args[name].schemaLoader = schemaLoader
this.metaInfo.args[name].dependsOn = dependsOn
}
if (label) {
this.ensureMetaInfoArgument(name)
this.metaInfo.args[name].label = label
}
if (uiComponent) {
this.ensureMetaInfoArgument(name)
this.metaInfo.args[name].uiComponent = uiComponent
}
if (defaultValue !== undefined) {
this.ensureMetaInfoArgument(name)
this.metaInfo.args[name].defaultValue = defaultValue
}
if (hidden) {
this.ensureMetaInfoArgument(name)
this.metaInfo.args[name].hidden = true
}
}
addTag(name, value) {
this.tags[name] = value
}
}
class ParamDef {
constructor(name, label, type, optional, description, defaultValue) {
this.name = name
this.label = label || name
this.type = type
this.optional = optional
this.defaultValue = defaultValue
if (description) {
this.description = description
}
}
}
class TypeDef {
constructor(type) {
this.name = TypeDef.trim(type)
const elementType = TypeDef.parseElementType(type)
if (elementType) {
this.elementType = new TypeDef(elementType)
}
}
static trim(type) {
const splitterPos = type.indexOf('.')
return type.substring(0, splitterPos !== -1 ? splitterPos : undefined)
}
static parseElementType(type) {
const result = type.match(/<(.*)>/)
return result && result[1] || ''
}
static fromDoc(doc) {
const name = doc && doc.names && doc.names[0]
return name && new TypeDef(name)
}
}
class FileDescriptor {
constructor() {
this.classes = {}
}
addClass(name) {
if (!this.classes[name]) {
this.classes[name] = new ClassDef(name)
}
return this.classes[name]
}
}
function resolveJSDocPath() {
const jsDocHolders = require.resolve.paths('jsdoc/lib')
const jsDocPaths = jsDocHolders.map(p => path.join(p, 'jsdoc/lib'))
for (let i = 0; i < jsDocPaths.length; i++) {
const jsDocPath = jsDocPaths[i]
if (fs.existsSync(jsDocPath)) {
return jsDocPath
}
}
throw new Error('Can not resolve path to JSDoc module.')
}
const jsdocPath = resolveJSDocPath()
const jsdocModulePath = module => path.join(jsdocPath, ...module.split('/'))
let _jsdocRequire
const jsdocRequire = modulePath => {
if (!_jsdocRequire) {
_jsdocRequire = require('requizzle')({
infect : true,
requirePaths: { before: [jsdocPath] }
})
//init jsdoc env
const Config = _jsdocRequire(jsdocModulePath('jsdoc/config'))
const env = _jsdocRequire(jsdocModulePath('jsdoc/env'))
env.conf = new Config().get()
}
return _jsdocRequire(jsdocModulePath(modulePath))
}
const parseJSDoc = fileName => {
const parser = jsdocRequire('jsdoc/src/parser').createParser()
const handlers = jsdocRequire('jsdoc/src/handlers')
handlers.attachTo(parser)
return parser.parse([fileName])
}
const ServiceTagProcessors = {
'requireoauth' : t => ({ requireOAuth: t.value !== 'false' }),
'integrationname': t => ({ integrationName: t.value }),
'integrationicon': t => ({ integrationIcon: t.value }),
}
const tagHandlers = {
paramdef: (tag, methodDef, registeredParams) => {
try {
const paramDef = JSON.parse(tag.value)
methodDef.addParam({
name : paramDef.name,
label : paramDef.label,
type : TypeDef.fromDoc({ names: [paramDef.type] }),
optional : !paramDef.required,
description : paramDef.description,
dictionary : paramDef.dictionary,
schemaLoader: paramDef.schemaLoader,
dependsOn : paramDef.dependsOn,
uiComponent : paramDef.uiComponent,
defaultValue: paramDef.defaultValue,
hidden : paramDef.hidden,
})
registeredParams[paramDef.name] = 1
} catch (error) {
throw new Error(
'Can not compose the deployment model because of an error with reading method parameter definition. ' +
`@paramDef ${ tag.value }. ` +
`Error: ${ error.message }`,
)
}
},
default : (tag, methodDef) => {
methodDef.addTag(tag.title, tag.value)
}
}
exports.describeClasses = function(fileName) {
const docs = parseJSDoc(fileName)
const fd = new FileDescriptor()
docs.forEach(item => {
if (item.kind === 'typedef' || item.kind === 'class') { //class definition
const classDef = fd.addClass(item.name)
classDef.description = item.description
if (item.tags) {
item.tags.forEach(tag => {
if (tag.title === 'paramdef') {
const paramDef = JSON.parse(tag.value)
classDef.properties[paramDef.name] = {
name : paramDef.name,
label : paramDef.label,
type : TypeDef.fromDoc({ names: [paramDef.type] }),
optional : !paramDef.required,
description : paramDef.description,
dictionary : paramDef.dictionary,
schemaLoader: paramDef.schemaLoader,
dependsOn : paramDef.dependsOn,
uiComponent : paramDef.uiComponent,
defaultValue: paramDef.defaultValue,
}
return
}
const extender = ServiceTagProcessors[tag.title]
? ServiceTagProcessors[tag.title](tag)
: { [tag.originalTitle]: tag.value }
Object.assign(classDef.serviceInfo, extender)
})
}
item.properties && item.properties.forEach(prop => {
classDef.property(prop.name).type = TypeDef.fromDoc(prop.type)
})
} else if (item.memberof) { //class members
if (item.kind === 'function') { //class method
const methodDef = fd.addClass(item.memberof).method(item.name)
methodDef.returnType = TypeDef.fromDoc(item.returns && item.returns[0] && item.returns[0].type)
methodDef.access = item.access || 'public'
methodDef.description = item.description
const registeredParams = {}
if (item.tags) {
item.tags.forEach(tag => {
const handler = tagHandlers[tag.title] || tagHandlers.default
handler(tag, methodDef, registeredParams)
})
}
if (item.params) {
item.params.forEach(param => {
if (!registeredParams[param.name]) {
const {
name,
label,
type,
optional,
description,
dictionary,
dependsOn,
schemaLoader,
uiComponent,
defaultValue,
hidden,
} = param
methodDef.addParam({
name,
label,
type : TypeDef.fromDoc(type),
optional: !!optional,
description,
dictionary,
dependsOn,
schemaLoader,
uiComponent,
defaultValue,
hidden,
})
}
})
}
} else if (item.kind === 'member' && item.scope === 'instance' && item.type) { //class property
fd.addClass(item.memberof).property(item.name).type = TypeDef.fromDoc(item.type)
}
}
})
return Object.keys(fd.classes).map(name => fd.classes[name])
}