Skip to content

Commit

Permalink
Cleaned up relationship between bin/eslint, lib/cli.js, and lib/eslin…
Browse files Browse the repository at this point in the history
…t.js
  • Loading branch information
nzakas committed Jul 6, 2013
1 parent 90d7dcf commit 16585dd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
4 changes: 2 additions & 2 deletions bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
/*jshint node:true*/

var cli = require("../lib/cli");
cli.execute(Array.prototype.slice.call(process.argv, 2));

var exitCode = cli.execute(Array.prototype.slice.call(process.argv, 2));
process.exit(exitCode);
25 changes: 19 additions & 6 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ function isDirectory(name){
}
}

function isFile(name){
try {
return fs.statSync(name).isFile();
} catch (ex) {
return false;
}
}

function getFiles(dir){
var files = [];

Expand Down Expand Up @@ -72,7 +80,10 @@ function getFiles(dir){
}

/**
*
* 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) {
Expand All @@ -85,6 +96,7 @@ function processFile(filename, config, formatter) {

console.log(formatter(messages, filename, config));

// count all errors and return the total
return messages.reduce(function(previous, message) {
if (message.fatal || config.rules[message.ruleId] === 2) {
return previous + 1;
Expand All @@ -95,14 +107,17 @@ function processFile(filename, config, formatter) {
}

/**
*
* Processes all files from the command line.
* @param {string[]} files All of the filenames to process.
* @param {Object} config The configuration options for ESLint.
* @returns {int} The total number of errors.
*/
function processFiles(files, config) {

var fullFileList = [],
formatter;

// just in case an incorrect formatter was passed in
try {
formatter = require("./formatters/" + config.format);
} catch (ex) {
Expand All @@ -111,6 +126,7 @@ function processFiles(files, config) {
}

files.forEach(function(file) {

if (isDirectory(file)) {
fullFileList = fullFileList.concat(getFiles(file));
} else {
Expand All @@ -119,7 +135,6 @@ function processFiles(files, config) {
});

return fullFileList.reduce(function(previous, file) {
// TODO: Validate that file exists
return previous + processFile(file, config, formatter);
}, 0);
}
Expand Down Expand Up @@ -168,9 +183,7 @@ var cli = {
result = processFiles(files, config);

// result is the number of errors (not warnings)
if (result > 0) {
process.exit(1);
}
return result > 0 ? 1 : 0;
}

}
Expand Down
11 changes: 2 additions & 9 deletions lib/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,8 @@ module.exports = (function() {
// messages come as "Line X: Unexpected token foo", so strip off leading part
message: ex.message.substring(ex.message.indexOf(":") + 1).trim(),

// fake AST node to make life easier for reporters
node: {
loc: {
start: {
line: ex.lineNumber,
column: ex.column
}
}
}
line: ex.lineNumber,
column: ex.column
});
}

Expand Down

0 comments on commit 16585dd

Please sign in to comment.