Skip to content

Error if there is not a valid configuration file #73

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This behavior is a little surprising to me: this would mean if you explicitly told us to use .eslintrc.foo as a config file, but that file didn't exist, we would still pass that as valid?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wfleming yeah - if you've specified a specific config file this check would pass, but a later one would error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Makes sense.

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
}
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice tests!


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

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