Permalink
Browse files

Refactoring for scalability

  • Loading branch information...
arthurakay committed Dec 18, 2012
1 parent dd53fcd commit 52fa4e457261e7276fe0e6b51d4728f42301ccdd
Showing with 224 additions and 179 deletions.
  1. +6 −0 CHANGELOG
  2. +11 −8 examples/init.js
  3. +8 −2 examples/replaceTabsWithSpaces.js
  4. +5 −6 hooks/pre-commit.js
  5. +1 −1 package.json
  6. +20 −145 src/LintRoller.js
  7. +82 −0 src/jshint.js
  8. +90 −0 src/jslint.js
  9. +1 −17 src/util.js
View
@@ -2,6 +2,12 @@
VERSION 2.x
===============
Version 2.2.0 (December 17, 2012) - BREAKING CHANGES
- Changed configuration of linters - now using "linters" array
- Broke supported linters into specific modules
- Updated examples
Version 2.1.0 (December 14, 2012) - BREAKING CHANGES
- Moved code into /src/ folder
- Added "util" namespace and utilities
View
@@ -6,23 +6,26 @@ var config = {
logFile : './error.log',
//recursively include JS files in these folders
filepaths : [
filepaths : [
'./'
],
//but ignore anything in these folders
exclusions : [
exclusions : [
'./node_modules/',
'./assets/',
'./docs/'
],
jsLint : {
optons : {}
},
//optionally disable a linter
jsHint : false
linters : [
{
type : 'jsLint'
},
{
type : 'jsHint',
options : {}
}
]
};
LintRoller.init(config);
@@ -1,8 +1,8 @@
var LintRoller = require('../src/LintRoller.js');
var config = {
verbose : false,
logFile : './error.log',
verbose : false,
logFile : './error.log',
//recursively include JS files in these folders
filepaths : [
@@ -14,6 +14,12 @@ var config = {
'../node_modules/',
'../assets/',
'../docs/'
],
linters : [
{
type : 'jsLint'
}
]
};
View
@@ -16,12 +16,11 @@ var config = {
'../docs/'
],
jsLint : {
optons : {}
},
//optionally disable a linter
jsHint : false
linters : [
{
type : 'jsLint'
}
]
};
try {
View
@@ -1,7 +1,7 @@
{
"name" : "lintroller",
"preferGlobal" : "true",
"version" : "2.1.0",
"version" : "2.2.0",
"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.",
View
@@ -53,40 +53,9 @@ LintRoller = {
/**
* @cfg
* An object containing an "options" property (False to disable usage.).
*
* - "options" is an object containing the optional lint flags.
* An array of lint module config objects
*/
jsLint : {
lib : null,
options : {
nomen : true, //if names may have dangling _
plusplus : true, //if increment/decrement should be allowed
sloppy : true, //if the 'use strict'; pragma is optional
vars : true, //if multiple var statements per function should be allowed
white : true, //if sloppy whitespace is tolerated
undef : true, //if variables can be declared out of order,
node : true, //if Node.js globals should be predefined
browser : true, //if the standard browser globals should be predefined
stupid : true //if really stupid practices are tolerated... namely blocking synchronous operations
}
},
/**
* @cfg
* An object containing an "options" property (False to disable usage.).
*
* - "options" is an object containing the optional lint flags.
*
*/
jsHint : {
lib : null,
options : {
}
},
linters : [],
/**
* @cfg
@@ -115,11 +84,6 @@ LintRoller = {
*/
files : [],
/**
* @private
*/
linters : [],
/**
* @private
*/
@@ -133,14 +97,8 @@ LintRoller = {
for (i in config) {
if (config.hasOwnProperty(i)) {
switch (i) {
case 'jsLint':
case 'jsHint':
if (typeof config[i] !== 'boolean') {
this.applyLintOptions(this[i], config.options);
break;
}
this[i] = config[i];
case 'linters':
this.setLinters(config[i]);
break;
default:
@@ -150,46 +108,32 @@ LintRoller = {
}
}
this.setLinters();
},
/**
* @private
*/
setLinters : function () {
if (this.jsLint) {
this.log('Loading JSLint... ', true);
this.linters.push(this.jsLint.lib);
setLinters : function (linters) {
if (!(linters instanceof Array) || linters.length === 0) {
process.exit(1);
}
if (this.jsHint) {
this.log('Loading JSHint... ', true);
this.linters.push(this.jsHint.lib);
}
var i = 0,
linter, linterCfg;
if (this.linters.length === 0) {
process.exit(1);
}
},
for (i; i<linters.length; i++) {
linterCfg = linters[i];
/**
* @private
*/
applyLintOptions : function (linter, options) {
var i;
this.log('Initializing linter: ' + linterCfg.type, true);
if (!options) {
return false;
}
linter = require('./' + linterCfg.type);
linter.applyLintOptions(linterCfg.options);
for (i in options) {
if (options.hasOwnProperty(i)) {
linter.options[i] = options[i];
}
this.linters.push(linter);
}
},
/**
* @private
*/
@@ -306,8 +250,7 @@ LintRoller = {
*/
lintFiles : function () {
var x = 0,
jsLintErrors = [],
jsHintErrors = [],
newErrors = [],
errorList = [],
errors = 0,
j,
@@ -321,77 +264,17 @@ LintRoller = {
for (x; x < this.linters.length; x++) {
linter = this.linters[x];
if (linter === this.jsLint.lib) {
this.log('Running JSLint against code...', false);
jsLintErrors = this.runLinter(this.jsLint);
newErrors = linter.runLinter(this);
errors += newErrors.length - 1; //ignore the first record, which is a title
errors += jsLintErrors.length;
jsLintErrors.splice(0, 0, '=============== Running JSLint... ===============\n\n');
}
else if (linter === this.jsHint.lib) {
this.log('Running JSHint against code...', false);
jsHintErrors = this.runLinter(this.jsHint);
errors += jsHintErrors.length;
jsHintErrors.splice(0, 0, '=============== Running JSHint... ===============\n\n');
}
errorList = errorList.concat(newErrors);
}
if (errors > 0) {
errorList = errorList.concat(jsLintErrors, jsHintErrors);
this.announceErrors(errorList);
}
},
/**
* @private
*/
runLinter : function (linter) {
var j = 0,
errorList = [],
file, js;
for (j; j < this.files.length; j++) {
file = this.files[j];
js = this.fs.readFileSync(file, 'utf8');
var i = 0,
result = linter.lib(js, linter.options),
totalErrors = linter.lib.errors.length,
error;
if (!result) {
for (i; i < totalErrors; i++) {
error = linter.lib.errors[i];
if (error) {
errorList.push(
file,
' Line #: ' + error.line,
' Char #: ' + error.character,
' Reason: ' + error.reason,
'',
''
);
if (this.stopOnFirstError) {
break;
}
}
}
if (this.stopOnFirstError && errorList.length > 0) {
this.announceErrors(errorList);
}
}
}
return errorList;
},
/**
* @private
*/
@@ -432,14 +315,6 @@ 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);
Oops, something went wrong.

0 comments on commit 52fa4e4

Please sign in to comment.