Skip to content

Commit 0e9034b

Browse files
pointlessonemaxjacobson
authored andcommitted
Fix upgrader with specified ESLint config
Upgrader improperly kept detecting ESLint config file when engine had a specific config file specified. This behaviour resulted in providing upgrade instructions for the wrong config file or in worse case trowing an exception of missing config file.
1 parent 448e0d5 commit 0e9034b

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

bin/eslint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ function analyzeFiles() {
236236
if (validateConfig(options.configFile)) {
237237
console.error("ESLint is running with the " + cli.getConfigForFile(null).parser + " parser.");
238238

239-
for (const line of ConfigUpgrader.upgradeInstructions(analysisFiles, process.cwd())) {
239+
for (const line of ConfigUpgrader.upgradeInstructions(options.configFile, analysisFiles, process.cwd())) {
240240
console.error(line);
241241
}
242242

lib/config_upgrader.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,23 @@ class ConfigUpgrader {
245245
return Array.from(configs);
246246
}
247247

248-
static upgradeInstructions(analysisFiles, root) {
249-
const reports = this.configs(analysisFiles).map(function(configFile) {
248+
static upgradeInstructions(configFile, analysisFiles, root) {
249+
let configs;
250+
if (configFile) {
251+
configs = [configFile];
252+
}
253+
else {
254+
configs = this.configs(analysisFiles);
255+
}
256+
257+
const reports = configs.map(function(configFile) {
250258
let report = [];
251259

252260
const upgrader = new ConfigUpgrader();
253-
const config = new Config({configFile: configFile});
261+
const config = new Config({
262+
configFile: configFile,
263+
cwd: process.cwd()
264+
});
254265
upgrader.upgrade(config.useSpecificConfig);
255266

256267
if (path.extname(configFile) === '') {
@@ -269,6 +280,10 @@ class ConfigUpgrader {
269280
return report;
270281
});
271282

283+
if (reports.length === 0) {
284+
reports.push([]);
285+
}
286+
272287
return reports.reduce(function(a, b) { return a.concat([""]).concat(b); });
273288
}
274289
}

lib/validate_config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
var CLIEngine = require("eslint").CLIEngine
2-
, cli = new CLIEngine()
32
, fs = require("fs");
43

54
module.exports = function(configPath) {
65
if (configPath) {
76
return true;
87
} else {
9-
var config = cli.getConfigForFile(null);
8+
let cli = new CLIEngine();
9+
let config = cli.getConfigForFile(null);
1010

1111
return Object.keys(config.rules).length > 0;
1212
}

test/config_upgrater_test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("ConfigUpgrader", function() {
1919

2020

2121
let report = ConfigUpgrader
22-
.upgradeInstructions([directory + '/file.js'], directory);
22+
.upgradeInstructions(null, [directory + '/file.js'], directory);
2323
expect(report).to.deep.eq([
2424
".eslintrc appears to be incompatible with ESLint 3.",
2525
"To upgrade it do the following:\n",
@@ -47,14 +47,36 @@ describe("ConfigUpgrader", function() {
4747
if (err) { throw err; }
4848

4949
let report = ConfigUpgrader
50-
.upgradeInstructions([directory + '/file.js'], directory);
50+
.upgradeInstructions(null, [directory + '/file.js'], directory);
5151
expect(report).to.deep.eq([]);
5252
done();
5353
});
5454
});
5555
});
5656
});
5757
});
58+
59+
it("uses specific configs", function(done) {
60+
temp.mkdir("code", function(err, directory) {
61+
if (err) { throw err; }
62+
63+
process.chdir(directory);
64+
65+
const configPath = path.join(directory, "codeclimate-eslint");
66+
fs.writeFile(configPath, "{}", function(err) {
67+
if (err) { throw err; }
68+
69+
let report = ConfigUpgrader
70+
.upgradeInstructions("codeclimate-eslint", [directory + '/file.js'], directory);
71+
expect(report).to.deep.eq([
72+
"codeclimate-eslint appears to be incompatible with ESLint 3.",
73+
"To upgrade it do the following:\n",
74+
"* Add .yml or .json to the config file name. Extension-less config file names are deprecated."
75+
]);
76+
done();
77+
});
78+
});
79+
});
5880
});
5981

6082

0 commit comments

Comments
 (0)