google clousure compiler AST to source ?

206 views
Skip to first unread message

mitja

unread,
May 7, 2012, 4:16:05 PM5/7/12
to Closure Compiler Discuss
Hi. How can i get from AST (generated from parse code) back javascript
source?
I'm talking about google clousure AST (not rhino). For example:

Node ast = compiler.parse(input);
// make some changes to ast ...
// how to get from ast (root node) back javascript code (which is now
update) ?

Are there any resource for AST "walking/parsing"
(basically I want to custom process some javascript functions).

Thanks for any info!

Regards,
Mitja

John Lenz

unread,
May 7, 2012, 6:45:32 PM5/7/12
to closure-comp...@googlegroups.com
If you are looking to by pass the rest of the compiler, you should use the CodePrinter:

String js = new CodePrinter.Builder(ast).build();

mitja.kr...@vizius.si

unread,
May 8, 2012, 3:58:53 AM5/8/12
to closure-comp...@googlegroups.com
@jonh
That's exatly what i was looking for.
Thank you.

What is the best way to do custom code transformation (modify javascript code).
Modify the AST and then use CodePrinter to get javascript or something like custom compilerpass?


Dne torek, 08. maj 2012 00:45:32 UTC+2 je oseba John napisala:

Mitja Kramberger

unread,
May 8, 2012, 2:55:43 AM5/8/12
to Closure Compiler Discuss
@john
That's exactly what I was looking for (code printing).
Thank you.

If i want to do some custom JS transformation (code modification),
what is the best way?
Operate directly on AST (compiler.parse > AST modification) or is
there a better way (custom compiler pass or something)?

John Lenz

unread,
May 8, 2012, 11:19:01 AM5/8/12
to closure-comp...@googlegroups.com
I would write it as a compiler pass regardless (that gives your flexibility) but whether you run it with the rest of the compiler depends on what you are trying to accomplish (do you want to check it or optimize it afterwards?).

There isn't much boilerplate.  Here is a pretty minimal pass:


And a minimal code changing pass:

mitja.kr...@vizius.si

unread,
May 12, 2012, 4:53:12 PM5/12/12
to closure-comp...@googlegroups.com
How can I get reference to CodeBuilder inside my code (separated from google clousure compiler) and why this class is not public.


Dne torek, 08. maj 2012 00:45:32 UTC+2 je oseba John napisala:
If you are looking to by pass the rest of the compiler, you should use the CodePrinter:

John Lenz

unread,
Jun 14, 2012, 11:58:50 AM6/14/12
to closure-comp...@googlegroups.com
Why do you think it isn't public?
Reply all
Reply to author
Forward
0 new messages