forked from sarbbottam/eslint-find-rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule-finder.js
108 lines (90 loc) · 3.2 KB
/
rule-finder.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const path = require('path');
const eslint = require('eslint');
const isAbsolute = require('path-is-absolute');
const difference = require('./array-diff');
const getSortedRules = require('./sort-rules');
function _getConfigFile(specifiedFile) {
if (specifiedFile) {
if (isAbsolute(specifiedFile)) {
return specifiedFile;
}
return path.join(process.cwd(), specifiedFile); // eslint-disable-line import/no-dynamic-require
}
// this is not being called with an arg. Use the package.json `main`
return require(path.join(process.cwd(), 'package.json')).main; // eslint-disable-line import/no-dynamic-require
}
function _getConfig(configFile) {
const cliEngine = new eslint.CLIEngine({
// ignore any config applicable depending on the location on the filesystem
useEslintrc: false,
// point to the particular config
configFile
});
return cliEngine.getConfigForFile();
}
function _getCurrentRules(config) {
return Object.keys(config.rules);
}
function _normalizePluginName(name) {
const scopedRegex = /(@[^/]+)\/(.+)/;
const match = scopedRegex.exec(name);
if (match) {
return {
module: `${match[1]}/eslint-plugin-${match[2]}`,
prefix: match[2]
};
}
return {
module: `eslint-plugin-${name}`,
prefix: name
};
}
function _getPluginRules(config) {
let pluginRules = [];
const plugins = config.plugins;
if (plugins) {
plugins.forEach(plugin => {
const normalized = _normalizePluginName(plugin);
const pluginConfig = require(normalized.module); // eslint-disable-line import/no-dynamic-require
const rules = pluginConfig.rules === undefined ? {} : pluginConfig.rules;
pluginRules = pluginRules.concat(
Object.keys(rules).map(rule => {
return normalized.prefix + '/' + rule;
})
);
});
}
return pluginRules;
}
function _getAllAvailableRules(pluginRules) {
return [
...eslint.linter.getRules().keys(),
...pluginRules
];
}
function _isNotCore(rule) {
return rule.indexOf('/') !== '-1';
}
function RuleFinder(specifiedFile, noCore) {
const configFile = _getConfigFile(specifiedFile);
const config = _getConfig(configFile);
let currentRules = _getCurrentRules(config);
const pluginRules = _getPluginRules(config);
const allRules = noCore ? pluginRules : _getAllAvailableRules(pluginRules);
if (noCore) {
currentRules = currentRules.filter(_isNotCore);
}
const unusedRules = difference(allRules, currentRules); // eslint-disable-line vars-on-top
// get all the current rules instead of referring the extended files or documentation
this.getCurrentRules = () => getSortedRules(currentRules);
// get all the current rules' particular configuration
this.getCurrentRulesDetailed = () => config.rules;
// get all the plugin rules instead of referring the extended files or documentation
this.getPluginRules = () => getSortedRules(pluginRules);
// get all the available rules instead of referring eslint and plugin packages or documentation
this.getAllAvailableRules = () => getSortedRules(allRules);
this.getUnusedRules = () => getSortedRules(unusedRules);
}
module.exports = function (specifiedFile, noCore) {
return new RuleFinder(specifiedFile, noCore);
};