Transform AST in UglifyJS

36 views
Skip to first unread message

han liu

unread,
Jul 5, 2015, 2:11:25 AM7/5/15
to ugli...@googlegroups.com

I am using UglifyJS to parse, minify and transform JS codes. One of my goal is to transform the AST by inserting new variable definitions. For example:

var x;
var y;
x = 1;
y = x;
x = 3;

I want to insert a new variable definition "var _x" into a random position, such as before the statement "y = x". The transformed code should be like this:

var x;
var y;
x = 1;
var _x = x;
y = _x;
_x = 3;

I have tried the TreeTransformer in UglifyJS. Updating the symbol reference to be the new one (x -> _x) is no problem. But I am unclear about obtaining the right position for the insertion with TreeTransformer. Can anyone share some insights? Some code examples would be better!

Reply all
Reply to author
Forward
0 new messages