Skip to content

Commit

Permalink
Merge pull request #73 from codeclimate/gd-no-config
Browse files Browse the repository at this point in the history
Error if there is not a valid configuration file
  • Loading branch information
gdiggs committed Feb 12, 2016
2 parents 7a6f4d6 + 506ef50 commit b726324
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 4 deletions.
13 changes: 10 additions & 3 deletions bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ var options = { extensions: [".js"], ignore: true, reset: false, useEslintrc: tr
var cli = new CLIEngine(options);
var debug = false;
var checks = require("../lib/checks");

console.error("ESLint is running with the " + cli.getConfigForFile(null).parser + " parser.");
var validateConfig = require("../lib/validate_config");

// a wrapper for emitting perf timing
function runWithTiming(name, fn) {
Expand Down Expand Up @@ -232,4 +231,12 @@ function analyzeFiles() {
}
}

analyzeFiles();
if (validateConfig(options.configFile)) {
console.error("ESLint is running with the " + cli.getConfigForFile(null).parser + " parser.");

analyzeFiles();
} else {
console.error("No rules are configured. Make sure you have added a config file with rules enabled.");
console.error("See our documentation at https://docs.codeclimate.com/docs/eslint for more information.");
process.exit(1);
}
13 changes: 13 additions & 0 deletions lib/validate_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var CLIEngine = require("eslint").CLIEngine
, cli = new CLIEngine()
, fs = require("fs");

module.exports = function(configPath) {
if (configPath) {
return true;
} else {
var config = cli.getConfigForFile(null);

return Object.keys(config.rules).length > 0;
}
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"devDependencies": {
"chai": "^2.1.0",
"mocha": "^2.3.4"
"mocha": "^2.3.4",
"temp": "^0.8.3"
},
"scripts": {
"test": "mocha test"
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/config/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
"rules": {
"strict": 0
}
};
46 changes: 46 additions & 0 deletions test/validate_config_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var expect = require("chai").expect
, fs = require("fs")
, path = require("path")
, temp = require('temp')
, validateConfig = require("../lib/validate_config");

temp.track();

describe("validateConfig", function() {
it("returns true if given a file", function() {
expect(validateConfig("foo.config")).to.eq(true);
});

it("returns false if no files exist", function(done) {
temp.mkdir("no-config", function(err, directory) {
if (err) throw err;

process.chdir(directory);

expect(validateConfig(null)).to.eq(false);
done();
});
});

it("returns true if an eslintrc exists", function(done) {
temp.mkdir("config", function(err, directory) {
if (err) throw err;

process.chdir(directory);

var configPath = path.join(directory, ".eslintrc.json");
var config = {
rules: {
strict: 0
}
};

fs.writeFile(configPath, JSON.stringify(config), function(err) {
if (err) throw err;

expect(validateConfig(null)).to.eq(true);
done();
});
});
});
});

0 comments on commit b726324

Please sign in to comment.