jscheckstyle
Advanced tools
Comparing version 0.0.8 to 0.0.9
@@ -54,2 +54,31 @@ var burrito = require('burrito'); | ||
function ConfigExtractor(field) { | ||
this.regexp = new RegExp('\\@jscheckstyle\\.' + field + '=(\\d+)', 'g'); | ||
this.extract = function(comment, config) { | ||
var match = this.regexp.exec(comment); | ||
if (match) { | ||
config[field] = match[1]; | ||
} | ||
}; | ||
} | ||
var configExtractors = [ | ||
new ConfigExtractor('functionLength'), | ||
new ConfigExtractor('cyclomaticComplexity'), | ||
new ConfigExtractor('numberOfArguments') | ||
]; | ||
function getConfig(node) { | ||
var config = {}; | ||
if (node.start.comments_before) { | ||
node.start.comments_before.forEach(function(commentNode) { | ||
configExtractors.forEach(function(extractor) { | ||
extractor.extract(commentNode.value, config); | ||
}); | ||
}); | ||
} | ||
return config; | ||
} | ||
function analyse(source) { | ||
@@ -74,3 +103,4 @@ var results = []; | ||
ins: node.value[1].length, | ||
complexity: complexity(node) | ||
complexity: complexity(node), | ||
config: getConfig(node) | ||
}); | ||
@@ -77,0 +107,0 @@ } |
@@ -31,4 +31,7 @@ var rules = module.exports = []; | ||
new Checker("FunctionLength", function(result) { | ||
if (result.shortName !== 'module.exports' && result.lines > 30) { | ||
return result.shortName + " is " + result.lines + " lines long, maximum allowed is 30"; | ||
var config = result.config || {}; | ||
var functionLength = config.functionLength || 30; | ||
if (result.shortName !== 'module.exports' && result.lines > functionLength) { | ||
return result.shortName + " is " + result.lines + " lines long, maximum allowed is " + functionLength; | ||
} | ||
@@ -41,4 +44,7 @@ return null; | ||
new Checker("CyclomaticComplexity", function(result) { | ||
if (result.complexity > 10) { | ||
return result.shortName + " has a cyclomatic complexity of " + result.complexity + ", maximum should be 10"; | ||
var config = result.config || {}; | ||
var cyclomaticComplexity = config.cyclomaticComplexity || 10; | ||
if (result.complexity > cyclomaticComplexity) { | ||
return result.shortName + " has a cyclomatic complexity of " + result.complexity + ", maximum should be " + cyclomaticComplexity; | ||
} | ||
@@ -51,4 +57,7 @@ return null; | ||
new Checker("NumberOfArguments", function(result) { | ||
if (result.ins > 5) { | ||
return result.shortName + " has " + result.ins + " arguments, maximum allowed is 5"; | ||
var config = result.config || {}; | ||
var numberOfArguments = config.numberOfArguments || 5; | ||
if (result.ins > numberOfArguments) { | ||
return result.shortName + " has " + result.ins + " arguments, maximum allowed is " + numberOfArguments; | ||
} | ||
@@ -55,0 +64,0 @@ return null; |
{ | ||
"name": "jscheckstyle", | ||
"version": "0.0.8", | ||
"description": "Static analysis tool for javascript - calculates cyclomatic complexity, amongst other things", | ||
"keywords": [ | ||
"static", | ||
"analysis", | ||
"cyclomatic", | ||
"complexity" | ||
], | ||
"main": "lib/jscheckstyle", | ||
"directories": { | ||
"bin": "bin", | ||
"lib": "lib" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/nomiddlename/jscheckstyle" | ||
}, | ||
"dependencies": { | ||
"cli-table": ">=0.2.0", | ||
"burrito": ">=0.2.8", | ||
"file": ">=0.1.1" | ||
}, | ||
"devDependencies": { | ||
"vows": "0.5.13", | ||
"sandboxed-module": "0.1.3" | ||
}, | ||
"scripts": { | ||
"test": "vows --spec test/*.js" | ||
} | ||
"name": "jscheckstyle", | ||
"version": "0.0.9", | ||
"description": "Static analysis tool for javascript - calculates cyclomatic complexity, amongst other things", | ||
"keywords": [ | ||
"static", | ||
"analysis", | ||
"cyclomatic", | ||
"complexity" | ||
], | ||
"main": "lib/jscheckstyle", | ||
"directories": { | ||
"bin": "bin", | ||
"lib": "lib" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/nomiddlename/jscheckstyle" | ||
}, | ||
"dependencies": { | ||
"cli-table": ">=0.2.0", | ||
"burrito": "http://github.com/danbell/node-burrito/tarball/master", | ||
"file": ">=0.1.1" | ||
}, | ||
"devDependencies": { | ||
"vows": "0.5.13", | ||
"sandboxed-module": "0.1.3" | ||
}, | ||
"scripts": { | ||
"test": "vows --spec test/*.js" | ||
} | ||
} |
@@ -29,7 +29,19 @@ [![Build Status](https://secure.travis-ci.org/nomiddlename/jscheckstyle.png?branch=master)](http://travis-ci.org/nomiddlename/jscheckstyle) | ||
## TODO | ||
* Configurable rules (maybe you like functions that are longer or shorter) | ||
## Configuration | ||
It is possible to change the default rules by specifying different limits in comments before a function. | ||
For example: | ||
/* | ||
* @jscheckstyle.functionLength=20 | ||
* @jscheckstyle.numberOfArguments=10 | ||
* @jscheckstyle.cyclomaticComplexity=30 | ||
*/ | ||
function theFunction() { | ||
... | ||
} | ||
## History | ||
Originally I started out modifying [jsmeter](http://jsmeter.info) to run as a command-line tool. Then I started hacking away, added some tests, the command-line interface, added the rules checking and the extra output types, replaced the parser with [node-burrito](https://github.com/substack/node-burrito) and then realised there wasn't much of jsmeter left. So this tool is inspired by, and owes a huge debt to, jsmeter - but there's very little of the original code to be found (possibly only the HTML output renderer). | ||
@@ -57,3 +57,3 @@ var vows = require('vows'), | ||
'should have the correct properties': function(result) { | ||
assert.deepEqual(result, { shortName: 'cheese', lineStart: 1, lines: 3, ins: 1, complexity: 2 }); | ||
assert.deepEqual(result, { shortName: 'cheese', lineStart: 1, lines: 3, ins: 1, complexity: 2, config: {} }); | ||
} | ||
@@ -66,3 +66,3 @@ }, | ||
'should have the correct properties': function(result) { | ||
assert.deepEqual(result, { shortName: 'pants', lineStart: 4, lines: 3, ins: 0, complexity: 2 }); | ||
assert.deepEqual(result, { shortName: 'pants', lineStart: 4, lines: 3, ins: 0, complexity: 2, config: {} }); | ||
} | ||
@@ -88,4 +88,27 @@ } | ||
} | ||
}, | ||
'given a function with "functionLength" comment preceding': { | ||
topic: analyser.analyse('/* @jscheckstyle.functionLength=7 */ function func1() {}'), | ||
'first result should have a config structure with functionLength=7': function(result) { | ||
assert.equal(result[0].shortName, 'func1'); | ||
assert.equal(result[0].config.functionLength, 7); | ||
} | ||
}, | ||
'given a function with "cyclomaticComplexity" comment preceding': { | ||
topic: analyser.analyse('// @jscheckstyle.cyclomaticComplexity=7\nfunction func1() {}'), | ||
'first result should have a config structure with cyclomaticComplexity=7': function(result) { | ||
assert.equal(result[0].shortName, 'func1'); | ||
assert.equal(result[0].config.cyclomaticComplexity, 7); | ||
} | ||
}, | ||
'given a function with "numberOfArguments" comment preceding': { | ||
topic: analyser.analyse('// @jscheckstyle.numberOfArguments=7\nfunction func1() {}'), | ||
'first result should have a config structure with numberOfArguments=7': function(result) { | ||
assert.equal(result[0].shortName, 'func1'); | ||
assert.equal(result[0].config.numberOfArguments, 7); | ||
} | ||
} | ||
}).exportTo(module); |
@@ -37,2 +37,3 @@ var vows = require('vows'), | ||
'with a function length violation': given('long-function.js', expect('long-output.xml')), | ||
'with a configured function length violation': given('configured-long-function.js', expect('configured-long-output.xml')), | ||
'with a number of arguments violation': given('too-many-arguments.js', expect('too-many-arguments-output.xml')), | ||
@@ -39,0 +40,0 @@ 'with multiple violations in a single function': given('lots-of-args-and-long.js', expect('lots-of-args-and-long-output.xml')), |
@@ -122,3 +122,22 @@ var vows = require('vows'), | ||
} | ||
}, | ||
'when passed a function with configured functionLength limit': { | ||
topic: function(checker) { | ||
return checker.check([ | ||
{ lineStart: 1, shortName: "module.exports", lines: 50, config: { functionLength: 40 } }, | ||
{ lineStart: 5, shortName: "shortFunction", lines: 31, config: { functionLength: 40 } }, | ||
{ lineStart: 16, shortName: "longFunction", lines: 41, config: { functionLength: 40 } } | ||
]); | ||
}, | ||
'should ignore module.exports': function(violations) { | ||
assert.lengthOf(violations.filter(function(result) { return result.shortName === 'module.exports' && result.violations; }), 0); | ||
}, | ||
'should ignore the short function': function(violations) { | ||
assert.lengthOf(violations.filter(function(result) { return result.shortName === 'shortFunction' && result.violations; }), 0); | ||
}, | ||
'should not ignore the long function': function(violations) { | ||
assert.lengthOf(violations.filter(function(result) { return result.shortName === 'longFunction' && result.violations && result.violations.length === 1; }), 1); | ||
} | ||
} | ||
}, | ||
@@ -150,3 +169,12 @@ 'NumberOfArguments checker': { | ||
} | ||
}, | ||
'when passed a function with less than configured number of arguments': { | ||
topic: function(checker) { | ||
return checker.check([{ lineStart: 4, shortName: "cheese", ins: 7, config: { numberOfArguments: 10 } }]); | ||
}, | ||
'should return the original result with no violations': function(violations) { | ||
assert.deepEqual(violations, [{ lineStart: 4, shortName: "cheese", ins: 7, config: { numberOfArguments: 10 } }]); | ||
} | ||
} | ||
}, | ||
@@ -178,4 +206,13 @@ 'CyclomaticComplexity checker': { | ||
} | ||
}, | ||
'when passed a function with a cyclomatic complexity less than a configured limit': { | ||
topic: function(checker) { | ||
return checker.check([{ lineStart: 4, shortName: "cheese", complexity: 12, config: { cyclomaticComplexity: 15 } }]); | ||
}, | ||
'should return the original result with no violations': function(violations) { | ||
assert.deepEqual(violations, [{ lineStart: 4, shortName: "cheese", complexity: 12, config: { cyclomaticComplexity: 15 } }]); | ||
} | ||
} | ||
} | ||
}).exportTo(module); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
HTTP dependency
Supply chain riskContains a dependency which resolves to a remote HTTP URL which could be used to inject untrusted code and reduce overall package reliability.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
53501
33
0
1268
47
1
- Removedburrito@0.2.12(transitive)
- Removedtraverse@0.5.2(transitive)
- Removeduglify-js@1.1.1(transitive)
Updatedburrito@http://github.com/danbell/node-burrito/tarball/master