-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy patheslint.js
76 lines (61 loc) · 1.72 KB
/
eslint.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
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
'use strict';
const chalk = require('chalk');
const {ESLint} = require('eslint');
module.exports = grunt => {
grunt.registerMultiTask('eslint', 'Validate files with ESLint', function () {
const done = this.async();
(async () => {
try {
const {
format,
quiet,
maxWarnings,
failOnError,
outputFile,
...options
} = this.options({
outputFile: false,
quiet: false,
maxWarnings: -1,
failOnError: true,
format: 'stylish'
});
if (this.filesSrc.length === 0) {
grunt.log.writeln(chalk.magenta('Could not find any files to validate'));
return true;
}
const engine = new ESLint(options);
const formatter = await engine.loadFormatter(format);
if (!formatter) {
grunt.warn(`Could not find formatter ${format}`);
return false;
}
let results = await engine.lintFiles(this.filesSrc);
if (options.fix) {
await ESLint.outputFixes(results);
}
if (quiet) {
results = ESLint.getErrorResults(results);
}
const output = formatter.format(results);
if (outputFile) {
grunt.file.write(outputFile, output);
} else if (output) {
console.log(output);
}
const {warningCount, errorCount} = results.reduce((count, {warningCount, errorCount}) => {
count.warningCount += warningCount;
count.errorCount += errorCount;
return count;
}, {warningCount: 0, errorCount: 0});
const tooManyWarnings = maxWarnings >= 0 && warningCount > maxWarnings;
if (errorCount === 0 && tooManyWarnings) {
grunt.warn(`ESLint found too many warnings (maximum: ${maxWarnings})`);
}
done(failOnError ? errorCount === 0 : 0);
} catch (error) {
done(error);
}
})();
});
};