forked from sindresorhus/eslint-plugin-unicorn
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint.js
executable file
·59 lines (46 loc) · 1.65 KB
/
lint.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
#!/usr/bin/env node
'use strict';
const {CLIEngine} = require('eslint');
const unicorn = require('../..');
const {recommended} = unicorn.configs;
const files = [process.argv[2] || '.'];
const fix = process.argv.includes('--fix');
const ruleIds = Object.keys(unicorn.rules);
const unicornRules = new Map(Object.entries(unicorn.rules));
const cli = new CLIEngine({
baseConfig: recommended,
rules: {
// TODO: remove this override, when #391 is fixed
'unicorn/consistent-function-scoping': 'off'
},
useEslintrc: false,
fix
});
cli.addPlugin('eslint-plugin-unicorn', unicorn);
// Make sure rules are loaded from codebase
const loadedRules = cli.getRules();
if (!ruleIds.every(ruleId => unicornRules.get(ruleId) === loadedRules.get(`unicorn/${ruleId}`))) {
console.error('`eslint-plugin-unicorn` rules are not loaded from codebase.');
process.exit(1);
}
const report = cli.executeOnFiles(files);
const {errorCount, warningCount, fixableErrorCount, fixableWarningCount} = report;
const hasFixable = fixableErrorCount || fixableWarningCount;
if (fix && hasFixable) {
CLIEngine.outputFixes(report);
}
if (errorCount || warningCount) {
const formatter = cli.getFormatter();
console.log(formatter(report.results));
console.log();
console.log(`You need to fix the failed test${errorCount + warningCount > 1 ? 's' : ''} above and run \`npm run lint <file>\` to check again.`);
if (hasFixable) {
console.log();
console.log('You may also want run `npm run lint <file> --fix` to fix fixable problems.');
}
console.log();
console.log('* If you\'re making a new rule, you can fix this later. *');
} else {
console.log('All tests have passed.');
}
process.exit(errorCount);