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
@@ -1,8 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path('../../test_helper.rb', File.dirname(__FILE__))
2
4
 
3
5
  describe RipperRubyParser::Parser do
4
6
  describe '#parse' do
5
- describe 'for blocks' do
7
+ describe 'for do blocks' do
6
8
  it 'works with no statements in the block body' do
7
9
  'foo do; end'.
8
10
  must_be_parsed_as s(:iter,
@@ -17,6 +19,33 @@ describe RipperRubyParser::Parser do
17
19
  0,
18
20
  s(:redo))
19
21
  end
22
+
23
+ it 'works with nested begin..end' do
24
+ 'foo do; begin; bar; end; end;'.
25
+ must_be_parsed_as s(:iter,
26
+ s(:call, nil, :foo),
27
+ 0,
28
+ s(:call, nil, :bar))
29
+ end
30
+
31
+ it 'works with nested begin..end plus other statements' do
32
+ 'foo do; bar; begin; baz; end; end;'.
33
+ must_be_parsed_as s(:iter,
34
+ s(:call, nil, :foo),
35
+ 0,
36
+ s(:block,
37
+ s(:call, nil, :bar),
38
+ s(:call, nil, :baz)))
39
+ end
40
+ end
41
+
42
+ describe 'for brace blocks' do
43
+ it 'works with no statements in the block body' do
44
+ 'foo { }'.
45
+ must_be_parsed_as s(:iter,
46
+ s(:call, nil, :foo),
47
+ 0)
48
+ end
20
49
  end
21
50
 
22
51
  describe 'for block parameters' do
@@ -94,112 +123,85 @@ describe RipperRubyParser::Parser do
94
123
  s(:args, :bar, :baz))
95
124
  end
96
125
 
97
- it 'works with a single splat argument' do
98
- 'foo do |*bar|; end'.
126
+ it 'works with an argument with a default value' do
127
+ 'foo do |bar=baz|; end'.
99
128
  must_be_parsed_as s(:iter,
100
129
  s(:call, nil, :foo),
101
- s(:args, :"*bar"))
130
+ s(:args,
131
+ s(:lasgn, :bar, s(:call, nil, :baz))))
102
132
  end
103
133
 
104
- it 'works with a combination of regular arguments and a splat argument' do
105
- 'foo do |bar, *baz|; end'.
134
+ it 'works with a keyword argument with no default value' do
135
+ 'foo do |bar:|; end'.
106
136
  must_be_parsed_as s(:iter,
107
137
  s(:call, nil, :foo),
108
- s(:args, :bar, :"*baz"))
109
- end
110
- end
111
-
112
- describe 'for begin' do
113
- it 'works for an empty begin..end block' do
114
- 'begin end'.must_be_parsed_as s(:nil)
115
- end
116
-
117
- it 'works for a simple begin..end block' do
118
- 'begin; foo; end'.must_be_parsed_as s(:call, nil, :foo)
138
+ s(:args,
139
+ s(:kwarg, :bar)))
119
140
  end
120
141
 
121
- it 'works for begin..end block with more than one statement' do
122
- 'begin; foo; bar; end'.
123
- must_be_parsed_as s(:block,
142
+ it 'works with a keyword argument with a default value' do
143
+ 'foo do |bar: baz|; end'.
144
+ must_be_parsed_as s(:iter,
124
145
  s(:call, nil, :foo),
125
- s(:call, nil, :bar))
126
- end
127
-
128
- it 'keeps :begin for the argument of a unary operator' do
129
- '- begin; foo; end'.
130
- must_be_parsed_as s(:call,
131
- s(:begin, s(:call, nil, :foo)),
132
- :-@)
133
- end
134
-
135
- it 'keeps :begin for the first argument of a binary operator' do
136
- 'begin; bar; end + foo'.
137
- must_be_parsed_as s(:call,
138
- s(:begin, s(:call, nil, :bar)),
139
- :+,
140
- s(:call, nil, :foo))
146
+ s(:args,
147
+ s(:kwarg, :bar, s(:call, nil, :baz))))
141
148
  end
142
149
 
143
- it 'keeps :begin for the second argument of a binary operator' do
144
- 'foo + begin; bar; end'.
145
- must_be_parsed_as s(:call,
150
+ it 'works with a single splat argument' do
151
+ 'foo do |*bar|; end'.
152
+ must_be_parsed_as s(:iter,
146
153
  s(:call, nil, :foo),
147
- :+,
148
- s(:begin, s(:call, nil, :bar)))
154
+ s(:args, :"*bar"))
149
155
  end
150
156
 
151
- it 'does not keep :begin for the first argument of a boolean operator' do
152
- 'begin; bar; end and foo'.
153
- must_be_parsed_as s(:and,
154
- s(:call, nil, :bar),
155
- s(:call, nil, :foo))
157
+ it 'works with a combination of regular arguments and a splat argument' do
158
+ 'foo do |bar, *baz|; end'.
159
+ must_be_parsed_as s(:iter,
160
+ s(:call, nil, :foo),
161
+ s(:args, :bar, :"*baz"))
156
162
  end
157
163
 
158
- it 'keeps :begin for the second argument of a boolean operator' do
159
- 'foo and begin; bar; end'.
160
- must_be_parsed_as s(:and,
164
+ it 'works with a kwrest argument' do
165
+ 'foo do |**bar|; baz bar; end'.
166
+ must_be_parsed_as s(:iter,
161
167
  s(:call, nil, :foo),
162
- s(:begin, s(:call, nil, :bar)))
168
+ s(:args, :"**bar"),
169
+ s(:call, nil, :baz,
170
+ s(:lvar, :bar)))
163
171
  end
164
172
 
165
- it 'does not keep :begin for the first argument of a shift operator' do
166
- 'begin; bar; end << foo'.
167
- must_be_parsed_as s(:call,
168
- s(:call, nil, :bar),
169
- :<<,
170
- s(:call, nil, :foo))
173
+ it 'works with a regular argument after a splat argument' do
174
+ 'foo do |*bar, baz|; end'.
175
+ must_be_parsed_as s(:iter,
176
+ s(:call, nil, :foo),
177
+ s(:args, :"*bar", :baz))
171
178
  end
172
179
 
173
- it 'does not keep :begin for the second argument of a shift operator' do
174
- 'foo >> begin; bar; end'.
175
- must_be_parsed_as s(:call,
180
+ it 'works with a combination of regular arguments and a kwrest argument' do
181
+ 'foo do |bar, **baz|; qux bar, baz; end'.
182
+ must_be_parsed_as s(:iter,
176
183
  s(:call, nil, :foo),
177
- :>>,
178
- s(:call, nil, :bar))
184
+ s(:args, :bar, :"**baz"),
185
+ s(:call, nil, :qux,
186
+ s(:lvar, :bar),
187
+ s(:lvar, :baz)))
179
188
  end
189
+ end
180
190
 
181
- it 'keeps :begin for the first argument of a ternary operator' do
182
- 'begin; foo; end ? bar : baz'.
183
- must_be_parsed_as s(:if,
184
- s(:begin, s(:call, nil, :foo)),
185
- s(:call, nil, :bar),
186
- s(:call, nil, :baz))
191
+ describe 'for begin' do
192
+ it 'works for an empty begin..end block' do
193
+ 'begin end'.must_be_parsed_as s(:nil)
187
194
  end
188
195
 
189
- it 'keeps :begin for the second argument of a ternary operator' do
190
- 'foo ? begin; bar; end : baz'.
191
- must_be_parsed_as s(:if,
192
- s(:call, nil, :foo),
193
- s(:begin, s(:call, nil, :bar)),
194
- s(:call, nil, :baz))
196
+ it 'works for a simple begin..end block' do
197
+ 'begin; foo; end'.must_be_parsed_as s(:call, nil, :foo)
195
198
  end
196
199
 
197
- it 'keeps :begin for the third argument of a ternary operator' do
198
- 'foo ? bar : begin; baz; end'.
199
- must_be_parsed_as s(:if,
200
+ it 'works for begin..end block with more than one statement' do
201
+ 'begin; foo; bar; end'.
202
+ must_be_parsed_as s(:block,
200
203
  s(:call, nil, :foo),
201
- s(:call, nil, :bar),
202
- s(:begin, s(:call, nil, :baz)))
204
+ s(:call, nil, :bar))
203
205
  end
204
206
 
205
207
  it 'keeps :begin for the truepart of a postfix if' do
@@ -336,12 +338,31 @@ describe RipperRubyParser::Parser do
336
338
  s(:call, nil, :qux)))
337
339
  end
338
340
 
339
- it 'works in the postfix case' do
340
- 'foo rescue bar'.
341
+ it 'works rescuing a splatted list of exception types' do
342
+ 'begin; foo; rescue *bar; baz; end'.
341
343
  must_be_parsed_as s(:rescue,
342
344
  s(:call, nil, :foo),
343
345
  s(:resbody,
344
- s(:array),
346
+ s(:splat, s(:call, nil, :bar)),
347
+ s(:call, nil, :baz)))
348
+ end
349
+
350
+ it 'works rescuing a complex list of exception types' do
351
+ 'begin; foo; rescue *bar, Baz; qux; end'.
352
+ must_be_parsed_as s(:rescue,
353
+ s(:call, nil, :foo),
354
+ s(:resbody,
355
+ s(:array,
356
+ s(:splat, s(:call, nil, :bar)),
357
+ s(:const, :Baz)),
358
+ s(:call, nil, :qux)))
359
+ end
360
+
361
+ it 'works with a nested begin..end block' do
362
+ 'begin; foo; rescue; begin; bar; end; end'.
363
+ must_be_parsed_as s(:rescue,
364
+ s(:call, nil, :foo),
365
+ s(:resbody, s(:array),
345
366
  s(:call, nil, :bar)))
346
367
  end
347
368
 
@@ -357,7 +378,7 @@ describe RipperRubyParser::Parser do
357
378
  s(:call, nil, :baz))))
358
379
  end
359
380
 
360
- it 'works in a method body inside begin..end' do
381
+ it 'works in a method body inside begin..end with rescue' do
361
382
  'def foo; bar; begin; baz; rescue; qux; end; quuz; end'.
362
383
  must_be_parsed_as s(:defn,
363
384
  :foo,
@@ -368,6 +389,47 @@ describe RipperRubyParser::Parser do
368
389
  s(:resbody, s(:array), s(:call, nil, :qux))),
369
390
  s(:call, nil, :quuz))
370
391
  end
392
+
393
+ it 'works in a method body inside begin..end without rescue' do
394
+ 'def foo; bar; begin; baz; qux; end; quuz; end'.
395
+ must_be_parsed_as s(:defn,
396
+ :foo,
397
+ s(:args),
398
+ s(:call, nil, :bar),
399
+ s(:block,
400
+ s(:call, nil, :baz),
401
+ s(:call, nil, :qux)),
402
+ s(:call, nil, :quuz))
403
+ end
404
+
405
+ it 'works in a method body fully inside begin..end' do
406
+ 'def foo; begin; bar; baz; end; end'.
407
+ must_be_parsed_as s(:defn,
408
+ :foo,
409
+ s(:args),
410
+ s(:call, nil, :bar),
411
+ s(:call, nil, :baz))
412
+ end
413
+ end
414
+
415
+ describe 'for the postfix rescue modifier' do
416
+ it 'works in the basic case' do
417
+ 'foo rescue bar'.
418
+ must_be_parsed_as s(:rescue,
419
+ s(:call, nil, :foo),
420
+ s(:resbody,
421
+ s(:array),
422
+ s(:call, nil, :bar)))
423
+ end
424
+
425
+ it 'works when the fallback value is a keyword' do
426
+ 'foo rescue next'.
427
+ must_be_parsed_as s(:rescue,
428
+ s(:call, nil, :foo),
429
+ s(:resbody,
430
+ s(:array),
431
+ s(:next)))
432
+ end
371
433
  end
372
434
 
373
435
  describe 'for the ensure statement' do
@@ -554,6 +616,14 @@ describe RipperRubyParser::Parser do
554
616
  s(:call, nil, :bar))
555
617
  end
556
618
 
619
+ it 'works in the simple case without parentheses' do
620
+ '-> foo { bar }'.
621
+ must_be_parsed_as s(:iter,
622
+ s(:call, nil, :lambda),
623
+ s(:args, :foo),
624
+ s(:call, nil, :bar))
625
+ end
626
+
557
627
  it 'works when there are zero arguments' do
558
628
  '->() { bar }'.
559
629
  must_be_parsed_as s(:iter,
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path('../../test_helper.rb', File.dirname(__FILE__))
2
4
 
3
5
  describe RipperRubyParser::Parser do
@@ -29,6 +31,14 @@ describe RipperRubyParser::Parser do
29
31
  nil)
30
32
  end
31
33
 
34
+ it 'works with a begin..end block' do
35
+ 'if foo; begin; bar; end; end'.
36
+ must_be_parsed_as s(:if,
37
+ s(:call, nil, :foo),
38
+ s(:call, nil, :bar),
39
+ nil)
40
+ end
41
+
32
42
  it 'works with an else clause' do
33
43
  'if foo; bar; else; baz; end'.
34
44
  must_be_parsed_as s(:if,
@@ -61,6 +71,14 @@ describe RipperRubyParser::Parser do
61
71
  nil)
62
72
  end
63
73
 
74
+ it 'handles bare integer literal in condition' do
75
+ 'if 1; bar; end'.
76
+ must_be_parsed_as s(:if,
77
+ s(:lit, 1),
78
+ s(:call, nil, :bar),
79
+ nil)
80
+ end
81
+
64
82
  it 'handles bare regex literal in condition' do
65
83
  'if /foo/; bar; end'.
66
84
  must_be_parsed_as s(:if,
@@ -108,6 +126,37 @@ describe RipperRubyParser::Parser do
108
126
  s(:call, nil, :qux),
109
127
  s(:call, nil, :baz))
110
128
  end
129
+
130
+ it 'cleans up begin..end block in condition' do
131
+ 'if begin foo end; bar; end'.
132
+ must_be_parsed_as s(:if,
133
+ s(:call, nil, :foo),
134
+ s(:call, nil, :bar), nil)
135
+ end
136
+
137
+ it 'handles special conditions inside begin..end block' do
138
+ 'if begin foo..bar end; baz; end'.
139
+ must_be_parsed_as s(:if,
140
+ s(:flip2, s(:call, nil, :foo), s(:call, nil, :bar)),
141
+ s(:call, nil, :baz),
142
+ nil)
143
+ end
144
+
145
+ it 'works with assignment in the condition' do
146
+ 'if foo = bar; baz; end'.
147
+ must_be_parsed_as s(:if,
148
+ s(:lasgn, :foo,
149
+ s(:call, nil, :bar)),
150
+ s(:call, nil, :baz), nil)
151
+ end
152
+
153
+ it 'works with bracketed assignment in the condition' do
154
+ 'if (foo = bar); baz; end'.
155
+ must_be_parsed_as s(:if,
156
+ s(:lasgn, :foo,
157
+ s(:call, nil, :bar)),
158
+ s(:call, nil, :baz), nil)
159
+ end
111
160
  end
112
161
 
113
162
  describe 'for postfix if' do
@@ -150,6 +199,13 @@ describe RipperRubyParser::Parser do
150
199
  nil,
151
200
  s(:call, nil, :baz))
152
201
  end
202
+
203
+ it 'cleans up begin..end block in condition' do
204
+ 'foo if begin bar end'.
205
+ must_be_parsed_as s(:if,
206
+ s(:call, nil, :bar),
207
+ s(:call, nil, :foo), nil)
208
+ end
153
209
  end
154
210
 
155
211
  describe 'for regular unless' do
@@ -285,6 +341,17 @@ describe RipperRubyParser::Parser do
285
341
  nil))
286
342
  end
287
343
 
344
+ it 'works with an else' do
345
+ 'if foo; bar; elsif baz; qux; else; quuz; end'.
346
+ must_be_parsed_as s(:if,
347
+ s(:call, nil, :foo),
348
+ s(:call, nil, :bar),
349
+ s(:if,
350
+ s(:call, nil, :baz),
351
+ s(:call, nil, :qux),
352
+ s(:call, nil, :quuz)))
353
+ end
354
+
288
355
  it 'works with an empty else' do
289
356
  'if foo; bar; elsif baz; qux; else; end'.
290
357
  must_be_parsed_as s(:if,
@@ -330,6 +397,17 @@ describe RipperRubyParser::Parser do
330
397
  s(:call, nil, :quuz),
331
398
  nil))
332
399
  end
400
+
401
+ it 'cleans up begin..end block in condition' do
402
+ 'if foo; bar; elsif begin baz end; qux; end'.
403
+ must_be_parsed_as s(:if,
404
+ s(:call, nil, :foo),
405
+ s(:call, nil, :bar),
406
+ s(:if,
407
+ s(:call, nil, :baz),
408
+ s(:call, nil, :qux),
409
+ nil))
410
+ end
333
411
  end
334
412
 
335
413
  describe 'for case block' do
@@ -377,6 +455,19 @@ describe RipperRubyParser::Parser do
377
455
  s(:call, nil, :qux))
378
456
  end
379
457
 
458
+ it 'works with multiple statements in the else block' do
459
+ 'case foo; when bar; baz; else; qux; quuz end'.
460
+ must_be_parsed_as s(:case,
461
+ s(:call, nil, :foo),
462
+ s(:when,
463
+ s(:array,
464
+ s(:call, nil, :bar)),
465
+ s(:call, nil, :baz)),
466
+ s(:block,
467
+ s(:call, nil, :qux),
468
+ s(:call, nil, :quuz)))
469
+ end
470
+
380
471
  it 'works with an empty when block' do
381
472
  'case foo; when bar; end'.
382
473
  must_be_parsed_as s(:case,