ripper_ruby_parser 1.7.2 → 1.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +28 -0
  3. data/README.md +4 -4
  4. data/lib/ripper_ruby_parser/commenting_ripper_parser.rb +24 -12
  5. data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +33 -54
  6. data/lib/ripper_ruby_parser/sexp_handlers/conditionals.rb +17 -19
  7. data/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +34 -1
  8. data/lib/ripper_ruby_parser/sexp_handlers/method_calls.rb +1 -1
  9. data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +12 -15
  10. data/lib/ripper_ruby_parser/sexp_handlers/string_literals.rb +21 -15
  11. data/lib/ripper_ruby_parser/sexp_processor.rb +4 -17
  12. data/lib/ripper_ruby_parser/unescape.rb +36 -12
  13. data/lib/ripper_ruby_parser/version.rb +1 -1
  14. metadata +110 -78
  15. data/Rakefile +0 -33
  16. data/test/end_to_end/comments_test.rb +0 -59
  17. data/test/end_to_end/comparison_test.rb +0 -104
  18. data/test/end_to_end/lib_comparison_test.rb +0 -18
  19. data/test/end_to_end/line_numbering_test.rb +0 -31
  20. data/test/end_to_end/samples_comparison_test.rb +0 -13
  21. data/test/end_to_end/test_comparison_test.rb +0 -18
  22. data/test/pt_testcase/pt_test.rb +0 -44
  23. data/test/ripper_ruby_parser/commenting_ripper_parser_test.rb +0 -200
  24. data/test/ripper_ruby_parser/parser_test.rb +0 -576
  25. data/test/ripper_ruby_parser/sexp_handlers/assignment_test.rb +0 -597
  26. data/test/ripper_ruby_parser/sexp_handlers/blocks_test.rb +0 -717
  27. data/test/ripper_ruby_parser/sexp_handlers/conditionals_test.rb +0 -536
  28. data/test/ripper_ruby_parser/sexp_handlers/literals_test.rb +0 -165
  29. data/test/ripper_ruby_parser/sexp_handlers/loops_test.rb +0 -209
  30. data/test/ripper_ruby_parser/sexp_handlers/method_calls_test.rb +0 -237
  31. data/test/ripper_ruby_parser/sexp_handlers/methods_test.rb +0 -429
  32. data/test/ripper_ruby_parser/sexp_handlers/operators_test.rb +0 -405
  33. data/test/ripper_ruby_parser/sexp_handlers/string_literals_test.rb +0 -973
  34. data/test/ripper_ruby_parser/sexp_processor_test.rb +0 -327
  35. data/test/ripper_ruby_parser/version_test.rb +0 -7
  36. data/test/samples/assignment.rb +0 -22
  37. data/test/samples/comments.rb +0 -13
  38. data/test/samples/conditionals.rb +0 -23
  39. data/test/samples/lambdas.rb +0 -5
  40. data/test/samples/loops.rb +0 -36
  41. data/test/samples/misc.rb +0 -285
  42. data/test/samples/number.rb +0 -9
  43. data/test/samples/operators.rb +0 -18
  44. data/test/samples/strings.rb +0 -147
  45. data/test/test_helper.rb +0 -111
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5d4b6b4c9b802cf538662716b22a68c98d26af9dd0fba04aa72c53515d869738
4
- data.tar.gz: e34339faf0080e288368618bd0688525e253300c25883fc74b8f86adfd572f9b
3
+ metadata.gz: 799011b06831426c0ebecad641082d58bfaa8da0aea9359a3a2d152ea01bd196
4
+ data.tar.gz: eec3804031490d2382f59ddc450af14f73b696b9e14f0d2919b64a3df3557853
5
5
  SHA512:
6
- metadata.gz: 1262aa2a0cb8b14e99650b8e49f1f1cf4ba672c59ed662b46204c0fcbd1d83580681d3b16440047b29dbc542a8c3dc2bd794071d32e09290a51a4fa4c6180476
7
- data.tar.gz: aedb6d0c231afd60183a0cf1152f34c5cc14ec088a22360d63ae4d3aacb50d2e8bc07548e8617611086ed1eddfa1ebb4c6d9aeb12f2093ad6d677e86e2ee363f
6
+ metadata.gz: eab6dc676f7c5806bcd09f9adf87e5b67dd5320e043c07370014fdfc8e5ca1dffbaa9583754ac74bc354a30c6b27392c3310a7172b20699d475d9122e9dae748
7
+ data.tar.gz: b776c9d833e7808c7b2ac17ec6bb5a358a93cb1bb925ee94199aeddf85c0cca2e102cdcdfff17ca8d17ac406e0f76c4b981b640085fbd02d2868bca36fdd840d
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ Notable changes to this project will be documented in this file.
4
+
5
+ This project adheres to [Semantic Versioning 2.0.0][1].
6
+
7
+ This document is formatted based on [Keep A CHANGELOG][2].
8
+
9
+ ## 1.8.0 / 2021-05-18
10
+
11
+ ### Changes
12
+
13
+ * Drop support for Ruby 2.4 ([2cf6b4050] and [#145])
14
+ * Update RubyParser compatibility target to 3.15.0 ([#105])
15
+ * Fix handling of dedented heredoc ([#106])
16
+ * Add support for running on Ruby 3.0 ([#123])
17
+ * Various code improvements and other internal changes
18
+
3
19
  ## 1.7.2 / 2020-02-28
4
20
 
5
21
  ### Bug fixes
@@ -201,6 +217,10 @@
201
217
  * Initial release
202
218
 
203
219
  <!-- Pull request links -->
220
+ [#145]: https://github.com/mvz/ripper_ruby_parser/pull/145
221
+ [#123]: https://github.com/mvz/ripper_ruby_parser/pull/123
222
+ [#106]: https://github.com/mvz/ripper_ruby_parser/pull/106
223
+ [#105]: https://github.com/mvz/ripper_ruby_parser/pull/105
204
224
  [#101]: https://github.com/mvz/ripper_ruby_parser/pull/101
205
225
  [#100]: https://github.com/mvz/ripper_ruby_parser/pull/100
206
226
  [#99]: https://github.com/mvz/ripper_ruby_parser/pull/99
@@ -231,3 +251,11 @@
231
251
  [#67]: https://github.com/mvz/ripper_ruby_parser/pull/67
232
252
  [#66]: https://github.com/mvz/ripper_ruby_parser/pull/66
233
253
  [#65]: https://github.com/mvz/ripper_ruby_parser/pull/65
254
+
255
+ <!-- Git sha links -->
256
+ [2cf6b4050]: https://github.com/mvz/ripper_ruby_parser/commit/2cf6b40501576003b916196e23222276b51aa53c
257
+
258
+ <!-- Other links -->
259
+
260
+ [1]: https://semver.org/spec/v2.0.0.html
261
+ [2]: https://keepachangelog.com/en/1.0.0/
data/README.md CHANGED
@@ -10,8 +10,8 @@ Parse with Ripper, produce sexps that are compatible with RubyParser.
10
10
 
11
11
  * Drop-in replacement for RubyParser
12
12
  * Should handle 1.9 and later syntax gracefully
13
- * Requires Ruby 2.4 or higher
14
- * Compatible with RubyParser 3.14.0
13
+ * Requires Ruby 2.5 or higher
14
+ * Compatible with RubyParser 3.15.0
15
15
 
16
16
  ## Known incompatibilities
17
17
 
@@ -44,7 +44,7 @@ parser.parse "foo[bar] += baz qux"
44
44
 
45
45
  ## Requirements
46
46
 
47
- * Ruby 2.4 or higher
47
+ * Ruby 2.5 or higher
48
48
  * `sexp_processor`
49
49
 
50
50
  ## Hacking and contributing
@@ -64,7 +64,7 @@ If you want to send pull requests or patches, please:
64
64
 
65
65
  (The MIT License)
66
66
 
67
- Copyright (c) 2012, 2014-2019 Matijs van Zuijlen
67
+ Copyright (c) 2012, 2014-2020 Matijs van Zuijlen
68
68
 
69
69
  Permission is hereby granted, free of charge, to any person obtaining
70
70
  a copy of this software and associated documentation files (the
@@ -27,6 +27,8 @@ module RipperRubyParser
27
27
  Sexp.from_array(result)
28
28
  end
29
29
 
30
+ private
31
+
30
32
  def on_backtick(delimiter)
31
33
  @delimiter_stack.push delimiter
32
34
  super
@@ -101,6 +103,18 @@ module RipperRubyParser
101
103
  @delimiter_stack.push delimiter
102
104
  end
103
105
 
106
+ def on_heredoc_dedent(val, width)
107
+ next_dedent = true
108
+ val.map! do |e|
109
+ if e.is_a?(Array) && e[0] == :@tstring_content
110
+ e = dedent_element(e, width) if next_dedent
111
+ next_dedent = e[1].end_with? "\n"
112
+ end
113
+ e
114
+ end
115
+ val
116
+ end
117
+
104
118
  def on_heredoc_end(_delimiter)
105
119
  @delimiter_stack.pop
106
120
  end
@@ -293,28 +307,26 @@ module RipperRubyParser
293
307
  commentize(:END, super)
294
308
  end
295
309
 
296
- def on_parse_error(*args)
297
- raise SyntaxError, *args
310
+ def on_parse_error(message)
311
+ raise SyntaxError, message
298
312
  end
299
313
 
300
- def on_class_name_error(*args)
301
- raise SyntaxError, *args
314
+ def on_class_name_error(message, *)
315
+ raise SyntaxError, message
302
316
  end
303
317
 
304
- def on_alias_error(*args)
305
- raise SyntaxError, *args
318
+ def on_alias_error(message, *)
319
+ raise SyntaxError, message
306
320
  end
307
321
 
308
- def on_assign_error(*args)
309
- raise SyntaxError, *args
322
+ def on_assign_error(message, *)
323
+ raise SyntaxError, message
310
324
  end
311
325
 
312
- def on_param_error(*args)
313
- raise SyntaxError, *args
326
+ def on_param_error(message, *)
327
+ raise SyntaxError, message
314
328
  end
315
329
 
316
- private
317
-
318
330
  def commentize(_name, exp)
319
331
  (_, _kw, loc), comment = @comment_stack.pop
320
332
  @comment = ""
@@ -17,13 +17,14 @@ module RipperRubyParser
17
17
  def process_params(exp)
18
18
  _, normal, defaults, splat, rest, kwargs, doublesplat, block = exp.shift 8
19
19
 
20
- args = handle_normal_arguments normal
21
- args += handle_default_arguments defaults
22
- args += handle_splat splat
23
- args += handle_normal_arguments rest
24
- args += handle_kwargs kwargs
25
- args += handle_double_splat doublesplat
26
- args += handle_block_argument block
20
+ args =
21
+ handle_normal_arguments(normal) +
22
+ handle_default_arguments(defaults) +
23
+ handle_splat(splat) +
24
+ handle_normal_arguments(rest) +
25
+ handle_kwargs(kwargs) +
26
+ handle_double_splat(doublesplat) +
27
+ handle_block_argument(block)
27
28
 
28
29
  s(:args, *args)
29
30
  end
@@ -49,7 +50,7 @@ module RipperRubyParser
49
50
  def process_begin(exp)
50
51
  _, body, pos = exp.shift 3
51
52
 
52
- body = convert_empty_to_nil_symbol process(body)
53
+ body = convert_void_stmt_to_nil_symbol process(body)
53
54
  with_position pos, s(:begin, body)
54
55
  end
55
56
 
@@ -58,19 +59,7 @@ module RipperRubyParser
58
59
  rescue_block = map_process_list_compact block.sexp_body
59
60
  rescue_block << nil if rescue_block.empty?
60
61
 
61
- capture = if eclass.nil?
62
- s(:array)
63
- elsif eclass.first.is_a? Symbol
64
- eclass = process(eclass)
65
- body = eclass.sexp_body
66
- if eclass.sexp_type == :mrhs
67
- body.first
68
- else
69
- s(:array, *body)
70
- end
71
- else
72
- s(:array, process(eclass.first))
73
- end
62
+ capture = handle_rescue_class_list eclass
74
63
 
75
64
  capture << create_assignment_sub_type(process(evar), s(:gvar, :$!)) if evar
76
65
 
@@ -80,23 +69,22 @@ module RipperRubyParser
80
69
  def process_bodystmt(exp)
81
70
  _, main, rescue_block, else_block, ensure_block = exp.shift 5
82
71
 
83
- body = s()
72
+ body_list = []
84
73
 
85
- main_list = map_unwrap_begin_list map_process_list main.sexp_body
86
- line = main_list.first.line
87
- main = wrap_in_block reject_void_stmt main_list
88
- body << main if main
74
+ main_block = process(main)
75
+ line = main_block.line
76
+ body_list << main_block if main_block.sexp_type != :void_stmt
89
77
 
90
- body.push(*process(rescue_block)) if rescue_block
91
- body << process(else_block) if else_block
92
- body = s(s(:rescue, *body)) if rescue_block
78
+ body_list.push(*process(rescue_block)) if rescue_block
79
+ body_list << process(else_block) if else_block
80
+ body_list = [s(:rescue, *body_list)] if rescue_block
93
81
 
94
82
  if ensure_block
95
- body << process(ensure_block)
96
- body = s(s(:ensure, *body))
83
+ body_list << process(ensure_block)
84
+ body_list = [s(:ensure, *body_list)]
97
85
  end
98
86
 
99
- wrap_in_block(body) || s().line(line)
87
+ wrap_in_block(body_list, line)
100
88
  end
101
89
 
102
90
  def process_rescue_mod(exp)
@@ -189,12 +177,7 @@ module RipperRubyParser
189
177
  def handle_double_splat(doublesplat)
190
178
  return [] unless doublesplat
191
179
 
192
- # Anonymous kwrest arguments are parsed into an Integer in Ruby 2.4
193
- if RUBY_VERSION < "2.5.0" && doublesplat.is_a?(Integer)
194
- [s(:dsplat, s(:lvar, :""))]
195
- else
196
- [s(:dsplat, process(doublesplat))]
197
- end
180
+ [s(:dsplat, process(doublesplat))]
198
181
  end
199
182
 
200
183
  def handle_block_argument(block)
@@ -203,12 +186,19 @@ module RipperRubyParser
203
186
  [process(block)]
204
187
  end
205
188
 
206
- def convert_empty_to_nil_symbol(block)
207
- case block.length
208
- when 0
209
- s(:nil)
189
+ def handle_rescue_class_list(eclass)
190
+ if eclass.nil?
191
+ s(:array)
192
+ elsif eclass.first.is_a? Symbol
193
+ eclass = process(eclass)
194
+ body = eclass.sexp_body
195
+ if eclass.sexp_type == :mrhs
196
+ body.first
197
+ else
198
+ s(:array, *body)
199
+ end
210
200
  else
211
- block
201
+ s(:array, process(eclass.first))
212
202
  end
213
203
  end
214
204
 
@@ -221,17 +211,6 @@ module RipperRubyParser
221
211
  s(:iter, call, args, stmt)
222
212
  end
223
213
  end
224
-
225
- def wrap_in_block(statements)
226
- case statements.length
227
- when 0
228
- nil
229
- when 1
230
- statements.first
231
- else
232
- s(:block, *statements)
233
- end
234
- end
235
214
  end
236
215
  end
237
216
  end
@@ -47,33 +47,20 @@ module RipperRubyParser
47
47
 
48
48
  def process_case(exp)
49
49
  _, expr, clauses = exp.shift 3
50
- s(:case, process(expr), *process(clauses))
50
+ s(:case, process(expr), *process(clauses).sexp_body)
51
51
  end
52
52
 
53
53
  def process_when(exp)
54
54
  _, values, truepart, falsepart = exp.shift 4
55
55
 
56
- falsepart = process(falsepart)
57
- falsepart = unwrap_nil falsepart if falsepart
58
-
59
- if falsepart.nil?
60
- falsepart = [nil]
61
- elsif falsepart.first.is_a? Symbol
62
- falsepart = s(falsepart)
63
- end
56
+ falsepart ||= s(:void_stmt)
64
57
 
58
+ falsepart = unwrap_case_body process(falsepart)
65
59
  values = process(values).sexp_body
60
+ truepart = unwrap_block process(truepart)
66
61
 
67
- truepart = map_process_list_compact truepart.sexp_body
68
- truepart = if truepart.empty?
69
- [nil]
70
- else
71
- unwrap_block(truepart.shift) + truepart
72
- end
73
-
74
- s(s(:when,
75
- s(:array, *values),
76
- *truepart),
62
+ s(:case_body,
63
+ s(:when, s(:array, *values), *truepart),
77
64
  *falsepart)
78
65
  end
79
66
 
@@ -109,6 +96,17 @@ module RipperRubyParser
109
96
  s(:if, cond, truepart, falsepart)
110
97
  end
111
98
  end
99
+
100
+ def unwrap_case_body(exp)
101
+ case exp.sexp_type
102
+ when :case_body
103
+ exp.sexp_body
104
+ when :void_stmt
105
+ [nil]
106
+ else
107
+ [exp]
108
+ end
109
+ end
112
110
  end
113
111
  end
114
112
  end
@@ -81,13 +81,46 @@ module RipperRubyParser
81
81
  end
82
82
 
83
83
  def unwrap_block(exp)
84
- if exp.sexp_type == :block
84
+ case exp.sexp_type
85
+ when :block
85
86
  exp.sexp_body
87
+ when :void_stmt
88
+ [nil]
86
89
  else
87
90
  [exp]
88
91
  end
89
92
  end
90
93
 
94
+ def wrap_in_block(statements, line)
95
+ case statements.length
96
+ when 0
97
+ s(:void_stmt).line(line)
98
+ when 1
99
+ statements.first
100
+ else
101
+ first = statements.shift
102
+ s(:block, *unwrap_block(first), *statements)
103
+ end
104
+ end
105
+
106
+ def convert_void_stmt_to_nil_symbol(block)
107
+ case block.sexp_type
108
+ when :void_stmt
109
+ s(:nil)
110
+ else
111
+ block
112
+ end
113
+ end
114
+
115
+ def convert_empty_to_nil_symbol(block)
116
+ case block.length
117
+ when 0
118
+ s(:nil)
119
+ else
120
+ block
121
+ end
122
+ end
123
+
91
124
  def handle_return_argument_list(arglist)
92
125
  args = process(arglist).sexp_body
93
126
 
@@ -17,7 +17,7 @@ module RipperRubyParser
17
17
 
18
18
  def process_arg_paren(exp)
19
19
  _, args = exp.shift 2
20
- return s() if args.nil?
20
+ return s(:arglist) if args.nil?
21
21
 
22
22
  process(args)
23
23
  end
@@ -83,8 +83,8 @@ module RipperRubyParser
83
83
 
84
84
  def method_body(exp)
85
85
  block = process exp
86
- case block.length
87
- when 0
86
+ case block.sexp_type
87
+ when :void_stmt
88
88
  [s(:nil).line(block.line)]
89
89
  else
90
90
  unwrap_block block
@@ -99,23 +99,20 @@ module RipperRubyParser
99
99
 
100
100
  def convert_arguments(args)
101
101
  args.line ||= args.sexp_body.first&.line
102
- args.map! { |item| convert_argument item }
102
+ args.sexp_body = args.sexp_body.map { |item| convert_argument item }
103
+ args
103
104
  end
104
105
 
105
106
  def convert_argument(item)
106
- if item.is_a? Symbol
107
- item
107
+ case item.sexp_type
108
+ when :lvar
109
+ item.last
110
+ when *SPECIAL_ARG_MARKER.keys
111
+ convert_marked_argument(item)
112
+ when :masgn
113
+ convert_masgn_argument(item)
108
114
  else
109
- case item.sexp_type
110
- when :lvar
111
- item.last
112
- when *SPECIAL_ARG_MARKER.keys
113
- convert_marked_argument(item)
114
- when :masgn
115
- convert_masgn_argument(item)
116
- else
117
- item
118
- end
115
+ item
119
116
  end
120
117
  end
121
118