ripper_ruby_parser 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +3 -3
  4. data/Rakefile +4 -4
  5. data/lib/ripper_ruby_parser/commenting_ripper_parser.rb +5 -7
  6. data/lib/ripper_ruby_parser/parser.rb +2 -3
  7. data/lib/ripper_ruby_parser/sexp_handlers/arguments.rb +2 -6
  8. data/lib/ripper_ruby_parser/sexp_handlers/assignment.rb +16 -17
  9. data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +5 -5
  10. data/lib/ripper_ruby_parser/sexp_handlers/conditionals.rb +6 -7
  11. data/lib/ripper_ruby_parser/sexp_handlers/hashes.rb +4 -5
  12. data/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +7 -11
  13. data/lib/ripper_ruby_parser/sexp_handlers/literals.rb +35 -26
  14. data/lib/ripper_ruby_parser/sexp_handlers/loops.rb +19 -18
  15. data/lib/ripper_ruby_parser/sexp_handlers/method_calls.rb +15 -15
  16. data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +9 -18
  17. data/lib/ripper_ruby_parser/sexp_handlers/operators.rb +10 -10
  18. data/lib/ripper_ruby_parser/sexp_processor.rb +11 -14
  19. data/lib/ripper_ruby_parser/syntax_error.rb +1 -3
  20. data/lib/ripper_ruby_parser/version.rb +1 -1
  21. data/test/end_to_end/comparison_test.rb +0 -1
  22. data/test/end_to_end/lib_comparison_test.rb +0 -2
  23. data/test/end_to_end/line_numbering_test.rb +0 -1
  24. data/test/end_to_end/samples_comparison_test.rb +1 -1
  25. data/test/end_to_end/test_comparison_test.rb +0 -3
  26. data/test/pt_testcase/pt_test.rb +4 -4
  27. data/test/samples/inline.rb +704 -0
  28. data/test/test_helper.rb +39 -37
  29. data/test/unit/commenting_ripper_parser_test.rb +57 -59
  30. data/test/unit/parser_blocks_test.rb +19 -3
  31. data/test/unit/parser_conditionals_test.rb +0 -1
  32. data/test/unit/parser_literals_test.rb +25 -25
  33. data/test/unit/parser_method_calls_test.rb +19 -15
  34. data/test/unit/parser_test.rb +31 -24
  35. data/test/unit/sexp_processor_test.rb +1 -14
  36. metadata +67 -51
  37. data/lib/ripper_ruby_parser/sexp_ext.rb +0 -14
data/test/test_helper.rb CHANGED
@@ -1,9 +1,6 @@
1
- begin
2
- require 'simplecov'
3
- SimpleCov.start do
4
- add_filter "/test/"
5
- end
6
- rescue LoadError
1
+ require 'simplecov'
2
+ SimpleCov.start do
3
+ add_filter "/test/"
7
4
  end
8
5
 
9
6
  require 'minitest/autorun'
@@ -12,45 +9,50 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
9
 
13
10
  require 'ripper_ruby_parser'
14
11
 
15
- class MiniTest::Spec
16
- def formatted exp
17
- exp.to_s.gsub(/\), /, "),\n")
18
- end
12
+ module MiniTest
13
+ class Spec
14
+ def formatted exp
15
+ exp.to_s.gsub(/\), /, "),\n")
16
+ end
19
17
 
20
- def to_comments exp
21
- inner = exp.map do |sub_exp|
22
- if sub_exp.is_a? Sexp
23
- to_comments sub_exp
18
+ def to_comments exp
19
+ inner = exp.map do |sub_exp|
20
+ if sub_exp.is_a? Sexp
21
+ to_comments sub_exp
22
+ else
23
+ sub_exp
24
+ end
25
+ end
26
+
27
+ comments = exp.comments.to_s.gsub(/\n\s*\n/, "\n")
28
+ if comments.empty?
29
+ s(*inner)
24
30
  else
25
- sub_exp
31
+ s(:comment, comments, s(*inner))
26
32
  end
27
33
  end
28
34
 
29
- comments = exp.comments.to_s.gsub(/\n\s*\n/, "\n")
30
- if comments.empty?
31
- s(*inner)
32
- else
33
- s(:comment, comments, s(*inner))
35
+ def assert_parsed_as sexp, code
36
+ parser = RipperRubyParser::Parser.new
37
+ result = parser.parse code
38
+ if sexp.nil?
39
+ assert_nil result
40
+ else
41
+ assert_equal sexp, result
42
+ end
34
43
  end
35
- end
36
44
 
37
- def assert_parsed_as sexp, code
38
- parser = RipperRubyParser::Parser.new
39
- result = parser.parse code
40
- assert_equal sexp, result
45
+ def assert_parsed_as_before code
46
+ oldparser = RubyParser.new
47
+ newparser = RipperRubyParser::Parser.new
48
+ expected = oldparser.parse code.dup
49
+ result = newparser.parse code
50
+ assert_equal formatted(expected), formatted(result)
51
+ end
41
52
  end
42
53
 
43
- def assert_parsed_as_before code
44
- oldparser = RubyParser.new
45
- newparser = RipperRubyParser::Parser.new
46
- expected = oldparser.parse code.dup
47
- result = newparser.parse code
48
- assert_equal formatted(expected), formatted(result)
54
+ module Expectations
55
+ infect_an_assertion :assert_parsed_as, :must_be_parsed_as
56
+ infect_an_assertion :assert_parsed_as_before, :must_be_parsed_as_before, :unary
49
57
  end
50
58
  end
51
-
52
- module MiniTest::Expectations
53
- infect_an_assertion :assert_parsed_as, :must_be_parsed_as
54
- infect_an_assertion :assert_parsed_as_before, :must_be_parsed_as_before, :unary
55
- end
56
-
@@ -11,111 +11,109 @@ describe RipperRubyParser::CommentingRipperParser do
11
11
  num_params = RUBY_VERSION < "2.0.0" ? 5 : 7
12
12
  s(:params, *([nil] * num_params))
13
13
  end
14
-
15
14
  end
16
15
 
17
16
  describe "handling comments" do
18
17
  it "produces a comment node surrounding a commented def" do
19
18
  result = parse_with_builder "# Foo\ndef foo; end"
20
19
  result.must_equal s(:program,
21
- s(s(:comment,
22
- "# Foo\n",
23
- s(:def,
24
- s(:@ident, "foo", s(2, 4)),
25
- empty_params_list,
26
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
20
+ s(s(:comment,
21
+ "# Foo\n",
22
+ s(:def,
23
+ s(:@ident, "foo", s(2, 4)),
24
+ empty_params_list,
25
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
27
26
  end
28
27
 
29
28
  it "produces a blank comment node surrounding a def that has no comment" do
30
29
  result = parse_with_builder "def foo; end"
31
30
  result.must_equal s(:program,
32
- s(s(:comment,
33
- "",
34
- s(:def,
35
- s(:@ident, "foo", s(1, 4)),
36
- empty_params_list,
37
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
31
+ s(s(:comment,
32
+ "",
33
+ s(:def,
34
+ s(:@ident, "foo", s(1, 4)),
35
+ empty_params_list,
36
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
38
37
  end
39
38
 
40
39
  it "produces a comment node surrounding a commented class" do
41
40
  result = parse_with_builder "# Foo\nclass Foo; end"
42
41
  result.must_equal s(:program,
43
- s(s(:comment,
44
- "# Foo\n",
45
- s(:class,
46
- s(:const_ref, s(:@const, "Foo", s(2, 6))),
47
- nil,
48
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
42
+ s(s(:comment,
43
+ "# Foo\n",
44
+ s(:class,
45
+ s(:const_ref, s(:@const, "Foo", s(2, 6))),
46
+ nil,
47
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
49
48
  end
50
49
 
51
50
  it "produce a blank comment node surrounding a class that has no comment" do
52
51
  result = parse_with_builder "class Foo; end"
53
52
  result.must_equal s(:program,
54
- s(s(:comment,
55
- "",
56
- s(:class,
57
- s(:const_ref, s(:@const, "Foo", s(1, 6))),
58
- nil,
59
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
53
+ s(s(:comment,
54
+ "",
55
+ s(:class,
56
+ s(:const_ref, s(:@const, "Foo", s(1, 6))),
57
+ nil,
58
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
60
59
  end
61
60
 
62
61
  it "produces a comment node surrounding a commented module" do
63
62
  result = parse_with_builder "# Foo\nmodule Foo; end"
64
63
  result.must_equal s(:program,
65
- s(s(:comment,
66
- "# Foo\n",
67
- s(:module,
68
- s(:const_ref, s(:@const, "Foo", s(2, 7))),
69
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
64
+ s(s(:comment,
65
+ "# Foo\n",
66
+ s(:module,
67
+ s(:const_ref, s(:@const, "Foo", s(2, 7))),
68
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
70
69
  end
71
70
 
72
71
  it "produces a blank comment node surrounding a module that has no comment" do
73
72
  result = parse_with_builder "module Foo; end"
74
73
  result.must_equal s(:program,
75
- s(s(:comment,
76
- "",
77
- s(:module,
78
- s(:const_ref, s(:@const, "Foo", s(1, 7))),
79
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
74
+ s(s(:comment,
75
+ "",
76
+ s(:module,
77
+ s(:const_ref, s(:@const, "Foo", s(1, 7))),
78
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
80
79
  end
81
80
 
82
81
  it "is not confused by a symbol containing a keyword" do
83
82
  result = parse_with_builder ":class; def foo; end"
84
83
  result.must_equal s(:program,
85
- s(s(:symbol_literal, s(:symbol, s(:@kw, "class", s(1, 1)))),
86
- s(:comment,
87
- "",
88
- s(:def,
89
- s(:@ident, "foo", s(1, 12)),
90
- empty_params_list,
91
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
84
+ s(s(:symbol_literal, s(:symbol, s(:@kw, "class", s(1, 1)))),
85
+ s(:comment,
86
+ "",
87
+ s(:def,
88
+ s(:@ident, "foo", s(1, 12)),
89
+ empty_params_list,
90
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
92
91
  end
93
92
 
94
93
  it "is not confused by a dynamic symbol" do
95
94
  result = parse_with_builder ":'foo'; def bar; end"
96
95
  result.must_equal s(:program,
97
- s(s(:dyna_symbol, s(s(:@tstring_content, "foo", s(1, 2)))),
98
- s(:comment,
99
- "",
100
- s(:def,
101
- s(:@ident, "bar", s(1, 12)),
102
- empty_params_list,
103
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
96
+ s(s(:dyna_symbol, s(s(:@tstring_content, "foo", s(1, 2)))),
97
+ s(:comment,
98
+ "",
99
+ s(:def,
100
+ s(:@ident, "bar", s(1, 12)),
101
+ empty_params_list,
102
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))
104
103
  end
105
104
 
106
105
  it "is not confused by a dynamic symbol containing a class definition" do
107
106
  result = parse_with_builder ":\"foo\#{class Bar;end}\""
108
107
  result.must_equal s(:program,
109
- s(s(:dyna_symbol,
110
- s(s(:@tstring_content, "foo", s(1, 2)),
111
- s(:string_embexpr,
112
- s(s(:comment,
113
- "",
114
- s(:class,
115
- s(:const_ref, s(:@const, "Bar", s(1, 13))),
116
- nil,
117
- s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))))))
108
+ s(s(:dyna_symbol,
109
+ s(s(:@tstring_content, "foo", s(1, 2)),
110
+ s(:string_embexpr,
111
+ s(s(:comment,
112
+ "",
113
+ s(:class,
114
+ s(:const_ref, s(:@const, "Bar", s(1, 13))),
115
+ nil,
116
+ s(:bodystmt, s(s(:void_stmt)), nil, nil, nil)))))))))
118
117
  end
119
118
  end
120
119
  end
121
-
@@ -33,11 +33,19 @@ describe RipperRubyParser::Parser do
33
33
  s(:args, :bar, :"&baz"))
34
34
  end
35
35
 
36
+ it "handles absent parameter specs" do
37
+ "foo do; bar; end".
38
+ must_be_parsed_as s(:iter,
39
+ s(:call, nil, :foo),
40
+ 0,
41
+ s(:call, nil, :bar))
42
+ end
43
+
36
44
  it "handles empty parameter specs" do
37
45
  "foo do ||; bar; end".
38
46
  must_be_parsed_as s(:iter,
39
47
  s(:call, nil, :foo),
40
- 0,
48
+ s(:args),
41
49
  s(:call, nil, :bar))
42
50
  end
43
51
 
@@ -261,7 +269,15 @@ describe RipperRubyParser::Parser do
261
269
  must_be_parsed_as s(:iter,
262
270
  s(:call, nil, :lambda),
263
271
  s(:args, :foo),
264
- s(:call, nil, :bar))
272
+ s(:call, nil, :bar))
273
+ end
274
+
275
+ it "works when there are zero arguments" do
276
+ "->() { bar }".
277
+ must_be_parsed_as s(:iter,
278
+ s(:call, nil, :lambda),
279
+ s(:args),
280
+ s(:call, nil, :bar))
265
281
  end
266
282
 
267
283
  it "works when there are no arguments" do
@@ -269,7 +285,7 @@ describe RipperRubyParser::Parser do
269
285
  must_be_parsed_as s(:iter,
270
286
  s(:call, nil, :lambda),
271
287
  0,
272
- s(:call, nil, :bar))
288
+ s(:call, nil, :bar))
273
289
  end
274
290
  end
275
291
  end
@@ -264,7 +264,6 @@ describe RipperRubyParser::Parser do
264
264
  s(:splat, s(:call, nil, :bar))),
265
265
  s(:call, nil, :baz)),
266
266
  nil)
267
-
268
267
  end
269
268
  end
270
269
  end
@@ -1,4 +1,3 @@
1
- # coding: utf-8
2
1
  require File.expand_path('../test_helper.rb', File.dirname(__FILE__))
3
2
 
4
3
  describe RipperRubyParser::Parser do
@@ -140,7 +139,7 @@ describe RipperRubyParser::Parser do
140
139
  result[1].encoding.to_s.must_equal "UTF-8"
141
140
  end
142
141
 
143
- describe "with escape sequences" do
142
+ describe "with double-quoted strings with escape sequences" do
144
143
  it "works for strings with escape sequences" do
145
144
  "\"\\n\"".
146
145
  must_be_parsed_as s(:str, "\n")
@@ -156,12 +155,12 @@ describe RipperRubyParser::Parser do
156
155
  must_be_parsed_as s(:str, "\\n")
157
156
  end
158
157
 
159
- it "works for a double-quoted string representing a regex literal with escaped right bracket" do
158
+ it "works for a representation of a regex literal with escaped right bracket" do
160
159
  "\"/\\\\)/\"".
161
160
  must_be_parsed_as s(:str, "/\\)/")
162
161
  end
163
162
 
164
- it "works for a double-quoted string containing a uselessly escaped right bracket" do
163
+ it "works for a uselessly escaped right bracket" do
165
164
  "\"/\\)/\"".
166
165
  must_be_parsed_as s(:str, "/)/")
167
166
  end
@@ -247,8 +246,8 @@ describe RipperRubyParser::Parser do
247
246
 
248
247
  it "works for cvars" do
249
248
  "\"foo\#@@bar\"".must_be_parsed_as s(:dstr,
250
- "foo",
251
- s(:evstr, s(:cvar, :@@bar)))
249
+ "foo",
250
+ s(:evstr, s(:cvar, :@@bar)))
252
251
  end
253
252
  end
254
253
 
@@ -288,10 +287,10 @@ describe RipperRubyParser::Parser do
288
287
 
289
288
  it "works for strings with interpolations followed by escape sequences" do
290
289
  '"#{foo}\\n"'.
291
- must_be_parsed_as s(:dstr,
292
- "",
293
- s(:evstr, s(:call, nil, :foo)),
294
- s(:str, "\n"))
290
+ must_be_parsed_as s(:dstr,
291
+ "",
292
+ s(:evstr, s(:call, nil, :foo)),
293
+ s(:str, "\n"))
295
294
  end
296
295
 
297
296
  it "works with an empty interpolation" do
@@ -324,21 +323,23 @@ describe RipperRubyParser::Parser do
324
323
  s(:str, "baz"))
325
324
  end
326
325
 
327
- it "performs the concatenation when both strings have interpolations" do
328
- "\"foo\#{bar}\" \"baz\#{qux}\"".
329
- must_be_parsed_as s(:dstr,
330
- "foo",
331
- s(:evstr, s(:call, nil, :bar)),
332
- s(:str, "baz"),
333
- s(:evstr, s(:call, nil, :qux)))
334
- end
326
+ describe "when both strings have interpolations" do
327
+ it "performs the concatenation" do
328
+ "\"foo\#{bar}\" \"baz\#{qux}\"".
329
+ must_be_parsed_as s(:dstr,
330
+ "foo",
331
+ s(:evstr, s(:call, nil, :bar)),
332
+ s(:str, "baz"),
333
+ s(:evstr, s(:call, nil, :qux)))
334
+ end
335
335
 
336
- it "removes empty substrings from the concatenation when both strings have interpolations" do
337
- "\"foo\#{bar}\" \"\#{qux}\"".
338
- must_be_parsed_as s(:dstr,
339
- "foo",
340
- s(:evstr, s(:call, nil, :bar)),
341
- s(:evstr, s(:call, nil, :qux)))
336
+ it "removes empty substrings from the concatenation" do
337
+ "\"foo\#{bar}\" \"\#{qux}\"".
338
+ must_be_parsed_as s(:dstr,
339
+ "foo",
340
+ s(:evstr, s(:call, nil, :bar)),
341
+ s(:evstr, s(:call, nil, :qux)))
342
+ end
342
343
  end
343
344
  end
344
345
  end
@@ -514,6 +515,5 @@ describe RipperRubyParser::Parser do
514
515
  must_be_parsed_as s(:lit, 448)
515
516
  end
516
517
  end
517
-
518
518
  end
519
519
  end
@@ -103,8 +103,8 @@ describe RipperRubyParser::Parser do
103
103
  s(:call,
104
104
  s(:call, nil, :foo),
105
105
  :bar),
106
- s(:args),
107
- s(:call, nil, :baz))
106
+ 0,
107
+ s(:call, nil, :baz))
108
108
  end
109
109
 
110
110
  it "works for a do block with several statements" do
@@ -113,33 +113,38 @@ describe RipperRubyParser::Parser do
113
113
  s(:call,
114
114
  s(:call, nil, :foo),
115
115
  :bar),
116
- s(:args),
117
- s(:block,
118
- s(:call, nil, :baz),
119
- s(:call, nil, :qux)))
116
+ 0,
117
+ s(:block,
118
+ s(:call, nil, :baz),
119
+ s(:call, nil, :qux)))
120
120
  end
121
121
  end
122
122
  end
123
123
 
124
124
  describe "for calls to super" do
125
125
  specify { "super".must_be_parsed_as s(:zsuper) }
126
- specify { "super foo".must_be_parsed_as s(:super,
127
- s(:call, nil, :foo)) }
128
- specify {
126
+ specify do
127
+ "super foo".must_be_parsed_as s(:super,
128
+ s(:call, nil, :foo))
129
+ end
130
+ specify do
129
131
  "super foo, bar".must_be_parsed_as s(:super,
130
132
  s(:call, nil, :foo),
131
- s(:call, nil, :bar)) }
132
- specify {
133
+ s(:call, nil, :bar))
134
+ end
135
+ specify do
133
136
  "super foo, *bar".must_be_parsed_as s(:super,
134
137
  s(:call, nil, :foo),
135
138
  s(:splat,
136
- s(:call, nil, :bar))) }
137
- specify {
139
+ s(:call, nil, :bar)))
140
+ end
141
+ specify do
138
142
  "super foo, *bar, &baz".
139
143
  must_be_parsed_as s(:super,
140
144
  s(:call, nil, :foo),
141
145
  s(:splat, s(:call, nil, :bar)),
142
- s(:block_pass, s(:call, nil, :baz))) }
146
+ s(:block_pass, s(:call, nil, :baz)))
147
+ end
143
148
  end
144
149
 
145
150
  it "handles calling a proc" do
@@ -148,4 +153,3 @@ describe RipperRubyParser::Parser do
148
153
  end
149
154
  end
150
155
  end
151
-