Skip to content

Commit

Permalink
Merge pull request eslint#81 from xjamundx/multi-formatters
Browse files Browse the repository at this point in the history
Formatters can now process multiple files at once
  • Loading branch information
nzakas committed Jul 18, 2013
2 parents 2b4a7d5 + 45f5e34 commit 9637601
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 46 deletions.
42 changes: 28 additions & 14 deletions lib/cli.js
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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) {

Expand All @@ -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;

}

//------------------------------------------------------------------------------
Expand Down
22 changes: 15 additions & 7 deletions lib/formatters/compact.js
Expand Up @@ -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;
};
87 changes: 62 additions & 25 deletions tests/lib/formatters/compact.js
Expand Up @@ -17,21 +17,24 @@ 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) {
var config = {
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);
},

Expand All @@ -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);
}

Expand All @@ -49,44 +52,78 @@ 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) {
var config = {
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);

0 comments on commit 9637601

Please sign in to comment.