diff --git a/lib/cli.js b/lib/cli.js index 7c44579020d..af0a8746ad2 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -23,6 +23,8 @@ var DEFAULT_CONFIG = "../conf/eslint.json"; // Helpers //------------------------------------------------------------------------------ +var results = []; + function readConfig(options) { var configLocation = path.resolve(__dirname, options.c || options.config || DEFAULT_CONFIG); @@ -68,14 +70,28 @@ function getFiles(dir){ return files; } +function storeResults(filename, messages) { + results.push({filePath: filename, messages: messages}); +} + +function printResults(config) { + var formatter; + try { + formatter = require("./formatters/" + config.format); + } catch (ex) { + console.log("Could not find formatter '%s'.", config.format); + process.exit(1); + } + console.log(formatter(results, config)); +} + /** * Processes an individual file using ESLint. * @param {string} filename The filename of the file being checked. * @param {Object} config The configuration object for ESLint. - * @param {Function} formatter The formatter to use to output results. * @returns {int} The total number of errors. */ -function processFile(filename, config, formatter) { +function processFile(filename, config) { // clear all existing settings for a new file eslint.reset(); @@ -92,7 +108,8 @@ function processFile(filename, config, formatter) { process.exit(1); } - console.log(formatter(messages, filename, config)); + // save the results for printing later + storeResults(filename, messages); // count all errors and return the total return messages.reduce(function(previous, message) { @@ -113,15 +130,7 @@ function processFile(filename, config, formatter) { function processFiles(files, config) { var fullFileList = [], - formatter; - - // just in case an incorrect formatter was passed in - try { - formatter = require("./formatters/" + config.format); - } catch (ex) { - console.log("Could not find formatter '%s'.", config.format); - process.exit(1); - } + errors = 0; files.forEach(function(file) { @@ -132,9 +141,14 @@ function processFiles(files, config) { } }); - return fullFileList.reduce(function(previous, file) { - return previous + processFile(file, config, formatter); + errors = fullFileList.reduce(function(previous, file) { + return previous + processFile(file, config); }, 0); + + printResults(config); + + return errors; + } //------------------------------------------------------------------------------ diff --git a/lib/formatters/compact.js b/lib/formatters/compact.js index fb026d37cd8..16d82b18417 100644 --- a/lib/formatters/compact.js +++ b/lib/formatters/compact.js @@ -22,20 +22,28 @@ function getMessageType(message, rules) { // Public Interface //------------------------------------------------------------------------------ -module.exports = function(messages, filename, config) { +module.exports = function(results, config) { var output = "", + total = 0, rules = config.rules || {}; - messages.forEach(function(message) { + results.forEach(function(result) { + + var messages = result.messages; + total += messages.length; + + messages.forEach(function(message) { + + output += result.filePath + ": "; + output += "line " + message.line + ", col " + + message.column + ", " + getMessageType(message, rules); + output += " - " + message.message + "\n"; + }); - output += filename + ": "; - output += "line " + message.line + ", col " + - message.column + ", " + getMessageType(message, rules); - output += " - " + message.message + "\n"; }); - output += "\n" + messages.length + " problems"; + output += "\n" + total + " problems"; return output; }; diff --git a/tests/lib/formatters/compact.js b/tests/lib/formatters/compact.js index 11436ef088e..600ab6e32cd 100644 --- a/tests/lib/formatters/compact.js +++ b/tests/lib/formatters/compact.js @@ -17,13 +17,16 @@ var vows = require("vows"), vows.describe("formatter:compact").addBatch({ - "when passed a message": { + "when passed a single message": { topic: [{ - message: "Unexpected foo.", - line: 5, - column: 10, - ruleId: "foo" + filePath: "foo.js", + messages: [{ + message: "Unexpected foo.", + line: 5, + column: 10, + ruleId: "foo" + }] }], "should return a string in the format filename: line x, col y, Error - z for errors": function(topic) { @@ -31,7 +34,7 @@ vows.describe("formatter:compact").addBatch({ rules: { foo: 2 } }; - var result = formatter(topic, "foo.js", config); + var result = formatter(topic, config); assert.equal("foo.js: line 5, col 10, Error - Unexpected foo.\n\n1 problems", result); }, @@ -40,7 +43,7 @@ vows.describe("formatter:compact").addBatch({ rules: { foo: 1 } }; - var result = formatter(topic, "foo.js", config); + var result = formatter(topic, config); assert.equal("foo.js: line 5, col 10, Warning - Unexpected foo.\n\n1 problems", result); } @@ -49,33 +52,38 @@ vows.describe("formatter:compact").addBatch({ "when passed a fatal error message": { topic: [{ - fatal: true, - message: "Unexpected foo.", - line: 5, - column: 10, - ruleId: "foo" + filePath: "foo.js", + messages: [{ + fatal: true, + message: "Unexpected foo.", + line: 5, + column: 10, + ruleId: "foo" + }] }], "should return a string in the format filename: line x, col y, Error - z": function(topic) { var config = {}; // doesn't matter what's in the config for this test - var result = formatter(topic, "foo.js", config); + var result = formatter(topic, config); assert.equal("foo.js: line 5, col 10, Error - Unexpected foo.\n\n1 problems", result); } }, "when passed multiple messages": { topic: [{ - message: "Unexpected foo.", - line: 5, - column: 10, - ruleId: "foo" - }, { - message: "Unexpected bar.", - line: 6, - column: 11, - ruleId: "bar" - + filePath: "foo.js", + messages: [{ + message: "Unexpected foo.", + line: 5, + column: 10, + ruleId: "foo" + }, { + message: "Unexpected bar.", + line: 6, + column: 11, + ruleId: "bar" + }] }], "should return a string with multiple entries": function(topic) { @@ -83,10 +91,39 @@ vows.describe("formatter:compact").addBatch({ rules: { foo: 2, bar: 1 } }; - var result = formatter(topic, "foo.js", config); + var result = formatter(topic, config); assert.equal("foo.js: line 5, col 10, Error - Unexpected foo.\nfoo.js: line 6, col 11, Warning - Unexpected bar.\n\n2 problems", result); } + }, + + "when passed multiple files with 1 message each": { + topic: [{ + filePath: "foo.js", + messages: [{ + message: "Unexpected foo.", + line: 5, + column: 10, + ruleId: "foo" + }] + }, { + filePath: "bar.js", + messages: [{ + message: "Unexpected bar.", + line: 6, + column: 11, + ruleId: "bar" + }] + }], + + "should return a string with multiple entries": function(topic) { + var config = { + rules: { foo: 2, bar: 1 } + }; + + var result = formatter(topic, config); + assert.equal("foo.js: line 5, col 10, Error - Unexpected foo.\nbar.js: line 6, col 11, Warning - Unexpected bar.\n\n2 problems", result); + } } -}).export(module); + }).export(module);