Module: Ruby2JS::Filter::Functions

Includes:
SEXP
Defined in:
lib/ruby2js/filter/functions.rb

Constant Summary collapse

VAR_TO_ASSIGN =
{
  lvar: :lvasgn,
  ivar: :ivasgn,
  cvar: :cvasgn,
  gvar: :gvasgn
}

Instance Method Summary collapse

Methods included from SEXP

#S, #s

Instance Method Details

#initialize(*args) ⇒ Object



19
20
21
22
# File 'lib/ruby2js/filter/functions.rb', line 19

def initialize(*args)
  @jsx = false
  super
end

#on_block(node) ⇒ Object



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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
# File 'lib/ruby2js/filter/functions.rb', line 553

def on_block(node)
  call = node.children.first
  method = call.children[1]
  return super if excluded?(method)

  if [:setInterval, :setTimeout, :set_interval, :set_timeout].include? method
    return super unless call.children.first == nil
    block = process s(:block, s(:send, nil, :proc), *node.children[1..-1])
    on_send call.updated nil, [*call.children[0..1], block,
      *call.children[2..-1]]

  elsif [:sub, :gsub, :sub!, :gsub!, :sort!].include? method
    return super if call.children.first == nil
    block = s(:block, s(:send, nil, :proc), node.children[1],
      s(:autoreturn, *node.children[2..-1]))
    process call.updated(nil, [*call.children, block])

  elsif method == :select and call.children.length == 2
    call = call.updated nil, [call.children.first, :filter]
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif method == :any? and call.children.length == 2
    call = call.updated nil, [call.children.first, :some]
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif method == :all? and call.children.length == 2
    call = call.updated nil, [call.children.first, :every]
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif method == :find and call.children.length == 2
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif method == :find_index and call.children.length == 2
    call = call.updated nil, [call.children.first, :findIndex]
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif method == :map and call.children.length == 2
    node.updated nil, [process(call), process(node.children[1]),
      s(:autoreturn, *process_all(node.children[2..-1]))]

  elsif [:map!, :select!].include? method
    # input: a.map! {expression}
    # output: a.splice(0, a.length, *a.map {expression})
    method = (method == :map! ? :map : :select)
    target = call.children.first
    process call.updated(:send, [target, :splice, s(:splat, s(:send,
      s(:array, s(:int, 0), s(:attr, target, :length)), :concat,
      s(:block, s(:send, target, method, *call.children[2..-1]),
      *node.children[1..-1])))])

  elsif node.children[0..1] == [s(:send, nil, :loop), s(:args)]
    # input: loop {statements}
    # output: while(true) {statements}
    S(:while, s(:true), node.children[2])

  elsif method == :delete
    # restore delete methods that are prematurely mapped to undef
    result = super

    if result.children[0].type == :undef
      call = result.children[0].children[0]
      if call.type == :attr
        call = call.updated(:send,
          [call.children[0], :delete, s(:str, call.children[1])])
        result = result.updated(nil, [call, *result.children[1..-1]])
      else
        call = call.updated(nil,
          [call.children[0], :delete, *call.children[2..-1]])
        result = result.updated(nil, [call, *result.children[1..-1]])
      end
    end

    result

  elsif method == :downto
    range = s(:irange, call.children[0], call.children[2])
    call = call.updated(nil, [s(:begin, range), :step, s(:int, -1)])
    process node.updated(nil, [call, *node.children[1..-1]])

  elsif method == :upto
    range = s(:irange, call.children[0], call.children[2])
    call = call.updated(nil, [s(:begin, range), :step, s(:int, 1)])
    process node.updated(nil, [call, *node.children[1..-1]])

  elsif \
    method == :each and call.children[0].type == :send and
    call.children[0].children[1] == :step
  then
    # i.step(j, n).each {|v| ...}
    range = call.children[0]
    step = range.children[3] || s(:int, 1)
    call = call.updated(nil, [s(:begin,
      s(:irange, range.children[0], range.children[2])),
      :step, step])
    process node.updated(nil, [call, *node.children[1..-1]])

  elsif \
    # (a..b).each {|v| ...}
    method == :each and
    call.children[0].type == :begin and
    call.children[0].children.length == 1 and
    [:irange, :erange].include? call.children[0].children[0].type and
    node.children[1].children.length == 1
  then
    process s(:for, s(:lvasgn, node.children[1].children[0].children[0]),
      call.children[0].children[0], node.children[2])

  elsif \
    [:each, :each_value].include? method
  then
    if es2015 or @jsx
      if node.children[1].children.length > 1
        process node.updated(:for_of,
          [s(:mlhs, *node.children[1].children.map {|child|
            s(:lvasgn, child.children[0])}),
          node.children[0].children[0], node.children[2]])
      elsif node.children[1].children[0].type == :mlhs
        process node.updated(:for_of,
          [s(:mlhs, *node.children[1].children[0].children.map {|child|
            s(:lvasgn, child.children[0])}),
          node.children[0].children[0], node.children[2]])
      else
        process node.updated(:for_of,
          [s(:lvasgn, node.children[1].children[0].children[0]),
          node.children[0].children[0], node.children[2]])
      end
    else
      process node.updated(nil, [s(:send, call.children[0],
        :forEach), *node.children[1..2]])
    end

  elsif \
    method == :each_key and
    [:each, :each_key].include? method and
    node.children[1].children.length == 1
  then
    process node.updated(:for,
      [s(:lvasgn, node.children[1].children[0].children[0]),
      node.children[0].children[0], node.children[2]])

  elsif es2015 and method == :inject
    process node.updated(:send, [call.children[0], :reduce,
      s(:block, s(:send, nil, :lambda), *node.children[1..2]),
      *call.children[2..-1]])

  elsif method == :each_pair and node.children[1].children.length == 2
    if es2017
      # Object.entries(a).forEach(([key, value]) => {})
      process node.updated(nil, [s(:send, s(:send,
      s(:const, nil, :Object), :entries, call.children[0]), :each),
      node.children[1], node.children[2]])
    else
      # for (key in a). {var value = a[key]; ...}
      process node.updated(:for, [s(:lvasgn,
        node.children[1].children[0].children[0]), call.children[0],
        s(:begin, s(:lvasgn, node.children[1].children[1].children[0],
        s(:send, call.children[0], :[], 
        s(:lvar, node.children[1].children[0].children[0]))),
        node.children[2])])
    end

  elsif method == :scan and call.children.length == 3
    process call.updated(nil, [*call.children, s(:block,
      s(:send, nil, :proc), *node.children[1..-1])])

  elsif method == :yield_self and call.children.length == 2
    process node.updated(:send, [s(:block, s(:send, nil, :proc),
      node.children[1], s(:autoreturn, node.children[2])),
      :[], call.children[0]])

  elsif method == :tap and call.children.length == 2
    process node.updated(:send, [s(:block, s(:send, nil, :proc),
      node.children[1], s(:begin, node.children[2],
      s(:return, s(:lvar, node.children[1].children[0].children[0])))),
      :[], call.children[0]])

  else
    super
  end
end

#on_class(node) ⇒ Object



739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
# File 'lib/ruby2js/filter/functions.rb', line 739

def on_class(node)
  name, inheritance, *body = node.children
  body.compact!

  if inheritance == s(:const, nil, :Exception)
    unless
      body.any? {|statement| statement.type == :def and
      statement.children.first == :initialize}
    then
      body.unshift S(:def, :initialize, s(:args, s(:arg, :message)),
        s(:begin, S(:send, s(:self), :message=, s(:lvar, :message)),
        S(:send, s(:self), :name=, s(:sym, name.children[1])),
        S(:send, s(:self), :stack=, s(:send, s(:send, nil, :Error,
        s(:lvar, :message)), :stack))))
    end

    body = [s(:begin, *body)] if body.length > 1
    S(:class, name, s(:const, nil, :Error), *body)
  else
    super
  end
end

#on_send(node) ⇒ Object



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
        # Array.from(str.matchAll(/.../g), s => s.slice(1))
        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
        # str.match(/.../g).map(s => s.match(/.../).slice(1))
        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
      # str.match(/.../g)
      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 == :[]
    # resolve negative literal indexes
    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

      # No need for the last argument if it's -1
      # This means take all to the end of array
      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?
    # input: a.reverse!
    # output: a.splice(0, a.length, *a.reverse)
    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
    # prevent chained delete methods from being converted to undef
    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