-
Notifications
You must be signed in to change notification settings - Fork 26.7k
/
Copy pathwhitespace.js
61 lines (50 loc) · 1.73 KB
/
whitespace.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
/* eslint global-require: 0 */
const { isArray } = Array;
const { entries } = Object;
const { CLIEngine } = require('eslint');
if (CLIEngine) {
/* eslint no-inner-declarations: 0 */
const whitespaceRules = require('./whitespaceRules');
const baseConfig = require('.');
const severities = ['off', 'warn', 'error'];
function getSeverity(ruleConfig) {
if (isArray(ruleConfig)) {
return getSeverity(ruleConfig[0]);
}
if (typeof ruleConfig === 'number') {
return severities[ruleConfig];
}
return ruleConfig;
}
function onlyErrorOnRules(rulesToError, config) {
const errorsOnly = { ...config };
const cli = new CLIEngine({ baseConfig: config, useEslintrc: false });
const baseRules = cli.getConfigForFile(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;
}
module.exports = onlyErrorOnRules(whitespaceRules, baseConfig);
} else {
const path = require('path');
const { execSync } = require('child_process');
// NOTE: ESLint adds runtime statistics to the output (so it's no longer JSON) if TIMING is set
module.exports = JSON.parse(String(execSync(path.join(__dirname, 'whitespace-async.js'), {
env: {
...process.env,
TIMING: undefined,
}
})));
}