forked from documentationjs/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
288 lines (265 loc) · 9.64 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
281
282
283
284
285
286
287
288
'use strict';
var fs = require('fs'),
_ = require('lodash'),
sort = require('./lib/sort'),
nest = require('./lib/nest'),
filterAccess = require('./lib/filter_access'),
filterJS = require('./lib/filter_js'),
dependency = require('./lib/input/dependency'),
shallow = require('./lib/input/shallow'),
parseJavaScript = require('./lib/parsers/javascript'),
polyglot = require('./lib/parsers/polyglot'),
github = require('./lib/github'),
hierarchy = require('./lib/hierarchy'),
inferName = require('./lib/infer/name'),
inferKind = require('./lib/infer/kind'),
inferAugments = require('./lib/infer/augments'),
inferParams = require('./lib/infer/params'),
inferProperties = require('./lib/infer/properties'),
inferMembership = require('./lib/infer/membership'),
inferReturn = require('./lib/infer/return'),
inferAccess = require('./lib/infer/access'),
inferTypedefType = require('./lib/infer/typedef_type'),
formatLint = require('./lib/lint').formatLint,
garbageCollect = require('./lib/garbage_collect'),
lintComments = require('./lib/lint').lintComments,
markdownAST = require('./lib/output/markdown_ast');
/**
* Build a pipeline of comment handlers.
* @param {...Function|null} args - Pipeline elements. Each is a function that accepts
* a comment and can return a comment or undefined (to drop that comment).
* @returns {Function} pipeline
* @private
*/
function pipeline() {
var elements = arguments;
return function (comment) {
for (var i = 0; comment && i < elements.length; i++) {
if (elements[i]) {
comment = elements[i](comment);
}
}
return comment;
};
}
/**
* Given an array of indexes and options for whether to resolve shallow
* or deep dependencies, resolve dependencies.
*
* @param {Array<string>|string} indexes files to process
* @param {Object} options options
* @param {Function} callback called with results
* @returns {undefined}
*/
function expandInputs(indexes, options, callback) {
var inputFn = (options.polyglot || options.shallow) ? shallow : dependency;
inputFn(indexes, options, callback);
}
/**
* Generate JavaScript documentation as a list of parsed JSDoc
* comments, given a root file as a path.
*
* @param {Array<string>|string} indexes files to process
* @param {Object} options options
* @param {Array<string>} options.external a string regex / glob match pattern
* that defines what external modules will be whitelisted and included in the
* generated documentation.
* @param {boolean} [options.polyglot=false] parse comments with a regex rather than
* a proper parser. This enables support of non-JavaScript languages but
* reduces documentation's ability to infer structure of code.
* @param {boolean} [options.shallow=false] whether to avoid dependency parsing
* even in JavaScript code. With the polyglot option set, this has no effect.
* @param {Array<string|Object>} [options.order=[]] optional array that
* defines sorting order of documentation
* @param {Array<string>} [options.access=[]] an array of access levels
* to output in documentation
* @param {Object} [options.hljs] hljs optional options
* @param {boolean} [options.hljs.highlightAuto=false] hljs automatically detect language
* @param {Array} [options.hljs.languages] languages for hljs to choose from
* @param {Function} callback to be called when the documentation generation
* is complete, with (err, result) argumentsj
* @returns {undefined} calls callback
* @public
* @example
* var documentation = require('documentation');
*
* documentation.build(['index.js'], {
*
* // only output comments with an explicit @public tag
* access: ['public']
*
* }, function (err, res) {
*
* // res is an array of parsed comments with inferred properties
* // and more: everything you need to build documentation or
* // any other kind of code data.
*
* });
*/
function build(indexes, options, callback) {
options = options || {};
if (typeof indexes === 'string') {
indexes = [indexes];
}
return expandInputs(indexes, options, function (error, inputs) {
if (error) {
return callback(error);
}
try {
callback(null, buildSync(inputs, options));
} catch (e) {
callback(e);
}
});
}
/**
* Generate JavaScript documentation given a list of inputs. This internal
* method does not support require-following and it returns its results
* synchronously, rather than by calling a callback.
*
* @param {Array<string>} indexes files to process
* @param {Object} options options
* @param {Array<string>} options.external a string regex / glob match pattern
* that defines what external modules will be whitelisted and included in the
* generated documentation.
* @param {boolean} [options.polyglot=false] parse comments with a regex rather than
* a proper parser. This enables support of non-JavaScript languages but
* reduces documentation's ability to infer structure of code.
* @param {boolean} [options.shallow=false] whether to avoid dependency parsing
* even in JavaScript code. With the polyglot option set, this has no effect.
* @param {Array<string|Object>} [options.order=[]] optional array that
* defines sorting order of documentation
* @param {Array<string>} [options.access=[]] an array of access levels
* to output in documentation
* @param {Object} [options.hljs] hljs optional options
* @param {boolean} [options.hljs.highlightAuto=false] hljs automatically detect language
* @param {Array} [options.hljs.languages] languages for hljs to choose from
* @returns {Object} list of results
* @public
* @example
* var documentation = require('documentation');
*
* var results = documentation.buildSync(['index.js']);
* // results is an array of parsed comments with inferred properties
* // and more: everything you need to build documentation or
* // any other kind of code data.
*/
function buildSync(indexes, options) {
options = options || {};
options.hljs = options.hljs || {};
if (!options.access) {
options.access = ['public', 'undefined', 'protected'];
}
var parseFn = (options.polyglot) ? polyglot : parseJavaScript;
var buildPipeline = pipeline(
inferName(),
inferAccess(options.inferPrivate),
inferAugments(),
inferKind(),
inferParams(),
inferProperties(),
inferReturn(),
inferMembership(),
inferTypedefType(),
nest,
options.github && github,
garbageCollect);
var jsFilterer = filterJS(options.extension, options.polyglot);
return filterAccess(options.access,
hierarchy(
sort(
_.flatMap(indexes, function (index) {
var indexObject = null;
if (typeof index === 'string') {
indexObject = {
source: fs.readFileSync(index, 'utf8'),
file: index
};
} else {
indexObject = index;
}
if (!jsFilterer(indexObject)) {
return [];
}
return parseFn(indexObject, options).map(buildPipeline);
})
.filter(Boolean), options)));
}
/**
* Lint files for non-standard or incorrect documentation
* information, returning a potentially-empty string
* of lint information intended for human-readable output.
*
* @param {Array<string>|string} indexes files to process
* @param {Object} options options
* @param {Array<string>} options.external a string regex / glob match pattern
* that defines what external modules will be whitelisted and included in the
* generated documentation.
* @param {boolean} [options.polyglot=false] parse comments with a regex rather than
* a proper parser. This enables support of non-JavaScript languages but
* reduces documentation's ability to infer structure of code.
* @param {boolean} [options.shallow=false] whether to avoid dependency parsing
* even in JavaScript code. With the polyglot option set, this has no effect.
* @param {Function} callback to be called when the documentation generation
* is complete, with (err, result) arguments
* @returns {undefined} calls callback
* @public
*/
function lint(indexes, options, callback) {
options = options || {};
if (typeof indexes === 'string') {
indexes = [indexes];
}
var parseFn = (options.polyglot) ? polyglot : parseJavaScript;
var lintPipeline = pipeline(
lintComments,
inferName(),
inferAccess(options.inferPrivate),
inferAugments(),
inferKind(),
inferParams(),
inferProperties(),
inferReturn(),
inferMembership(),
inferTypedefType(),
nest);
return expandInputs(indexes, options, function (error, inputs) {
if (error) {
return callback(error);
}
callback(null,
formatLint(hierarchy(
inputs
.filter(filterJS(options.extension, options.polyglot))
.reduce(function (memo, file) {
return memo.concat(parseFn(file, options).map(lintPipeline));
}, [])
.filter(Boolean))));
});
}
/**
* Documentation's formats are modular methods that take comments
* and options as input and call a callback with writable objects,
* like stringified JSON, markdown strings, or Vinyl objects for HTML
* output.
* @public
*/
var formats = {
html: require('./lib/output/html'),
md: require('./lib/output/markdown'),
remark: function (comments, options, callback) {
markdownAST(comments, options, function (err, res) {
callback(err, JSON.stringify(res, null, 2));
});
},
json: require('./lib/output/json')
};
module.exports.lint = lint;
module.exports.expandInputs = expandInputs;
module.exports.buildSync = buildSync;
module.exports.build = build;
module.exports.formats = formats;
module.exports.util = {
createFormatters: require('./lib/output/util/formatters'),
createLinkerStack: require('./lib/output/util/linker_stack')
};