Browse files

Refactoring for scalability

  • Loading branch information...
1 parent dd53fcd commit 52fa4e457261e7276fe0e6b51d4728f42301ccdd @arthurakay committed Dec 17, 2012
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
6 CHANGELOG
@@ -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
19 examples/init.js
@@ -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);
View
10 examples/replaceTabsWithSpaces.js
@@ -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
11 hooks/pre-commit.js
@@ -16,12 +16,11 @@ var config = {
'../docs/'
],
- jsLint : {
- optons : {}
- },
-
- //optionally disable a linter
- jsHint : false
+ linters : [
+ {
+ type : 'jsLint'
+ }
+ ]
};
try {
View
2 package.json
@@ -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
165 src/LintRoller.js
@@ -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
@@ -118,11 +87,6 @@ LintRoller = {
/**
* @private
*/
- linters : [],
-
- /**
- * @private
- */
initConfigs : function (config) {
var i;
@@ -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,80 +264,20 @@ 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
- */
logToFile : function (errorList) {
this.log('\nWriting ' + ((errorList.length - this.linters.length ) / 6) + ' errors to new log file.', true);
@@ -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);
View
82 src/jshint.js
@@ -0,0 +1,82 @@
+var JSHINT = require('jshint').JSHINT;
+
+var linter = {
+
+ lib : JSHINT,
+
+ /**
+ * @cfg
+ */
+ options : {
+
+ },
+
+ /**
+ * @private
+ */
+ applyLintOptions : function (options) {
+ var i;
+
+ if (!options) {
+ return false;
+ }
+
+ for (i in options) {
+ if (options.hasOwnProperty(i)) {
+ this.options[i] = options[i];
+ }
+ }
+ },
+
+ /**
+ * @private
+ */
+ runLinter : function (parentModule) {
+ var j = 0,
+ errorList = ['=============== Running JSHint... ===============\n\n'],
+ file, js;
+
+ parentModule.log('Running JSHint against code...', false);
+
+ for (j; j < parentModule.files.length; j++) {
+
+ file = parentModule.files[j];
+ js = parentModule.fs.readFileSync(file, 'utf8');
+
+ var i = 0,
+ result = this.lib(js, this.options),
+ totalErrors = this.lib.errors.length,
+ error;
+
+ if (!result) {
+ for (i; i < totalErrors; i++) {
+ error = this.lib.errors[i];
+
+ if (error) {
+ errorList.push(
+ file,
+ ' Line #: ' + error.line,
+ ' Char #: ' + error.character,
+ ' Reason: ' + error.reason,
+ '',
+ ''
+ );
+
+ if (parentModule.stopOnFirstError) {
+ break;
+ }
+ }
+ }
+
+ if (parentModule.stopOnFirstError && errorList.length > 0) {
+ parentModule.announceErrors(errorList);
+ }
+ }
+ }
+
+ return errorList;
+ }
+
+};
+
+module.exports = linter;
View
90 src/jslint.js
@@ -0,0 +1,90 @@
+var JSLINT = require('jslint');
+
+var linter = {
+
+ lib : JSLINT,
+
+ /**
+ * @cfg
+ */
+ 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
+ },
+
+ /**
+ * @private
+ */
+ applyLintOptions : function (options) {
+ var i;
+
+ if (!options) {
+ return false;
+ }
+
+ for (i in options) {
+ if (options.hasOwnProperty(i)) {
+ this.options[i] = options[i];
+ }
+ }
+ },
+
+ /**
+ * @private
+ */
+ runLinter : function (parentModule) {
+ var j = 0,
+ errorList = ['=============== Running JSLint... ===============\n\n'],
+ file, js;
+
+ parentModule.log('Running JSLint against code...', false);
+
+ for (j; j < parentModule.files.length; j++) {
+
+ file = parentModule.files[j];
+ js = parentModule.fs.readFileSync(file, 'utf8');
+
+ var i = 0,
+ result = this.lib(js, this.options),
+ totalErrors = this.lib.errors.length,
+ error;
+
+ if (!result) {
+ for (i; i < totalErrors; i++) {
+ error = this.lib.errors[i];
+
+ if (error) {
+ errorList.push(
+ file,
+ ' Line #: ' + error.line,
+ ' Char #: ' + error.character,
+ ' Reason: ' + error.reason,
+ '',
+ ''
+ );
+
+ if (parentModule.stopOnFirstError) {
+ break;
+ }
+ }
+ }
+
+ if (parentModule.stopOnFirstError && errorList.length > 0) {
+ parentModule.announceErrors(errorList);
+ }
+ }
+ }
+
+ return errorList;
+ }
+
+};
+
+module.exports = linter;
View
18 src/util.js
@@ -79,7 +79,7 @@ util = {
for (i = 0; i < this.parent.linters.length; i++) {
linter = this.parent.linters[i];
- newFiles = this.findLintErrors(linter, this.getLintOptions(linter), msg);
+ newFiles = this.findLintErrors(linter.lib, linter.options, msg);
offendingFiles = offendingFiles.concat(newFiles);
}
@@ -113,22 +113,6 @@ util = {
/**
* @private
*/
- getLintOptions : function (linter) {
- if (linter === this.parent.jsLint.lib) {
- return this.parent.jsLint.options;
- }
-
- if (linter === this.parent.jsHint.lib) {
- return this.parent.jsHint.options;
- }
-
- return {};
- },
-
-
- /**
- * @private
- */
findLintErrors : function (linter, options, msg) {
var j = 0,
offendingFiles = [],

0 comments on commit 52fa4e4

Please sign in to comment.