ripper_ruby_parser 1.4.2 → 1.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.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -1
  3. data/README.md +41 -9
  4. data/Rakefile +2 -0
  5. data/lib/ripper_ruby_parser.rb +2 -0
  6. data/lib/ripper_ruby_parser/commenting_ripper_parser.rb +23 -45
  7. data/lib/ripper_ruby_parser/parser.rb +11 -1
  8. data/lib/ripper_ruby_parser/sexp_handlers.rb +2 -6
  9. data/lib/ripper_ruby_parser/sexp_handlers/assignment.rb +49 -35
  10. data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +78 -39
  11. data/lib/ripper_ruby_parser/sexp_handlers/conditionals.rb +16 -15
  12. data/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +19 -15
  13. data/lib/ripper_ruby_parser/sexp_handlers/literals.rb +138 -30
  14. data/lib/ripper_ruby_parser/sexp_handlers/loops.rb +10 -6
  15. data/lib/ripper_ruby_parser/sexp_handlers/method_calls.rb +59 -14
  16. data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +56 -32
  17. data/lib/ripper_ruby_parser/sexp_handlers/operators.rb +20 -27
  18. data/lib/ripper_ruby_parser/sexp_processor.rb +40 -10
  19. data/lib/ripper_ruby_parser/syntax_error.rb +2 -0
  20. data/lib/ripper_ruby_parser/unescape.rb +32 -11
  21. data/lib/ripper_ruby_parser/version.rb +3 -1
  22. data/test/end_to_end/comments_test.rb +2 -0
  23. data/test/end_to_end/comparison_test.rb +2 -0
  24. data/test/end_to_end/lib_comparison_test.rb +2 -0
  25. data/test/end_to_end/line_numbering_test.rb +2 -0
  26. data/test/end_to_end/samples_comparison_test.rb +5 -29
  27. data/test/end_to_end/test_comparison_test.rb +2 -0
  28. data/test/pt_testcase/pt_test.rb +2 -0
  29. data/test/ripper_ruby_parser/commenting_ripper_parser_test.rb +16 -2
  30. data/test/ripper_ruby_parser/parser_test.rb +17 -688
  31. data/test/ripper_ruby_parser/sexp_handlers/assignment_test.rb +459 -26
  32. data/test/ripper_ruby_parser/sexp_handlers/blocks_test.rb +152 -82
  33. data/test/ripper_ruby_parser/sexp_handlers/conditionals_test.rb +91 -0
  34. data/test/ripper_ruby_parser/sexp_handlers/literals_test.rb +331 -24
  35. data/test/ripper_ruby_parser/sexp_handlers/loops_test.rb +88 -0
  36. data/test/ripper_ruby_parser/sexp_handlers/method_calls_test.rb +58 -5
  37. data/test/ripper_ruby_parser/sexp_handlers/methods_test.rb +392 -0
  38. data/test/ripper_ruby_parser/sexp_handlers/operators_test.rb +174 -12
  39. data/test/ripper_ruby_parser/sexp_processor_test.rb +8 -18
  40. data/test/ripper_ruby_parser/version_test.rb +2 -0
  41. data/test/samples/comments.rb +13 -0
  42. data/test/samples/conditionals.rb +23 -0
  43. data/test/samples/loops.rb +36 -0
  44. data/test/samples/misc.rb +157 -5
  45. data/test/samples/number.rb +7 -0
  46. data/test/samples/strings.rb +39 -0
  47. data/test/test_helper.rb +22 -1
  48. metadata +18 -12
  49. data/lib/ripper_ruby_parser/sexp_handlers/arguments.rb +0 -29
  50. data/lib/ripper_ruby_parser/sexp_handlers/arrays.rb +0 -21
  51. data/lib/ripper_ruby_parser/sexp_handlers/hashes.rb +0 -48
@@ -0,0 +1,7 @@
1
+ # Samples of number-like literals
2
+
3
+ 3.14
4
+ 42
5
+ 100r
6
+ 0700
7
+ 0x100
@@ -5,8 +5,18 @@
5
5
  %W(foo\nbar baz)
6
6
  %w(foo\nbar baz)
7
7
 
8
+ "foo\u273bbar"
9
+ "\0"
10
+ "foo#{bar}\0"
11
+ "foo#{bar}baz\0"
12
+ "2\302\275"
13
+ "#{foo}2\302\275"
14
+ %W(2\302\275)
15
+ /2\302\275/
16
+
8
17
  # Encoding
9
18
  "日本語"
19
+ /日本語/
10
20
  "\xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E" # => "日本語"
11
21
  "\xAB\xE6\x97\xA5" # Invalid in UTF8
12
22
 
@@ -18,29 +28,53 @@ EOS
18
28
  \xE6\x97\xA5\xE6\x9C\xAC\xE8\xAA\x9E
19
29
  EOS
20
30
 
31
+ # Quotes around heredoc names
32
+ <<'FOO'
33
+ \n
34
+ FOO
35
+
36
+ <<"FOO"
37
+ \n
38
+ FOO
39
+
21
40
  # Line continuation
22
41
  "foo\
23
42
  bar"
43
+
24
44
  'foo2\
25
45
  bar'
46
+
26
47
  /foo3\
27
48
  bar/
49
+
28
50
  <<EOS
29
51
  foo4\
30
52
  bar
31
53
  EOS
54
+
55
+ <<'EOS'
56
+ foo4\
57
+ bar
58
+ EOS
59
+
32
60
  %Q[foo5\
33
61
  bar]
62
+
34
63
  %W[fooa\
35
64
  bar baz]
65
+
36
66
  %I[foob\
37
67
  bar baz]
68
+
38
69
  %q[fooc\
39
70
  bar]
71
+
40
72
  %w[food\
41
73
  bar baz]
74
+
42
75
  %i[fooe\
43
76
  bar baz]
77
+
44
78
  %r[foof\
45
79
  bar baz]
46
80
 
@@ -69,3 +103,8 @@ bar baz]
69
103
  bar baz]
70
104
  %r[fool\\
71
105
  bar baz]
106
+
107
+ eval(<<FOO, __FILE__, __LINE__)
108
+ bar
109
+ baz
110
+ FOO
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'simplecov'
2
4
  SimpleCov.start do
3
5
  add_filter '/test/'
@@ -15,6 +17,20 @@ module MiniTest
15
17
  exp.to_s.gsub(/\), /, "),\n")
16
18
  end
17
19
 
20
+ def fix_lines(exp)
21
+ return s(:lit, :__LINE__) if exp.sexp_type == :lit && exp.line == exp[1]
22
+
23
+ inner = exp.map do |sub_exp|
24
+ if sub_exp.is_a? Sexp
25
+ fix_lines sub_exp
26
+ else
27
+ sub_exp
28
+ end
29
+ end
30
+
31
+ s(*inner)
32
+ end
33
+
18
34
  def to_comments(exp)
19
35
  inner = exp.map do |sub_exp|
20
36
  if sub_exp.is_a? Sexp
@@ -32,21 +48,26 @@ module MiniTest
32
48
  end
33
49
  end
34
50
 
35
- def assert_parsed_as(sexp, code)
51
+ def assert_parsed_as(sexp, code, extra_compatible: false)
36
52
  parser = RipperRubyParser::Parser.new
53
+ parser.extra_compatible = extra_compatible
37
54
  result = parser.parse code
38
55
  if sexp.nil?
39
56
  assert_nil result
40
57
  else
41
58
  assert_equal sexp, result
59
+ assert_equal sexp.to_s, result.to_s
42
60
  end
43
61
  end
44
62
 
45
63
  def assert_parsed_as_before(code)
46
64
  oldparser = RubyParser.new
47
65
  newparser = RipperRubyParser::Parser.new
66
+ newparser.extra_compatible = true
48
67
  expected = oldparser.parse code.dup
49
68
  result = newparser.parse code
69
+ expected = to_comments fix_lines expected
70
+ result = to_comments fix_lines result
50
71
  assert_equal formatted(expected), formatted(result)
51
72
  end
52
73
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ripper_ruby_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matijs van Zuijlen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-04-03 00:00:00.000000000 Z
11
+ date: 2019-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sexp_processor
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 4.10.0
19
+ version: '4.10'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 4.10.0
26
+ version: '4.10'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 3.11.0
61
+ version: 3.12.0
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 3.11.0
68
+ version: 3.12.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: simplecov
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -97,12 +97,9 @@ files:
97
97
  - lib/ripper_ruby_parser/commenting_ripper_parser.rb
98
98
  - lib/ripper_ruby_parser/parser.rb
99
99
  - lib/ripper_ruby_parser/sexp_handlers.rb
100
- - lib/ripper_ruby_parser/sexp_handlers/arguments.rb
101
- - lib/ripper_ruby_parser/sexp_handlers/arrays.rb
102
100
  - lib/ripper_ruby_parser/sexp_handlers/assignment.rb
103
101
  - lib/ripper_ruby_parser/sexp_handlers/blocks.rb
104
102
  - lib/ripper_ruby_parser/sexp_handlers/conditionals.rb
105
- - lib/ripper_ruby_parser/sexp_handlers/hashes.rb
106
103
  - lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb
107
104
  - lib/ripper_ruby_parser/sexp_handlers/literals.rb
108
105
  - lib/ripper_ruby_parser/sexp_handlers/loops.rb
@@ -128,10 +125,15 @@ files:
128
125
  - test/ripper_ruby_parser/sexp_handlers/literals_test.rb
129
126
  - test/ripper_ruby_parser/sexp_handlers/loops_test.rb
130
127
  - test/ripper_ruby_parser/sexp_handlers/method_calls_test.rb
128
+ - test/ripper_ruby_parser/sexp_handlers/methods_test.rb
131
129
  - test/ripper_ruby_parser/sexp_handlers/operators_test.rb
132
130
  - test/ripper_ruby_parser/sexp_processor_test.rb
133
131
  - test/ripper_ruby_parser/version_test.rb
132
+ - test/samples/comments.rb
133
+ - test/samples/conditionals.rb
134
+ - test/samples/loops.rb
134
135
  - test/samples/misc.rb
136
+ - test/samples/number.rb
135
137
  - test/samples/operators.rb
136
138
  - test/samples/strings.rb
137
139
  - test/test_helper.rb
@@ -149,15 +151,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
149
151
  requirements:
150
152
  - - ">="
151
153
  - !ruby/object:Gem::Version
152
- version: 2.2.0
154
+ version: 2.3.0
153
155
  required_rubygems_version: !ruby/object:Gem::Requirement
154
156
  requirements:
155
157
  - - ">="
156
158
  - !ruby/object:Gem::Version
157
159
  version: '0'
158
160
  requirements: []
159
- rubyforge_project:
160
- rubygems_version: 2.7.6
161
+ rubygems_version: 3.0.3
161
162
  signing_key:
162
163
  specification_version: 4
163
164
  summary: Parse with Ripper, produce sexps that are compatible with RubyParser.
@@ -177,10 +178,15 @@ test_files:
177
178
  - test/ripper_ruby_parser/sexp_handlers/literals_test.rb
178
179
  - test/ripper_ruby_parser/sexp_handlers/loops_test.rb
179
180
  - test/ripper_ruby_parser/sexp_handlers/method_calls_test.rb
181
+ - test/ripper_ruby_parser/sexp_handlers/methods_test.rb
180
182
  - test/ripper_ruby_parser/sexp_handlers/operators_test.rb
181
183
  - test/ripper_ruby_parser/sexp_processor_test.rb
182
184
  - test/ripper_ruby_parser/version_test.rb
185
+ - test/samples/comments.rb
186
+ - test/samples/conditionals.rb
187
+ - test/samples/loops.rb
183
188
  - test/samples/misc.rb
189
+ - test/samples/number.rb
184
190
  - test/samples/operators.rb
185
191
  - test/samples/strings.rb
186
192
  - test/test_helper.rb
@@ -1,29 +0,0 @@
1
- module RipperRubyParser
2
- module SexpHandlers
3
- # Sexp handlers for argument lists
4
- module Arguments
5
- def process_args_add_block(exp)
6
- _, regular, block = exp.shift 3
7
- args = process(regular)
8
- args << s(:block_pass, process(block)) if block
9
- s(:arglist, *args.sexp_body)
10
- end
11
-
12
- def process_args_add_star(exp)
13
- generic_add_star exp
14
- end
15
-
16
- def process_arg_paren(exp)
17
- _, args = exp.shift 2
18
- args = s() if args.nil?
19
- args.unshift :arglist unless args.first.is_a? Symbol
20
- process(args)
21
- end
22
-
23
- def process_rest_param(exp)
24
- _, ident = exp.shift 2
25
- s(:splat, process(ident))
26
- end
27
- end
28
- end
29
- end
@@ -1,21 +0,0 @@
1
- module RipperRubyParser
2
- module SexpHandlers
3
- # Sexp handlers for array literals
4
- module Arrays
5
- def process_array(exp)
6
- _, elems = exp.shift 2
7
- return s(:array) if elems.nil?
8
- s(:array, *handle_array_elements(elems))
9
- end
10
-
11
- def process_aref(exp)
12
- _, coll, idx = exp.shift 3
13
-
14
- coll = process(coll)
15
- idx = process(idx) || s(:arglist)
16
- idx.shift
17
- s(:call, coll, :[], *idx)
18
- end
19
- end
20
- end
21
- end
@@ -1,48 +0,0 @@
1
- module RipperRubyParser
2
- module SexpHandlers
3
- # Sexp handlers for hash literals
4
- module Hashes
5
- # Handle hash literals sexps. These can be either empty, or contain a
6
- # nested :assoclist_from_args Sexp.
7
- #
8
- # @example Empty hash
9
- # s(:hash, nil)
10
- # @example Hash with contents
11
- # s(:hash, s(:assoclist_from_args, ...))
12
- def process_hash(exp)
13
- _, body = exp.shift 2
14
- return s(:hash) unless body
15
- _, elems = body
16
- s(:hash, *make_hash_items(elems))
17
- end
18
-
19
- # @example
20
- # s(:assoc_splat, s(:vcall, s(:@ident, "bar")))
21
- def process_assoc_splat(exp)
22
- _, param = exp.shift 2
23
- s(:kwsplat, process(param))
24
- end
25
-
26
- # Handle implied hashes, such as at the end of argument lists.
27
- def process_bare_assoc_hash(exp)
28
- _, elems = exp.shift 2
29
- s(:hash, *make_hash_items(elems))
30
- end
31
-
32
- private
33
-
34
- # Process list of items that can be either :assoc_new or :assoc_splat
35
- def make_hash_items(elems)
36
- result = s()
37
- elems.each do |sub_exp|
38
- if sub_exp.sexp_type == :assoc_new
39
- sub_exp.sexp_body.each { |elem| result << process(elem) }
40
- else # :assoc_splat
41
- result << process(sub_exp)
42
- end
43
- end
44
- result
45
- end
46
- end
47
- end
48
- end