-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileLinter.js
More file actions
64 lines (54 loc) · 1.85 KB
/
FileLinter.js
File metadata and controls
64 lines (54 loc) · 1.85 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
const { ESLint } = require('eslint');
const eslintignore = require('../lib/eslintignore.js');
const pad = require('../lib/pad.js');
const resultDefaults = {
time: {
start: 0,
end: 0,
get duration() {
return this.end - this.start;
},
},
reports: [],
};
let results;
/**
* @param {string} path The root path begin the scan in
* @param {*} config A set of options for scanning
* @param {boolean} [logResults=false] Whether or not to log the result to the console
* @return {Promise} A promise that will resolve once the scan is complete
*/
async function run(path, config, logResults = false) {
results = Object.assign({}, resultDefaults);
results.time.start = Date.now();
const eslint = new ESLint({
overrideConfigFile: config,
});
// resolve the list of files and lint them
const eslintIgnorePath = eslintignore.findEslintIgnore(path);
const filesToIgnore = eslintIgnorePath === null ? new Set() : eslintignore.expandEslintIgnore(eslintIgnorePath);
const filesToLint = eslintignore.expandRootToNonIgnoredFiles(path, filesToIgnore);
const report = await eslint.lintFiles(filesToLint);
for (const record of report) {
if (record.errorCount === 0) {
continue;
}
for (const message of record.messages) {
const lineNumber = pad.left(`${message.line}`, 4, ' ');
const column = pad.right(`${message.column}`, 4, ' ');
const rule = pad.left(`${message.ruleId}`, 25, ' ');
const paddedMessage = pad.left(`${message.message}`, 50, ' ');
results.reports.push(`${record.filePath} ->${lineNumber}:${column} ${rule} ${paddedMessage}`);
}
}
results.time.end = Date.now();
if (logResults && results.reports.length > 0) {
console.log(results.reports.join('\n'));
}
if (results.reports.length === 0) {
return results;
} else {
throw results;
}
}
module.exports = { run };