Created
February 20, 2012 19:36
-
-
Save johnbintz/1870923 to your computer and use it in GitHub Desktop.
Run JSMeter on your code, getting a reasonable output to start cleaning up code complexities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// npm install -g jsmeter | |
(function () { | |
var verbose = false; | |
var args = process.argv; | |
var fs = require("fs"); | |
var parse = require('jsmeter/parse').make_parse(); | |
var tokens = require('jsmeter/tokens').setup() | |
var complexity = require('jsmeter/complexity').make_complexity(); | |
for (var a = 2; a < args.length; a++) { | |
// for debugging | |
if (args[a] === "--verbose") { | |
verbose = true; | |
continue; | |
} | |
// if your code throws syntax errors but it's actually ok, ask to be forgiven | |
if (args[a] === "--forgive") { | |
Object.prototype.error = function (message, t) { | |
t = t || this; | |
t.name = "SyntaxError"; | |
t.message = message; | |
}; | |
continue; | |
} | |
fs.readFile(args[a], "utf8", (function (name) { | |
return function (err, data) { | |
if (err) throw err; | |
var tree = parse(data); | |
complexity.complexity(tree, name); | |
var result = complexity.getFunctions(); | |
result = result.sort(function(a, b) { | |
if (a.halsteadLevelF() < b.halsteadLevelF()) { | |
return -1; | |
} else { | |
return 1; | |
} | |
}); | |
for (var i = 0; i < result.length; i++) { | |
if (!result[i].shortName.match(/Anonymous/)) { | |
if (verbose) { | |
console.dir(result[i]); | |
} | |
var output = result[i].linesF() + '\t' + (1 - result[i].halsteadLevelF()) + '\t' + result[i].shortName.replace(/^\[\[[^\]]*\]\]\.?/, "") ; | |
console.log(output); | |
} | |
} | |
}; | |
})(args[a])); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment