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
|
# File 'lib/ruby2js/filter/functions.rb', line 24
def on_send(node)
target, method, *args = node.children
return super if excluded?(method)
if [:max, :min].include? method and args.length == 0
return super unless node.is_method?
process S(:send, s(:const, nil, :Math), node.children[1],
s(:splat, target))
elsif method == :call and target and target.type == :ivar
process S(:send, s(:self), "_#{target.children.first.to_s[1..-1]}",
*args)
elsif method == :call and target and target.type == :cvar
process S(:send, s(:attr, s(:self), :constructor),
"_#{target.children.first.to_s[2..-1]}", *args)
elsif method == :keys and args.length == 0 and node.is_method?
process S(:send, s(:const, nil, :Object), :keys, target)
elsif method == :[]= and args.length == 3 and
args[0].type == :regexp and args[1].type == :int
index = args[1].children.first
parts = Regexp::Parser.parse(args[0].children.first.children.first)
split = parts.index do |part|
part.type == :group and part.number == index
end
return super unless split
rewritten = parts[split].to_s
dstr = [args.last]
if rewritten == '()'
index -= 1
rewritten = ''
end
parts = parts.to_a
pre = ''
if parts.first.type == :anchor
pre = parts.shift.to_s
split -= 1
end
if split > 0
if split == 1 and parts.first.type == :group
rewritten = parts.first.to_s + rewritten
else
rewritten = '(' + parts[0 .. split - 1].join + ')' + rewritten
index += 1
end
dstr.unshift s(:send, s(:lvar, :match), :[], s(:int, 0))
end
post = ''
if parts.last.type == :anchor
post = parts.pop.to_s
end
if split + 1 < parts.length
if split + 2 == parts.length and parts.last.type == :group
rewritten += parts.last.to_s
else
rewritten += '(' + parts[split + 1 .. -1].join + ')'
end
dstr << s(:send, s(:lvar, :match), :[], s(:int, index))
end
rewritten = pre + rewritten + post
regex = process s(:regexp, s(:str, rewritten), args[0].children.last)
block = s(:block,
s(:send, target, :replace, regex),
s(:args, s(:arg, :match)),
process(s(:dstr, *dstr)))
if VAR_TO_ASSIGN.keys.include? target.type
S(VAR_TO_ASSIGN[target.type], target.children.first, block)
elsif target.type == :send
if target.children[0] == nil
S(:lvasgn, target.children[1], block)
else
S(:send, target.children[0], :"#{target.children[1]}=", block)
end
else
super
end
elsif method == :merge
args.unshift target
if es2015
if es2018
process S(:hash, *args.map {|arg| s(:kwsplat, arg)})
else
process S(:send, s(:const, nil, :Object), :assign, s(:hash),
*args)
end
else
copy = [s(:gvasgn, :$$, s(:hash))]
s(:send, s(:block, s(:send, nil, :lambda), s(:args),
s(:begin, *copy, *args.map {|modname|
if modname.type == :hash
s(:begin, *modname.children.map {|pair|
s(:send, s(:gvar, :$$), :[]=, *pair.children)
})
else
s(:for, s(:lvasgn, :$_), modname,
s(:send, s(:gvar, :$$), :[]=,
s(:lvar, :$_), s(:send, modname, :[], s(:lvar, :$_))))
end
}, s(:return, s(:gvar, :$$)))), :[])
end
elsif method == :merge!
if es2015
process S(:send, s(:const, nil, :Object), :assign, target, *args)
else
copy = []
unless
target.type == :send and target.children.length == 2 and
target.children[0] == nil
then
copy << s(:gvasgn, :$0, target)
target = s(:gvar, :$0)
end
s(:send, s(:block, s(:send, nil, :lambda), s(:args),
s(:begin, *copy, *args.map {|modname|
if modname.type == :hash
s(:begin, *modname.children.map {|pair|
s(:send, target, :[]=, *pair.children)
})
else
s(:for, s(:lvasgn, :$_), modname,
s(:send, target, :[]=,
s(:lvar, :$_), s(:send, modname, :[], s(:lvar, :$_))))
end
}, s(:return, target))), :[])
end
elsif method == :delete and args.length == 1
if not target
process S(:undef, args.first)
elsif args.first.type == :str
process S(:undef, S(:attr, target, args.first.children.first))
else
process S(:undef, S(:send, target, :[], args.first))
end
elsif method == :to_s
process S(:call, target, :toString, *args)
elsif method == :Array and target == nil
if es2015
process S(:send, s(:const, nil, :Array), :from, *args)
else
process S(:send, s(:attr, s(:attr, s(:const, nil, :Array),
:prototype), :slice), :call, *args)
end
elsif method == :to_i
process node.updated :send, [nil, :parseInt, target, *args]
elsif method == :to_f
process node.updated :send, [nil, :parseFloat, target, *args]
elsif method == :sub and args.length == 2
if args[1].type == :str
args[1] = s(:str, args[1].children.first.gsub(/\\(\d)/, "$\\1"))
end
process node.updated nil, [target, :replace, *args]
elsif [:sub!, :gsub!].include? method
method = :"#{method.to_s[0..-2]}"
if VAR_TO_ASSIGN.keys.include? target.type
process S(VAR_TO_ASSIGN[target.type], target.children[0],
S(:send, target, method, *node.children[2..-1]))
elsif target.type == :send
if target.children[0] == nil
process S(:lvasgn, target.children[1], S(:send,
S(:lvar, target.children[1]), method, *node.children[2..-1]))
else
process S(:send, target.children[0], :"#{target.children[1]}=",
S(:send, target, method, *node.children[2..-1]))
end
else
super
end
elsif method == :scan and args.length == 1
arg = args.first
if arg.type == :str
arg = arg.updated(:regexp,
[s(:str, Regexp.escape(arg.children.first)), s(:regopt)])
end
if arg.type == :regexp
pattern = arg.children.first.children.first
pattern = pattern.gsub(/\\./, '').gsub(/\[.*\]/, '')
gpattern = arg.updated(:regexp, [*arg.children[0...-1],
s(:regopt, :g, *arg.children.last)])
else
gpattern = s(:send, s(:const, nil, :RegExp), :new, arg, s(:str, 'g'))
end
if arg.type != :regexp or pattern.include? '('
if es2020
s(:send, s(:const, nil, :Array), :from,
s(:send, process(target), :matchAll, gpattern),
s(:block, s(:send, nil, :proc), s(:args, s(:arg, :s)),
s(:send, s(:lvar, :s), :slice, s(:int, 1))))
else
s(:block, s(:send,
s(:send, process(target), :match, gpattern), :map),
s(:args, s(:arg, :s)),
s(:return, s(:send, s(:send, s(:lvar, :s), :match, arg),
:slice, s(:int, 1))))
end
else
S(:send, process(target), :match, gpattern)
end
elsif method == :gsub and args.length == 2
before, after = args
if before.type == :regexp
before = before.updated(:regexp, [*before.children[0...-1],
s(:regopt, :g, *before.children.last)])
elsif before.type == :str
before = before.updated(:regexp,
[s(:str, Regexp.escape(before.children.first)), s(:regopt, :g)])
end
if after.type == :str
after = s(:str, after.children.first.gsub(/\\(\d)/, "$\\1"))
end
process node.updated nil, [target, :replace, before, after]
elsif method == :ord and args.length == 0
if target.type == :str
process S(:int, target.children.last.ord)
else
process S(:send, target, :charCodeAt, s(:int, 0))
end
elsif method == :chr and args.length == 0
if target.type == :int
process S(:str, target.children.last.chr)
else
process S(:send, s(:const, nil, :String), :fromCharCode, target)
end
elsif method == :empty? and args.length == 0
process S(:send, S(:attr, target, :length), :==, s(:int, 0))
elsif method == :nil? and args.length == 0
process S(:send, target, :==, s(:nil))
elsif [:start_with?, :end_with?].include? method and args.length == 1
if es2015
if method == :start_with?
process S(:send, target, :startsWith, *args)
else
process S(:send, target, :endsWith, *args)
end
else
if args.first.type == :str
length = S(:int, args.first.children.first.length)
else
length = S(:attr, *args, :length)
end
if method == :start_with?
process S(:send, S(:send, target, :substring, s(:int, 0),
length), :==, *args)
else
process S(:send, S(:send, target, :slice,
S(:send, length, :-@)), :==, *args)
end
end
elsif method == :clear and args.length == 0 and node.is_method?
process S(:send, target, :length=, s(:int, 0))
elsif method == :replace and args.length == 1
process S(:begin, S(:send, target, :length=, s(:int, 0)),
S(:send, target, :push, s(:splat, node.children[2])))
elsif method == :include? and args.length == 1
while target.type == :begin and target.children.length == 1
target = target.children.first
end
if target.type == :irange
S(:and, s(:send, args.first, :>=, target.children.first),
s(:send, args.first, :<=, target.children.last))
elsif target.type == :erange
S(:and, s(:send, args.first, :>=, target.children.first),
s(:send, args.first, :<, target.children.last))
else
if es2016
process S(:send, target, :includes, args.first)
else
process S(:send, S(:send, target, :indexOf, args.first), :!=,
s(:int, -1))
end
end
elsif method == :respond_to? and args.length == 1
process S(:in?, args.first, target)
elsif method == :each
process S(:send, target, :forEach, *args)
elsif method == :downcase and args.length == 0
process S(:send, target, :toLowerCase)
elsif method == :upcase and args.length == 0
process S(:send, target, :toUpperCase)
elsif method == :strip and args.length == 0
process s(:send, target, :trim)
elsif node.children[0..1] == [nil, :puts]
process S(:send, s(:attr, nil, :console), :log, *args)
elsif method == :first
if node.children.length == 2
process S(:send, target, :[], s(:int, 0))
elsif node.children.length == 3
process on_send S(:send, target, :[], s(:erange,
s(:int, 0), node.children[2]))
else
super
end
elsif method == :last
if node.children.length == 2
process on_send S(:send, target, :[], s(:int, -1))
elsif node.children.length == 3
process S(:send, target, :slice,
s(:send, s(:attr, target, :length), :-, node.children[2]),
s(:attr, target, :length))
else
super
end
elsif method == :[] and target == s(:const, nil, :Hash)
s(:send, s(:const, nil, :Object), :fromEntries, *process_all(args))
elsif method == :[]
i = proc do |index|
if index.type == :int and index.children.first < 0
process S(:send, S(:attr, target, :length), :-,
s(:int, -index.children.first))
else
index
end
end
index = args.first
if not index
super
elsif index.type == :regexp
if es2020
process S(:csend,
S(:send, process(target), :match, index),
:[], args[1] || s(:int, 0))
else
process S(:send,
s(:or, S(:send, process(target), :match, index), s(:array)),
:[], args[1] || s(:int, 0))
end
elsif node.children.length != 3
super
elsif index.type == :int and index.children.first < 0
process S(:send, target, :[], i.(index))
elsif index.type == :erange
start, finish = index.children
if not finish
process S(:send, target, :slice, start)
elsif finish.type == :int
process S(:send, target, :slice, i.(start), finish)
else
process S(:send, target, :slice, i.(start), i.(finish))
end
elsif index.type == :irange
start, finish = index.children
if finish and finish.type == :int
final = S(:int, finish.children.first+1)
else
final = S(:send, finish, :+, s(:int, 1))
end
if not finish or finish.children.first == -1
process S(:send, target, :slice, start)
else
process S(:send, target, :slice, start, final)
end
else
super
end
elsif method == :reverse! and node.is_method?
process S(:send, target, :splice, s(:int, 0),
s(:attr, target, :length), s(:splat, S(:send, target,
:reverse, *node.children[2..-1])))
elsif method == :each_with_index
process S(:send, target, :forEach, *args)
elsif method == :inspect and args.length == 0
S(:send, s(:const, nil, :JSON), :stringify, process(target))
elsif method == :* and target.type == :str
process S(:send, s(:send, s(:const, nil, :Array), :new,
s(:send, args.first, :+, s(:int, 1))), :join, target)
elsif [:is_a?, :kind_of?].include? method and args.length == 1
if args[0].type == :const
parent = args[0].children.last
parent = :Number if parent == :Float
parent = :Object if parent == :Hash
parent = :Function if parent == :Proc
parent = :Error if parent == :Exception
parent = :RegExp if parent == :Regexp
if parent == :Array
S(:send, s(:const, nil, :Array), :isArray, target)
elsif [:Arguments, :Boolean, :Date, :Error, :Function, :Number,
:Object, :RegExp, :String].include? parent
S(:send, s(:send, s(:attr, s(:attr, s(:const, nil, Object),
:prototype), :toString), :call, target), :===,
s(:str, "[object #{parent.to_s}]"))
else
super
end
else
super
end
elsif target && target.type == :send and target.children[1] == :delete
S(:send, target.updated(:sendw), *node.children[1..-1])
elsif es2017 and method==:entries and args.length==0 and node.is_method?
process node.updated(nil, [s(:const, nil, :Object), :entries, target])
elsif es2017 and method==:values and args.length==0 and node.is_method?
process node.updated(nil, [s(:const, nil, :Object), :values, target])
elsif es2017 and method==:rjust
process node.updated(nil, [target, :padStart, *args])
elsif es2017 and method==:ljust
process node.updated(nil, [target, :padEnd, *args])
elsif es2019 and method==:flatten and args.length == 0
process node.updated(nil, [target, :flat, s(:lvar, :Infinity)])
elsif es2019 and method==:to_h and args.length==0
process node.updated(nil, [s(:const, nil, :Object), :fromEntries,
target])
elsif method==:rstrip
if es2019
process node.updated(nil, [target, :trimEnd, *args])
else
node.updated(nil, [process(target), :replace,
s(:regexp, s(:str, '\s+\z') , s(:regopt)), s(:str, '')])
end
elsif method==:lstrip and args.length == 0
if es2019
process s(:send, target, :trimStart)
else
node.updated(nil, [process(target), :replace,
s(:regexp, s(:str, '\A\s+') , s(:regopt)), s(:str, '')])
end
elsif method == :class and args.length==0 and not node.is_method?
process node.updated(:attr, [target, :constructor])
elsif method == :new and target == s(:const, nil, :Exception)
process S(:send, s(:const, nil, :Error), :new, *args)
elsif method == :block_given? and target == nil and args.length == 0
process process s(:lvar, "_implicitBlockYield")
elsif method == :abs and args.length == 0
process S(:send, s(:const, nil, :Math), :abs, target)
elsif method == :ceil and args.length == 0
process S(:send, s(:const, nil, :Math), :ceil, target)
elsif method == :floor and args.length == 0
process S(:send, s(:const, nil, :Math), :floor, target)
elsif method == :sum and args.length == 0
process S(:send, target, :reduce, s(:block, s(:send, nil, :proc),
s(:args, s(:arg, :a), s(:arg, :b)),
s(:send, s(:lvar, :a), :+, s(:lvar, :b))), s(:int, 0))
else
super
end
end
|