Skip to content

Commit

Permalink
Moved code into /src/ folder.
Browse files Browse the repository at this point in the history
Moved examples into /examples/ folder.
Added util module and refactored LintRoller
  • Loading branch information
arthurakay committed Dec 14, 2012
1 parent c29441f commit 26acfcf
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 44 deletions.
2 changes: 1 addition & 1 deletion test.bat → examples/init.bat
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
@echo off @echo off


cls cls
node test.js node init.js
2 changes: 1 addition & 1 deletion test.js → examples/init.js
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
var LintRoller = require('./LintRoller.js'); var LintRoller = require('../src/LintRoller');


var config = { var config = {
verbose : false, verbose : false,
Expand Down
2 changes: 2 additions & 0 deletions examples/init.sh
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
clear
node init.js
20 changes: 20 additions & 0 deletions examples/replaceTabsWithSpaces.js
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,20 @@
var LintRoller = require('../src/LintRoller.js');

var config = {
verbose : false,
logFile : './error.log',

//recursively include JS files in these folders
filepaths : [
'../'
],

//but ignore anything in these folders
exclusions : [
'../node_modules/',
'../assets/',
'../docs/'
]
};

LintRoller.util.replaceTabsWithSpaces(config, 4);
2 changes: 1 addition & 1 deletion hooks/pre-commit.js
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,4 @@
var LintRoller = require('../lintroller'); var LintRoller = require('../src/lintroller');


var config = { var config = {
verbose : false, verbose : false,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
{ {
"name" : "lintroller", "name" : "lintroller",
"preferGlobal" : "true", "preferGlobal" : "true",
"version" : "2.0.0", "version" : "2.1.0",


"author" : "Arthur Kay <art@akawebdesign.com>", "author" : "Arthur Kay <art@akawebdesign.com>",
"description" : "Lint your JavaScript code and output errors to a log file. Convenient for pre-commit hooks and build systems to maintain code quality.", "description" : "Lint your JavaScript code and output errors to a log file. Convenient for pre-commit hooks and build systems to maintain code quality.",
Expand All @@ -17,7 +17,7 @@


}, },


"main" : "./LintRoller.js", "main" : "./src/LintRoller.js",


"repository" : { "repository" : {
"type" : "git", "type" : "git",
Expand Down
101 changes: 64 additions & 37 deletions LintRoller.js → src/LintRoller.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@
THE SOFTWARE. THE SOFTWARE.
*/ */


var fs = require('fs'),
JSLINT = require('jslint'),
JSHINT = require('jshint').JSHINT;

/** /**
* @class LintRoller * @class LintRoller
* @author Arthur Kay (http://www.akawebdesign.com) * @author Arthur Kay (http://www.akawebdesign.com)
* @singleton * @singleton
* @version 2.0.0 * @version 2.0.0
* *
* GitHub Project: https://github.com/arthurakay/PhantomLint * GitHub Project: https://github.com/arthurakay/LintRoller
*/ */
LintRoller = { LintRoller = {
/** /**
Expand Down Expand Up @@ -62,6 +58,8 @@ LintRoller = {
* - "options" is an object containing the optional lint flags. * - "options" is an object containing the optional lint flags.
*/ */
jsLint : { jsLint : {
lib : null,

options : { options : {
nomen : true, //if names may have dangling _ nomen : true, //if names may have dangling _
plusplus : true, //if increment/decrement should be allowed plusplus : true, //if increment/decrement should be allowed
Expand All @@ -83,6 +81,8 @@ LintRoller = {
* *
*/ */
jsHint : { jsHint : {
lib : null,

options : { options : {


} }
Expand All @@ -101,20 +101,6 @@ LintRoller = {
//APPLY CONFIG OPTIONS //APPLY CONFIG OPTIONS
this.initConfigs(config); this.initConfigs(config);


if (this.jsLint) {
this.log('Loading JSLint... ', true);
this.linters.push(JSLINT);
}

if (this.jsHint) {
this.log('Loading JSHint... ', true);
this.linters.push(JSHINT);
}

if (!JSLINT && !JSHINT) {
process.exit(1);
}

this.parseTree(config.filepaths); this.parseTree(config.filepaths);
this.log('\nFilesystem has been parsed. Looping through available files...'); this.log('\nFilesystem has been parsed. Looping through available files...');


Expand Down Expand Up @@ -164,6 +150,27 @@ LintRoller = {


} }
} }

this.setLinters();
},

/**
* @private
*/
setLinters : function () {
if (this.jsLint) {
this.log('Loading JSLint... ', true);
this.linters.push(this.jsLint.lib);
}

if (this.jsHint) {
this.log('Loading JSHint... ', true);
this.linters.push(this.jsHint.lib);
}

if (this.linters.length === 0) {
process.exit(1);
}
}, },


/** /**
Expand Down Expand Up @@ -207,7 +214,7 @@ LintRoller = {
* @private * @private
*/ */
getFiles : function (path) { getFiles : function (path) {
var tree = fs.readdirSync(path); var tree = this.fs.readdirSync(path);


this.log('\nFILES FOUND AT PATH: ' + path); this.log('\nFILES FOUND AT PATH: ' + path);
this.log(tree); this.log(tree);
Expand Down Expand Up @@ -258,7 +265,7 @@ LintRoller = {
var spacer = ' ', var spacer = ' ',
childPath, childTree; childPath, childTree;


var stats = fs.statSync(currPath + list[x]); var stats = this.fs.statSync(currPath + list[x]);


if (stats.isFile()) { if (stats.isFile()) {
this.log(spacer + list[x] + ' IS A FILE'); this.log(spacer + list[x] + ' IS A FILE');
Expand Down Expand Up @@ -314,19 +321,19 @@ LintRoller = {
for (x; x < this.linters.length; x++) { for (x; x < this.linters.length; x++) {
linter = this.linters[x]; linter = this.linters[x];


if (linter === JSLINT) { if (linter === this.jsLint.lib) {
this.log('Running JSLint against code...', false); this.log('Running JSLint against code...', false);
jsLintErrors = this.runJSLint(); jsLintErrors = this.runJSLint();


errors += jsLintErrors.length; errors += jsLintErrors.length;
jsLintErrors.splice(0,0, '=============== Running JSLint... ===============\n\n'); jsLintErrors.splice(0, 0, '=============== Running JSLint... ===============\n\n');
} }
else if (linter === JSHINT) { else if (linter === this.jsHint.lib) {
this.log('Running JSHint against code...', false); this.log('Running JSHint against code...', false);
jsHintErrors = this.runJSHint(); jsHintErrors = this.runJSHint();


errors += jsHintErrors.length; errors += jsHintErrors.length;
jsHintErrors.splice(0,0, '=============== Running JSHint... ===============\n\n'); jsHintErrors.splice(0, 0, '=============== Running JSHint... ===============\n\n');
} }
} }


Expand All @@ -348,16 +355,16 @@ LintRoller = {
for (j; j < this.files.length; j++) { for (j; j < this.files.length; j++) {


file = this.files[j]; file = this.files[j];
js = fs.readFileSync(file, 'utf8'); js = this.fs.readFileSync(file, 'utf8');


var i = 0, var i = 0,
result = JSLINT(js, this.jsLint.options), result = this.jsLint.lib(js, this.jsLint.options),
totalErrors = JSLINT.errors.length, totalErrors = this.jsLint.lib.errors.length,
error; error;


if (!result) { if (!result) {
for (i; i < totalErrors; i++) { for (i; i < totalErrors; i++) {
error = JSLINT.errors[i]; error = this.jsLint.lib.errors[i];


if (error) { if (error) {
errorList.push( errorList.push(
Expand Down Expand Up @@ -395,16 +402,16 @@ LintRoller = {
for (j; j < this.files.length; j++) { for (j; j < this.files.length; j++) {


file = this.files[j]; file = this.files[j];
js = fs.readFileSync(file, 'utf8'); js = this.fs.readFileSync(file, 'utf8');


var i = 0, var i = 0,
result = JSHINT.jshint(js, this.jsHint.options), result = this.jsHint.lib.jshint(js, this.jsHint.options),
totalErrors = JSHINT.errors.length, totalErrors = this.jsHint.lib.errors.length,
error; error;


if (!result) { if (!result) {
for (i; i < totalErrors; i++) { for (i; i < totalErrors; i++) {
error = JSHINT.errors[i]; error = this.jsHint.lib.errors[i];


if (error) { if (error) {
errorList.push( errorList.push(
Expand Down Expand Up @@ -435,20 +442,20 @@ LintRoller = {
* @private * @private
*/ */
logToFile : function (errorList) { logToFile : function (errorList) {
this.log('\nWriting ' + ((errorList.length - this.linters.length ) / 6) + ' errors to new log file.', true); this.log('\nWriting ' + ((errorList.length - this.linters.length ) / 6) + ' errors to new log file.', true);


var header = 'LintRoller : Output for ' + new Date() + '\n\n'; var header = 'LintRoller : Output for ' + new Date() + '\n\n';
errorList.splice(0, 0, header); errorList.splice(0, 0, header);


var output = errorList.join().replace(/,/g, '\n'); var output = errorList.join().replace(/,/g, '\n');


fs.writeFileSync(this.logFile, output); this.fs.writeFileSync(this.logFile, output);
}, },


clearLogFile : function() { clearLogFile : function () {
try { try {
this.log('\nDeleting old log file...', true); this.log('\nDeleting old log file...', true);
fs.unlinkSync(this.logFile); this.fs.unlinkSync(this.logFile);
this.log('Done.', true); this.log('Done.', true);
} }
catch (err) { catch (err) {
Expand All @@ -464,6 +471,26 @@ LintRoller = {
console.log(msg); console.log(msg);
} }
} }

}; };


var initModules = function (me) {
//filesystem API
me.fs = require('fs');

if (me.jsLint) {
me.jsLint.lib = require('jslint');
}

if (me.jsHint) {
me.jsHint.lib = require('jshint').JSHINT;
}

//other utilities
var util = require('./util');
me.util = util.init(me);
};

initModules(LintRoller);

module.exports = LintRoller; module.exports = LintRoller;
Loading

0 comments on commit 26acfcf

Please sign in to comment.