/
runLintWithFixtures.js
51 lines (48 loc) · 1.67 KB
/
runLintWithFixtures.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
"use strict";
const { ESLint } = require("eslint");
const path = require("path");
/**
* Run ESlint and return the result per files
* @param type lint type, which is in lib directory
* @returns {Object} lint results {[fileName]: {errors: [ruleNames], warnings: [ruleNames]}}
*/
const runLintWithFixtures = async (type, configFile = `lib/${type}.js`) => {
const eslint = new ESLint({
overrideConfigFile: path.resolve(process.cwd(), configFile),
ignore: false,
useEslintrc: false,
extensions: [".js", ".jsx", ".ts", ".tsx"]
});
const targetDir = path.resolve(__dirname, "..", "fixtures", type);
const lintResult = await eslint.lintFiles([targetDir]);
// console.log(JSON.stringify(lintResult, null, 2));
return lintResult.reduce((results, { filePath, messages }) => {
// strip path
const fileName = filePath.replace(`${targetDir}/`, "");
return Object.assign(results, {
[fileName]: messages.reduce((acc, { severity, ruleId }) => {
const resultPerFile = acc;
switch (severity) {
// warning
case 1:
if (typeof resultPerFile.warnings === "undefined") {
resultPerFile.warnings = [];
}
resultPerFile.warnings.push(ruleId);
break;
// errors
case 2:
if (typeof resultPerFile.errors === "undefined") {
resultPerFile.errors = [];
}
resultPerFile.errors.push(ruleId);
break;
default:
throw new Error(`Got an unknown severity: ${severity}(${ruleId})`);
}
return resultPerFile;
}, {})
});
}, {});
};
module.exports = runLintWithFixtures;