Skip to content

Commit

Permalink
add setting blocklist (#432)
Browse files Browse the repository at this point in the history
Related to #427 and #426
  • Loading branch information
modosc authored and filipesperandio committed Jan 22, 2019
1 parent 54e0a46 commit abde489
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 60 deletions.
7 changes: 5 additions & 2 deletions lib/eslint-patch.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const ConfigFile = require("eslint/lib/config/config-file");

const Config = require("eslint/lib/config");
const RuleBlocklist = require("./rule_blocklist");
const SettingBlocklist = require("./setting_blocklist");

module.exports = function patch() {
const skippedModules = [];
Expand Down Expand Up @@ -51,9 +52,11 @@ module.exports = function patch() {
Config.prototype.getConfig = function(filePath) {
const originalConfig = originalGetConfig.apply(this, [filePath]);
const ruleBlocklist = new RuleBlocklist();

const settingBlocklist = new SettingBlocklist();
return ruleBlocklist.filter(
originalConfig
settingBlocklist.filter(
originalConfig
)
);
};

Expand Down
5 changes: 4 additions & 1 deletion lib/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const checks = require("./checks");
const validateConfig = require("./validate_config");
const computeFingerprint = require("./compute_fingerprint");
const RuleBlocklist = require("./rule_blocklist");
const SettingBlocklist = require("./setting_blocklist");

const findConfigs = require("./config_finder");

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

let report = [
RuleBlocklist.report(configs)
RuleBlocklist.report(configs),
SettingBlocklist.report(configs),
].reduce(function(a, b) { return a.concat([""]).concat(b); });

for (const line of report) {
Expand Down
78 changes: 78 additions & 0 deletions lib/setting_blocklist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"use strict";

const Config = require("eslint/lib/config")
, Linter = require("eslint/lib/linter")
, merge = require("eslint/lib/config/config-ops").merge;

const blocklistedSettings = [
"import/resolver"
];

function filterSettings(settings) {
let report = [];

for (const name of blocklistedSettings) {
if (Reflect.has(settings, name)) {
delete settings[name];
report.push(`* ${name}`);
}
}
return report;
}

class SettingBlocklist {
constructor() {
this._report = [];
}

filter(originalConfig) {
if (typeof originalConfig === "undefined" || originalConfig === null) {
return {};
}

let config = merge({}, originalConfig);

this._report = [];

if (Reflect.has(config, "settings")) {
let report = filterSettings(config.settings);
this._report = this._report.concat(report);
}

return config;
}

get report() {
return [].concat(this._report);
}

static report(configs) {
const reports = configs.map(function(configFile) {
let report = [];

const blocklist = new SettingBlocklist();
const config = new Config({
configFile: configFile,
cwd: process.cwd()
},
new Linter());
blocklist.filter(config.specificConfig);

if (report.length > 0 || blocklist.report.length > 0) {
report = report.concat(blocklist.report);
}

return report;
}).filter(function(report) { return report.length > 0; });

if (reports.length === 0) {
return [];
} else {
return [["Ignoring the following settings that rely on module resolution:"]]
.concat(reports)
.reduce(function(a, b) { return a.concat([""]).concat(b); });
}
}
}

module.exports = SettingBlocklist;
116 changes: 59 additions & 57 deletions test/rule_blocklist_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,81 +6,83 @@ const RuleBlocklist = require("../lib/rule_blocklist")
, temp = require('temp');

describe("ConfigUpgrader", function() {
describe(".report", function() {
it("returns blocklist report with the blocked rules", function(done) {
temp.mkdir("code ", function(err, directory) {
if (err) { throw err; }
describe("rules", function() {
describe(".report", function() {
it("returns blocklist report with the blocked rules", function(done) {
temp.mkdir("code ", function(err, directory) {
if (err) { throw err; }

process.chdir(directory);
process.chdir(directory);

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

let report = RuleBlocklist.report([directory + '/.eslintrc']);
expect(report).to.deep.eq([
"Ignoring the following rules that rely on module resolution:",
"",
"* import/no-unresolved"
]);
done();
let report = RuleBlocklist.report([directory + '/.eslintrc']);
expect(report).to.deep.eq([
"Ignoring the following rules that rely on module resolution:",
"",
"* import/no-unresolved"
]);
done();
});
});
});
});

it("when no blocked rules, it returns meaningful blocklist report", function(done) {
temp.mkdir("code ", function(err, directory) {
if (err) { throw err; }
it("when no blocked rules, it returns meaningful blocklist report", function(done) {
temp.mkdir("code ", function(err, directory) {
if (err) { throw err; }

process.chdir(directory);
process.chdir(directory);

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

let report = RuleBlocklist.report([directory + '/.eslintrc']);
expect(report).to.deep.eq([]);
done();
let report = RuleBlocklist.report([directory + '/.eslintrc']);
expect(report).to.deep.eq([]);
done();
});
});
});
});
});


describe("#filter", function() {
it("doesn't fail with null config", function(done) {
let blocklist = new RuleBlocklist();
expect(function() {
blocklist.filter(null);
}).to.not.throw(TypeError);
done();
});
describe("#filter", function() {
it("doesn't fail with null config", function(done) {
let blocklist = new RuleBlocklist();
expect(function() {
blocklist.filter(null);
}).to.not.throw(TypeError);
done();
});

describe("rules", function() {
[
[
{rules: {"import/no-restricted-paths": 1}},
{rules: {"import/no-restricted-paths": "off"}}
],
[
{rules: {"import/no-unresolved": [2, "opt1", "opt2"]}},
{rules: {"import/no-unresolved": "off"}}
],
describe("rules", function() {
[
{rules: {"node/no-hide-code-modules": 2}},
{rules: {"node/no-hide-code-modules": "off"}}
]
].forEach(function(example){
let originalConfig = example[0];
let convertedConfig = example[1];
[
{rules: {"import/no-restricted-paths": 1}},
{rules: {"import/no-restricted-paths": "off"}}
],
[
{rules: {"import/no-unresolved": [2, "opt1", "opt2"]}},
{rules: {"import/no-unresolved": "off"}}
],
[
{rules: {"node/no-hide-code-modules": 2}},
{rules: {"node/no-hide-code-modules": "off"}}
]
].forEach(function(example){
let originalConfig = example[0];
let convertedConfig = example[1];

it(`filters out ${stringify(originalConfig)}`, function(done){
let blocklist = new RuleBlocklist();
let actualConfig = blocklist.filter(originalConfig);
it(`filters out ${stringify(originalConfig)}`, function(done){
let blocklist = new RuleBlocklist();
let actualConfig = blocklist.filter(originalConfig);

expect(actualConfig).to.deep.eq(convertedConfig);
expect(blocklist.report).to.lengthOf(1);
done();
expect(actualConfig).to.deep.eq(convertedConfig);
expect(blocklist.report).to.lengthOf(1);
done();
});
});
});
});
Expand Down
86 changes: 86 additions & 0 deletions test/setting_blocklist_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const SettingBlocklist = require("../lib/setting_blocklist")
, expect = require("chai").expect
, fs = require("fs")
, path = require("path")
, stringify = require("json-stable-stringify")
, temp = require('temp');

describe("ConfigUpgrader", function() {
describe("settings", function() {
describe(".report", function() {
it("returns blocklist report with the blocked settings", function(done) {
temp.mkdir("code ", function(err, directory) {
if (err) { throw err; }

process.chdir(directory);

const configPath = path.join(directory, ".eslintrc");
fs.writeFile(configPath, '{"settings":{"import/resolver":{"webpack": {"config": "webpack.config.js"}}}}', function(err) {
if (err) { throw err; }

let report = SettingBlocklist.report([directory + '/.eslintrc']);
expect(report).to.deep.eq([
"Ignoring the following settings that rely on module resolution:",
"",
"* import/resolver"
]);
done();
});
});
});

it("when no blocked settings, it returns meaningful blocklist report", function(done) {
temp.mkdir("code ", function(err, directory) {
if (err) { throw err; }

process.chdir(directory);

const configPath = path.join(directory, ".eslintrc");
fs.writeFile(configPath, '{"settings":{"foo/bar":2}}', function(err) {
if (err) { throw err; }

let report = SettingBlocklist.report([directory + '/.eslintrc']);
expect(report).to.deep.eq([]);
done();
});
});
});
});


describe("#filter", function() {
it("doesn't fail with null config", function(done) {
let blocklist = new SettingBlocklist();
expect(function() {
blocklist.filter(null);
}).to.not.throw(TypeError);
done();
});

describe("settings", function() {
[
[
{settings: {"import/resolver": {} }},
{settings: {}}
],
[
{settings: {"import/resolver": { webpack: null} }},
{settings: {}}
]
].forEach(function(example){
let originalConfig = example[0];
let convertedConfig = example[1];

it(`filters out ${stringify(originalConfig)}`, function(done){
let blocklist = new SettingBlocklist();
let actualConfig = blocklist.filter(originalConfig);

expect(actualConfig).to.deep.eq(convertedConfig);
expect(blocklist.report).to.lengthOf(1);
done();
});
});
});
});
});
});

0 comments on commit abde489

Please sign in to comment.