Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add javascriptlint, add test for it

  • Loading branch information...
commit ea2d23576e63414629926e81994b600063e88ff9 1 parent da3d8eb
@kof authored
View
2  Makefile
@@ -1,5 +1,5 @@
test:
- node bin/test.js
+ qunit -c lib/linter.js -t test/test.js --cov false
lint:
linter -f lib -r
View
10 bin/test.js
@@ -1,10 +0,0 @@
-var join = require('path').join,
- root = join(__dirname, '..'),
- qunit = require('qunit');
-
-//qunit.options.coverage = false;
-
-qunit.run({
- code: join(root, 'lib', 'linter.js'),
- tests: join(root, 'test', 'test.js')
-});
View
28 lib/linter.js
@@ -1,7 +1,11 @@
var fs = require('fs'),
vm = require('vm'),
+ $ = require('sharedjs'),
join = require('path').join;
+var start;
+
+
/**
* Runner options object
* @type {Object}
@@ -12,16 +16,11 @@ exports.options = {
// use server.json if no config passed
config: 'server',
confRoot: join(__dirname, '..', 'conf'),
+ linterRoot: __dirname + '/linter',
callback: function() {},
recursive: false,
// format errors report for console
- format: false,
- linter: {
- jslint: join(__dirname, 'linter', 'jslint'),
- 'closure-linter': join(__dirname, 'linter', 'closure-linter'),
- 'closure-compiler': join(__dirname, 'linter', 'closure-compiler'),
- javascriptlint: join(__dirname, 'linter', 'javascriptlint')
- }
+ format: false
};
function readdir(path, r) {
@@ -55,8 +54,6 @@ function colorize(str, color) {
return '\x1B[' + colors[color] + 'm' + str + '\x1B[0m';
}
-var start;
-
/**
* Format errors for console output.
* @param {Array} errors report.
@@ -91,6 +88,7 @@ function format(errors) {
return arr.join('\n');
}
+
/**
* Run all checker
* @param {Object} opts runner options: files, config, callback.
@@ -98,19 +96,13 @@ function format(errors) {
*/
exports.run = function(opts) {
start = Date.now();
- var o = exports.options,
+ var o = $.extend({}, exports.options, opts),
sandbox = {},
errors = [],
done = 0,
linters = 0,
key;
- if (opts) {
- for (key in opts) {
- o[key] = opts[key];
- }
- }
-
if (typeof o.files === 'string') {
if (fs.statSync(o.files).isDirectory()) {
o.files = readdir(o.files, o.recursive);
@@ -132,10 +124,10 @@ exports.run = function(opts) {
}
Object.keys(o.config).forEach(function(linter) {
+ var linterPath = o.linterRoot + '/' + linter;
linters++;
o.files.forEach(function(file) {
- require(o.linter[linter])(file, o.config[linter], function(err) {
-
+ require(linterPath)(file, o.config[linter], function(err) {
if (err && err.length > 0) {
errors = errors.concat(err);
}
View
16 lib/linter/closure-compiler.js
@@ -20,7 +20,15 @@ module.exports = function(file, options, callback) {
var linter,
errors = [],
name,
- opts = [];
+ opts;
+
+
+ opts = [
+ '-jar', join(root, 'deps', 'closure-compiler', 'compiler.jar'),
+ '--js', file,
+ '--js_output_file', '/dev/null'
+ ];
+
// convert options to args
for (name in options) {
@@ -32,12 +40,6 @@ module.exports = function(file, options, callback) {
}
}
- opts.unshift(
- '-jar', join(root, 'deps', 'closure-compiler', 'compiler.jar'),
- '--js', file,
- '--js_output_file', '/dev/null'
- );
-
linter = spawn('java', opts);
linter.stderr.on('data', function(data) {
View
50 lib/linter/javascriptlint.js
@@ -20,9 +20,14 @@ module.exports = function(file, options, callback) {
var linter,
errors = [],
name,
- opts = [];
+ opts;
- // convert options to args. this may not act like other linters. oops?
+ opts = [
+ '-process', file,
+ '-nologo'
+ ];
+
+ // convert options to opts. this may not act like other linters. oops?
for (name in options) {
if (options[name]) {
opts.push('-' + name);
@@ -32,18 +37,10 @@ module.exports = function(file, options, callback) {
}
}
- opts.unshift(
- '-process', file,
- '-nologo'
- // this can be supplied if people want it
- // '-conf', ''
- );
-
linter = spawn(root + '/deps/javascriptlint/jsl', opts);
-
- linter.stdout.on('data', function (data) {
+
+ linter.stdout.on('data', function(data) {
data = data.toString().split(file);
- // console.log(data);
data.forEach(function(str, i) {
if (i === 0) {
@@ -51,20 +48,24 @@ module.exports = function(file, options, callback) {
// filename = str.replace('\n', '');
return;
}
+
if (i === data.length - 1) {
// of form
- // `(22): lint warning: undeclared identifier: z\n\n0 error(s), 11 warning(s)\n`
+ // `(22): lint warning: undeclared identifier:
+ // z\n\n0 error(s), 11 warning(s)\n`
// gotta remove the ending.
str = str.substring(0, str.indexOf('\n\n') + 5);
return;
}
-
- var line = /(\d+)/.exec(str)[0];
-
- // handle both cases. This matters b/c a warning is more severe than a lint warning.
+
+ var line = /(\d+)/.exec(str)[0],
+ msg;
+
+ // handle both cases. This matters b/c a warning
+ // is more severe than a lint warning.
// (9): warning: test for equality (==) mistyped as assignmen ...
// (23): lint warning: missing default case in ...
- var msg = str.substring(str.indexOf(':') + 2, str.length);
+ msg = str.substring(str.indexOf(':') + 2, str.length);
errors.push({
linter: 'javascriptlint',
file: file,
@@ -72,20 +73,9 @@ module.exports = function(file, options, callback) {
line: line || 'no line found'
});
});
-
});
-
+
linter.on('exit', function(code) {
- // console.log('javascriptlint done linting.');
callback(errors);
});
};
-
-// testing
-
-// var debug = true;
-// debug && function() {
-// module.exports(root + '/deps/javascriptlint/jsl-test.js', {}, function(errors) {
-// console.log(errors);
-// });
-// }();
View
5 package.json
@@ -1,7 +1,7 @@
{
"name": "linter",
"description": "Code quality tools collection in one module",
- "version": "0.0.5",
+ "version": "0.0.6",
"author": "Oleg Slobodskoi <oleg008@gmail.com>",
"repository": {
"type": "git",
@@ -13,7 +13,8 @@
"main": "./lib/linter.js",
"engines": { "node": ">= 0.3.6" },
"dependencies": {
- "argsparser": ">= 0.0.3"
+ "argsparser": ">= 0.0.3",
+ "sharedjs": ">= 0.0.1"
},
"scripts": {
"install": "make install"
View
4 readme.md
@@ -93,7 +93,7 @@ Errors array passed to callback contains objects in this format:
## License
-- node-linter - MIT
+- node-linter - MIT Style
- jslint - read `./deps/JSLint/fulljslint.js`
- closure-linter and closure-compiler - Apache License, Version 2.0
-- javascript lint - ?
+- javascript lint - GNU GENERAL PUBLIC LICENSE Version 2 http://javascriptlint.svn.sourceforge.net/viewvc/javascriptlint/trunk/COPYING
View
1  test/fixtures/javascriptlint.js
@@ -0,0 +1 @@
+var x = x+++y;
View
3  test/fixtures/javascriptlint.json
@@ -0,0 +1,3 @@
+{
+ "javascriptlint": {}
+}
View
20 test/test.js
@@ -5,10 +5,6 @@ var root = join(__dirname, '..'),
lib = join(root, 'lib'),
fixtures = join(root, 'test', 'fixtures');
-options.linter.jslint = join(lib, 'linter', 'jslint');
-options.linter['closure-linter'] = join(lib, 'linter', 'closure-linter');
-options.linter['closure-compiler'] = join(lib, 'linter', 'closure-compiler');
-
test('jslint', 1, function() {
stop();
run({
@@ -45,13 +41,25 @@ test('closure-compiler', 1, function() {
});
});
+test('javascriptlint', 1, function() {
+ stop();
+ run({
+ files: join(fixtures, 'javascriptlint.js'),
+ config: join(fixtures, 'javascriptlint.json'),
+ callback: function(err) {
+ equal(err.length, 1, 'errors count');
+ start();
+ }
+ });
+});
+
test('dir lint with all linters', 1, function() {
stop();
run({
files: fixtures,
config: join(root, 'conf', 'server.json'),
callback: function(err) {
- equal(err.length, 11, 'errors count');
+ equal(err.length, 18, 'errors count');
start();
}
});
@@ -64,7 +72,7 @@ test('dir lint with all linters recursively', 1, function() {
config: join(root, 'conf', 'server.json'),
recursive: true,
callback: function(err) {
- equal(err.length, 13, 'errors count');
+ equal(err.length, 20, 'errors count');
start();
}
});
Please sign in to comment.
Something went wrong with that request. Please try again.