-
Notifications
You must be signed in to change notification settings - Fork 26.7k
/
Copy pathwhitespace-async.js
executable file
·49 lines (40 loc) · 1.31 KB
/
whitespace-async.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
#!/usr/bin/env node
const { isArray } = Array;
const { entries } = Object;
const { ESLint } = require('eslint');
const baseConfig = require('.');
const whitespaceRules = require('./whitespaceRules');
const severities = ['off', 'warn', 'error'];
function getSeverity(ruleConfig) {
if (isArray(ruleConfig)) {
return getSeverity(ruleConfig[0]);
}
if (typeof ruleConfig === 'number') {
return severities[ruleConfig];
}
return ruleConfig;
}
async function onlyErrorOnRules(rulesToError, config) {
const errorsOnly = { ...config };
const cli = new ESLint({
useEslintrc: false,
baseConfig: config
});
const baseRules = (await cli.calculateConfigForFile(require.resolve('./'))).rules;
entries(baseRules).forEach((rule) => {
const ruleName = rule[0];
const ruleConfig = rule[1];
const severity = getSeverity(ruleConfig);
if (rulesToError.indexOf(ruleName) === -1 && severity === 'error') {
if (isArray(ruleConfig)) {
errorsOnly.rules[ruleName] = ['warn'].concat(ruleConfig.slice(1));
} else if (typeof ruleConfig === 'number') {
errorsOnly.rules[ruleName] = 1;
} else {
errorsOnly.rules[ruleName] = 'warn';
}
}
});
return errorsOnly;
}
onlyErrorOnRules(whitespaceRules, baseConfig).then((config) => console.log(JSON.stringify(config)));