Hi Tauren,
The API should be quite simple to use — though indeed there is no documentation for now. Take a look at tmp/instrument.js [1] for an example that takes a piece of code and adds some trace() calls, passing the line number, before various statements.
The idea is:
- you'll create an AST walker — w = ast_walker()
- you'll define custom walkers using w.with_walkers. For your case you'll want to catch "defun" or "function" (or both).
- from your walker you can return null or undefined to keep the original AST, or you can return a new AST instead.
- w.walk returns the new AST.
A "function" or "defun" AST is an array that looks like this:
[ "function" / "defun", NAME, ARGS, BODY ]
NAME can be null if it's an anonymous function, otherwise it's the function name. ARGS is an array of strings (the argument names) and body is an array of statements (which are ASTs themselves). To replace the function with its content, for example, you'd return from that walker [ "block", BODY ] — where BODY is the same component from the function's AST.
Finally, call gen_code(ast) to render an AST into JS code.
Hope this helps.
-Mihai
[1]
https://github.com/mishoo/UglifyJS/blob/master/tmp/instrument.js--
Mihai Bazon,
http://mihai.bazon.net/blog