-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathast2code.js
557 lines (459 loc) · 17.1 KB
/
ast2code.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
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
/*global global exports require minify need$ load codeparse cp2fmtree*/
var global, exports;
//#BEGIN_BUILD_STRIP
// With Node.js
if (typeof require === 'function')
{
// No dependency for now.
}
//#END_BUILD_STRIP
// Without Node.js (browser, or V8 alone)
// Support both use cases: browser development (example: jsm_dev) and
// command-line transformation (example: jsm_dev -> jsm_out).
// No dependency for now.
(function (global) {
// ---------- Public API
global.ast2code = ast2code;
function ast2code( /*object*/ast, /*?object?*/opt )
// Returns a code string.
{
return ast2arr( ast, opt ).join( '' );
}
// ---------- Private implementation
function ast2arr( /*object | array*/ast, /*?object?*/opt, /*?object?*/extra )
// Returns an array of code strings.
//
// Notes:
//
// When `opt.ret` array defined, modifies it in-place (imperative
// mode, useful for recursion, avoids creating many small arrays).
//
// When `opt.ret` array not defined, a new array is returned
// (functional mode, useful for the top-level call).
//
// Guillaume Lathoud
{
opt || (opt = {});
var ret = opt.ret || (opt.ret = [])
, jsm = opt.jsm
;
var array_sep = extra && extra.array_sep
, parent = extra && extra.parent
, isLeftChild = extra && extra.isLeftChild
;
if (ast instanceof Array)
{
for (var n = ast.length, i = 0; i < n; i++)
{
if (0 < i && array_sep)
ret.push( array_sep );
ast2arr( ast[ i ], opt, { parent : extra && extra.parent } );
}
return ret;
}
if (array_sep)
throw new Error( 'bug' );
switch(ast.type) {
case 'ArrayExpression':
ret.push( '[' );
var elt = ast.elements;
for (var ne = elt.length, ie = 0; ie < ne; ie++)
{
if (0 < ie)
ret.push( ',' );
ast2arr( elt[ ie ], opt, { parent : ast } );
}
ret.push( ']' );
return ret;
case 'AssignmentExpression':
var nds_prn = needs_parentheses( parent, ast, isLeftChild );
if (nds_prn) ret.push( '(' );
ast2arr( ast.left, opt, { parent : ast, isLeftChild : true } );
ret.push( ast.operator );
ast2arr( ast.right, opt, { parent : ast, isLeftChild : false } );
if (nds_prn) ret.push( ')' );
return ret;
case 'BinaryExpression':
case 'LogicalExpression':
var nds_prn = needs_parentheses( parent, ast, isLeftChild )
, left = ast.left
, right = ast.right
;
if (nds_prn) ret.push( '(' );
ast2arr( left, opt, { parent : ast, isLeftChild : true } );
ret.push( /^\w+$/.test( ast.operator ) ? ' ' + ast.operator + ' ' : ast.operator );
ast2arr( right, opt, { parent : ast, isLeftChild : false } );
if (nds_prn) ret.push( ')' );
return ret;
case 'BlockStatement':
ret.push( '{' );
ast2arr( ast.body, opt, { parent : ast } );
ret.push( '}' );
return ret;
case 'BreakStatement':
case 'ContinueStatement':
ret.push( ast.type === 'BreakStatement' ? 'break' : 'continue' );
var label = ast.label;
if (label)
{
ret.push( ' ' );
ast2arr( label, opt, { parent : ast } );
}
ret.push( ';' );
return ret;
case 'CallExpression':
var isFun = /function/i.test( ast.callee.type );
if (isFun) ret.push( '(' );
ast2arr( ast.callee, opt, { parent : ast, isLeftChild : true } );
if (isFun) ret.push( ')' );
ret.push( '(' );
ast2arr( ast.arguments, opt, { parent : ast, array_sep : ',' } );
ret.push( ')' );
return ret;
case 'CatchClause':
ret.push( 'catch(' );
ast2arr( ast.param, opt, { parent : ast } );
ret.push( ')' );
ast2arr( ast.body, opt );
return ret;
case 'ConditionalExpression':
var nds_prn = needs_parentheses( parent, ast, isLeftChild );
if (nds_prn) ret.push( '(' );
ast2arr( ast.test, opt, { parent : ast, isLeftChild : true } );
ret.push( '?' );
ast2arr( ast.consequent, opt, { parent : ast, isLeftChild : false } );
ret.push( ':' );
ast2arr( ast.alternate, opt, { parent : ast, isLeftChild : false } );
if (nds_prn) ret.push( ')' );
return ret;
case 'DoWhileStatement':
ret.push( 'do ' );
ast2arr( ast.body, opt, { parent : ast } );
ret.push( ' while(' );
ast2arr( ast.test, opt, { parent : ast } );
ret.push( ')' );
return ret;
case 'EmptyStatement':
ret.push( ';' );
return ret;
case 'ExpressionStatement':
ast2arr( ast.expression, opt, { parent : ast } );
ret.push( ';' );
return ret;
case 'ForStatement':
ret.push( 'for(' );
if (ast.init)
{
ast2arr( ast.init, opt, { parent : ast } );
}
else
{
ret.push( ';' );
}
if (ast.test)
{
ast2arr( ast.test, opt, { parent : ast } );
ret.push( ';' );
}
else
{
ret.push( ';' );
}
if (ast.update)
ast2arr( ast.update, opt, { parent : ast } );
ret.push( ')' );
ast2arr( ast.body, opt, { parent : ast } );
return ret;
case 'FunctionDeclaration':
case 'FunctionExpression':
ret.push( 'function ' );
if (ast.id)
ast2arr( ast.id, opt, { parent : ast } );
ret.push( '(' );
ast2arr( ast.params, opt, { parent : ast, array_sep : ',' } );
ret.push( ')' );
ast2arr( ast.body, opt, { parent : ast } );
return ret;
case 'ForInStatement':
ret.push( 'for(' );
ast2arr( ast.left, opt, { parent : ast } );
var ilast = ret.length - 1;
ret[ ilast ] = ret[ ilast ].replace( /;\s*$/, '' );
ret.push( ' in ' );
ast2arr( ast.right, opt, { parent : ast } );
ret.push( ')' );
ast2arr( ast.body, opt, { parent : ast } );
return ret;
case 'Identifier':
ret.push( ast.name );
return ret;
case 'IfStatement':
ret.push( 'if(' );
ast2arr( ast.test, opt, { parent : ast } );
ret.push( ')' );
ast2arr( ast.consequent, opt, { parent : ast } );
if (ast.alternate)
{
ret.push( ' else ' );
ast2arr( ast.alternate, opt, { parent : ast } );
}
return ret;
case 'LabeledStatement':
ast2arr( ast.label, opt, { parent : ast } );
ret.push( ':' );
ast2arr( ast.body, opt, { parent : ast } );
return ret;
case 'Literal':
ret.push( ast.raw );
return ret;
case 'MemberExpression':
ast2arr( ast.object, opt, { parent : ast } );
ret.push( ast.computed ? '[' : '.' );
ast2arr( ast.property, opt, { parent : ast } );
if (ast.computed)
ret.push( ']' );
return ret;
case 'NewExpression':
ret.push( 'new ' );
ast2arr( ast.callee, opt, { parent : ast, isLeftChild : true } );
var arg = ast.arguments;
if (arg)
{
ret.push( '(' );
ast2arr( arg, opt, { parent : ast, array_sep : ',' } );
ret.push( ')' );
}
return ret;
case 'ObjectExpression':
ret.push( '{' );
var prop = ast.properties;
for (var np = prop.length, ip = 0; ip < np; ip++)
{
if (0 < ip)
ret.push( ',' );
var pi = prop[ ip ];
if (pi.kind !== 'init')
throw new Error( 'ast2arr: unknown kind of object property' );
ast2arr( pi.key, opt, { parent : ast } );
ret.push( ':' );
ast2arr( pi.value, opt, { parent : ast } );
}
ret.push( '}' );
return ret;
case 'Program':
ast2arr( ast.body, opt, { parent : ast } );
return ret;
case 'ReturnStatement':
ret.push( 'return' );
if (ast.argument)
{
ret.push( ' ' );
ast2arr( ast.argument, opt, { parent : ast } );
}
ret.push( ';' );
return ret;
case 'SequenceExpression':
var es = ast.expressions;
for (var nes = es.length, ies = 0; ies < nes; ies++)
{
var e = es[ ies ];
if (0 < ies) ret.push( ',' );
ast2arr( e, opt, { parent : ast } );
}
return ret;
case 'SwitchCase':
if (!ast.test)
{
ret.push( 'default:' );
}
else
{
ret.push( 'case ' );
ast2arr( ast.test, opt, { parent : ast } );
ret.push( ':' );
}
ast2arr( ast.consequent, opt, { parent : ast } );
return ret;
case 'SwitchStatement':
ret.push( 'switch(' );
ast2arr( ast.discriminant, opt, { parent : ast } );
ret.push( '){' );
ast2arr( ast.cases, opt, { parent : ast } );
ret.push( '}' );
return ret;
case 'ThisExpression':
ret.push( 'this' );
return ret;
case 'ThrowStatement':
ret.push( 'throw ' );
ast2arr( ast.argument, opt, { parent : ast } );
ret.push( ';' );
return ret;
case 'TryStatement':
ret.push( 'try ' );
ast2arr( ast.block, opt, { parent : ast } );
if (ast.handler) ast2arr( ast.handler, opt );
if (ast.finalizer) ast2arr( ast.finalizer, opt, { parent : ast } );
return ret;
case 'UnaryExpression':
case 'UpdateExpression':
var nds_prn = needs_parentheses( parent, ast, isLeftChild )
, op = ast.operator
, arg = ast.argument
;
if (nds_prn) ret.push( '(' );
if (ast.prefix)
{
ret.push( op );
if (/^\w+$/.test( op )) ret.push( ' ' );
ast2arr( arg, opt, { parent : ast, isLeftChild : false } );
}
else
{
ast2arr( arg, opt, { parent : ast, isLeftChild : true } );
ret.push( op );
}
if (nds_prn) ret.push( ')' );
return ret;
case 'VariableDeclaration':
var decl = ast.declarations;
for (var nd = decl.length, id = 0; id < nd; id++)
{
ret.push( id < 1 ? 'var ' : ',' );
ast2arr( decl[ id ], opt, { parent : ast } );
}
ret.push(';');
return ret;
case 'VariableDeclarator':
ret.push( ast.id.name );
var init = ast.init;
if (init)
{
ret.push( '=' );
ast2arr( init, opt, { parent : ast } );
}
return ret;
case 'WhileStatement':
ret.push( 'while(' );
ast2arr( ast.test, opt, { parent : ast } );
ret.push( ')' );
ast2arr( ast.body, opt, { parent : ast } );
return ret;
}
console.error('xxx unknown type ' + ast.type );
error.bug;
}
function needs_parentheses( parent, ast, isLeftChild )
{
if (!parent)
return false;
// Special cases: they already wrap arguments with parentheses/brackets anyway
if (((parent.type === 'CallExpression' || parent.type === 'NewExpression') && ast !== parent.callee) ||
(parent.type === 'MemberExpression' && parent.computed && ast === parent.property)
) return false;
var ppar = precedence( parent )
, past = precedence( ast )
;
// Parent not an operator, no need for extra parentheses.
// e.g. `if(a > b)` and not `if((a > b))`
if (ppar == null)
return false;
// Both parent and child are operators, and have the same
// precedence. Try to reduce the number of parentheses based
// on associativity direction.
var l2r;
if (ppar === past &&
null != isLeftChild &&
null != (l2r = has_l2r_associativity( parent )) &&
isLeftChild === l2r
)
return false;
// All other cases
return !(ppar > past);
}
function precedence( ast )
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
{
var t = ast.type // always
, op = ast.operator // if any
, pre = ast.prefix // if any
, tNE = t === 'NewExpression'
;
if (t === 'MemberExpression' || tNE && ast.arguments)
return 1;
if (t === 'CallExpression' || tNE && !ast.arguments)
return 2;
var tUpE = t === 'UpdateExpression';
if (tUpE && !pre && (op === '++' || op === '--'))
return 3;
var tUnE = t === 'UnaryExpression';
if ((tUpE && pre && (op === '++' || op === '--')) ||
(tUnE && pre && (op === '!' || op === '~' || op === '+' || op === '-' || op === 'typeof' || op === 'void' || op === 'delete'))
)
return 4;
if (t === 'BinaryExpression')
{
if (op === '*' || op === '/' || op === '%')
return 5;
if (op === '+' || op === '-')
return 6;
if (op === '<<' || op === '>>' || op === '>>>')
return 7;
if (op === '<' || op === '<=' || op === '>' || op === '>=' || op === 'in' || op === 'instanceof')
return 8;
if (op === '==' || op === '!=' || op === '===' || op === '!==')
return 9;
if (op === '&')
return 10;
if (op === '^')
return 11;
if (op === '|')
return 12;
}
if (t === 'LogicalExpression')
{
if (op === '&&')
return 13;
if (op === '||')
return 14;
}
if (t === 'ConditionalExpression')
return 15;
// yield not supported yet by acorn
if (t === 'AssignmentExpression')
return 17;
// spread not supported yet by acorn
if (t === 'SequenceExpression')
return 19;
}
function has_l2r_associativity( ast )
// Returns `true`, `false` or `null` if not applicable.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence
{
var t = ast.type // always
, op = ast.operator // if any
, pre = ast.prefix // if any
;
if (t === 'MemberExpression')
return true;
if (t === 'CallExpression')
return true;
if (t === 'NewExpression' && !ast.arguments)
return false;
var tUpE = t === 'UpdateExpression'
, tUnE = t === 'UnaryExpression'
;
if ((tUpE && pre && (op === '++' || op === '--')) ||
(tUnE && pre && (op === '!' || op === '~' || op === '+' || op === '-' || op === 'typeof' || op === 'void' || op === 'delete'))
)
return false;
if (t === 'BinaryExpression' || t === 'LogicalExpression')
return true;
if (t === 'ConditionalExpression' || t === 'AssignmentExpression') // yield not supported yet by acorn
return false;
if (t === 'SequenceExpression')
return true;
return null;
}
})(global || exports || this);