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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627 | 1
493
493
1
1
493
493
1
493
493
1
21282
21282
19270
1
19763
19744
19744
1
1196
1
1297
3119
186
2933
1
1063
4437
1
493
1
493
1
1
1482
1
840
1
1859
1859
210
1649
1859
1
1
6739
1
1
678
678
678
678
678
678
678
678
1
3
3
3
3
3
3
3
1
1
1
1
1
1
1
1
1
804
804
107
697
1
345
345
345
345
345
1
2981
2981
206
2981
1
1058
1058
1058
1057
1
243
243
243
243
243
243
342
342
342
342
342
139
139
243
243
1
1
342
342
1
237
237
237
237
237
453
453
453
453
237
237
1
1
1
99
1
1751
1751
1751
87
1664
1659
5
4
1
1
1
1
64
64
64
64
1
24
24
24
24
1
12
12
12
12
12
12
12
12
1
9
9
9
9
9
1
978
978
978
978
1007
1006
1006
977
977
977
977
916
977
1
1007
936
71
1
1
67
67
67
1
39
39
39
39
39
39
39
1
5
1
22
22
22
22
22
22
22
1
1
1
1
1
1
1
11
11
11
11
1
6
6
6
6
1
2
1
1
1
3
1
3
3
3
3
3
3
3
1
15
15
14
1
15
7
8
6
15
1
1
3
3
3
3
3
1
3
3
1
3
3
3
3
3
3
1
3
3
3
3
1
1
1
1
1
1
1
1
1
1
1
| module.exports = function (ast, opts) {
var gen = new CodeGenerator;
return gen.generate(ast, opts);
};
var _ = require("lodash");
function CodeGenerator() {
this.indent = this.indent.bind(this);
this.print = this.print.bind(this);
}
CodeGenerator.prototype.generate = function (ast, opts) {
opts = opts || {};
return {
map: null,
ast: ast,
code: this.print(ast)
};
};
CodeGenerator.prototype.buildPrint = function (parent) {
var self = this;
return function (node) {
return self.print(node, parent);
};
};
CodeGenerator.prototype.print = function (node, parent) {
if (!node) return "";
Eif (this[node.type]) {
return this[node.type](node, this.buildPrint(node), parent);
} else {
throw new ReferenceError("unknown node " + node.type + " " + JSON.stringify(node));
}
};
CodeGenerator.prototype.printSequence = function (nodes, print) {
return nodes.map(print).join("\n");
};
CodeGenerator.prototype.removeEmptyExpressions = function (nodes) {
return nodes.filter(function (node) {
if (node.type === "EmptyStatement") {
return false;
} else {
return true;
}
});
};
CodeGenerator.prototype.indent = function (str) {
return str.split("\n").map(function (line) {
return " " + line;
}).join("\n");
};
CodeGenerator.prototype.File = function (node, print) {
return print(node.program);
};
CodeGenerator.prototype.Program = function (node, print) {
return this.printSequence(this.removeEmptyExpressions(node.body), print);
};
CodeGenerator.prototype.EmptyStatement = function (node, print) {
return "";
};
CodeGenerator.prototype.ExpressionStatement = function (node, print) {
return print(node.expression) + ";";
};
CodeGenerator.prototype.BinaryExpression =
CodeGenerator.prototype.LogicalExpression =
CodeGenerator.prototype.AssignmentExpression = function (node, print) {
return print(node.left) + " " + node.operator + " " + print(node.right);
};
CodeGenerator.prototype.MemberExpression = function (node, print) {
var code = this._maybeParans(node.object, print);
if (node.computed) {
code += "[" + print(node.property) + "]";
} else {
code += "." + print(node.property);
}
return code;
};
CodeGenerator.prototype.Path = function (node, print) {
return "." + print(node.body);
};
CodeGenerator.prototype.Identifier = function (node, print) {
return node.name;
};
CodeGenerator.prototype.SpreadElement =
CodeGenerator.prototype.SpreadElementPattern =
CodeGenerator.prototype.SpreadProperty =
CodeGenerator.prototype.SpreadPropertyPattern = function (node, print) {
return "..." + print(node.argument);
};
CodeGenerator.prototype.FunctionDeclaration =
CodeGenerator.prototype.FunctionExpression = function (node, print) {
var code = "";
Iif (node.async) code += "async ";
code += "function";
Iif (node.generator) code += "*";
if (node.id) code += " " + print(node.id);
code += "(" + node.params.map(print).join(", ") + ")";
code += " " + print(node.body);
return code;
};
CodeGenerator.prototype.ArrowFunctionExpression = function (node, print) {
var code = "";
Iif (node.async) code += "async ";
Eif (node.params.length === 1) {
code += print(node.params[0]);
} else {
code += "(" + node.params.map(this.buildPrint(node)).join(", ") + ")";
}
code += " => ";
code += print(node.body);
return code;
};
CodeGenerator.prototype.MethodDefinition = function (node, print) {
throw new Error("MethodDefinition");
return;
};
CodeGenerator.prototype.YieldExpression = function (node, print) {
var code = "yield";
if (node.delegate) code += "*";
if (node.argument) code += " " + print(node.argument);
return code;
};
CodeGenerator.prototype.AwaitExpression = function (node, print) {
var code = "await";
if (node.all) code += "*";
if (node.argument) code += print(node.argument);
return code;
};
CodeGenerator.prototype.ModuleDeclaration = function (node, print) {
var code = "module " + print(node.id);
if (node.source) {
code += " from " + print(node.source);
} else {
code += print(node.body);
}
return code;
};
CodeGenerator.prototype.ImportSpecifier =
CodeGenerator.prototype.ExportSpecifier = function (node, print) {
var code = print(node.id);
if (node.name) code += " as " + print(node.name);
return code;
};
CodeGenerator.prototype.ExportBatchSpecifier = function (node, print) {
return "*";
};
CodeGenerator.prototype.ExportDeclaration = function (node, print) {
throw new Error("ExportDeclaration");
};
CodeGenerator.prototype.ImportDeclaration = function (node, print) {
throw new Error("ImportDeclaration");
};
CodeGenerator.prototype.BlockStatement = function (node, print) {
var body = this.removeEmptyExpressions(node.body);
if (body.length === 0) {
return "{}";
} else {
return "{\n" + this.indent(this.printSequence(body, print)) + "\n}"
}
};
CodeGenerator.prototype.ReturnStatement = function (node, print) {
var code = "return";
Eif (node.argument) {
code += " " + print(node.argument);
}
code += ";";
return code;
};
CodeGenerator.prototype._maybeParans = function (node, print) {
var code = print(node);
if (node.type === "AssignmentExpression" ||
node.type === "FunctionExpression" ||
node.type === "BinaryExpression") {
code = "(" + code + ")";
}
return code;
};
CodeGenerator.prototype.CallExpression = function (node, print) {
var code = "";
code += this._maybeParans(node.callee, print);
code += "(" + node.arguments.map(this.buildPrint(node)).join(", ") + ")";
return code;
};
CodeGenerator.prototype.ObjectExpression =
CodeGenerator.prototype.ObjectPattern = function (node, print) {
var allowBreak = false;
var indent = this.indent;
var print = this.buildPrint(node);
var parts = [len > 0 ? "{\n" : "{"];
var len = node.properties.length;
_.each(node.properties, function (prop, i) {
var lines = indent(print(prop));
var multiLine = lines.length > 1;
Iif (multiLine && allowBreak) {
// Similar to the logic for BlockStatement.
parts.push("\n");
}
parts.push(lines);
if (i < len - 1) {
// Add an extra line break if the previous object property
// had a multi-line value.
parts.push(multiLine ? ",\n\n" : ",\n");
allowBreak = !multiLine;
}
});
parts.push(len > 0 ? "\n}" : "}");
return parts.join("\n");
};
CodeGenerator.prototype.PropertyPattern = function (node, print) {
return print(node.key) + ": " + print(node.pattern);
};
CodeGenerator.prototype.Property = function (node, print) {
Iif (node.method || node.kind === "get" || node.kind === "set") {
throw new Error("Property");
} else {
return print(node.key) + ": " + print(node.value);
}
};
CodeGenerator.prototype.ArrayExpression =
CodeGenerator.prototype.ArrayPattern = function (node, print) {
var elems = node.elements;
var parts = ["["];
var print = this.buildPrint(node);
var len = elems.length;
_.each(elems, function(elem, i) {
Iif (!elem) {
// If the array expression ends with a hole, that hole
// will be ignored by the interpreter, but if it ends with
// two (or more) holes, we need to write out two (or more)
// commas so that the resulting code is interpreted with
// both (all) of the holes.
parts.push(",");
} else {
if (i > 0) parts.push(" ");
parts.push(print(elem));
if (i < len - 1) parts.push(",");
}
});
parts.push("]");
return parts.join("");
};
CodeGenerator.prototype.SequenceExpression = function (node, print) {
return node.expressions.map(print).join(", ");
};
CodeGenerator.prototype.ThisExpression = function (node, print) {
return "this";
};
CodeGenerator.prototype.Literal = function (node, print) {
var val = node.value;
var type = typeof val;
if (val === null) {
return "null";
}
if (type === "string" || type === "number" || type === "boolean") {
return JSON.stringify(val);
}
if (node.raw) {
return node.raw;
}
Iif (node.regex) {
return "/" + node.regex.pattern + "/" + node.regex.flags;
}
throw new Error("cannot print literal " + JSON.stringify(node));
};
CodeGenerator.prototype.ModuleSpecifier = function (node, print) {
return "\"" + node.value + "\"";
};
CodeGenerator.prototype.UnaryExpression = function (node, print) {
var code = node.operator;
if (/[a-z]$/.test(node.operator)) code += " ";
code += this._maybeParans(node.argument, print);
return code;
};
CodeGenerator.prototype.UpdateExpression = function (node, print) {
var parts = [print(node.argument)];
parts.push(node.operator);
Iif (node.prefix) parts.reverse();
return parts.join("");
};
CodeGenerator.prototype.ConditionalExpression = function (node, print) {
var code = "(";
code += print(node.test);
code += " ? ";
code += print(node.consequent);
code += " : ";
code += print(node.alternate);
code += ")";
return code;
};
CodeGenerator.prototype.NewExpression = function (node, print) {
var code = "new ";
code += print(node.callee);
Eif (node.arguments) {
code += "(" + node.arguments.map(print).join(", ") + ")";
}
return code;
};
CodeGenerator.prototype.VariableDeclaration = function (node, print, parent) {
var print = print;
var code = node.kind + " ";
var maxLen = 0;
var printed = node.declarations.map(function (declar) {
var lines = print(declar);
maxLen = Math.max(maxLen, lines.length);
return lines;
});
switch (maxLen) {
case 0:
code += printed[0];
default:
code += printed.join(",\n ");
break;
}
if (
parent.type !== "ForStatement" &&
parent.type !== "ForInStatement" &&
parent.type !== "ForOfStatement" &&
parent.type !== "ForOfStatement"
) {
code += ";";
}
return code;
};
CodeGenerator.prototype.VariableDeclarator = function (node, print) {
if (node.init) {
return print(node.id) + " = " + print(node.init);
} else {
return print(node.id);
}
};
CodeGenerator.prototype.WithStatement = function (node, print) {
return "with (" + print(node.object) + ") " + print(node.body);
};
CodeGenerator.prototype.IfStatement = function (node, print) {
var code = "if (" + print(node.test) + ") ";
code += print(node.consequent);
return code;
};
CodeGenerator.prototype.ForStatement = function (node, print) {
var code = "for (";
code += print(node.init) + "; ";
code += print(node.test) + "; ";
code += print(node.update)
code += ") ";
code += print(node.body);
return code;
};
CodeGenerator.prototype.WhileStatement = function (node, print) {
return "while (" + print(node.test) + ") " + print(node.body);
};
CodeGenerator.prototype.ForInStatement = function (node, print) {
var code = node.each ? "for each (" : "for (";
code += print(node.left);
code += " in ";
code += print(node.right);
code += ") ";
code += print(node.body);
return code;
};
CodeGenerator.prototype.DoWhileStatement = function (node, print) {
var code = "do " + print(node.body);
Eif (/\}$/.test(code)) {
code += " while";
} else {
code += "\nwhile";
}
code += " (" + print(node.test) + ");";
return code;
};
CodeGenerator.prototype.BreakStatement = function (node, print) {
var code = "break";
if (node.label) code += " " + print(node.label);
code += ";";
return code;
};
CodeGenerator.prototype.ContinueStatement = function (node, print) {
var code = "continue";
if (node.label) code += " " + print(node.label);
code += ";";
return code;
};
CodeGenerator.prototype.LabeledStatement = function (node, print) {
return print(node.label) + ":\n" + print(node.body);
};
CodeGenerator.prototype.TryStatement = function (node, print) {
var code = "try " + print(node.block);
code += " " + print(node.handler);
if (node.finalizer) {
code += " finally " + print(node.finalizer);
}
return code;
};
CodeGenerator.prototype.CatchClause = function (node, print) {
var code = "catch (" + print(node.param);
if (node.guard) {
code += " if " + print(node.guard);
}
code += ") " + print(node.body);
return code;
};
CodeGenerator.prototype.ThrowStatement = function (node, print) {
return "throw " + print(node.argument) + ";";
};
CodeGenerator.prototype.SwitchStatement = function (node, print) {
var code = "switch (";
code += print(node.discriminant);
code += ") {";
Eif (node.cases.length > 0) {
code += "\n" + node.cases.map(print).join("\n") + "\n";
}
code += "}";
return code;
};
CodeGenerator.prototype.SwitchCase = function (node, print) {
var code = "";
if (node.test) {
code += "case " + print(node.test) + ":";
} else {
code += "default:";
}
if (node.consequent.length === 1) {
code += " " + print(node.consequent[0]);
} else if (node.consequent.length > 1) {
code += "\n" + this.indent(this.printSequence(node.consequent, print));
}
return this.indent(code);
};
CodeGenerator.prototype.DebuggerStatement = function (node, print) {
return "debugger;";
};
CodeGenerator.prototype.ClassExpression =
CodeGenerator.prototype.ClassDeclaration = function (node, print) {
var parts = ["class"];
Eif (node.id) parts.push(" ", print(node.id));
Iif (node.superClass) parts.push(" extends ", print(node.superClass));
parts.push(" ", print(node.body));
return parts.join("");
};
CodeGenerator.prototype.ClassBody = function (node, print) {
Iif (node.body.length === 0) {
return "{}";
}
return [
"{\n",
this.indent(node.body.map(print).join("")),
"\n}"
].join("");
};
CodeGenerator.prototype._method = function (kind, key, value, print) {
var parts = [];
Iif (value.async) {
parts.push("async ");
}
Eif (!kind || kind === "init") {
Iif (value.generator) {
parts.push("*");
}
} else {
assert.ok(kind === "get" || kind === "set");
parts.push(kind, " ");
}
parts.push(
print(key),
"(" + value.params.map(print).join(", ") + ")",
print(value.body)
);
return parts.join("");
}
CodeGenerator.prototype.MethodDefinition = function (node, print) {
var parts = [];
Iif (node.static) {
parts.push("static ");
}
parts.push(this._method(
node.kind,
node.key,
node.value,
print
));
return parts.join("");
};
CodeGenerator.prototype.XJSAttribute = function (node, print) {
var code = print(node.name);
if (node.value) code += "=" + print(node.value);
return code;
};
CodeGenerator.prototype.XJSIdentifier = function (node, print) {
return node.name;
};
CodeGenerator.prototype.XJSNamespacedName = function (node, print) {
return print(node.namespace) + ":" + print(node.name);
};
CodeGenerator.prototype.XJSMemberExpression = function (node, print) {
return print(node.object) + "." + print(node.property);
};
CodeGenerator.prototype.XJSSpreadAttribute = function (node, print) {
return "{..." + print(node.argument) + "}";
};
CodeGenerator.prototype.XJSExpressionContainer = function (node, print) {
return "{" + print(node.expression) + "}";
};
CodeGenerator.prototype.XJSElement = function (node, print) {
throw new Error("XJSElement");
};
CodeGenerator.prototype.XJSOpeningElement = function (node, print) {
var code = "<" + print(node.name);
if (node.attributes.length < 0) {
code += " " + node.attributes.map(print).join(" ");
}
code += node.selfClosing ? " />" : ">";
return code;
};
CodeGenerator.prototype.XJSClosingElement = function (node, print) {
return "</" + node.name + ">";
};
CodeGenerator.prototype.XJSText = function (node, print) {
return node.value;
};
CodeGenerator.prototype.XJSEmptyExpression = function (node, print) {
return "";
};
|