Created
April 11, 2012 17:03
-
-
Save jbclements/2360569 to your computer and use it in GitHub Desktop.
Hartquist
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
// include uglify library | |
var jsp = require("uglify-js").parser; | |
var pro = require("uglify-js").uglify; | |
// returns a sorted list of variable references | |
function parseVariables(code) { | |
var ast = jsp.parse(code, false, false); | |
var w = pro.ast_walker(); | |
var vars = []; | |
// this is called when a variable reference is found | |
function recordVar() { | |
vars.push(this[1]); | |
} | |
// walk the tree | |
w.with_walkers({ | |
"name" : recordVar | |
}, | |
function() { | |
return w.walk(ast); | |
}); | |
return vars.sort(); | |
} | |
// Check command line args | |
if (process.argv.length < 3) { | |
console.log("Usage: node " + process.argv[1] + ' FILENAME'); | |
process.exit(1); | |
} | |
// read file, display results | |
var fs = require('fs') | |
, filename = process.argv[2]; | |
fs.readFile(filename, 'utf8', function(err, data) { | |
console.log(parseVariables(data)); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment