Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / uglify-js / tmp / instrument.js
1 // sample on how to use the parser and walker API to instrument some code
2
3 var jsp = require("uglify-js").parser;
4 var pro = require("uglify-js").uglify;
5
6 function 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         // we're gonna need this to push elements that we're currently looking at, to avoid
13         // endless recursion.
14         var analyzing = [];
15         function do_stat() {
16                 var ret;
17                 if (this[0].start && analyzing.indexOf(this) < 0) {
18                         // without the `analyzing' hack, w.walk(this) would re-enter here leading
19                         // to infinite recursion
20                         analyzing.push(this);
21                         ret = [ "splice", // XXX: "block" is safer
22                                 [ [ "stat",
23                                     [ "call", [ "name", "trace" ],
24                                       [ [ "string", this[0].toString() ],
25                                         [ "num", this[0].start.line ],
26                                         [ "num", this[0].start.col ],
27                                         [ "num", this[0].end.line ],
28                                         [ "num", this[0].end.col ]]]],
29                                   w.walk(this) ]];
30                         analyzing.pop(this);
31                 }
32                 return ret;
33         };
34         var new_ast = w.with_walkers({
35                 "stat"     : do_stat,
36                 "label"    : do_stat,
37                 "break"    : do_stat,
38                 "continue" : do_stat,
39                 "debugger" : do_stat,
40                 "var"      : do_stat,
41                 "const"    : do_stat,
42                 "return"   : do_stat,
43                 "throw"    : do_stat,
44                 "try"      : do_stat,
45                 "defun"    : do_stat,
46                 "if"       : do_stat,
47                 "while"    : do_stat,
48                 "do"       : do_stat,
49                 "for"      : do_stat,
50                 "for-in"   : do_stat,
51                 "switch"   : do_stat,
52                 "with"     : do_stat
53         }, function(){
54                 return w.walk(ast);
55         });
56         return pro.gen_code(new_ast, { beautify: true });
57 }
58
59
60
61
62 ////// test code follows.
63
64 var code = instrument(test.toString());
65 console.log(code);
66
67 function test() {
68         // simple stats
69         a = 5;
70         c += a + b;
71         "foo";
72
73         // var
74         var foo = 5;
75         const bar = 6, baz = 7;
76
77         // switch block.  note we can't track case lines the same way.
78         switch ("foo") {
79             case "foo":
80                 return 1;
81             case "bar":
82                 return 2;
83         }
84
85         // for/for in
86         for (var i = 0; i < 5; ++i) {
87                 console.log("Hello " + i);
88         }
89         for (var i in [ 1, 2, 3]) {
90                 console.log(i);
91         }
92
93         // note however that the following is broken.  I guess we
94         // should add the block brackets in this case...
95         for (var i = 0; i < 5; ++i)
96                 console.log("foo");
97 }