Skip to content

Commit b726324

Browse files
committed
Merge pull request #73 from codeclimate/gd-no-config
Error if there is not a valid configuration file
2 parents 7a6f4d6 + 506ef50 commit b726324

File tree

5 files changed

+76
-4
lines changed

5 files changed

+76
-4
lines changed

bin/eslint.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ var options = { extensions: [".js"], ignore: true, reset: false, useEslintrc: tr
1515
var cli = new CLIEngine(options);
1616
var debug = false;
1717
var checks = require("../lib/checks");
18-
19-
console.error("ESLint is running with the " + cli.getConfigForFile(null).parser + " parser.");
18+
var validateConfig = require("../lib/validate_config");
2019

2120
// a wrapper for emitting perf timing
2221
function runWithTiming(name, fn) {
@@ -232,4 +231,12 @@ function analyzeFiles() {
232231
}
233232
}
234233

235-
analyzeFiles();
234+
if (validateConfig(options.configFile)) {
235+
console.error("ESLint is running with the " + cli.getConfigForFile(null).parser + " parser.");
236+
237+
analyzeFiles();
238+
} else {
239+
console.error("No rules are configured. Make sure you have added a config file with rules enabled.");
240+
console.error("See our documentation at https://docs.codeclimate.com/docs/eslint for more information.");
241+
process.exit(1);
242+
}

lib/validate_config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var CLIEngine = require("eslint").CLIEngine
2+
, cli = new CLIEngine()
3+
, fs = require("fs");
4+
5+
module.exports = function(configPath) {
6+
if (configPath) {
7+
return true;
8+
} else {
9+
var config = cli.getConfigForFile(null);
10+
11+
return Object.keys(config.rules).length > 0;
12+
}
13+
};

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
},
1818
"devDependencies": {
1919
"chai": "^2.1.0",
20-
"mocha": "^2.3.4"
20+
"mocha": "^2.3.4",
21+
"temp": "^0.8.3"
2122
},
2223
"scripts": {
2324
"test": "mocha test"

test/fixtures/config/.eslintrc.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
"rules": {
3+
"strict": 0
4+
}
5+
};

test/validate_config_test.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
var expect = require("chai").expect
2+
, fs = require("fs")
3+
, path = require("path")
4+
, temp = require('temp')
5+
, validateConfig = require("../lib/validate_config");
6+
7+
temp.track();
8+
9+
describe("validateConfig", function() {
10+
it("returns true if given a file", function() {
11+
expect(validateConfig("foo.config")).to.eq(true);
12+
});
13+
14+
it("returns false if no files exist", function(done) {
15+
temp.mkdir("no-config", function(err, directory) {
16+
if (err) throw err;
17+
18+
process.chdir(directory);
19+
20+
expect(validateConfig(null)).to.eq(false);
21+
done();
22+
});
23+
});
24+
25+
it("returns true if an eslintrc exists", function(done) {
26+
temp.mkdir("config", function(err, directory) {
27+
if (err) throw err;
28+
29+
process.chdir(directory);
30+
31+
var configPath = path.join(directory, ".eslintrc.json");
32+
var config = {
33+
rules: {
34+
strict: 0
35+
}
36+
};
37+
38+
fs.writeFile(configPath, JSON.stringify(config), function(err) {
39+
if (err) throw err;
40+
41+
expect(validateConfig(null)).to.eq(true);
42+
done();
43+
});
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)