Skip to content

Commit abde489

Browse files
modoscfilipesperandio
authored andcommitted
add setting blocklist (codeclimate#432)
Related to codeclimate#427 and codeclimate#426
1 parent 54e0a46 commit abde489

File tree

5 files changed

+232
-60
lines changed

5 files changed

+232
-60
lines changed

lib/eslint-patch.js

Lines changed: 5 additions & 2 deletions
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 RuleBlocklist = require("./rule_blocklist");
10+
const SettingBlocklist = require("./setting_blocklist");
1011

1112
module.exports = function patch() {
1213
const skippedModules = [];
@@ -51,9 +52,11 @@ module.exports = function patch() {
5152
Config.prototype.getConfig = function(filePath) {
5253
const originalConfig = originalGetConfig.apply(this, [filePath]);
5354
const ruleBlocklist = new RuleBlocklist();
54-
55+
const settingBlocklist = new SettingBlocklist();
5556
return ruleBlocklist.filter(
56-
originalConfig
57+
settingBlocklist.filter(
58+
originalConfig
59+
)
5760
);
5861
};
5962

lib/eslint.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const checks = require("./checks");
1111
const validateConfig = require("./validate_config");
1212
const computeFingerprint = require("./compute_fingerprint");
1313
const RuleBlocklist = require("./rule_blocklist");
14+
const SettingBlocklist = require("./setting_blocklist");
15+
1416
const findConfigs = require("./config_finder");
1517

1618
const CLIEngine = eslint.CLIEngine;
@@ -243,7 +245,8 @@ function run(console, runOptions) {
243245
let configs = findConfigs(options.configFile, analysisFiles);
244246

245247
let report = [
246-
RuleBlocklist.report(configs)
248+
RuleBlocklist.report(configs),
249+
SettingBlocklist.report(configs),
247250
].reduce(function(a, b) { return a.concat([""]).concat(b); });
248251

249252
for (const line of report) {

lib/setting_blocklist.js

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

test/rule_blocklist_test.js

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,83 @@ const RuleBlocklist = require("../lib/rule_blocklist")
66
, temp = require('temp');
77

88
describe("ConfigUpgrader", function() {
9-
describe(".report", function() {
10-
it("returns blocklist report with the blocked rules", function(done) {
11-
temp.mkdir("code ", function(err, directory) {
12-
if (err) { throw err; }
9+
describe("rules", function() {
10+
describe(".report", function() {
11+
it("returns blocklist report with the blocked rules", function(done) {
12+
temp.mkdir("code ", function(err, directory) {
13+
if (err) { throw err; }
1314

14-
process.chdir(directory);
15+
process.chdir(directory);
1516

16-
const configPath = path.join(directory, ".eslintrc");
17-
fs.writeFile(configPath, '{"rules":{"import/no-unresolved":2}}', function(err) {
18-
if (err) { throw err; }
17+
const configPath = path.join(directory, ".eslintrc");
18+
fs.writeFile(configPath, '{"rules":{"import/no-unresolved":2}}', function(err) {
19+
if (err) { throw err; }
1920

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();
21+
let report = RuleBlocklist.report([directory + '/.eslintrc']);
22+
expect(report).to.deep.eq([
23+
"Ignoring the following rules that rely on module resolution:",
24+
"",
25+
"* import/no-unresolved"
26+
]);
27+
done();
28+
});
2729
});
2830
});
29-
});
3031

31-
it("when no blocked rules, it returns meaningful blocklist report", function(done) {
32-
temp.mkdir("code ", function(err, directory) {
33-
if (err) { throw err; }
32+
it("when no blocked rules, it returns meaningful blocklist report", function(done) {
33+
temp.mkdir("code ", function(err, directory) {
34+
if (err) { throw err; }
3435

35-
process.chdir(directory);
36+
process.chdir(directory);
3637

37-
const configPath = path.join(directory, ".eslintrc");
38-
fs.writeFile(configPath, '{"rules":{"foo/bar":2}}', function(err) {
39-
if (err) { throw err; }
38+
const configPath = path.join(directory, ".eslintrc");
39+
fs.writeFile(configPath, '{"rules":{"foo/bar":2}}', function(err) {
40+
if (err) { throw err; }
4041

41-
let report = RuleBlocklist.report([directory + '/.eslintrc']);
42-
expect(report).to.deep.eq([]);
43-
done();
42+
let report = RuleBlocklist.report([directory + '/.eslintrc']);
43+
expect(report).to.deep.eq([]);
44+
done();
45+
});
4446
});
4547
});
4648
});
47-
});
4849

4950

50-
describe("#filter", function() {
51-
it("doesn't fail with null config", function(done) {
52-
let blocklist = new RuleBlocklist();
53-
expect(function() {
54-
blocklist.filter(null);
55-
}).to.not.throw(TypeError);
56-
done();
57-
});
51+
describe("#filter", function() {
52+
it("doesn't fail with null config", function(done) {
53+
let blocklist = new RuleBlocklist();
54+
expect(function() {
55+
blocklist.filter(null);
56+
}).to.not.throw(TypeError);
57+
done();
58+
});
5859

59-
describe("rules", function() {
60-
[
61-
[
62-
{rules: {"import/no-restricted-paths": 1}},
63-
{rules: {"import/no-restricted-paths": "off"}}
64-
],
65-
[
66-
{rules: {"import/no-unresolved": [2, "opt1", "opt2"]}},
67-
{rules: {"import/no-unresolved": "off"}}
68-
],
60+
describe("rules", function() {
6961
[
70-
{rules: {"node/no-hide-code-modules": 2}},
71-
{rules: {"node/no-hide-code-modules": "off"}}
72-
]
73-
].forEach(function(example){
74-
let originalConfig = example[0];
75-
let convertedConfig = example[1];
62+
[
63+
{rules: {"import/no-restricted-paths": 1}},
64+
{rules: {"import/no-restricted-paths": "off"}}
65+
],
66+
[
67+
{rules: {"import/no-unresolved": [2, "opt1", "opt2"]}},
68+
{rules: {"import/no-unresolved": "off"}}
69+
],
70+
[
71+
{rules: {"node/no-hide-code-modules": 2}},
72+
{rules: {"node/no-hide-code-modules": "off"}}
73+
]
74+
].forEach(function(example){
75+
let originalConfig = example[0];
76+
let convertedConfig = example[1];
7677

77-
it(`filters out ${stringify(originalConfig)}`, function(done){
78-
let blocklist = new RuleBlocklist();
79-
let actualConfig = blocklist.filter(originalConfig);
78+
it(`filters out ${stringify(originalConfig)}`, function(done){
79+
let blocklist = new RuleBlocklist();
80+
let actualConfig = blocklist.filter(originalConfig);
8081

81-
expect(actualConfig).to.deep.eq(convertedConfig);
82-
expect(blocklist.report).to.lengthOf(1);
83-
done();
82+
expect(actualConfig).to.deep.eq(convertedConfig);
83+
expect(blocklist.report).to.lengthOf(1);
84+
done();
85+
});
8486
});
8587
});
8688
});

test/setting_blocklist_test.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const SettingBlocklist = require("../lib/setting_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("settings", function() {
10+
describe(".report", function() {
11+
it("returns blocklist report with the blocked settings", function(done) {
12+
temp.mkdir("code ", function(err, directory) {
13+
if (err) { throw err; }
14+
15+
process.chdir(directory);
16+
17+
const configPath = path.join(directory, ".eslintrc");
18+
fs.writeFile(configPath, '{"settings":{"import/resolver":{"webpack": {"config": "webpack.config.js"}}}}', function(err) {
19+
if (err) { throw err; }
20+
21+
let report = SettingBlocklist.report([directory + '/.eslintrc']);
22+
expect(report).to.deep.eq([
23+
"Ignoring the following settings that rely on module resolution:",
24+
"",
25+
"* import/resolver"
26+
]);
27+
done();
28+
});
29+
});
30+
});
31+
32+
it("when no blocked settings, it returns meaningful blocklist report", function(done) {
33+
temp.mkdir("code ", function(err, directory) {
34+
if (err) { throw err; }
35+
36+
process.chdir(directory);
37+
38+
const configPath = path.join(directory, ".eslintrc");
39+
fs.writeFile(configPath, '{"settings":{"foo/bar":2}}', function(err) {
40+
if (err) { throw err; }
41+
42+
let report = SettingBlocklist.report([directory + '/.eslintrc']);
43+
expect(report).to.deep.eq([]);
44+
done();
45+
});
46+
});
47+
});
48+
});
49+
50+
51+
describe("#filter", function() {
52+
it("doesn't fail with null config", function(done) {
53+
let blocklist = new SettingBlocklist();
54+
expect(function() {
55+
blocklist.filter(null);
56+
}).to.not.throw(TypeError);
57+
done();
58+
});
59+
60+
describe("settings", function() {
61+
[
62+
[
63+
{settings: {"import/resolver": {} }},
64+
{settings: {}}
65+
],
66+
[
67+
{settings: {"import/resolver": { webpack: null} }},
68+
{settings: {}}
69+
]
70+
].forEach(function(example){
71+
let originalConfig = example[0];
72+
let convertedConfig = example[1];
73+
74+
it(`filters out ${stringify(originalConfig)}`, function(done){
75+
let blocklist = new SettingBlocklist();
76+
let actualConfig = blocklist.filter(originalConfig);
77+
78+
expect(actualConfig).to.deep.eq(convertedConfig);
79+
expect(blocklist.report).to.lengthOf(1);
80+
done();
81+
});
82+
});
83+
});
84+
});
85+
});
86+
});

0 commit comments

Comments
 (0)