-
Notifications
You must be signed in to change notification settings - Fork 19
/
transform.js
411 lines (363 loc) · 11.4 KB
/
transform.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
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
const testValue = require('test-value')
const where = testValue.where
const arrayify = require('array-back')
const extract = require('reduce-extract')
const omit = require('lodash.omit')
function pick (object, keys) {
return keys.reduce((obj, key) => {
if (object && object.hasOwnProperty(key)) {
obj[key] = object[key]
}
return obj
}, {})
}
/**
* @module transform
*/
module.exports = transform
/**
* Returns a duplex stream - jsdoc explain data in, transformed jsdoc-parse data out.
* @return {Duplex}
*/
function transform (data) {
data = fixES6ConstructorMemberLongnames(data)
/* remove undocumented, package and file doclets */
let json = data.filter(i => !i.undocumented && !/package|file/.test(i.kind))
json = json.map(setIsExportedFlag)
json = json.map(setCodename)
json = insertConstructors(json)
json = json.map(function (doclet) {
doclet = setID(doclet)
doclet = removeQuotes(doclet)
doclet = cleanProperties(doclet)
doclet = buildTodoList(doclet)
doclet = extractTypicalName(doclet)
doclet = extractCategory(doclet)
doclet = extractChainable(doclet)
doclet = extractCustomTags(doclet)
doclet = setTypedefScope(doclet)
doclet = renameThisProperty(doclet)
doclet = removeMemberofFromModule(doclet)
doclet = convertIsEnumFlagToKind(doclet)
return doclet
})
const exported = json.filter(where({ isExported: true }))
const newIDs = exported.map(d => d.id)
newIDs.forEach(function (newID) {
update(json, { isExported: undefined, '!kind': 'module' }, function (doclet) {
return updateIDReferences(doclet, newID)
})
})
json = removeEnumChildren(json)
json = json.map(removeUnwanted)
json = json.map(sortIdentifier)
/* add order field, representing the original order of the documentation */
json.forEach(function (doclet, index) {
doclet.order = index
})
return json
}
/**
Create a unique ID (the jsdoc `longname` field is not guaranteed unique)
@depends setIsExportedFlag
@depends setCodename
*/
function setID (doclet) {
if (doclet.longname) {
doclet.id = doclet.longname
}
if (doclet.kind === 'constructor') {
if (doclet.scope === 'static') {
doclet.id = doclet.longname
delete doclet.scope
} else {
doclet.id = doclet.longname + '()'
}
}
if (doclet.isExported) {
doclet.id = doclet.longname + '--' + doclet.codeName
}
return doclet
}
/**
run after setIsExportedFlag has processed using old name value
@depends setIsExportedFlag
*/
function setCodename (doclet) {
if (doclet.meta && doclet.meta.code) {
doclet.codeName = doclet.meta.code.name
if (doclet.isExported) doclet.name = doclet.codeName
}
return doclet
}
function setIsExportedFlag (doclet) {
if (/module:/.test(doclet.name) && doclet.kind !== 'module' && doclet.kind !== 'constructor') {
doclet.isExported = true
doclet.memberof = doclet.longname
}
return doclet
}
/**
converts .classdesc to .description
@returns Array - contains the class and constructor identifiers
@depends setCodename
*/
function createConstructor (class_) {
if (class_.kind !== 'class') {
throw new Error('should only pass a class to createConstructor')
}
const replacements = []
class_ = Object.assign({}, class_)
const constructorProperties = ['description', 'params', 'examples', 'returns', 'exceptions']
const constructor = pick(class_, constructorProperties)
for (const prop of constructorProperties) delete class_[prop]
if (class_.classdesc) {
class_.description = class_.classdesc
delete class_.classdesc
}
replacements.push(class_)
/* only output a constructor if it's documentated */
if (constructor.description || (constructor.params && constructor.params.length)) {
constructor.id = class_.id
constructor.longname = class_.longname
constructor.name = class_.codeName || class_.name
constructor.kind = 'constructor'
constructor.memberof = class_.longname
replacements.push(constructor)
}
return replacements
}
/* split each class found into two new items, then re-insert them over the original class */
function insertConstructors (data) {
const replacements = []
data.forEach(function (doclet, index) {
if (doclet.kind === 'class') {
replacements.push({ index, items: createConstructor(doclet) })
}
})
replacements.reverse().forEach(function (replacement) {
const spliceArgs = [replacement.index, 1].concat(replacement.items)
data.splice.apply(data, spliceArgs)
})
return data
}
/* unfortunately the jsdoc data structure differs between es5 and es6 classes,
hence the need for these four functions */
function getEs6Constructor (data, parent) {
return data.find(i => isES6Constructor(i) && i.memberof === parent.longname)
}
function isES6Class (doclet) {
return testValue(doclet, {
kind: 'class',
meta: { code: { type: 'ClassDeclaration' } }
})
}
function isES6Constructor (doclet) {
return testValue(doclet, {
kind: 'class',
meta: { code: { type: 'MethodDefinition' } }
})
}
function replaceID (id, oldID, newID) {
/* this is required by command-line-args which has similar class names like Definition and Definitions */
return id.replace(new RegExp(`${oldID}`), newID)
}
function updateIDReferences (doclet, newID) {
const oldID = newID.split('--')[0]
if (oldID && !doclet.isExported) {
if (doclet.id) doclet.id = replaceID(doclet.id, oldID, newID)
if (doclet.memberof) doclet.memberof = replaceID(doclet.memberof, oldID, newID)
if (doclet.name) doclet.name = replaceID(doclet.name, oldID, newID)
if (doclet.type && doclet.type.names) {
doclet.type.names = doclet.type.names.map(function (id) {
return replaceID(id, oldID, newID)
})
}
if (doclet.returns) {
doclet.returns = doclet.returns.map(function (doclet) {
if (doclet.type && doclet.type.names) {
doclet.type.names = doclet.type.names.map(function (id) {
return replaceID(id, oldID, newID)
})
}
return doclet
})
}
}
return doclet
}
/* removes unwanted quotes set by jsdoc if you specifiy a memberof such as `jquery.fn` */
function removeQuotes (doclet) {
const re = /["']/g
if (doclet.name) doclet.name = doclet.name.replace(re, '')
if (doclet.memberof) doclet.memberof = doclet.memberof.replace(re, '')
if (doclet.longname) doclet.longname = doclet.longname.replace(re, '')
if (doclet.id) doclet.id = doclet.id.replace(re, '')
return doclet
}
function removeUnwanted (doclet) {
delete doclet.todo
delete doclet.tags
delete doclet.codeName
delete doclet.comment
delete doclet.undocumented
delete doclet.___id
delete doclet.___s
if (doclet.meta) {
const oldMeta = doclet.meta
doclet.meta = {
lineno: oldMeta.lineno,
filename: oldMeta.filename,
path: oldMeta.path
}
}
return doclet
}
function cleanProperties (doclet) {
if (doclet.properties) {
doclet.properties = doclet.properties.map(function (prop) {
return wantedProperties(prop)
})
}
return doclet
}
function wantedProperties (input) {
return omit(input, ['comment', 'meta', 'undocumented', '___id', '___s'])
}
function buildTodoList (doclet) {
let todoList = []
if (doclet.todo) {
const todo = arrayify(doclet.todo)
todoList = todoList.concat(todo.map(function (task) {
return { done: false, task }
}))
}
/*
Convert @typicalname and @category from custom to regular tags.
Combine @todo array with @done custom tags to make @todoList
*/
if (doclet.tags) {
const done = doclet.tags.reduce(extract({ title: 'done' }), [])
if (!doclet.tags.length) delete doclet.tags
todoList = todoList.concat(done.map(function (task) {
return { done: true, task: task.value }
}))
}
if (todoList.length) {
doclet.todoList = todoList
}
return doclet
}
function extractTypicalName (doclet) {
if (doclet.tags) {
const typicalName = doclet.tags.reduce(extract({ title: 'typicalname' }), [])
if (typicalName.length) doclet.typicalname = typicalName[0].value
}
return doclet
}
function extractCategory (doclet) {
if (doclet.tags) {
const category = doclet.tags.reduce(extract({ title: 'category' }), [])
if (category.length) doclet.category = category[0].value
}
return doclet
}
function extractChainable (doclet) {
if (doclet.tags) {
const chainable = doclet.tags.reduce(extract({ title: 'chainable' }), [])
if (chainable.length) doclet.chainable = true
}
return doclet
}
/**
@depends removeUnwanted
*/
function extractCustomTags (doclet) {
if (doclet.tags && doclet.tags.length > 0) {
doclet.customTags = doclet.tags.map(function (tag) {
return {
tag: tag.title,
value: tag.value
}
})
}
return doclet
}
function setTypedefScope (doclet) {
if (doclet.kind === 'typedef' && !doclet.scope) doclet.scope = 'global'
return doclet
}
function sort (object, sortFunction) {
const output = {}
const newPropertyOrder = Object.keys(object).filter(function (prop) {
return typeof object[prop] !== 'function'
}).sort(sortFunction)
newPropertyOrder.forEach(function (prop) {
output[prop] = object[prop]
})
return output
}
function sortIdentifier (doclet) {
const fieldOrder = ['id', 'parentId', 'longname', 'name', 'kind', 'scope', 'isExported', 'classdesc', 'augments', 'inherits', 'inherited', 'implements', 'overrides', 'mixes', 'description', 'memberof', 'alias', 'params', 'fires', 'examples', 'returns', 'type', 'defaultvalue', 'readonly', 'thisvalue', 'isEnum', 'properties', 'optional', 'nullable', 'variable', 'author', 'deprecated', 'ignore', 'access', 'requires', 'version', 'since', 'licenses', 'license', 'typicalname', 'category', 'see', 'exceptions', 'codeName', 'todoList', 'customTags', 'chainable', 'meta', 'order']
return sort(doclet, function (a, b) {
if (fieldOrder.indexOf(a) === -1 && fieldOrder.indexOf(b) > -1) {
return 1
} else {
return fieldOrder.indexOf(a) - fieldOrder.indexOf(b)
}
})
}
function update (array, query, newValues) {
for (let i = 0; i < array.length; i++) {
if (testValue(array[i], query)) {
const values = typeof newValues === 'function' ? newValues(array[i]) : newValues
for (const prop in values) {
if (values[prop] !== undefined) array[i][prop] = values[prop]
}
}
}
}
function renameThisProperty (doclet) {
doclet.thisvalue = doclet.this
delete doclet.this
return doclet
}
function removeMemberofFromModule (doclet) {
if (doclet.kind === 'module') {
delete doclet.memberof
delete doclet.scope
}
return doclet
}
function fixES6ConstructorMemberLongnames (data) {
data.forEach(i => {
if (isES6Class(i)) {
const es6constructor = getEs6Constructor(data, i)
if (es6constructor) {
const constructorChildren = data.filter(where({ memberof: es6constructor.longname }))
constructorChildren.forEach(child => {
child.memberof = i.longname
})
}
}
})
return data
}
function convertIsEnumFlagToKind (doclet) {
if (doclet.isEnum) {
doclet.kind = 'enum'
delete doclet.isEnum
}
return doclet
}
/* remove properties which have enum parents.. depends on convertIsEnumFlagToKind */
function removeEnumChildren (json) {
return json.filter(function (doclet) {
const parent = json.find(where({ id: doclet.memberof }))
if (parent && parent.kind === 'enum') {
return false
} else {
return true
}
})
}