sorcerer 1.0.2 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 8dcf3cdd1a1b69cbc70d2768c27dce5582958aaf
4
- data.tar.gz: 25a67f7a15125bfba2759b37ccda0afeb9eaa5fb
2
+ SHA256:
3
+ metadata.gz: '086949b9939c7dcc5b702d516b2c8c5050e1aec12a4f9e0b6f93bd16883b4311'
4
+ data.tar.gz: 8e98466c01a8758aa2a93a0f9b420bab5ab71b44855882a535d7713dbf6cccfb
5
5
  SHA512:
6
- metadata.gz: 35d606b74d95f711284738d5d72878967ae569c664a25d2bc1f0f786fc06061df63cbcc66d7f870972176afc49a3ec95e0c4d41d66c12879f8ef244f00056653
7
- data.tar.gz: 5fb9dbda6db23f37cbec9d5d823fd17cdee7a6ee88289bc484745480215fd511995f0711e61701578f28b5d9c7798ba12633e0a02d9111631fd1f382fa593f02
6
+ metadata.gz: 8d04f7f34a703da571ce0b5821f183ea25775bce5c95d10e8b98682f7c0e156f8d257dcf40f6f8d00fc915f0872f7b3264b24f645fab23e7d6563455704fbc2c
7
+ data.tar.gz: e959656fc9dbf13fe5c8a580a13330a40f259560fb16fc8e99746235f91c4047dd9b884a17fbc2cbbca7d792053c1df89b6a234e5c79519addfcab213f13f2fc
data/README.md CHANGED
@@ -11,7 +11,7 @@ Sorcerer is targetted mainly at small snippets of Ruby code,
11
11
  expressable in a single line. Longer examples may be re-sourced, but
12
12
  they will be rendered in a single line format.
13
13
 
14
- **Version: 1.0.0**
14
+ **Version: 2.0.0**
15
15
 
16
16
  ## Limitations
17
17
 
@@ -101,7 +101,7 @@ module Sorcerer
101
101
  resource(sexp[2]) # Statements
102
102
  end
103
103
  if !void?(sexp[2])
104
- soft_newline
104
+ soft_newline unless sexp[2].first == :bodystmt
105
105
  else
106
106
  emit(" ")
107
107
  end
@@ -186,7 +186,7 @@ module Sorcerer
186
186
  if sig.opts_arg
187
187
  first = emit_separator(", ", first)
188
188
  emit("**")
189
- emit(sig.opts_arg[1])
189
+ DOESNT_WRAP_OPTS_ARG_IN_KWREST_PARAM ? emit(sig.opts_arg[1]) : resource(sig.opts_arg[1])
190
190
  end
191
191
  if sig.block_arg
192
192
  first = emit_separator(", ", first)
@@ -292,6 +292,17 @@ module Sorcerer
292
292
  MISSES_ARRAY_NODE_FOR_WORDS = RUBY_VERSION < '1.9.2' ||
293
293
  (RUBY_VERSION == '1.9.2' && RUBY_PATCHLEVEL < 320)
294
294
 
295
+ RUBY_MAJOR, RUBY_MINOR, RUBY_PATCH = RUBY_VERSION.split('.').map(&:to_i)
296
+
297
+ # As of 2.5, Ripper started wrapping **opts rest keyword args in a
298
+ # :kwrest_param s-expression
299
+ DOESNT_WRAP_OPTS_ARG_IN_KWREST_PARAM = RUBY_MAJOR < 2 || (RUBY_MAJOR == 2 && RUBY_MINOR < 5)
300
+
301
+ # As of 2.6, Ripper parses the period in fields and calls as s-expressions
302
+ # as opposed to raw strings
303
+ # See: https://github.com/rspec-given/sorcerer/pull/1
304
+ TREATS_MORE_PERIODS_AS_TEXT = RUBY_MAJOR < 2 || (RUBY_MAJOR == 2 && RUBY_MINOR < 6)
305
+
295
306
  HANDLERS = {
296
307
  # parser keywords
297
308
 
@@ -460,12 +471,15 @@ module Sorcerer
460
471
  },
461
472
  :call => lambda { |sexp|
462
473
  resource(sexp[1])
463
- emit(sexp[2])
474
+ TREATS_MORE_PERIODS_AS_TEXT || !sexp?(sexp[2]) ? emit(sexp[2]) : resource(sexp[2])
464
475
  resource(sexp[3]) unless sexp[3] == :call
465
476
  },
466
477
  :case => lambda { |sexp|
467
- emit("case ")
468
- resource(sexp[1])
478
+ emit("case")
479
+ if sexp[1]
480
+ emit(" ")
481
+ resource(sexp[1])
482
+ end
469
483
  soft_newline
470
484
  indent do
471
485
  resource(sexp[2])
@@ -494,7 +508,7 @@ module Sorcerer
494
508
  },
495
509
  :command_call => lambda { |sexp|
496
510
  resource(sexp[1])
497
- emit(sexp[2])
511
+ TREATS_MORE_PERIODS_AS_TEXT || !sexp?(sexp[2]) ? emit(sexp[2]) : resource(sexp[2])
498
512
  resource(sexp[3])
499
513
  emit(" ")
500
514
  resource(sexp[4])
@@ -579,7 +593,7 @@ module Sorcerer
579
593
  :fcall => PASS1,
580
594
  :field => lambda { |sexp|
581
595
  resource(sexp[1])
582
- emit(sexp[2])
596
+ TREATS_MORE_PERIODS_AS_TEXT || !sexp?(sexp[2]) ? emit(sexp[2]) : resource(sexp[2])
583
597
  resource(sexp[3])
584
598
  },
585
599
  :for => lambda { |sexp|
@@ -765,7 +779,7 @@ module Sorcerer
765
779
  },
766
780
  :rest_param => lambda { |sexp|
767
781
  emit("*")
768
- resource(sexp[1])
782
+ resource(sexp[1]) if sexp[1]
769
783
  },
770
784
  :retry => lambda { |sexp|
771
785
  emit("retry")
@@ -777,7 +791,15 @@ module Sorcerer
777
791
  :return0 => lambda { |sexp|
778
792
  emit("return")
779
793
  },
780
- :sclass => NYI,
794
+ :sclass => lambda { |sexp|
795
+ emit("class << ")
796
+ resource(sexp[1])
797
+ newline
798
+ indent do
799
+ resource(sexp[2]) unless void?(sexp[2])
800
+ end
801
+ emit("end")
802
+ },
781
803
  :stmts_add => lambda { |sexp|
782
804
  if sexp[1] != [:stmts_new] && ! void?(sexp[1])
783
805
  resource(sexp[1])
@@ -793,7 +815,10 @@ module Sorcerer
793
815
  resource(sexp[2])
794
816
  },
795
817
  :string_content => NOOP,
796
- :string_dvar => NYI,
818
+ :string_dvar => lambda { |sexp|
819
+ emit('#')
820
+ resource(sexp[1])
821
+ },
797
822
  :string_embexpr => lambda { |sexp|
798
823
  emit('#{')
799
824
  resource(sexp[1])
@@ -817,8 +842,10 @@ module Sorcerer
817
842
  words("I", sexp)
818
843
  },
819
844
  :symbols_new => NOOP,
820
- :top_const_field => NYI,
821
- :top_const_ref => NYI,
845
+ :top_const_ref => lambda { |sexp|
846
+ emit("::")
847
+ resource(sexp[1])
848
+ },
822
849
  :unary => lambda { |sexp|
823
850
  op = sexp[1].to_s
824
851
  op = op[0,1] if op =~ /^.@$/
@@ -916,10 +943,10 @@ module Sorcerer
916
943
 
917
944
  # Scanner keywords
918
945
 
919
- :@CHAR => NYI,
946
+ :@CHAR => EMIT1,
920
947
  :@__end__ => NYI,
921
- :@backref => NYI,
922
- :@backtick => NYI,
948
+ :@backref => EMIT1,
949
+ :@backtick => EMIT1,
923
950
  :@comma => NYI,
924
951
  :@comment => NYI,
925
952
  :@const => EMIT1,
@@ -964,6 +991,7 @@ module Sorcerer
964
991
  :@words_sep => NYI,
965
992
  }
966
993
  HANDLERS[:bodystmt] = HANDLERS[:body_stmt]
994
+ HANDLERS[:top_const_field] = HANDLERS[:top_const_ref]
967
995
  end
968
996
 
969
997
  end
@@ -1,8 +1,8 @@
1
1
  module Sorcerer
2
2
  VERSION_NUMBERS = [
3
- VERSION_MAJOR = 1,
3
+ VERSION_MAJOR = 2,
4
4
  VERSION_MINOR = 0,
5
- VERSION_BUILD = 2,
5
+ VERSION_BUILD = 0,
6
6
  ]
7
7
 
8
8
  VERSION = VERSION_NUMBERS.join('.')
@@ -61,7 +61,7 @@ module AssertResource
61
61
  string.
62
62
  gsub(/\b THEN~/, "; ").
63
63
  gsub(/~/, "\n").
64
- gsub(/; /, "\n").
64
+ gsub(/;\s+/, "\n").
65
65
  gsub(/#/,'') + "\n"
66
66
  end
67
67
 
@@ -69,7 +69,7 @@ module AssertResource
69
69
  string.
70
70
  gsub(/\b THEN~/, "; ").
71
71
  gsub(/~/, "\n").
72
- gsub(/; /, "\n").
72
+ gsub(/;\s+/, "\n").
73
73
  gsub(/#/,' ') + "\n"
74
74
  end
75
75
 
@@ -45,6 +45,7 @@ class ResourceTest < Test::Unit::TestCase
45
45
  assert_resource "A"
46
46
  assert_resource "Mod::X"
47
47
  assert_resource "Mod::NS::Y"
48
+ assert_resource "::Mod::NS::Y"
48
49
  end
49
50
 
50
51
  def test_can_source_keywords
@@ -55,8 +56,10 @@ class ResourceTest < Test::Unit::TestCase
55
56
 
56
57
  def test_can_source_constant_definition
57
58
  assert_resource "X = 1"
59
+ assert_resource "::X = 1"
58
60
  assert_resource "X::Y = 1"
59
61
  assert_resource "X::Y::Z = 1"
62
+ assert_resource "::X::Y::Z = 1"
60
63
  end
61
64
 
62
65
  def test_can_source_instance_variables
@@ -102,7 +105,11 @@ class ResourceTest < Test::Unit::TestCase
102
105
  assert_resource "obj.meth &code"
103
106
  assert_resource "obj.meth a, &code"
104
107
  assert_resource "obj.meth a, *args, &code"
105
- assert_resource "obj.meth a, *args do |x| x.y end"
108
+ if RUBY_VERSION >= "2.5"
109
+ assert_resource "obj.meth a, *args do |x| x.y; end"
110
+ else
111
+ assert_resource "obj.meth a, *args do |x| x.y end"
112
+ end
106
113
  end
107
114
 
108
115
  def test_can_source_method_called_with_scope_op_without_parans
@@ -151,7 +158,11 @@ class ResourceTest < Test::Unit::TestCase
151
158
  assert_resource "meth &code"
152
159
  assert_resource "meth a, &code"
153
160
  assert_resource "meth a, *args, &code"
154
- assert_resource "meth a, *args do |x| x.y end"
161
+ if RUBY_VERSION >= "2.5"
162
+ assert_resource "meth a, *args do |x| x.y; end"
163
+ else
164
+ assert_resource "meth a, *args do |x| x.y end"
165
+ end
155
166
  end
156
167
 
157
168
  def test_can_source_method_with_bare_assoc
@@ -172,7 +183,11 @@ class ResourceTest < Test::Unit::TestCase
172
183
  def test_can_source_method_with_do_block
173
184
  assert_resource_lines "meth do end"
174
185
  assert_resource_lines "meth do |a| end"
175
- assert_resource_lines "meth(x, y, *rest, &code) do |a, b=1, c=x, *args, &block|~#one; #two; #three~end"
186
+ if RUBY_VERSION >= "2.5"
187
+ assert_resource_lines "meth(x, y, *rest, &code) do |a, b=1, c=x, *args, &block|~#one; #two; #three;~end"
188
+ else
189
+ assert_resource_lines "meth(x, y, *rest, &code) do |a, b=1, c=x, *args, &block|~#one; #two; #three~end"
190
+ end
176
191
  end
177
192
 
178
193
  def test_can_source_method_with_block
@@ -219,6 +234,11 @@ class ResourceTest < Test::Unit::TestCase
219
234
  assert_resource "3.14"
220
235
  end
221
236
 
237
+ def test_can_source_chars
238
+ assert_resource "?/"
239
+ assert_resource "?q"
240
+ end
241
+
222
242
  def test_can_source_strings
223
243
  assert_resource '"HI"'
224
244
  assert_equal '"HI"', source("'HI'")
@@ -234,6 +254,11 @@ class ResourceTest < Test::Unit::TestCase
234
254
  assert_resource '"my name is #{x.a("B")}"'
235
255
  end
236
256
 
257
+ def test_can_source_string_dvars
258
+ assert_resource '"my program is #$0"'
259
+ assert_resource '"my global is #$glob"'
260
+ end
261
+
237
262
  def test_can_source_string_concat
238
263
  assert_resource '"a" "b"'
239
264
  assert_resource '"a" "b" "c"'
@@ -348,6 +373,11 @@ class ResourceTest < Test::Unit::TestCase
348
373
  assert_resource "not a"
349
374
  end
350
375
 
376
+ def test_can_source_backreferences
377
+ assert_resource "$1"
378
+ assert_resource "$9"
379
+ end
380
+
351
381
  def test_can_source_binary_expressions
352
382
  assert_resource "a + 1"
353
383
  assert_resource "a + b"
@@ -356,6 +386,10 @@ class ResourceTest < Test::Unit::TestCase
356
386
  assert_resource "a / b"
357
387
  assert_resource "a && b"
358
388
  assert_resource "a || b"
389
+ assert_resource "a < b"
390
+ assert_resource "a <= b"
391
+ assert_resource "a > b"
392
+ assert_resource "a >= b"
359
393
  end
360
394
 
361
395
  def test_can_source_trinary_expressions
@@ -513,6 +547,7 @@ class ResourceTest < Test::Unit::TestCase
513
547
  end
514
548
 
515
549
  def test_can_source_case
550
+ assert_resource_lines "case~when b; #c; end"
516
551
  assert_resource_lines "case a~when b; #c; end"
517
552
  assert_resource_lines "case a~when b; #c when d; #e; end"
518
553
  assert_resource_lines "case a~when b; #c when d; #e~else~#f; end"
@@ -584,10 +619,12 @@ class ResourceTest < Test::Unit::TestCase
584
619
  assert_resource_lines "def f(a); end"
585
620
  assert_resource_lines "def f(a, b); end"
586
621
  assert_resource_lines "def f(a, b=1); end"
622
+ assert_resource_lines "def f(a, *); end"
587
623
  assert_resource_lines "def f(a, *args); end"
588
624
  assert_resource_lines "def f(a, *args, &block); end"
589
625
  assert_resource_lines "def f(a); #x; end"
590
626
  assert_resource_lines "def f(a); #x; #y; end"
627
+ assert_resource_lines "def `(a); end"
591
628
  end
592
629
 
593
630
  def test_can_source_def_without_parens
@@ -651,6 +688,12 @@ class ResourceTest < Test::Unit::TestCase
651
688
  assert_resource_lines "class X; #def f(); #end; #private :f; end"
652
689
  end
653
690
 
691
+ def test_can_source_sclass
692
+ assert_resource_lines "class << self; end"
693
+ assert_resource_lines "class << a; end"
694
+ assert_resource_lines "class << self; #x; end"
695
+ end
696
+
654
697
  def test_can_source_module
655
698
  assert_resource_lines "module X; end"
656
699
  assert_resource_lines "module X; #x; end"
@@ -35,7 +35,11 @@ class SignatureTest < Test::Unit::TestCase
35
35
  assert_equal [[[:@ident, "b", [1, 11]], [:@int, "1", [1, 13]]]], sig.default_args
36
36
  assert_equal [:rest_param, [:@ident, "args", [1, 17]]], sig.rest_arg
37
37
  assert_equal [[[:@label, "c:", [1, 23]], [:@int, "2", [1, 26]]]], sig.keyword_args
38
- assert_equal [:@ident, "opts", [1, 31]], sig.opts_arg
38
+ if RUBY_VERSION >= "2.5"
39
+ assert_equal [:kwrest_param, [:@ident, "opts", [1, 31]]], sig.opts_arg
40
+ else
41
+ assert_equal [:@ident, "opts", [1, 31]], sig.opts_arg
42
+ end
39
43
  assert_equal [:blockarg, [:@ident, "block", [1, 38]]], sig.block_arg
40
44
  end
41
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sorcerer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Weirich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-05 00:00:00.000000000 Z
11
+ date: 2019-12-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Generate the original Ruby source from a Ripper-style abstract syntax
14
14
  tree.
@@ -20,14 +20,14 @@ files:
20
20
  - README.md
21
21
  - Rakefile
22
22
  - doc/jamis.rb
23
- - rakelib/git.rake
24
- - rakelib/readme.rake
25
- - rakelib/testing.rake
26
23
  - lib/sorcerer.rb
27
24
  - lib/sorcerer/resource.rb
28
25
  - lib/sorcerer/signature.rb
29
26
  - lib/sorcerer/subexpression.rb
30
27
  - lib/sorcerer/version.rb
28
+ - rakelib/git.rake
29
+ - rakelib/readme.rake
30
+ - rakelib/testing.rake
31
31
  - test/assert_resource_helper.rb
32
32
  - test/sorcerer/resource_test.rb
33
33
  - test/sorcerer/signature_test.rb
@@ -37,25 +37,24 @@ licenses: []
37
37
  metadata: {}
38
38
  post_install_message:
39
39
  rdoc_options:
40
- - --line-numbers
41
- - --inline-source
42
- - --title
40
+ - "--line-numbers"
41
+ - "--inline-source"
42
+ - "--title"
43
43
  - Rake -- Ruby Make
44
44
  require_paths:
45
45
  - lib
46
46
  required_ruby_version: !ruby/object:Gem::Requirement
47
47
  requirements:
48
- - - '>='
48
+ - - ">="
49
49
  - !ruby/object:Gem::Version
50
50
  version: '0'
51
51
  required_rubygems_version: !ruby/object:Gem::Requirement
52
52
  requirements:
53
- - - '>='
53
+ - - ">="
54
54
  - !ruby/object:Gem::Version
55
55
  version: '0'
56
56
  requirements: []
57
- rubyforge_project: sorcerer
58
- rubygems_version: 2.0.5
57
+ rubygems_version: 3.0.3
59
58
  signing_key:
60
59
  specification_version: 4
61
60
  summary: Generate Source from Ripper ASTs