-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.js
90 lines (76 loc) · 1.93 KB
/
debug.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*jshint evil:true*/
var compiler = require('./lib/compiler');
var path = require('path');
var sh = require('shelljs');
var esprima = require('./lib/esprima')
, escodegen = require('./lib/escodegen')
, transform = require('./lib/transform').transform;
var DEBUG = __dirname + "/debug";
var EXAMPLES = __dirname + "/examples";
/**
* create debug dir
*/
if (!sh.test('-d', DEBUG)) {
sh.mkdir(DEBUG);
}
/**
* util for saving debug files
*/
function save (data, file) {
if(typeof data !== 'string') {
data = JSON.stringify(data, null, ' ');
}
data.to(DEBUG + '/' + file);
}
function removeLoc(obj) {
var result;
if( Object.prototype.toString.call( obj ) === '[object Array]' ) {
result = [];
obj.forEach(function(o) {
result.push(removeLoc(o));
});
return result;
} else if (obj === null) {
return null;
} else if (typeof obj === 'object') {
result = {};
Object.keys(obj).forEach(function(k) {
if (k !== 'loc') {
result[k] = removeLoc(obj[k]);
}
});
return result;
} else {
return obj;
}
}
var sample = process.argv[2];
if (!sample) {
sample = EXAMPLES + '/sample.ws';
} else if (sample.indexOf('.ws') !== sample.length - 3) {
sample = EXAMPLES + '/' + sample + '.ws';
}
var fileName = path.basename(sample, '.ws');
var source = sh.cat(sample);
var ast, transformed, output;
try {
ast = esprima.parse(source, {loc: true});
save(removeLoc(ast), fileName + '-ast.json');
transformed = transform(ast);
save(removeLoc(transformed), fileName + '-transformed-ast.json');
output = escodegen.generate(transformed);
save(output, fileName + '.js');
} catch (ex) {
if (ex.messages) {
console.error(ex.message);
ex.messages.forEach(function(err) {
console.error(' - ' + err);
});
if (ex.newAst) {
save(removeLoc(ex.newAst), fileName + '-transformed-ast.json');
}
} else {
console.error(ex.stack);
}
}
eval(output);