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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 | 1
342
342
1
1
1
1
1
1
1
342
342
342
342
342
342
342
4
4
338
1
24046
24046
15226
15226
104
104
104
104
1
934
1
1621
1
16029
6977
6977
6977
16029
1
6977
1
475
1
475
1
1
342
342
320
320
320
1
12078
12078
11680
12078
739
12078
1
12103
12078
12078
12078
12078
12078
12078
11968
11968
11968
1
10
10
6
6
6
4
1
11968
11968
2
1
12078
12078
8
1
2487
2487
2487
2487
2487
2487
3619
238
3381
3553
1438
2421
1
1
9
| module.exports = function (code, ast, opts) {
var gen = new CodeGenerator(code, ast, opts);
return gen.generate();
};
module.exports.CodeGenerator = CodeGenerator;
var sourceMap = require("source-map");
var assert = require("assert");
var util = require("./util");
var t = require("./types");
var _ = require("lodash");
function CodeGenerator(code, ast, opts) {
opts = opts || {}
this.tabWidth = 2;
this._indent = 0;
this.opts = opts;
this.ast = ast;
this.buf = "";
if (opts.sourceMap) {
this.map = new sourceMap.SourceMapGenerator({
file: opts.sourceMapName
});
this.map.setSourceContent(opts.sourceFileName, code);
} else {
this.map = null;
}
}
CodeGenerator.prototype.mark = function (node, locType) {
var loc = node.loc;
if (!loc) return; // no locaiton info
var map = this.map;
if (!map) return; // no source map
var lines = this.buf.split("\n");
var line = lines.length;
var col = _.last(lines).length;
map.addMapping({
source: this.opts.sourceFileName,
generated: {
line: line,
column: col
},
original: loc[locType]
});
};
CodeGenerator.prototype.newline = function () {
this.buf += "\n";
};
CodeGenerator.prototype.semicolon = function () {
this.buf += ";";
};
CodeGenerator.prototype.push = function (str) {
if (this._indent) {
// we have an indent level and we aren't pushing a newline
var indent = this.getIndent();
// replace all newlines with newlines with the indentation
str = str.replace(/\n/g, "\n" + indent);
// we've got a newline before us so prepend on the indentation
if (_.last(this.buf) === "\n") str = indent + str;
}
this.buf += str;
};
CodeGenerator.prototype.getIndent = function () {
return util.repeat(this._indent * this.tabWidth);
};
CodeGenerator.prototype.indent = function () {
this._indent++;
};
CodeGenerator.prototype.dedent = function () {
this._indent--;
};
CodeGenerator.prototype.__indent = function (str) {
return str.split("\n").map(function (line) {
return " " + line;
}).join("\n");
};
CodeGenerator.prototype.generate = function () {
var ast = this.ast;
this.print(ast);
var map = this.map;
if (map) map = map.toJSON();
return {
map: map,
ast: ast,
code: this.buf
};
};
CodeGenerator.prototype.buildPrint = function (parent) {
var self = this;
var print = function (node) {
return self.print(node, parent);
};
print.sequence = function (nodes) {
return self.printJoin(print, nodes);
};
return print;
};
CodeGenerator.prototype.print = function (node, parent) {
if (!node) return "";
Eif (this[node.type]) {
this.printLeadingComments(node);
this.mark(node, "start");
var needsParans = t.needsParans(node, parent);
if (needsParans) this.push("(");
this[node.type](node, this.buildPrint(node), parent);
if (needsParans) this.push(")");
this.mark(node, "end");
this.printTrailingComments(node);
} else {
throw new ReferenceError("unknown node " + node.type + " " + JSON.stringify(node));
}
};
CodeGenerator.prototype.generateComment = function (comment) {
var val = comment.value;
if (comment.type === "Line") {
Eif (_.last(val) !== "\n") {
val += "\n";
}
return "//" + val;
} else {
return "/*" + val + "*/\n";
}
};
CodeGenerator.prototype.printTrailingComments = function (node) {
var self = this;
_.each(node.trailingComments, function (comment) {
self.push(self.generateComment(comment));
});
};
CodeGenerator.prototype.printLeadingComments = function (node) {
var self = this;
_.each(node.leadingComments, function (comment) {
self.push(self.generateComment(comment));
});
};
CodeGenerator.prototype.printJoin = function (print, nodes, sep, opts) {
opts = opts || {};
sep = sep || "\n";
var self = this;
var len = nodes.length;
if (opts.indent) self.indent();
_.each(nodes, function (node, i) {
if (opts.iterator) {
opts.iterator(node, i);
} else {
print(node);
}
if (i < len - 1) {
self.push(sep);
}
});
if (opts.indent) self.dedent();
};
CodeGenerator.generators = {
arrayComprehensions: require("./generators/array-comprehensions"),
base: require("./generators/base"),
classes: require("./generators/classes"),
expressions: require("./generators/expressions"),
methods: require("./generators/methods"),
modules: require("./generators/modules"),
statements: require("./generators/statements"),
types: require("./generators/types"),
jsx: require("./generators/jsx")
};
_.each(CodeGenerator.generators, function (generator) {
_.extend(CodeGenerator.prototype, generator);
});
|