From 506ef50aab5ecec8e9040b8c8593952cd4e9f50c Mon Sep 17 00:00:00 2001 From: Gordon Diggs Date: Fri, 12 Feb 2016 10:47:52 -0500 Subject: [PATCH] Error if there is not a valid configuration file If there is no eslintrc.* file, ESLint's behavior is to do nothing (and therefore emit no issues). Because this can be confusing, we should fail hard and show helpful information. --- bin/eslint.js | 13 +++++++-- lib/validate_config.js | 13 +++++++++ package.json | 3 +- test/fixtures/config/.eslintrc.js | 5 ++++ test/validate_config_test.js | 46 +++++++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 lib/validate_config.js create mode 100644 test/fixtures/config/.eslintrc.js create mode 100644 test/validate_config_test.js diff --git a/bin/eslint.js b/bin/eslint.js index 1bf309ef8..7221225eb 100755 --- a/bin/eslint.js +++ b/bin/eslint.js @@ -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) { @@ -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); +} diff --git a/lib/validate_config.js b/lib/validate_config.js new file mode 100644 index 000000000..91de99c7d --- /dev/null +++ b/lib/validate_config.js @@ -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; + } +}; diff --git a/package.json b/package.json index b1eaa44bf..6fd3de853 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/test/fixtures/config/.eslintrc.js b/test/fixtures/config/.eslintrc.js new file mode 100644 index 000000000..3eef553b4 --- /dev/null +++ b/test/fixtures/config/.eslintrc.js @@ -0,0 +1,5 @@ +module.exports = { + "rules": { + "strict": 0 + } +}; diff --git a/test/validate_config_test.js b/test/validate_config_test.js new file mode 100644 index 000000000..1d7b5bc8f --- /dev/null +++ b/test/validate_config_test.js @@ -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(); + }); + }); + }); +});