Skip to content

Commit 7c0ac0a

Browse files
committed
Add Rule Blocklist
1 parent 15a66ab commit 7c0ac0a

File tree

4 files changed

+165
-2
lines changed

4 files changed

+165
-2
lines changed

lib/eslint-patch.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const ConfigFile = require("eslint/lib/config/config-file");
77

88
const Config = require("eslint/lib/config");
99
const ConfigUpgrader = require("./config_upgrader");
10+
const RuleBlocklist = require("./rule_blocklist");
1011

1112
module.exports = function patch() {
1213
const skippedModules = [];
@@ -43,8 +44,11 @@ module.exports = function patch() {
4344
Config.prototype.getConfig = function(filePath) {
4445
const originalConfig = originalGetConfig.apply(this, [filePath]);
4546
const configUpgrader = new ConfigUpgrader();
47+
const ruleBlocklist = new RuleBlocklist();
4648

47-
return configUpgrader.upgrade(originalConfig);
49+
return ruleBlocklist.filter(
50+
configUpgrader.upgrade(originalConfig)
51+
);
4852
};
4953

5054
return require('eslint');

lib/eslint.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const checks = require("./checks");
1111
const validateConfig = require("./validate_config");
1212
const computeFingerprint = require("./compute_fingerprint");
1313
const ConfigUpgrader = require("./config_upgrader");
14+
const RuleBlocklist = require("./rule_blocklist");
1415
const findConfigs = require("./config_finder");
1516

1617
const CLIEngine = eslint.CLIEngine;
@@ -235,7 +236,8 @@ function run(console, runOptions) {
235236
let configs = findConfigs(options.configFile, analysisFiles);
236237

237238
let report = [
238-
ConfigUpgrader.upgradeInstructions(configs, process.cwd())
239+
ConfigUpgrader.upgradeInstructions(configs, process.cwd()),
240+
RuleBlocklist.report(configs)
239241
].reduce(function(a, b) { return a.concat([""]).concat(b); });
240242

241243
for (const line of report) {

lib/rule_blocklist.js

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
"use strict";
2+
3+
const Config = require("eslint/lib/config")
4+
, merge = require("eslint/lib/config/config-ops").merge;
5+
6+
const blocklistedRules = [
7+
"import/no-unresolved",
8+
"import/extensions",
9+
"import/no-absolute-path"
10+
];
11+
12+
function filterRules(rules) {
13+
let report = [];
14+
15+
for (const name of blocklistedRules) {
16+
if (Reflect.has(rules, name)) {
17+
let config = rules[name];
18+
if (config.constructor !== Array) {
19+
config = [config];
20+
}
21+
let severity = config.shift();
22+
23+
if (severity !== 0 && !(typeof severity === "string" && severity.toLowerCase() === "off")) {
24+
rules[name] = "off";
25+
report.push(`* ${name}`);
26+
}
27+
}
28+
}
29+
return report;
30+
}
31+
32+
class RuleBlocklist {
33+
constructor() {
34+
this._report = [];
35+
}
36+
37+
filter(originalConfig) {
38+
if (typeof originalConfig === "undefined" || originalConfig === null) {
39+
return {};
40+
}
41+
42+
let config = merge({}, originalConfig);
43+
44+
this._report = [];
45+
46+
if (Reflect.has(config, "rules")) {
47+
let report = filterRules(config.rules);
48+
this._report = this._report.concat(report);
49+
}
50+
51+
return config;
52+
}
53+
54+
get report() {
55+
return [].concat(this._report);
56+
}
57+
58+
static report(configs) {
59+
const reports = configs.map(function(configFile) {
60+
let report = [];
61+
62+
const blocklist = new RuleBlocklist();
63+
const config = new Config({
64+
configFile: configFile,
65+
cwd: process.cwd()
66+
});
67+
blocklist.filter(config.useSpecificConfig);
68+
69+
if (report.length > 0 || blocklist.report.length > 0) {
70+
report = report.concat(blocklist.report);
71+
}
72+
73+
return report;
74+
});
75+
76+
if (reports.length === 0) {
77+
return [];
78+
} else {
79+
return [["Ignoring the following rules that rely on module resolution:"]]
80+
.concat(reports)
81+
.reduce(function(a, b) { return a.concat([""]).concat(b); });
82+
}
83+
}
84+
}
85+
86+
module.exports = RuleBlocklist;

test/rule_blocklist_test.js

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const RuleBlocklist = require("../lib/rule_blocklist")
2+
, expect = require("chai").expect
3+
, fs = require("fs")
4+
, path = require("path")
5+
, stringify = require("json-stable-stringify")
6+
, temp = require('temp');
7+
8+
describe("ConfigUpgrader", function() {
9+
describe(".report", function() {
10+
it("returns blocklist report", function(done) {
11+
temp.mkdir("code ", function(err, directory) {
12+
if (err) { throw err; }
13+
14+
process.chdir(directory);
15+
16+
const configPath = path.join(directory, ".eslintrc");
17+
fs.writeFile(configPath, '{"rules":{"import/no-unresolved":2}}', function(err) {
18+
if (err) { throw err; }
19+
20+
let report = RuleBlocklist.report([directory + '/.eslintrc']);
21+
expect(report).to.deep.eq([
22+
"Ignoring the following rules that rely on module resolution:",
23+
"",
24+
"* import/no-unresolved"
25+
]);
26+
done();
27+
});
28+
});
29+
});
30+
});
31+
32+
33+
describe("#filter", function() {
34+
it("doesn't fail with null config", function(done) {
35+
let blocklist = new RuleBlocklist();
36+
expect(function() {
37+
blocklist.filter(null);
38+
}).to.not.throw(TypeError);
39+
done();
40+
});
41+
42+
describe("rules", function() {
43+
[
44+
[
45+
{rules: {"import/no-unresolved": [2, "opt1", "opt2"]}},
46+
{rules: {"import/no-unresolved": "off"}}
47+
],
48+
[
49+
{rules: {"import/extensions": 2}},
50+
{rules: {"import/extensions": "off"}}
51+
],
52+
[
53+
{rules: {"import/no-absolute-path": 1}},
54+
{rules: {"import/no-absolute-path": "off"}}
55+
]
56+
].forEach(function(example){
57+
let originalConfig = example[0];
58+
let convertedConfig = example[1];
59+
60+
it(`filters out ${stringify(originalConfig)}`, function(done){
61+
let blocklist = new RuleBlocklist();
62+
let actualConfig = blocklist.filter(originalConfig);
63+
64+
expect(actualConfig).to.deep.eq(convertedConfig);
65+
expect(blocklist.report).to.lengthOf(1);
66+
done();
67+
});
68+
});
69+
});
70+
});
71+
});

0 commit comments

Comments
 (0)