forked from documentationjs/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
233 lines (213 loc) · 7.34 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
import _ from 'lodash';
import sort from './sort.js';
import { nest } from './nest.js';
import filterAccess from './filter_access.js';
import dependency from './input/dependency.js';
import shallow from './input/shallow.js';
import parseJavaScript from './parsers/javascript.js';
import github from './github.js';
import hierarchy from './hierarchy.js';
import inferName from './infer/name.js';
import inferKind from './infer/kind.js';
import inferAugments from './infer/augments.js';
import inferImplements from './infer/implements.js';
import inferParams from './infer/params.js';
import inferProperties from './infer/properties.js';
import inferMembership from './infer/membership.js';
import inferReturn from './infer/return.js';
import inferAccess from './infer/access.js';
import inferType from './infer/type.js';
import { formatLint, lintComments } from './lint.js';
import garbageCollect from './garbage_collect.js';
import markdownAST from './output/markdown_ast.js';
import mergeConfig from './merge_config.js';
import html from './output/html.js';
import md from './output/markdown.js';
import json from './output/json.js';
import createFormatters from './output/util/formatters.js';
import LinkerStack from './output/util/linker_stack.js';
/**
* Build a pipeline of comment handlers.
* @param {Array<Function>} fns - 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(fns) {
return comment => {
for (let i = 0; comment && i < fns.length; i++) {
if (fns[i]) {
comment = fns[i](comment);
}
}
return comment;
};
}
function configure(indexes, args) {
const mergedConfig = mergeConfig(args);
return mergedConfig.then(config => {
const expandedInputs = expandInputs(indexes, config);
return expandedInputs.then(inputs => {
return {
inputs,
config
};
});
});
}
/**
* 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} config options
* @returns {Promise<Array<string>>} promise with results
*/
export function expandInputs(indexes, config) {
// Ensure that indexes is an array of strings
indexes = [].concat(indexes);
if (config.shallow || config.documentExported) {
return shallow(indexes, config);
}
return dependency(indexes, config);
}
function buildInternal(inputsAndConfig) {
const config = inputsAndConfig.config;
const inputs = inputsAndConfig.inputs;
if (!config.access) {
config.access = ['public', 'undefined', 'protected'];
}
const buildPipeline = pipeline([
inferName,
inferAccess(config.inferPrivate),
inferAugments,
inferImplements,
inferKind,
nest,
inferParams,
inferProperties,
inferReturn,
inferMembership(),
inferType,
config.github && github,
garbageCollect
]);
const extractedComments = _.flatMap(inputs, function (sourceFile) {
return parseJavaScript(sourceFile, config).map(buildPipeline);
}).filter(Boolean);
return filterAccess(
config.access,
hierarchy(sort(extractedComments, config))
);
}
function lintInternal(inputsAndConfig) {
const inputs = inputsAndConfig.inputs;
const config = inputsAndConfig.config;
const lintPipeline = pipeline([
lintComments,
inferName,
inferAccess(config.inferPrivate),
inferAugments,
inferKind,
inferParams,
inferProperties,
inferReturn,
inferMembership(),
inferType,
nest
]);
const extractedComments = _.flatMap(inputs, sourceFile => {
return parseJavaScript(sourceFile, config).map(lintPipeline);
}).filter(Boolean);
return formatLint(hierarchy(extractedComments));
}
/**
* 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} args args
* @param {Array<string>} args.external a string regex / glob match pattern
* that defines what external modules will be whitelisted and included in the
* generated documentation.
* @param {boolean} [args.shallow=false] whether to avoid dependency parsing
* even in JavaScript code.
* @param {string} [args.inferPrivate] a valid regular expression string
* to infer whether a code element should be private, given its naming structure.
* For instance, you can specify `inferPrivate: '^_'` to automatically treat
* methods named like `_myMethod` as private.
* @param {string|Array<string>} [args.extension] treat additional file extensions
* as JavaScript, extending the default set of `js`, `es6`, and `jsx`.
* @returns {Promise} promise with lint results
* @public
* @example
* documentation.lint('file.js').then(lintOutput => {
* if (lintOutput) {
* console.log(lintOutput);
* process.exit(1);
* } else {
* process.exit(0);
* }
* });
*/
export const lint = (indexes, args) =>
configure(indexes, args).then(lintInternal);
/**
* 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} args args
* @param {Array<string>} args.external a string regex / glob match pattern
* that defines what external modules will be whitelisted and included in the
* generated documentation.
* @param {boolean} [args.shallow=false] whether to avoid dependency parsing
* even in JavaScript code.
* @param {Array<string|Object>} [args.order=[]] optional array that
* defines sorting order of documentation
* @param {Array<string>} [args.access=[]] an array of access levels
* to output in documentation
* @param {Object} [args.hljs] hljs optional args
* @param {boolean} [args.hljs.highlightAuto=false] hljs automatically detect language
* @param {Array} [args.hljs.languages] languages for hljs to choose from
* @param {string} [args.inferPrivate] a valid regular expression string
* to infer whether a code element should be private, given its naming structure.
* For instance, you can specify `inferPrivate: '^_'` to automatically treat
* methods named like `_myMethod` as private.
* @param {string|Array<string>} [args.extension] treat additional file extensions
* as JavaScript, extending the default set of `js`, `es6`, and `jsx`.
* @returns {Promise} results
* @public
* @example
* var documentation = require('documentation');
*
* documentation.build(['index.js'], {
* // only output comments with an explicit @public tag
* access: ['public']
* }).then(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.
* });
*/
export const build = (indexes, args) =>
configure(indexes, args).then(buildInternal);
/**
* Documentation's formats are modular methods that take comments
* and config as input and return Promises with results,
* like stringified JSON, markdown strings, or Vinyl objects for HTML
* output.
* @public
*/
export const formats = {
html,
md,
remark: (comments, config) =>
markdownAST(comments, config).then(res => JSON.stringify(res, null, 2)),
json
};
export const util = {
createFormatters,
LinkerStack
};