-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.mjs
80 lines (69 loc) · 1.74 KB
/
index.mjs
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
'use strict';
import createLinterEngine from './engine.mjs';
import reporters from './reporters/index.mjs';
import rules from './rules/index.mjs';
/**
* Creates a linter instance to validate mdast trees
*
* @param {boolean} dryRun Whether to run the engine in dry-run mode
* @param {string[]} disabledRules List of disabled rules names
* @returns {import('./types').Linter}
*/
const createLinter = (dryRun, disabledRules) => {
/**
* Retrieves all enabled rules
*
* @returns {import('./types').LintRule[]}
*/
const getEnabledRules = () => {
return Object.entries(rules)
.filter(([ruleName]) => !disabledRules.includes(ruleName))
.map(([, rule]) => rule);
};
const engine = createLinterEngine(getEnabledRules(disabledRules));
/**
* Lint issues found during validations
*
* @type {Array<import('./types').LintIssue>}
*/
const issues = [];
/**
* Lints all entries using the linter engine
*
* @param {import('vfile').VFile} file
* @param {import('mdast').Root} tree
* @returns {void}
*/
const lint = (file, tree) => {
issues.push(...engine.lint(file, tree));
};
/**
* Reports found issues using the specified reporter
*
* @param {keyof typeof reporters} reporterName Reporter name
* @returns {void}
*/
const report = reporterName => {
if (dryRun) {
return;
}
const reporter = reporters[reporterName];
for (const issue of issues) {
reporter(issue);
}
};
/**
* Checks if any error-level issues were found during linting
*
* @returns {boolean}
*/
const hasError = () => {
return issues.some(issue => issue.level === 'error');
};
return {
lint,
report,
hasError,
};
};
export default createLinter;