Created
October 29, 2012 03:32
-
-
Save zeekay/3971330 to your computer and use it in GitHub Desktop.
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
var fs = require('fs'), | |
uglify = require('uglify-js2'), | |
code = fs.readFileSync('./orig.js', 'utf8'); | |
var ast = uglify.parse(code); | |
// List all functions | |
var functions = []; | |
var walker = new uglify.TreeWalker(function(node){ | |
if (node instanceof uglify.AST_Defun) { | |
functions.push(node.name.name); | |
} | |
}); | |
ast.walk(walker); | |
// Annotate all functions | |
var tt = new uglify.TreeTransformer(function(node, descend) { | |
if (node instanceof uglify.AST_Defun) { | |
var logging = new uglify.AST_SymbolConst({name: 'console.log("' + node.name.name + ' called.")'}); | |
node.body.unshift(logging); | |
} | |
descend(node, this); | |
return node; | |
}); | |
ast.transform(tt); | |
// Output annotated code. | |
console.log(ast.print_to_string({beautify: true})); | |
console.log(functions); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment