-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54e0a46
commit abde489
Showing
5 changed files
with
232 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |