1 | var FS = require("fs");
|
2 | var Path = require("path");
|
3 | var Esprima = require("esprima");
|
4 |
|
5 | function stringify(js, indent) {
|
6 | if (typeof indent === 'undefined') indent = '';
|
7 | var t = typeof js;
|
8 | if (t === 'string') {
|
9 | return '"' + js + '"';
|
10 | }
|
11 | if (t === 'number') {
|
12 | return js;
|
13 | }
|
14 | if (t === 'boolean') {
|
15 | return js ? "true" : "false";
|
16 | }
|
17 | var out = '';
|
18 | if (Array.isArray(js)) {
|
19 | out = '[';
|
20 | js.forEach(
|
21 | function(itm, idx) {
|
22 | if (idx > 0) {
|
23 | out += ",\n" + indent;
|
24 | }
|
25 | out += stringify(itm, indent + ' ');
|
26 | }
|
27 | );
|
28 | out += ']';
|
29 | return out;
|
30 | }
|
31 | if (t === 'object') {
|
32 | out = '{';
|
33 | var k, v, idx = 0;
|
34 | for (k in js) {
|
35 | v = js[k];
|
36 | if (idx > 0) {
|
37 | out += ",\n" + indent;
|
38 | }
|
39 | out += k + ": " + stringify(v, indent + ' ');
|
40 | idx++;
|
41 | }
|
42 | out += '}';
|
43 | return out;
|
44 | }
|
45 | return '<' + t + '>';
|
46 | }
|
47 |
|
48 | var code = FS.readFileSync(Path.join(__dirname, "test-code.js"));
|
49 | var tree = Esprima.parse(code, {comment: true, range: true});
|
50 | console.log(stringify(tree));
|
51 |
|
52 |
|