1 | const fs = require('fs');
|
2 | const path = require('path');
|
3 | const _ = require('lodash');
|
4 | const sort = require('./sort');
|
5 | const nest = require('./nest');
|
6 | const filterAccess = require('./filter_access');
|
7 | const dependency = require('./input/dependency');
|
8 | const shallow = require('./input/shallow');
|
9 | const parseJavaScript = require('./parsers/javascript');
|
10 | const parseVueScript = require('./parsers/vue');
|
11 | const github = require('./github');
|
12 | const hierarchy = require('./hierarchy');
|
13 | const inferName = require('./infer/name');
|
14 | const inferKind = require('./infer/kind');
|
15 | const inferAugments = require('./infer/augments');
|
16 | const inferImplements = require('./infer/implements');
|
17 | const inferParams = require('./infer/params');
|
18 | const inferProperties = require('./infer/properties');
|
19 | const inferMembership = require('./infer/membership');
|
20 | const inferReturn = require('./infer/return');
|
21 | const inferAccess = require('./infer/access');
|
22 | const inferType = require('./infer/type');
|
23 | const formatLint = require('./lint').formatLint;
|
24 | const garbageCollect = require('./garbage_collect');
|
25 | const lintComments = require('./lint').lintComments;
|
26 | const markdownAST = require('./output/markdown_ast');
|
27 | const mergeConfig = require('./merge_config');
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 | function pipeline(fns) {
|
37 | return comment => {
|
38 | for (let i = 0; comment && i < fns.length; i++) {
|
39 | if (fns[i]) {
|
40 | comment = fns[i](comment);
|
41 | }
|
42 | }
|
43 | return comment;
|
44 | };
|
45 | }
|
46 |
|
47 | function configure(indexes, args) {
|
48 | const mergedConfig = mergeConfig(args);
|
49 |
|
50 | return mergedConfig.then(config => {
|
51 | const expandedInputs = expandInputs(indexes, config);
|
52 |
|
53 | return expandedInputs.then(inputs => {
|
54 | return {
|
55 | inputs,
|
56 | config
|
57 | };
|
58 | });
|
59 | });
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function expandInputs(indexes, config) {
|
71 |
|
72 | indexes = [].concat(indexes);
|
73 |
|
74 | if (config.shallow || config.documentExported) {
|
75 | return shallow(indexes, config);
|
76 | }
|
77 |
|
78 | return dependency(indexes, config);
|
79 | }
|
80 |
|
81 | function buildInternal(inputsAndConfig) {
|
82 | const config = inputsAndConfig.config;
|
83 | const inputs = inputsAndConfig.inputs;
|
84 |
|
85 | if (!config.access) {
|
86 | config.access = ['public', 'undefined', 'protected'];
|
87 | }
|
88 |
|
89 | const buildPipeline = pipeline([
|
90 | inferName,
|
91 | inferAccess(config.inferPrivate),
|
92 | inferAugments,
|
93 | inferImplements,
|
94 | inferKind,
|
95 | nest,
|
96 | inferParams,
|
97 | inferProperties,
|
98 | inferReturn,
|
99 | inferMembership(),
|
100 | inferType,
|
101 | config.github && github,
|
102 | garbageCollect
|
103 | ]);
|
104 |
|
105 | const extractedComments = _.flatMap(inputs, function(sourceFile) {
|
106 | if (!sourceFile.source) {
|
107 | sourceFile.source = fs.readFileSync(sourceFile.file, 'utf8');
|
108 | }
|
109 |
|
110 | if (!sourceFile.file) {
|
111 | sourceFile.file = '';
|
112 | }
|
113 |
|
114 | if (path.extname(sourceFile.file) === '.vue') {
|
115 | return parseVueScript(sourceFile, config).map(buildPipeline);
|
116 | }
|
117 | return parseJavaScript(sourceFile, config).map(buildPipeline);
|
118 | }).filter(Boolean);
|
119 |
|
120 | return filterAccess(
|
121 | config.access,
|
122 | hierarchy(sort(extractedComments, config))
|
123 | );
|
124 | }
|
125 |
|
126 | function lintInternal(inputsAndConfig) {
|
127 | const inputs = inputsAndConfig.inputs;
|
128 | const config = inputsAndConfig.config;
|
129 |
|
130 | const lintPipeline = pipeline([
|
131 | lintComments,
|
132 | inferName,
|
133 | inferAccess(config.inferPrivate),
|
134 | inferAugments,
|
135 | inferKind,
|
136 | inferParams,
|
137 | inferProperties,
|
138 | inferReturn,
|
139 | inferMembership(),
|
140 | inferType,
|
141 | nest
|
142 | ]);
|
143 |
|
144 | const extractedComments = _.flatMap(inputs, sourceFile => {
|
145 | if (!sourceFile.source) {
|
146 | sourceFile.source = fs.readFileSync(sourceFile.file, 'utf8');
|
147 | }
|
148 |
|
149 | return parseJavaScript(sourceFile, config).map(lintPipeline);
|
150 | }).filter(Boolean);
|
151 |
|
152 | return formatLint(hierarchy(extractedComments));
|
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 | const lint = (indexes, args) => configure(indexes, args).then(lintInternal);
|
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 | const build = (indexes, args) => configure(indexes, args).then(buildInternal);
|
226 |
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | const formats = {
|
235 | html: require('./output/html'),
|
236 | md: require('./output/markdown'),
|
237 | remark: (comments, config) =>
|
238 | markdownAST(comments, config).then(res => JSON.stringify(res, null, 2)),
|
239 | json: require('./output/json')
|
240 | };
|
241 |
|
242 | module.exports.lint = lint;
|
243 | module.exports.expandInputs = expandInputs;
|
244 | module.exports.build = build;
|
245 | module.exports.formats = formats;
|
246 |
|
247 | module.exports.util = {
|
248 | createFormatters: require('./output/util/formatters'),
|
249 | LinkerStack: require('./output/util/linker_stack')
|
250 | };
|