ruby2ruby 2.4.4 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0a68c49fb78276f0904646fd04f48042d124b765d63cca627967c5f36d232ad
4
- data.tar.gz: 397b684e0d7a1d64b0ec2cc9a9c6419340021d7000302f72524d1af075cc63ea
3
+ metadata.gz: 81af7fc05fec7a9e31a0e7e0e707526277c4535d0b7a5b12f67ab524989bc355
4
+ data.tar.gz: df311f1adf30683d68b615494249bd903dfb962b52936031c02b1c3fe147e1eb
5
5
  SHA512:
6
- metadata.gz: a39d237921b8aa506673f3efb48c4c0cf570ad36eca10399eef3e0a89261eb6cc65f87f4023793f61a64b3db1b73c935ba24b0cbb0a9c5d6aa394a5c6ee94863
7
- data.tar.gz: 8a3fd4b2479cc4bdb89e420f6a4173c1e30719456bc3f3b53b9828bbc243fcc65d8fbd922ac5a9516a862d2059e16c19517020fca925b730b3bbccec4eb6e1cf
6
+ metadata.gz: 60363a4606501b46962382d2343a49e1687a85f608b039936b516619209f407292052da552b1328ae02c872d7972d7b839b2de4d28ca6c2945e2a77746046417
7
+ data.tar.gz: cb1f7cf0373c3f2f8ed7adfdd2688ad2cf9b262db3f058cf7f6bb43b195ba7a1e270b168039396a26923d0d21aa2a23233f4b0c8f2e2f5e49ce3ff87efcfd30b
checksums.yaml.gz.sig CHANGED
Binary file
data/History.rdoc CHANGED
@@ -1,3 +1,19 @@
1
+ === 2.5.0 / 2022-10-04
2
+
3
+ * 5 minor enhancements:
4
+
5
+ * Added support for (most?) pattern matching.
6
+ * Added support for **kwrest.
7
+ * Added support for forward args (...), defn and call.
8
+ * Added support for preexe (BEGIN { ... }).
9
+ * Support ruby 3.1 hash shorthand syntax. (presidentbeef)
10
+
11
+ * 3 bug fixes:
12
+
13
+ * Fix generation of case w/ no else.
14
+ * Fixed a bug block shadow vars (they're grouped)
15
+ * Fixed find_pat up (after fixing a find_pat bug in ruby_parser).
16
+
1
17
  === 2.4.4 / 2019-09-24
2
18
 
3
19
  * 1 bug fix:
data/Rakefile CHANGED
@@ -79,6 +79,24 @@ task :debug => :isolate do
79
79
  puts process(ruby, file)
80
80
  end
81
81
 
82
+ task :parse => :isolate do
83
+ require "ruby_parser"
84
+ require "pp"
85
+
86
+ parser = RubyParser.for_current_ruby
87
+
88
+ file = ENV["F"]
89
+ ruby = ENV["R"]
90
+
91
+ if ruby then
92
+ file = "env"
93
+ else
94
+ ruby = File.read file
95
+ end
96
+
97
+ pp parser.process(ruby, file)
98
+ end
99
+
82
100
  task :bugs do
83
101
  sh "for f in bug*.rb ; do #{Gem.ruby} -S rake debug F=$f && rm $f ; done"
84
102
  end
data/lib/ruby2ruby.rb CHANGED
@@ -31,7 +31,7 @@ end
31
31
  # Generate ruby code from a sexp.
32
32
 
33
33
  class Ruby2Ruby < SexpProcessor
34
- VERSION = "2.4.4" # :nodoc:
34
+ VERSION = "2.5.0" # :nodoc:
35
35
 
36
36
  # cutoff for one-liners
37
37
  LINE_LENGTH = 78
@@ -134,8 +134,10 @@ class Ruby2Ruby < SexpProcessor
134
134
  _, k, v = arg
135
135
  "#{k}: #{process v}"
136
136
  when :shadow then
137
- shadow << arg[1]
137
+ shadow << arg.sexp_body
138
138
  next
139
+ when :forward_args then
140
+ "..."
139
141
  else
140
142
  raise "unknown arg type #{arg.first.inspect}"
141
143
  end
@@ -322,9 +324,10 @@ class Ruby2Ruby < SexpProcessor
322
324
  end
323
325
 
324
326
  result.concat rest.map { |pt|
325
- if pt and pt.sexp_type == :when
327
+ if pt and %i[when in].include? pt.sexp_type
326
328
  "#{process pt}"
327
329
  else
330
+ next unless pt
328
331
  code = indent process pt
329
332
  code = indent("# do nothing") if code =~ /^\s*$/
330
333
  "else\n#{code}"
@@ -333,7 +336,7 @@ class Ruby2Ruby < SexpProcessor
333
336
 
334
337
  result << "end"
335
338
 
336
- result.join "\n"
339
+ result.compact.join "\n"
337
340
  end
338
341
 
339
342
  def process_cdecl exp # :nodoc:
@@ -366,13 +369,26 @@ class Ruby2Ruby < SexpProcessor
366
369
  def process_const exp # :nodoc:
367
370
  _, name = exp
368
371
 
372
+ name = process name if Sexp === name
373
+
369
374
  name.to_s
370
375
  end
371
376
 
377
+ def __var name
378
+ case context[1]
379
+ when /_pat$/ then
380
+ "^#{name}"
381
+ when :in then
382
+ "^#{name}"
383
+ else
384
+ name.to_s
385
+ end
386
+ end
387
+
372
388
  def process_cvar exp # :nodoc:
373
389
  _, name = exp
374
390
 
375
- name.to_s
391
+ __var name
376
392
  end
377
393
 
378
394
  def process_cvasgn exp # :nodoc:
@@ -528,6 +544,10 @@ class Ruby2Ruby < SexpProcessor
528
544
  result.join "\n"
529
545
  end
530
546
 
547
+ def process_forward_args exp
548
+ "..."
549
+ end
550
+
531
551
  def process_gasgn exp # :nodoc:
532
552
  process_iasgn exp
533
553
  end
@@ -535,7 +555,7 @@ class Ruby2Ruby < SexpProcessor
535
555
  def process_gvar exp # :nodoc:
536
556
  _, name = exp
537
557
 
538
- name.to_s
558
+ __var name
539
559
  end
540
560
 
541
561
  def process_hash(exp) # :nodoc:
@@ -544,6 +564,13 @@ class Ruby2Ruby < SexpProcessor
544
564
  result = pairs.each_slice(2).map { |k, v|
545
565
  if k.sexp_type == :kwsplat then
546
566
  "%s" % process(k)
567
+ elsif v.nil?
568
+ # Shorthand hash syntax
569
+ unless k.sexp_type == :lit and k.value.is_a? Symbol
570
+ raise "Expected symbol for hash key, but got #{k.inspect}"
571
+ end
572
+
573
+ "#{k.value}:"
547
574
  else
548
575
  t = v.sexp_type
549
576
 
@@ -568,6 +595,89 @@ class Ruby2Ruby < SexpProcessor
568
595
  end
569
596
  end
570
597
 
598
+ def process_in exp # :nodoc:
599
+ _, lhs, *rhs = exp
600
+
601
+ cond = process lhs
602
+ body = rhs.compact.map { |sexp| indent process sexp }
603
+
604
+ body << indent("# do nothing") if body.empty?
605
+ body = body.join "\n"
606
+
607
+ "in #{cond} then\n#{body.chomp}"
608
+ end
609
+
610
+ def process_find_pat exp # :nodoc:
611
+ _, a, b, *c, d = exp
612
+
613
+ a = process a # might be nil
614
+ b = process b if Sexp === b
615
+ c = c.map { |sexp| process sexp }.join(", ")
616
+ d = process d if Sexp === d
617
+
618
+ "%s[%s, %s, %s]" % [a, b, c, d]
619
+ end
620
+
621
+ def process_array_pat exp # :nodoc:
622
+ _, const, *rest = exp
623
+
624
+ const = process const if const
625
+
626
+ rest = rest.map { |sexp|
627
+ case sexp
628
+ when Sexp
629
+ process sexp
630
+ else
631
+ sexp
632
+ end
633
+ }
634
+
635
+ "%s[%s]" % [const, rest.join(", ")]
636
+ end
637
+
638
+ def process_kwrest exp # :nodoc:
639
+ _, name = exp
640
+
641
+ name.to_s
642
+ end
643
+
644
+ def process_hash_pat exp # :nodoc:
645
+ _, const, *rest = exp
646
+
647
+ if const then
648
+ const = process const
649
+ kwrest = process rest.pop if rest.last && rest.last.sexp_type == :kwrest
650
+
651
+ rest = rest.each_slice(2).map { |k, v|
652
+ k = process(k).delete_prefix ":"
653
+ v = process v
654
+
655
+ "#{k}: #{v}".strip
656
+ }
657
+
658
+ rest << kwrest if kwrest
659
+
660
+ rest.empty? ? "%s[]" % const : "%s[%s]" % [const, rest.join(", ")]
661
+ else
662
+ kwrest = process rest.pop if rest.last && rest.last.sexp_type == :kwrest
663
+
664
+ rest = rest.each_slice(2).map { |k, v|
665
+ k = process(k).delete_prefix ":"
666
+ v = process v
667
+
668
+ "#{k}: #{v}".strip
669
+ }
670
+
671
+ rest << kwrest if kwrest
672
+
673
+ rest.empty? ? "{}" : "{ %s }" % [rest.join(", ")]
674
+ end
675
+ end
676
+
677
+ def process_preexe exp # :nodoc:
678
+ "BEGIN"
679
+ end
680
+
571
681
  def process_if(exp) # :nodoc:
572
682
  _, c, t, f = exp
573
683
 
@@ -610,6 +720,8 @@ class Ruby2Ruby < SexpProcessor
610
720
  "->"
611
721
  end
612
722
 
723
+ MUST_BE_CURLY = %w[ BEGIN END ]
724
+
613
725
  def process_iter(exp) # :nodoc:
614
726
  _, iter, args, body = exp
615
727
 
@@ -627,7 +739,7 @@ class Ruby2Ruby < SexpProcessor
627
739
  " |#{process(args)[1..-2]}|"
628
740
  end
629
741
 
630
- b, e = if iter == "END" then
742
+ b, e = if MUST_BE_CURLY.include? iter then
631
743
  %w[ { } ]
632
744
  else
633
745
  %w[ do end ]
@@ -676,7 +788,8 @@ class Ruby2Ruby < SexpProcessor
676
788
 
677
789
  def process_ivar(exp) # :nodoc:
678
790
  _, name = exp
679
- name.to_s
791
+
792
+ __var name
680
793
  end
681
794
 
682
795
  def process_kwsplat(exp)
@@ -687,9 +800,18 @@ class Ruby2Ruby < SexpProcessor
687
800
  def process_lasgn(exp) # :nodoc:
688
801
  _, name, value = exp
689
802
 
690
- s = "#{name}"
691
- s += " = #{process value}" if value
692
- s
803
+ value = process value
804
+
805
+ if value then
806
+ "%s %s %s" % case context[1]
807
+ when /_pat$/ then
808
+ [value, "=>", name]
809
+ else
810
+ [name, "=", value]
811
+ end
812
+ else
813
+ "%s" % [name]
814
+ end
693
815
  end
694
816
 
695
817
  def process_lit exp # :nodoc:
@@ -704,7 +826,8 @@ class Ruby2Ruby < SexpProcessor
704
826
 
705
827
  def process_lvar(exp) # :nodoc:
706
828
  _, name = exp
707
- name.to_s
829
+
830
+ __var name
708
831
  end
709
832
 
710
833
  def process_masgn(exp) # :nodoc:
@@ -793,12 +916,20 @@ class Ruby2Ruby < SexpProcessor
793
916
 
794
917
  def process_op_asgn exp # :nodoc:
795
918
  # [[:lvar, :x], [:call, nil, :z, [:lit, 1]], :y, :"||"]
796
- _, lhs, rhs, index, op = exp
797
919
 
798
- lhs = process lhs
799
- rhs = process rhs
920
+ case exp.length
921
+ when 4
922
+ raise "NOT YET: op_asgn 4"
923
+ when 5
924
+ _, lhs, rhs, index, op = exp
925
+
926
+ lhs = process lhs
927
+ rhs = process rhs
800
928
 
801
- "#{lhs}.#{index} #{op}= #{rhs}"
929
+ "#{lhs}.#{index} #{op}= #{rhs}"
930
+ else
931
+ raise ArgumentError, "Don't know how to process this length: %p" % [exp]
932
+ end
802
933
  end
803
934
 
804
935
  def process_op_asgn1(exp) # :nodoc:
@@ -840,7 +971,15 @@ class Ruby2Ruby < SexpProcessor
840
971
  def process_or(exp) # :nodoc:
841
972
  _, lhs, rhs = exp
842
973
 
843
- "(#{process lhs} or #{process rhs})"
974
+ lhs = process lhs
975
+ rhs = process rhs
976
+
977
+ case context[1]
978
+ when :in then
979
+ "(#{lhs} | #{rhs})"
980
+ else
981
+ "(#{lhs} or #{rhs})"
982
+ end
844
983
  end
845
984
 
846
985
  def process_postexe(exp) # :nodoc:
@@ -1029,10 +1168,6 @@ class Ruby2Ruby < SexpProcessor
1029
1168
  end
1030
1169
 
1031
1170
  def process_when exp # :nodoc:
1032
- s(:when, s(:array, s(:lit, 1)),
1033
- s(:call, nil, :puts, s(:str, "something")),
1034
- s(:lasgn, :result, s(:str, "red")))
1035
-
1036
1171
  _, lhs, *rhs = exp
1037
1172
 
1038
1173
  cond = process(lhs)[1..-2]
@@ -100,8 +100,8 @@ class TestRuby2Ruby < R2RTestCase
100
100
  end
101
101
 
102
102
  def test_hash_parens_lvar
103
- inn = s(:hash, s(:lit, :k), s(:lvar, :x))
104
- out = "{ :k => x }"
103
+ inn = s(:hash, s(:lit, :k), s(:lvar, :a))
104
+ out = "{ :k => a }"
105
105
  assert_parse inn, out
106
106
  end
107
107
 
@@ -118,6 +118,23 @@ class TestRuby2Ruby < R2RTestCase
118
118
  assert_parse inn, out
119
119
  end
120
120
 
121
+ def test_hash_shorthand
122
+ inn = s(:hash, s(:lit, :k), nil)
123
+ out = '{ k: }'
124
+
125
+ assert_parse inn, out
126
+ end
127
+
128
+ def test_hash_shorthand_invalid_key_type
129
+ do_not_check_sexp!
130
+
131
+ inn = s(:hash, s(:str, 'k'), nil)
132
+ out = '{ k: }'
133
+ assert_raises do
134
+ assert_parse inn, out
135
+ end
136
+ end
137
+
121
138
  def test_and_alias
122
139
  inn = s(:and, s(:true), s(:alias, s(:lit, :a), s(:lit, :b)))
123
140
  out = "true and (alias :a :b)"
@@ -351,15 +368,6 @@ class TestRuby2Ruby < R2RTestCase
351
368
  assert_parse inn, out
352
369
  end
353
370
 
354
- def test_defn_kwargs
355
- inn = s(:defn, :initialize,
356
- s(:args, :arg, s(:kwarg, :keyword, s(:nil)), :"**args"),
357
- s(:nil))
358
- out = "def initialize(arg, keyword: nil, **args)\n # do nothing\nend"
359
-
360
- assert_parse inn, out
361
- end
362
-
363
371
  def test_defn_kwargs2
364
372
  inn = s(:defn, :initialize,
365
373
  s(:args, :arg,
@@ -373,55 +381,34 @@ class TestRuby2Ruby < R2RTestCase
373
381
  end
374
382
 
375
383
  def test_call_self_index
376
- assert_parse s(:call, nil, :[], s(:lit, 42)), "self[42]"
384
+ assert_parse s(:call, s(:self), :[], s(:lit, 42)), "self[42]"
377
385
  end
378
386
 
379
387
  def test_call_self_index_equals
380
388
  assert_parse(s(:attrasgn, s(:self), :[]=, s(:lit, 42), s(:lit, 24)),
381
389
  "self[42] = 24")
382
- end
383
-
384
- def test_call_self_index_equals_array
385
390
  assert_parse(s(:attrasgn, s(:self), :[]=, s(:lit, 1), s(:lit, 2), s(:lit, 3)),
386
391
  "self[1, 2] = 3")
387
392
  end
388
393
 
389
- def test_call_arglist_hash_first
390
- inn = s(:call, nil, :method,
391
- s(:hash, s(:lit, :a), s(:lit, 1)),
392
- s(:call, nil, :b))
393
- out = "method({ :a => 1 }, b)"
394
-
395
- assert_parse inn, out
396
- end
397
-
398
394
  def test_call_arglist_hash_first_last
399
395
  inn = s(:call, nil, :method,
400
396
  s(:hash, s(:lit, :a), s(:lit, 1)),
401
- s(:call, nil, :b),
397
+ s(:lvar, :b),
402
398
  s(:hash, s(:lit, :c), s(:lit, 1)))
403
399
  out = "method({ :a => 1 }, b, :c => 1)"
404
400
 
405
401
  assert_parse inn, out
406
402
  end
407
403
 
408
- def test_call_arglist_hash_last
409
- inn = s(:call, nil, :method,
410
- s(:call, nil, :b),
411
- s(:hash, s(:lit, :a), s(:lit, 1)))
412
- out = "method(b, :a => 1)"
413
-
414
- assert_parse inn, out
415
- end
416
-
417
404
  def test_call_arglist_if
418
405
  inn = s(:call,
419
- s(:call, nil, :a),
406
+ s(:lvar, :a),
420
407
  :+,
421
408
  s(:if,
422
- s(:call, nil, :b),
423
- s(:call, nil, :c),
424
- s(:call, nil, :d)))
409
+ s(:lvar, :b),
410
+ s(:lvar, :c),
411
+ s(:lvar, :d)))
425
412
 
426
413
  out = "(a + (b ? (c) : (d)))"
427
414
  assert_parse inn, out
@@ -443,12 +430,25 @@ class TestRuby2Ruby < R2RTestCase
443
430
  assert_parse inn, out
444
431
  end
445
432
 
433
+ def test_forward_args__defn
434
+ inn = s(:defn, :x, s(:args, :a, s(:forward_args)), s(:nil))
435
+ out = "def x(a, ...)\n # do nothing\nend"
436
+
437
+ assert_parse inn, out
438
+ end
439
+
440
+ def test_forward_args__call
441
+ inn = s(:call, nil, :y, s(:forward_args))
442
+ out = "y(...)"
443
+
444
+ assert_parse inn, out
445
+ end
446
+
446
447
  def test_shadow_block_args
447
448
  inn = s(:iter,
448
449
  s(:call, nil, :a),
449
450
  s(:args,
450
- s(:shadow, :b),
451
- s(:shadow, :c)))
451
+ s(:shadow, :b, :c)))
452
452
  out = 'a { |; b, c| }'
453
453
 
454
454
  assert_parse inn, out
@@ -481,19 +481,6 @@ class TestRuby2Ruby < R2RTestCase
481
481
  assert_parse inn, out
482
482
  end
483
483
 
484
- def test_multiple_nested_masgn_block_arg
485
- inn = s(:iter,
486
- s(:call, nil, :a),
487
- s(:args, :b,
488
- s(:masgn,
489
- s(:masgn, :c, :d),
490
- :e,
491
- s(:masgn, :f, :g))))
492
- out = "a { |b, ((c, d), e, (f, g))| }"
493
-
494
- assert_parse inn, out
495
- end
496
-
497
484
  def test_multiple_nested_masgn_array
498
485
  inn = s(:masgn,
499
486
  s(:array,
@@ -544,6 +531,13 @@ class TestRuby2Ruby < R2RTestCase
544
531
  assert_parse inn, out
545
532
  end
546
533
 
534
+ def test_preexe
535
+ inn = s(:iter, s(:preexe), 0, s(:block, s(:lit, 1), s(:lit, 2), s(:lit, 3)))
536
+ out = "BEGIN {\n 1\n 2\n 3\n}"
537
+
538
+ assert_parse inn, out
539
+ end
540
+
547
541
  def test_safe_attrasgn
548
542
  inn = s(:safe_attrasgn,
549
543
  s(:call, nil, :x),
@@ -578,6 +572,8 @@ class TestRuby2Ruby < R2RTestCase
578
572
  end
579
573
 
580
574
  def test_safe_op_asgn
575
+ do_not_check_sexp! # TODO: fix!
576
+
581
577
  inn = s(:safe_op_asgn,
582
578
  s(:call, nil, :x),
583
579
  s(:call, nil, :z, s(:lit, 1)),
@@ -611,38 +607,6 @@ class TestRuby2Ruby < R2RTestCase
611
607
  assert_parse inn, out
612
608
  end
613
609
 
614
- def test_resbody_block
615
- inn = s(:rescue,
616
- s(:call, nil, :x1),
617
- s(:resbody,
618
- s(:array),
619
- s(:call, nil, :x2),
620
- s(:call, nil, :x3)))
621
-
622
- out = "begin\n x1\nrescue\n x2\n x3\nend"
623
- assert_parse inn, out
624
- end
625
-
626
- def test_resbody_short_with_begin_end
627
- # "begin; blah; rescue; []; end"
628
- inn = s(:rescue,
629
- s(:call, nil, :blah),
630
- s(:resbody, s(:array), s(:array)))
631
- out = "blah rescue []"
632
- assert_parse inn, out
633
- end
634
-
635
- def test_resbody_short_with_begin_end_multiple
636
- # "begin; blah; rescue; []; end"
637
- inn = s(:rescue,
638
- s(:call, nil, :blah),
639
- s(:resbody, s(:array),
640
- s(:call, nil, :log),
641
- s(:call, nil, :raise)))
642
- out = "begin\n blah\nrescue\n log\n raise\nend"
643
- assert_parse inn, out
644
- end
645
-
646
610
  def test_resbody_short_with_defn_multiple
647
611
  inn = s(:defn,
648
612
  :foo,
@@ -678,9 +642,7 @@ class TestRuby2Ruby < R2RTestCase
678
642
  end
679
643
 
680
644
  def test_call_binary_call_with_hash_arg
681
- # if 42
682
- # args << {:key => 24}
683
- # end
645
+ # args << {:key => 24} if 42
684
646
 
685
647
  inn = s(:if, s(:lit, 42),
686
648
  s(:call, s(:call, nil, :args),
@@ -704,8 +666,8 @@ class TestRuby2Ruby < R2RTestCase
704
666
 
705
667
  def test_binary_operators_with_multiple_arguments
706
668
  Ruby2Ruby::BINARY.each do |op|
707
- inn = s(:call, s(:lvar, :x), op, s(:lit, 2), s(:lit, 3))
708
- out = "x.#{op}(2, 3)"
669
+ inn = s(:call, s(:lvar, :a), op, s(:lit, 2), s(:lit, 3))
670
+ out = "a.#{op}(2, 3)"
709
671
  assert_parse inn, out
710
672
  end
711
673
  end
@@ -722,6 +684,297 @@ class TestRuby2Ruby < R2RTestCase
722
684
  assert_parse inn, out
723
685
  end
724
686
 
687
+ def test_case_in_normal_01
688
+ assert_case_in "var", s(:lasgn, :var)
689
+ end
690
+
691
+ def test_case_in_normal_02
692
+ assert_case_in "^var", s(:lvar, :var)
693
+ end
694
+
695
+ def test_case_in_normal_04
696
+ assert_case_in "A if true", s(:if, s(:true), s(:const, :A), nil)
697
+ end
698
+
699
+ def test_case_in_normal_05
700
+ assert_case_in "A unless true", s(:if, s(:true), nil, s(:const, :A))
701
+ end
702
+
703
+ def test_case_in_normal_06
704
+ assert_case_in "A::B", s(:const, s(:colon2, s(:const, :A), :B))
705
+ end
706
+
707
+ def test_case_in_normal_07
708
+ assert_case_in "A | B", s(:or, s(:const, :A), s(:const, :B)), "(A | B)"
709
+
710
+ # TODO: assert_case_in "(A or B)", s(:const, s(:colon2, s(:colon3, :A), :B))
711
+ end
712
+
713
+ def test_case_in_normal_08
714
+ assert_case_in "::A::B", s(:const, s(:colon2, s(:colon3, :A), :B))
715
+ end
716
+
717
+ def test_case_in_normal_09
718
+ assert_case_in "A", s(:const, :A)
719
+ end
720
+
721
+ def test_case_in__array_pat_00
722
+ assert_case_in "Object[]", s(:array_pat, s(:const, :Object))
723
+ end
724
+
725
+ def test_case_in__array_pat_01
726
+ assert_case_in "[]", s(:array_pat)
727
+ end
728
+
729
+ def test_case_in__array_pat_02
730
+ assert_case_in "[*, ::NilClass]", s(:array_pat, nil, :*, s(:colon3, :NilClass))
731
+ end
732
+
733
+ def test_case_in__array_pat_03
734
+ assert_case_in "[*, :b, :c]", s(:array_pat, nil, :*, s(:lit, :b), s(:lit, :c))
735
+ end
736
+
737
+ def test_case_in__array_pat_04
738
+ assert_case_in "[[:b, ^c], [:d, ^e]]", s(:array_pat, nil, s(:array_pat, nil, s(:lit, :b), s(:lvar, :c)), s(:array_pat, nil, s(:lit, :d), s(:lvar, :e)))
739
+ end
740
+
741
+ def test_case_in__array_pat_06
742
+ assert_case_in "[A, *, B]", s(:array_pat, nil, s(:const, :A), :*, s(:const, :B))
743
+ end
744
+
745
+ def test_case_in__array_pat_07
746
+ assert_case_in "[-> (b) { true }, ^c]", s(:array_pat, nil, s(:iter, s(:lambda), s(:args, :b), s(:true)), s(:lvar, :c))
747
+ end
748
+
749
+ def test_case_in__array_pat_09
750
+ assert_case_in "[:a, ^b, ^c, [:d, *e, nil]]", s(:array_pat, nil, s(:lit, :a), s(:lvar, :b), s(:lvar, :c), s(:array_pat, nil, s(:lit, :d), :"*e", s(:nil)))
751
+ end
752
+
753
+ def test_case_in__array_pat_14
754
+ assert_case_in "A[*list]", s(:array_pat, s(:const, :A), :"*list")
755
+ end
756
+
757
+ def test_case_in__array_pat_15
758
+ assert_case_in "B[C => d]", s(:array_pat, s(:const, :B), s(:lasgn, :d, s(:const, :C)))
759
+ end
760
+
761
+ def test_case_in__array_pat_16
762
+ assert_case_in "B[^c]", s(:array_pat, s(:const, :B), s(:lvar, :c))
763
+ end
764
+
765
+ def test_case_in__array_pat_19
766
+ assert_case_in "[^@a, ^$b, ^@@c]", s(:array_pat, nil, s(:ivar, :@a), s(:gvar, :$b), s(:cvar, :@@c)) # HACK: really not sure about this one
767
+ end
768
+
769
+ def test_case_in__find_pat_1
770
+ assert_case_in "[*a, :+, *b]", s(:find_pat, nil, :"*a",
771
+ s(:lit, :+),
772
+ :"*b")
773
+ end
774
+
775
+ def test_case_in__find_pat_2
776
+ assert_case_in "[*, :b, ^c, *]", s(:find_pat, nil,
777
+ :*,
778
+ s(:lit, :b), s(:lvar, :c),
779
+ :*)
780
+ end
781
+
782
+ def test_case_in__find_pat_3
783
+ assert_case_in("Array(*b, n, { a: }, m, *a)",
784
+ s(:find_pat,
785
+ s(:const, :Array),
786
+ :"*b",
787
+ s(:lasgn, :n),
788
+ s(:hash_pat, nil, s(:lit, :a), nil),
789
+ s(:lasgn, :m),
790
+ :"*a"),
791
+ "Array[*b, n, { a: }, m, *a]")
792
+ end
793
+
794
+ def test_case_in__find_pat_4
795
+ assert_case_in("*b, n, { a: }, m, *a", s(:find_pat,
796
+ nil,
797
+ :"*b",
798
+ s(:lasgn, :n),
799
+ s(:hash_pat, nil, s(:lit, :a), nil),
800
+ s(:lasgn, :m),
801
+ :"*a"),
802
+ "[*b, n, { a: }, m, *a]")
803
+ end
804
+
805
+ def test_case_in__find_pat_5
806
+ assert_case_in("Array(*lhs, ^b, *rhs)", s(:find_pat,
807
+ s(:const, :Array),
808
+ :"*lhs",
809
+ s(:lvar, :b),
810
+ :"*rhs"),
811
+ "Array[*lhs, ^b, *rhs]")
812
+ end
813
+
814
+ def test_case_in__find_pat_6
815
+ assert_case_in("Array[*lhs, b, *rhs]", s(:find_pat,
816
+ s(:const, :Array),
817
+ :"*lhs",
818
+ s(:lasgn, :b),
819
+ :"*rhs"))
820
+ end
821
+
822
+ def test_case_in__find_pat_7
823
+ assert_case_in("Array[*lhs, :b, *rhs]", s(:find_pat,
824
+ s(:const, :Array),
825
+ :"*lhs",
826
+ s(:lit, :b),
827
+ :"*rhs"))
828
+ end
829
+
830
+ def test_case_in_10
831
+ assert_case_in("[nil, nil, nil]",
832
+ s(:array_pat,
833
+ nil,
834
+ s(:nil),
835
+ s(:nil),
836
+ s(:nil)))
837
+ end
838
+
839
+ def test_case_in_32_2
840
+ assert_case_in "1..3", s(:dot2, s(:lit, 1), s(:lit, 3)), "(1..3)"
841
+ end
842
+
843
+ def test_case_in_32_3
844
+ assert_case_in "1...3", s(:dot3, s(:lit, 1), s(:lit, 3)), "(1...3)"
845
+ end
846
+
847
+ def test_case_in_36
848
+ pt = s(:array_pat, nil,
849
+ s(:lit, :a), s(:lasgn, :b), s(:lvar, :d), :"*e", s(:nil))
850
+
851
+ assert_case_in "[:a, b, ^d, *e, nil]", pt
852
+ end
853
+
854
+ def test_case_in_42
855
+ rb = "case :a\nin [:b, *_] then\n nil\nend"
856
+ pt = s(:case, s(:lit, :a),
857
+ s(:in,
858
+ s(:array_pat,
859
+ nil,
860
+ s(:lit, :b),
861
+ :"*_",
862
+ ),
863
+ s(:nil)),
864
+ nil)
865
+
866
+ assert_parse pt, rb
867
+ assert_variant pt, "case :a\nin :b, *_ then\n nil\nend"
868
+ end
869
+
870
+ def test_case_in_42_2
871
+ rb = "case :a\nin A[*list] then\n nil\nend"
872
+ pt = s(:case, s(:lit, :a),
873
+ s(:in,
874
+ s(:array_pat,
875
+ s(:const, :A),
876
+ :"*list"),
877
+ s(:nil)),
878
+ nil)
879
+
880
+ assert_parse pt, rb
881
+ assert_variant pt, "case :a\nin A(*list) then\n nil\nend"
882
+ end
883
+
884
+ def test_case_in_67
885
+ rb = "case :a\nin 1.. then nil\nend"
886
+ rb = "case :a\nin (1..) then\n nil\nend"
887
+ pt = s(:case,
888
+ s(:lit, :a),
889
+ s(:in, s(:dot2, s(:lit, 1), nil),
890
+ s(:nil)),
891
+ nil)
892
+
893
+ assert_parse pt, rb
894
+ end
895
+
896
+ def test_case_in_76
897
+ assert_case_in "`echo hi`", s(:xstr, "echo hi")
898
+ end
899
+
900
+ def test_case_in_77
901
+ assert_case_in "/regexp/", s(:lit, /regexp/)
902
+ end
903
+
904
+ def test_case_in_79
905
+ assert_case_in "%w[a b]", s(:array_pat, nil, s(:str, "a"), s(:str, "b")), "[\"a\", \"b\"]"
906
+ end
907
+
908
+ def test_case_in_80
909
+ assert_case_in "%I[a b]", s(:array_pat, nil, s(:lit, :a), s(:lit, :b)), "[:a, :b]"
910
+ end
911
+
912
+ def test_case_in_83
913
+ pt = s(:array_pat, nil,
914
+ s(:iter, s(:lambda), s(:args, :b),
915
+ s(:true)),
916
+ s(:lasgn, :c))
917
+
918
+ assert_case_in "[-> (b) { true }, c]", pt
919
+ end
920
+
921
+ def test_case_in_85
922
+ pt = s(:array_pat, nil,
923
+ s(:array_pat, nil,
924
+ s(:lit, :b),
925
+ s(:lasgn, :c)),
926
+ s(:array_pat,
927
+ nil,
928
+ s(:lit, :d),
929
+ s(:lvar, :e)),
930
+ )
931
+
932
+ assert_case_in "[[:b, c], [:d, ^e]]", pt
933
+ end
934
+
935
+ def test_case_in__hash_pat_00
936
+ assert_case_in "{}", s(:hash_pat, nil)
937
+ end
938
+
939
+ def test_case_in__hash_pat_01
940
+ assert_case_in "**nil", s(:hash_pat, nil, s(:kwrest, :"**nil")), "{ **nil }"
941
+ end
942
+
943
+ def test_case_in__hash_pat_03
944
+ assert_case_in "a:", s(:hash_pat, nil, s(:lit, :a), nil), "{ a: }"
945
+ end
946
+
947
+ def test_case_in__hash_pat_06
948
+ assert_case_in "a:1, **r",s(:hash_pat, nil, s(:lit, :a), s(:lit, 1), s(:kwrest, :"**r")), "{ a: 1, **r }"
949
+ end
950
+
951
+ def test_case_in__hash_pat_08
952
+ assert_case_in "{ b: [Hash, *] }", s(:hash_pat, nil, s(:lit, :b), s(:array_pat, nil, s(:const, :Hash), :*))
953
+ end
954
+
955
+ def test_case_in__hash_pat_09
956
+ assert_case_in("{ b: Integer => x, d: \"e\", f: }",
957
+ s(:hash_pat, nil,
958
+ s(:lit, :b), s(:lasgn, :x, s(:const, :Integer)),
959
+ s(:lit, :d), s(:str, "e"), s(:lit, :f), nil))
960
+ end
961
+
962
+ def test_case_in__hash_pat_10
963
+ assert_case_in "{ b: ^c, **r }", s(:hash_pat, nil, s(:lit, :b), s(:lvar, :c), s(:kwrest, :"**r"))
964
+ end
965
+
966
+ def test_case_in__hash_pat_11
967
+ assert_case_in "{ b: \"c\", d: \"e\" }", s(:hash_pat, nil, s(:lit, :b), s(:str, "c"), s(:lit, :d), s(:str, "e"))
968
+ end
969
+
970
+ def test_case_in__hash_pat_12
971
+ assert_case_in "{ b: true }", s(:hash_pat, nil, s(:lit, :b), s(:true))
972
+ end
973
+
974
+ def test_case_in__hash_pat_14
975
+ assert_case_in "Object[b: 1]", s(:hash_pat, s(:const, :Object), s(:lit, :b), s(:lit, 1))
976
+ end
977
+
725
978
  def test_interpolation_and_escapes
726
979
  # log_entry = " \e[#{message_color}m#{message}\e[0m "
727
980
  inn = s(:lasgn, :log_entry,
@@ -756,12 +1009,6 @@ class TestRuby2Ruby < R2RTestCase
756
1009
  assert_parse inn, out
757
1010
  end
758
1011
 
759
- def test_basic_ensure
760
- inn = s(:ensure, s(:lit, 1), s(:lit, 2))
761
- out = "begin\n 1\nensure\n 2\nend"
762
- assert_parse inn, out
763
- end
764
-
765
1012
  def test_nested_ensure
766
1013
  inn = s(:ensure, s(:lit, 1), s(:ensure, s(:lit, 2), s(:lit, 3)))
767
1014
  out = "begin\n 1\nensure\n begin\n 2\n ensure\n 3\n end\nend"
@@ -775,18 +1022,18 @@ class TestRuby2Ruby < R2RTestCase
775
1022
  end
776
1023
 
777
1024
  def test_nested_rescue_exception
778
- inn = s(:ensure, s(:lit, 1), s(:rescue, s(:lit, 2), s(:resbody, s(:array, s(:const, :Exception)), s(:lit, 3))))
1025
+ inn = s(:ensure,
1026
+ s(:lit, 1),
1027
+ s(:rescue, s(:lit, 2),
1028
+ s(:resbody, s(:array, s(:const, :Exception)),
1029
+ s(:lit, 3))))
779
1030
  out = "begin\n 1\nensure\n begin\n 2\n rescue Exception\n 3\n end\nend"
780
1031
  assert_parse inn, out
781
1032
  end
782
1033
 
783
- def test_nested_rescue_exception2
784
- inn = s(:ensure, s(:rescue, s(:lit, 2), s(:resbody, s(:array, s(:const, :Exception)), s(:lit, 3))), s(:lit, 1))
785
- out = "begin\n 2\nrescue Exception\n 3\nensure\n 1\nend"
786
- assert_parse inn, out
787
- end
788
-
789
1034
  def test_op_asgn
1035
+ do_not_check_sexp! # TODO: fix!
1036
+
790
1037
  inn = s(:op_asgn,
791
1038
  s(:call, nil, :x),
792
1039
  s(:call, nil, :z, s(:lit, 1)),
@@ -809,42 +1056,80 @@ class TestRuby2Ruby < R2RTestCase
809
1056
 
810
1057
  def test_array_adds_parens_around_rescue
811
1058
  inn = s(:array,
812
- s(:call, nil, :a),
813
- s(:rescue, s(:call, nil, :b), s(:resbody, s(:array), s(:call, nil, :c))))
1059
+ s(:lvar, :a),
1060
+ s(:rescue, s(:lvar, :b), s(:resbody, s(:array), s(:lvar, :c))))
814
1061
  out = "[a, (b rescue c)]"
815
1062
 
816
1063
  assert_parse inn, out
817
1064
  end
818
1065
 
819
1066
  def test_call_arglist_rescue
820
- inn = s(:call,
821
- nil,
822
- :method,
1067
+ inn = s(:call, nil, :method,
823
1068
  s(:rescue,
824
- s(:call, nil, :a),
825
- s(:resbody, s(:array), s(:call, nil, :b))))
1069
+ s(:lvar, :a),
1070
+ s(:resbody, s(:array), s(:lvar, :b))))
826
1071
  out = "method((a rescue b))"
827
1072
  assert_parse inn, out
828
1073
  end
829
1074
 
830
1075
  def test_unless_vs_if_not
1076
+ do_not_check_sexp! # TODO: remove? dunno if that's possible w/ this one
1077
+
831
1078
  rb1 = "a unless b"
832
1079
  rb2 = "a if (not b)"
833
1080
  rb3 = "a if ! b"
834
1081
 
835
- assert_parse RubyParser.new.parse(rb1), rb1
1082
+ assert_parse ruby_parser.parse(rb1), rb1
836
1083
 
837
- assert_parse RubyParser.new.parse(rb2), rb1
1084
+ assert_parse ruby_parser.parse(rb2), rb1
838
1085
 
839
- assert_parse RubyParser.new.parse(rb3), rb1
1086
+ assert_parse ruby_parser.parse(rb3), rb1
840
1087
  end
841
1088
 
842
- def assert_parse sexp, expected_ruby, expected_eval = nil
843
- assert_equal sexp, RubyParser.new.process(expected_ruby), "ruby -> sexp" if
844
- @check_sexp
1089
+ def ruby_parser
1090
+ parser = RubyParser.for_current_ruby
1091
+
1092
+ %i[a b c d].each do |v|
1093
+ parser.env[v] = :lvar
1094
+ end
845
1095
 
1096
+ parser
1097
+ end
1098
+
1099
+ def assert_parse sexp, expected_ruby, expected_eval = nil
846
1100
  assert_equal expected_ruby, @processor.process(sexp), "sexp -> ruby"
847
1101
  assert_equal expected_eval, eval(expected_ruby) if expected_eval
1102
+ assert_variant sexp, expected_ruby if @check_sexp
1103
+ end
1104
+
1105
+ def assert_variant sexp, expected_ruby
1106
+ assert_equal sexp, ruby_parser.process(expected_ruby), "ruby -> sexp"
1107
+ end
1108
+
1109
+ def assert_case_in lit, exp_pt, normal_lit = nil
1110
+ rb = "case :a\nin #{normal_lit || lit} then\n # do nothing\nend"
1111
+
1112
+ exp_pt.deep_each { |s| s.line ||= 2 }
1113
+ exp_pt.line ||= 2
1114
+
1115
+ # flunk "redundant: #{lit.inspect}" if normal_lit == lit
1116
+ # flunk "redundant: #{lit.inspect}" if normal_lit && normal_lit[1..-2] == lit
1117
+ normal_lit, lit = lit, lit[1..-2] if lit =~ /^\[.+\]$/ && !normal_lit
1118
+
1119
+ if ENV["VERBOSE_TEST"] then
1120
+ puts
1121
+ puts rb
1122
+ end
1123
+
1124
+ pt = s(:case, s(:lit, :a).line(1),
1125
+ s(:in, exp_pt, nil).line(2),
1126
+ nil).line(1)
1127
+
1128
+ if normal_lit then
1129
+ assert_variant pt, "case :a\nin #{lit} then\n # do nothing\nend"
1130
+ end
1131
+
1132
+ assert_parse pt, rb
848
1133
  end
849
1134
 
850
1135
  def util_thingy(type)
@@ -864,22 +1149,17 @@ end
864
1149
  # s
865
1150
  # t new 2 3
866
1151
 
867
- tr2r = File.read(__FILE__).split(/\n/)[start + 1..__LINE__ - 2].join("\n")
1152
+ tr2r = File.read(__FILE__).lines[start + 1..__LINE__ - 2].join
868
1153
  ir2r = File.read("lib/ruby2ruby.rb")
869
1154
 
870
1155
  require "ruby_parser"
871
1156
 
872
- def silent_eval ruby
873
- old, $-w = $-w, nil
874
- eval ruby
875
- $-w = old
876
- end
877
-
878
1157
  def morph_and_eval src, from, to, processor
879
1158
  parser = RubyParser.for_current_ruby rescue RubyParser.new
880
1159
  new_src = processor.new.process(parser.process(src.sub(from, to)))
881
1160
 
882
- silent_eval new_src
1161
+ eval new_src
1162
+
883
1163
  new_src
884
1164
  end
885
1165
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,18 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.4
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Davis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
- MIIDPjCCAiagAwIBAgIBAzANBgkqhkiG9w0BAQsFADBFMRMwEQYDVQQDDApyeWFu
13
+ MIIDPjCCAiagAwIBAgIBBjANBgkqhkiG9w0BAQsFADBFMRMwEQYDVQQDDApyeWFu
14
14
  ZC1ydWJ5MRkwFwYKCZImiZPyLGQBGRYJemVuc3BpZGVyMRMwEQYKCZImiZPyLGQB
15
- GRYDY29tMB4XDTE4MTIwNDIxMzAxNFoXDTE5MTIwNDIxMzAxNFowRTETMBEGA1UE
15
+ GRYDY29tMB4XDTIxMTIyMzIzMTkwNFoXDTIyMTIyMzIzMTkwNFowRTETMBEGA1UE
16
16
  AwwKcnlhbmQtcnVieTEZMBcGCgmSJomT8ixkARkWCXplbnNwaWRlcjETMBEGCgmS
17
17
  JomT8ixkARkWA2NvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALda
18
18
  b9DCgK+627gPJkB6XfjZ1itoOQvpqH1EXScSaba9/S2VF22VYQbXU1xQXL/WzCkx
@@ -22,14 +22,14 @@ cert_chain:
22
22
  qhtV7HJxNKuPj/JFH0D2cswvzznE/a5FOYO68g+YCuFi5L8wZuuM8zzdwjrWHqSV
23
23
  gBEfoTEGr7Zii72cx+sCAwEAAaM5MDcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAw
24
24
  HQYDVR0OBBYEFEfFe9md/r/tj/Wmwpy+MI8d9k/hMA0GCSqGSIb3DQEBCwUAA4IB
25
- AQCbJwLmpJR2PomLU+Zzw3KRzH/hbyUWc/ftru71AopZ1fy4iY9J/BW5QYKVYwbP
26
- V0FSBWtvfI/RdwfKGtuGhPKECZgmLieGuZ3XCc09qPu1bdg7i/tu1p0t0c6163ku
27
- nDMDIC/t/DAFK0TY9I3HswuyZGbLW7rgF0DmiuZdN/RPhHq2pOLMLXJmFclCb/im
28
- 9yToml/06TJdUJ5p64mkBs0TzaK66DIB1Smd3PdtfZqoRV+EwaXMdx0Hb3zdR1JR
29
- Em82dBUFsipwMLCYj39kcyHWAxyl6Ae1Cn9r/ItVBCxoeFdrHjfavnrIEoXUt4bU
30
- UfBugfLD19bu3nvL+zTAGx/U
25
+ AQCKB5jfsuSnKb+t/Wrh3UpdkmX7TrEsjVmERC0pPqzQ5GQJgmEXDD7oMgaKXaAq
26
+ x2m+KSZDrqk7c8uho5OX6YMqg4KdxehfSLqqTZGoeV78qwf/jpPQZKTf+W9gUSJh
27
+ zsWpo4K50MP+QtdSbKXZwjAafpQ8hK0MnnZ/aeCsW9ov5vdXpYbf3dpg6ADXRGE7
28
+ lQY2y1tJ5/chqu6h7dQmnm2ABUqx9O+JcN9hbCYoA5i/EeubUEtFIh2w3SpO6YfB
29
+ JFmxn4h9YO/pVdB962BdBNNDia0kgIjI3ENnkLq0dKpYU3+F3KhEuTksLO0L6X/V
30
+ YsuyUzsMz6GQA4khyaMgKNSD
31
31
  -----END CERTIFICATE-----
32
- date: 2019-09-25 00:00:00.000000000 Z
32
+ date: 2022-10-05 00:00:00.000000000 Z
33
33
  dependencies:
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: sexp_processor
@@ -85,14 +85,14 @@ dependencies:
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: '3.18'
88
+ version: '3.25'
89
89
  type: :development
90
90
  prerelease: false
91
91
  version_requirements: !ruby/object:Gem::Requirement
92
92
  requirements:
93
93
  - - "~>"
94
94
  - !ruby/object:Gem::Version
95
- version: '3.18'
95
+ version: '3.25'
96
96
  description: |-
97
97
  ruby2ruby provides a means of generating pure ruby code easily from
98
98
  RubyParser compatible Sexps. This makes making dynamic language
@@ -118,8 +118,9 @@ files:
118
118
  homepage: https://github.com/seattlerb/ruby2ruby
119
119
  licenses:
120
120
  - MIT
121
- metadata: {}
122
- post_install_message:
121
+ metadata:
122
+ homepage_uri: https://github.com/seattlerb/ruby2ruby
123
+ post_install_message:
123
124
  rdoc_options:
124
125
  - "--main"
125
126
  - README.rdoc
@@ -136,8 +137,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
137
  - !ruby/object:Gem::Version
137
138
  version: '0'
138
139
  requirements: []
139
- rubygems_version: 3.0.6
140
- signing_key:
140
+ rubygems_version: 3.3.12
141
+ signing_key:
141
142
  specification_version: 4
142
143
  summary: ruby2ruby provides a means of generating pure ruby code easily from RubyParser
143
144
  compatible Sexps
metadata.gz.sig CHANGED
Binary file