xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / uglify-js / tmp / instrument2.js

4.5 KB
1// sample on how to use the parser and walker API to instrument some code
2
3var jsp = require("uglify-js").parser;
4var pro = require("uglify-js").uglify;
5
6function instrument(code) {
7        var ast = jsp.parse(code, false, true); // true for the third arg specifies that we want
8                                                // to have start/end tokens embedded in the
9                                                // statements
10        var w = pro.ast_walker();
11
12        function trace (line, comment) {
13                var code = pro.gen_code(line, { beautify: true });
14                var data = line[0]
15
16                var args = []
17                if (!comment) comment = ""
18                if (typeof data === "object") {
19                        code = code.split(/\n/).shift()
20                        args = [ [ "string", data.toString() ],
21                                 [ "string", code ],
22                                 [ "num", data.start.line ],
23                                 [ "num", data.start.col ],
24                                 [ "num", data.end.line ],
25                                 [ "num", data.end.col ]]
26                } else {
27                        args = [ [ "string", data ],
28                                 [ "string", code ]]
29
30                }
31                return [ "call", [ "name", "trace" ], args ];
32        }
33
34        // we're gonna need this to push elements that we're currently looking at, to avoid
35        // endless recursion.
36        var analyzing = [];
37        function do_stat() {
38                var ret;
39                if (this[0].start && analyzing.indexOf(this) < 0) {
40                        // without the `analyzing' hack, w.walk(this) would re-enter here leading
41                        // to infinite recursion
42                        analyzing.push(this);
43                        ret = [ "splice",
44                                [ [ "stat", trace(this) ],
45                                  w.walk(this) ]];
46                        analyzing.pop(this);
47                }
48                return ret;
49        }
50
51        function do_cond(c, t, f) {
52                return [ this[0], w.walk(c),
53                         ["seq", trace(t), w.walk(t) ],
54                         ["seq", trace(f), w.walk(f) ]];
55        }
56
57        function do_binary(c, l, r) {
58                if (c !== "&&" && c !== "||") {
59                        return [this[0], c, w.walk(l), w.walk(r)];
60                }
61                return [ this[0], c,
62                         ["seq", trace(l), w.walk(l) ],
63                         ["seq", trace(r), w.walk(r) ]];
64        }
65
66        var new_ast = w.with_walkers({
67                "stat"        : do_stat,
68                "label"       : do_stat,
69                "break"       : do_stat,
70                "continue"    : do_stat,
71                "debugger"    : do_stat,
72                "var"         : do_stat,
73                "const"       : do_stat,
74                "return"      : do_stat,
75                "throw"       : do_stat,
76                "try"         : do_stat,
77                "defun"       : do_stat,
78                "if"          : do_stat,
79                "while"       : do_stat,
80                "do"          : do_stat,
81                "for"         : do_stat,
82                "for-in"      : do_stat,
83                "switch"      : do_stat,
84                "with"        : do_stat,
85                "conditional" : do_cond,
86                "binary"      : do_binary
87        }, function(){
88                return w.walk(ast);
89        });
90        return pro.gen_code(new_ast, { beautify: true });
91}
92
93
94////// test code follows.
95
96var code = instrument(test.toString());
97console.log(code);
98
99function test() {
100        // simple stats
101        a = 5;
102        c += a + b;
103        "foo";
104
105        // var
106        var foo = 5;
107        const bar = 6, baz = 7;
108
109        // switch block.  note we can't track case lines the same way.
110        switch ("foo") {
111            case "foo":
112                return 1;
113            case "bar":
114                return 2;
115        }
116
117        // for/for in
118        for (var i = 0; i < 5; ++i) {
119                console.log("Hello " + i);
120        }
121        for (var i in [ 1, 2, 3]) {
122                console.log(i);
123        }
124
125        for (var i = 0; i < 5; ++i)
126                console.log("foo");
127
128        for (var i = 0; i < 5; ++i) {
129                console.log("foo");
130        }
131
132        var k = plurp() ? 1 : 0;
133        var x = a ? doX(y) && goZoo("zoo")
134              : b ? blerg({ x: y })
135              : null;
136
137        var x = X || Y;
138}