forked from nodejs/doc-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.mjs
More file actions
74 lines (63 loc) · 1.54 KB
/
index.mjs
File metadata and controls
74 lines (63 loc) · 1.54 KB
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
'use strict';
import createContext from './context.mjs';
import reporters from './reporters/index.mjs';
/**
* Creates a linter instance to validate API documentation ASTs against a
* defined set of rules.
*
* @param {import('./types').LintRule[]} rules - Lint rules to apply
* @param {boolean} [dryRun] - If true, the linter runs without reporting
* @returns {import('./types').Linter}
*/
const createLinter = (rules, dryRun = false) => {
/**
* Lint issues collected during validations.
*
* @type {Array<import('./types').LintIssue>}
*/
const issues = [];
/**
* Lints a API doc and collects issues.
*
* @param {import('vfile').VFile} file
* @param {import('mdast').Root} tree
* @returns {void}
*/
const lint = (file, tree) => {
const context = createContext(file, tree);
for (const rule of rules) {
rule(context);
}
issues.push(...context.getIssues());
};
/**
* Reports collected issues using the specified reporter.
*
* @param {keyof typeof reporters} [reporterName] Reporter name
* @returns {void}
*/
const report = (reporterName = 'console') => {
if (dryRun) {
return;
}
const reporter = reporters[reporterName];
for (const issue of issues) {
reporter(issue);
}
};
/**
* Checks if any error-level issues were collected.
*
* @returns {boolean}
*/
const hasError = () => {
return issues.some(issue => issue.level === 'error');
};
return {
issues,
lint,
report,
hasError,
};
};
export default createLinter;