-
Notifications
You must be signed in to change notification settings - Fork 9
/
stringifier.rb
450 lines (357 loc) · 9.01 KB
/
stringifier.rb
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
require "parsejs/visitor"
require "json"
module ParseJS
require "pp"
class Stringifier < Visitor
def self.to_string(ast)
stringifier = new(ast)
yield stringifier if block_given?
stringifier.to_string
end
attr_accessor :include_comments
def initialize(ast)
@ast = ast
@indent = 0
@include_comments = false
end
def accept(node)
return "" if node.nil?
nl = @newline
out = if node.cuddly?
" " << super
elsif @newline
@newline = false
current_indent << super
else
super
end
out << newline if node.needs_newline? && needs_newline?(out)
out
end
def indent
@indent += 1
end
def outdent
@indent -= 1
end
def current_indent
" " * @indent
end
def newline
@newline = true
"\n"
end
def cuddle(node, out, more=false)
if node.cuddly?
node.cuddle! if more
else
indent
out << newline
end
out << accept(node)
if more && node.cuddly?
out << " "
@newline = false
end
unless node.cuddly?
outdent
out << current_indent if more
end
end
def without_newline
old, @skip_newline = @skip_newline, true
ret = yield
@skip_newline = old
ret
end
def needs_newline?(out)
out !~ /\n$/ && !@skip_newline
end
def strip_newline(str)
@newline = false
str.sub(/\n$/, '')
end
def to_string
accept @ast
end
def params(node)
map(node).join(", ")
end
def each(node)
node.each { |element| accept(element) }
end
def visit_Program(program)
map(program.elements).join("")
end
def visit_ExpressionStatement(statement)
accept(statement.expression) + ";"
end
def visit_SequenceExpression(expression)
out = ""
out << "(" if expression.parens
exprs = params(expression.expressions)
exprs = strip_newline(exprs) if expression.parens
out << exprs
out << ")" if expression.parens
out
end
def visit_Literal(literal)
case val = literal.val
when nil
"null"
when ParseJS::AST::Node
accept val
else
val.inspect
end
end
def visit_String(string)
string.quote + super + string.quote
end
def visit_RegExp(regex)
"/#{super.join("/")}"
end
def visit_DebuggerStatement(expr)
"#{super};"
end
def visit_UnaryExpression(unary)
op, argument = super
space = op =~ /\w/ ? sp : ""
"#{op}#{space}#{argument}"
end
def visit_AssignmentExpression(expr)
left, op, right = super
"#{left} #{op} #{right}"
end
def visit_CallExpression(expr)
out = strip_newline(accept(expr.callee))
args = params(expr.args)
args = strip_newline(args)
out << "(" + args + ")"
end
def visit_ArrayExpression(expr)
"[" + begin
last, out = expr.elements.size - 1, ""
expr.elements.each_with_index do |element, i|
if element.nil?
out << ","
out << " " unless i == last
else
out << accept(element)
out << ", " unless i == last
end
end
out
end + "]"
end
def visit_ObjectExpression(expr)
if expr.properties.length > 2
out = "{" << newline
indent
last = expr.properties.size - 1
expr.properties.each_with_index do |prop, i|
out << strip_newline(accept(prop))
out << "," unless last == i
out << newline
end
outdent
out << current_indent << "}"
else
"{#{params(expr.properties)}}"
end
end
def visit_Property(property)
comments, key, value = super
"#{comments.join}#{key}: #{value}"
end
def visit_CommentedStatement(statement)
comments, statement = super
"#{comments.join}#{statement}"
end
def visit_MemberExpression(expr)
left = strip_newline(accept(expr.object))
right = accept(expr.property)
if expr.computed
"#{left}[#{right}]"
else
"#{left}.#{right}"
end
end
def visit_NewExpression(expr)
callee, args = super
left = "new #{callee}"
arg_string = "(#{args.join(", ")})" if args
return "#{left}#{arg_string}"
end
def visit_BinaryExpression(expr)
left = strip_newline(accept(expr.left))
right = accept(expr.right)
"#{left} #{expr.op} #{right}"
end
def visit_BlockStatement(statement)
out = "{" << newline
indent
out << super.join
outdent
out << current_indent << "}"
@newline = false unless statement.cuddly
out
end
def visit_IfStatement(statement)
consequent = statement.consequent
alternate = statement.alternate
out = "if (" + accept(statement.test) + ")"
cuddle(consequent, out, alternate)
if alternate
out << "else"
cuddle(alternate, out, false)
end
out
end
def visit_WhileStatement(statement)
test = statement.test
body = statement.body
out = "while (" + accept(test) + ")"
cuddle(body, out, false)
out
end
def visit_DoWhileStatement(statement)
out = "do"
cuddle(statement.body, out, true)
out << "while (" + accept(statement.test) + ");"
end
def visit_ForStatement(statement)
init = statement.init
test = statement.test
update = statement.update
body = statement.body
out = ""
without_newline do
out << "for (" + accept(init) + ";"
test = accept(test)
out << " #{test}" unless test.empty?
out << ";"
update = accept(update)
out << " #{update}" unless update.empty?
out << ")"
end
cuddle(body, out, false)
out
end
def visit_VariableDeclaration(decl)
kind, declarations, semicolon = super
"#{kind} #{declarations.join(", ")}#{";" if semicolon}"
end
def visit_VariableDeclarator(decl)
id, init = super
out = id
out << (init ? " = #{init}" : "")
end
def visit_UpdateExpression(expr)
op, prefix, argument = super
op += " " if op =~ /\w/
if prefix
"#{op}#{argument}"
else
"#{argument}#{op}"
end
end
def visit_ForInStatement(statement)
left = statement.left
right = statement.right
body = statement.body
out = ""
without_newline do
out << "for (" + accept(left) + " in " + accept(right) + ")"
end
cuddle(body, out, false)
out
end
def visit_SwitchStatement(statement)
out = ""
without_newline do
out << "switch (" + accept(statement.discriminant) + ") {" << newline
indent
end
out << map(statement.cases).join
outdent
out << current_indent << "}" << newline
end
def visit_SwitchCase(switch)
if switch.test
out = "case #{accept(switch.test)}:" << newline
else
out = "default:" << newline
end
indent
out << map(switch.consequent).join
outdent
out
end
def visit_ThrowStatement(statement)
"throw #{super};"
end
def visit_TryStatement(statement)
handler = statement.handler
finalizer = statement.finalizer
out = "try"
cuddle(statement.block, out, handler || finalizer)
if handler
out << "catch (" + accept(handler.param) + ")"
cuddle(handler.body, out, finalizer)
end
if finalizer
out << "finally"
cuddle(finalizer, out, false)
end
out
end
def visit_FunctionDeclaration(decl)
id = decl.id
parameters = decl.params.list
body = decl.body
out = "function " + accept(id) + "("
out << params(parameters)
out << ") {" << newline
indent
out << map(decl.body).join
outdent
out << current_indent << "}"
end
def labeled(name, label)
out = name
out << " #{label}" if label
out << ";"
end
def visit_ReturnStatement(statement)
labeled("return", super)
end
def visit_BreakStatement(statement)
labeled("break", super)
end
def visit_ContinueStatement(statement)
labeled("continue", super)
end
def visit_ConditionalExpression(expr)
out = strip_newline(accept(expr.test))
out << " ? " << strip_newline(accept(expr.consequent))
out << " : " << accept(expr.alternate)
end
def visit_Comment(comment)
return "" unless include_comments
if comment.type == 'singleline'
"//" + comment.body + newline
else
body = comment.body.split("\n")
first = body.shift
out = "/*" + first + newline + body.map { |s| "#{current_indent}#{s}" }.join(newline) + "*/"
out << "\n" if comment.newline
out
end
end
private
def sp
" "
end
end
end