-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.mjs
167 lines (145 loc) · 3.88 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
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
'use strict';
import { LINT_MESSAGES } from '../constants.mjs';
import createLinterEngine from './engine.mjs';
import reporters from './reporters/index.mjs';
import rules from './rules/index.mjs';
/**
* Creates a linter instance to validate ApiDocMetadataEntry entries
*
* @param {boolean} dryRun Whether to run the engine in dry-run mode
* @param {string[]} disabledRules List of disabled rules names
*/
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);
};
/**
* @type {import('./types').LintDeclarations}
*/
const declarations = {
skipDeprecation: [],
};
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 entries
*/
const lintAll = entries => {
issues.push(...engine.lintAll(entries, declarations));
};
/**
* 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);
}
};
/**
* Parse an inline-declaration found in the markdown input
*
* @param {string} declaration
*/
const parseLinterDeclaration = declaration => {
// Trim off any excess spaces from the beginning & end
declaration = declaration.trim();
// Extract the name for the declaration
const [name, ...value] = declaration.split(' ');
switch (name) {
case 'skip-deprecation': {
if (value.length !== 1) {
issues.push({
level: 'error',
location: {
// TODO,
path: '',
position: 0,
},
message: LINT_MESSAGES.malformedLinterDeclaration.replace(
'{{message}}',
`Expected 1 argument, got ${value.length}`
),
});
break;
}
// Get the deprecation code. This should be something like DEP0001.
const deprecation = value[0];
// Extract the number from the code
const deprecationCode = Number(deprecation.substring('DEP'.length));
// Make sure this is a valid deprecation code, output an error otherwise
if (
deprecation.length !== 7 ||
!deprecation.startsWith('DEP') ||
isNaN(deprecationCode)
) {
issues.push({
level: 'error',
location: {
// TODO,
path: '',
position: 0,
},
message: LINT_MESSAGES.malformedLinterDeclaration.replace(
'{{message}}',
`Invalid deprecation code ${deprecation}`
),
});
break;
}
declarations.skipDeprecation.push(deprecationCode);
break;
}
default: {
issues.push({
level: 'error',
location: {
// TODO
path: '',
position: 0,
},
message: LINT_MESSAGES.invalidLinterDeclaration.replace(
'{{declaration}}',
name
),
});
break;
}
}
};
/**
* Checks if any error-level issues were found during linting
*
* @returns {boolean}
*/
const hasError = () => {
return issues.some(issue => issue.level === 'error');
};
return {
lintAll,
report,
parseLinterDeclaration,
hasError,
};
};
export default createLinter;