ripper_ruby_parser 1.7.2 → 1.8.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 (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
data/Rakefile DELETED
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "rake/clean"
4
- require "bundler/gem_tasks"
5
- require "rake/testtask"
6
-
7
- namespace :test do
8
- Rake::TestTask.new(:unit) do |t|
9
- t.libs = ["lib"]
10
- t.test_files = FileList["test/ripper_ruby_parser/**/*_test.rb"]
11
- t.warning = true
12
- end
13
-
14
- Rake::TestTask.new(:end_to_end) do |t|
15
- t.libs = ["lib"]
16
- t.test_files = FileList["test/end_to_end/*_test.rb"]
17
- t.warning = true
18
- end
19
-
20
- Rake::TestTask.new(:pt_testcase) do |t|
21
- t.libs = ["lib"]
22
- t.test_files = FileList["test/pt_testcase/*_test.rb"]
23
- t.warning = true
24
- end
25
-
26
- desc "Run all three test suites"
27
- task run: [:unit, :end_to_end, :pt_testcase]
28
- end
29
-
30
- desc "Alias to test:run"
31
- task test: "test:run"
32
-
33
- task default: :test
@@ -1,59 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
4
- require "ruby_parser"
5
-
6
- describe "Using RipperRubyParser and RubyParser" do
7
- let :newparser do
8
- RipperRubyParser::Parser.new
9
- end
10
-
11
- let :oldparser do
12
- RubyParser.for_current_ruby
13
- end
14
-
15
- describe "for a program with quite some comments" do
16
- let :program do
17
- <<-RUBY
18
- # Foo
19
- class Foo
20
- # The foo
21
- # method
22
- def foo
23
- bar # bar
24
- # internal comment
25
- end
26
-
27
- def bar
28
- baz
29
- end
30
- end
31
- # Quux
32
- module Qux
33
- class Quux
34
- def bar
35
- end
36
- def baz
37
- end
38
- end
39
- end
40
- RUBY
41
- end
42
-
43
- let :original do
44
- oldparser.parse program
45
- end
46
-
47
- let :imitation do
48
- newparser.parse program
49
- end
50
-
51
- it "gives the same result" do
52
- _(imitation).must_equal original
53
- end
54
-
55
- it "gives the same result with comments" do
56
- _(to_comments(imitation)).must_equal to_comments(original)
57
- end
58
- end
59
- end
@@ -1,104 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
4
- require "ruby_parser"
5
-
6
- describe "Using RipperRubyParser and RubyParser" do
7
- describe "for a simple well known program" do
8
- let :program do
9
- "puts 'Hello World'"
10
- end
11
-
12
- it "gives the same result with line numbers" do
13
- _(program).must_be_parsed_as_before with_line_numbers: true
14
- end
15
- end
16
-
17
- describe "for a more complex program" do
18
- let :program do
19
- <<-RUBY
20
- module Quux
21
- class Foo
22
- def bar
23
- baz = 3
24
- qux baz
25
- end
26
- def qux it
27
- if it == 3
28
- [1,2,3].map {|i| 2*i}
29
- end
30
- end
31
- end
32
- end
33
-
34
- Quux::Foo.new.bar
35
- RUBY
36
- end
37
-
38
- it "gives the same result with line numbers" do
39
- _(program).must_be_parsed_as_before with_line_numbers: true
40
- end
41
- end
42
-
43
- describe "for an example with yield from Reek" do
44
- let :program do
45
- "def fred() yield(3) if block_given?; end"
46
- end
47
-
48
- it "gives the same result with line numbers" do
49
- _(program).must_be_parsed_as_before with_line_numbers: true
50
- end
51
- end
52
-
53
- describe "for an example with floats from Reek" do
54
- let :program do
55
- <<-RUBY
56
- def total_envy
57
- fred = @item
58
- total = 0
59
- total += fred.price
60
- total += fred.tax
61
- total *= 1.15
62
- end
63
- RUBY
64
- end
65
-
66
- it "gives the same result with line numbers" do
67
- _(program).must_be_parsed_as_before with_line_numbers: true
68
- end
69
- end
70
-
71
- describe "for an example with operators and explicit block parameter from Reek" do
72
- let :program do
73
- <<-RUBY
74
- def parse(arg, argv, &error)
75
- if !(val = arg) and (argv.empty? or /\\A-/ =~ (val = argv[0]))
76
- return nil, block, nil
77
- end
78
- opt = (val = parse_arg(val, &error))[1]
79
- val = conv_arg(*val)
80
- if opt and !arg
81
- argv.shift
82
- else
83
- val[0] = nil
84
- end
85
- val
86
- end
87
- RUBY
88
- end
89
-
90
- it "gives the same result with line numbers" do
91
- _(program).must_be_parsed_as_before with_line_numbers: true
92
- end
93
- end
94
-
95
- describe "for an example of a complex regular expression from Reek" do
96
- let :program do
97
- "/(\#{@types})\\s*(\\w+)\\s*\\(([^)]*)\\)/"
98
- end
99
-
100
- it "gives the same result with line numbers" do
101
- _(program).must_be_parsed_as_before with_line_numbers: true
102
- end
103
- end
104
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
4
- require "ruby_parser"
5
-
6
- describe "Using RipperRubyParser and RubyParser" do
7
- Dir.glob("lib/**/*.rb").each do |file|
8
- describe "for #{file}" do
9
- let :program do
10
- File.read file
11
- end
12
-
13
- it "gives the same result" do
14
- _(program).must_be_parsed_as_before
15
- end
16
- end
17
- end
18
- end
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
4
- require "ruby_parser"
5
-
6
- describe "Using RipperRubyParser and RubyParser" do
7
- describe "for a multi-line program" do
8
- let :program do
9
- <<-RUBY
10
- class Foo
11
- def foo()
12
- bar()
13
- baz(qux)
14
- end
15
- end
16
-
17
- module Bar
18
- @@baz = {}
19
- end
20
- RUBY
21
- end
22
-
23
- it "gives the same result" do
24
- _(program).must_be_parsed_as_before
25
- end
26
-
27
- it "gives the same result with line numbers" do
28
- _(program).must_be_parsed_as_before with_line_numbers: true
29
- end
30
- end
31
- end
@@ -1,13 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
4
- require "ruby_parser"
5
-
6
- describe "Using RipperRubyParser and RubyParser" do
7
- Dir.glob(File.expand_path("../samples/*.rb", File.dirname(__FILE__))).each do |file|
8
- it "gives the same result for #{file}" do
9
- program = File.read file
10
- _(program).must_be_parsed_as_before
11
- end
12
- end
13
- end
@@ -1,18 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
4
- require "ruby_parser"
5
-
6
- describe "Using RipperRubyParser and RubyParser" do
7
- Dir.glob("test/ripper_ruby_parser/**/*.rb").each do |file|
8
- describe "for #{file}" do
9
- let :program do
10
- File.read file
11
- end
12
-
13
- it "gives the same result" do
14
- _(program).must_be_parsed_as_before
15
- end
16
- end
17
- end
18
- end
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
4
- require "pt_testcase"
5
-
6
- class TestParser < RipperRubyParser::Parser
7
- def process(input)
8
- parse input
9
- end
10
- end
11
-
12
- SKIPPED_TESTS = ["dstr_heredoc_windoze_sucks"].freeze
13
-
14
- class RubyParserTestCase < ParseTreeTestCase
15
- def self.previous(_key)
16
- "Ruby"
17
- end
18
-
19
- def self.generate_test(klass, node, data, input_name, _output_name)
20
- if data["Ruby"].is_a? Array
21
- klass.send :define_method, "test_#{node}" do
22
- skip "Not a parser test"
23
- end
24
- return
25
- end
26
-
27
- if SKIPPED_TESTS.include? node
28
- klass.send :define_method, "test_#{node}" do
29
- skip "Can't or won't fix this difference"
30
- end
31
- return
32
- end
33
-
34
- super klass, node, data, input_name, "ParseTree"
35
- end
36
- end
37
-
38
- class TestRuby19Parser < RubyParserTestCase
39
- def setup
40
- super
41
-
42
- self.processor = TestParser.new
43
- end
44
- end
@@ -1,200 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
4
-
5
- describe RipperRubyParser::CommentingRipperParser do
6
- def parse_with_builder(str)
7
- builder = RipperRubyParser::CommentingRipperParser.new str
8
- builder.parse
9
- end
10
-
11
- def empty_params_list
12
- @empty_params_list ||= s(:params, *([nil] * 7))
13
- end
14
-
15
- describe "handling comments" do
16
- # Handle different results for dynamic symbol strings. This was changed in
17
- # Ruby 2.6.3 and up. See https://bugs.ruby-lang.org/issues/15670
18
- let(:dsym_string_type) { RUBY_VERSION < "2.6.3" ? :xstring : :string_content }
19
-
20
- it "produces a comment node surrounding a commented def" do
21
- result = parse_with_builder "# Foo\ndef foo; end"
22
- _(result).must_equal s(:program,
23
- s(:stmts,
24
- s(:comment,
25
- "# Foo\n",
26
- s(:def,
27
- s(:@ident, "foo", s(2, 4)),
28
- empty_params_list,
29
- s(:bodystmt,
30
- s(:stmts, s(:void_stmt, s(2, 12))), nil, nil, nil),
31
- s(2, 0)))))
32
- end
33
-
34
- it "produces a blank comment node surrounding a def that has no comment" do
35
- result = parse_with_builder "def foo; end"
36
- _(result).must_equal s(:program,
37
- s(:stmts,
38
- s(:comment,
39
- "",
40
- s(:def,
41
- s(:@ident, "foo", s(1, 4)),
42
- empty_params_list,
43
- s(:bodystmt,
44
- s(:stmts, s(:void_stmt, s(1, 12))), nil, nil, nil),
45
- s(1, 0)))))
46
- end
47
-
48
- it "produces a comment node surrounding a commented class" do
49
- result = parse_with_builder "# Foo\nclass Foo; end"
50
- _(result).must_equal s(:program,
51
- s(:stmts,
52
- s(:comment,
53
- "# Foo\n",
54
- s(:class,
55
- s(:const_ref, s(:@const, "Foo", s(2, 6))),
56
- nil,
57
- s(:bodystmt,
58
- s(:stmts, s(:void_stmt, s(2, 10))), nil, nil, nil),
59
- s(2, 0)))))
60
- end
61
-
62
- it "produce a blank comment node surrounding a class that has no comment" do
63
- result = parse_with_builder "class Foo; end"
64
- _(result).must_equal s(:program,
65
- s(:stmts,
66
- s(:comment,
67
- "",
68
- s(:class,
69
- s(:const_ref, s(:@const, "Foo", s(1, 6))),
70
- nil,
71
- s(:bodystmt,
72
- s(:stmts, s(:void_stmt, s(1, 10))), nil, nil, nil),
73
- s(1, 0)))))
74
- end
75
-
76
- it "produces a comment node surrounding a commented module" do
77
- result = parse_with_builder "# Foo\nmodule Foo; end"
78
- _(result).must_equal s(:program,
79
- s(:stmts,
80
- s(:comment,
81
- "# Foo\n",
82
- s(:module,
83
- s(:const_ref, s(:@const, "Foo", s(2, 7))),
84
- s(:bodystmt,
85
- s(:stmts, s(:void_stmt, s(2, 11))), nil, nil, nil),
86
- s(2, 0)))))
87
- end
88
-
89
- it "produces a blank comment node surrounding a module that has no comment" do
90
- result = parse_with_builder "module Foo; end"
91
- _(result).must_equal s(:program,
92
- s(:stmts,
93
- s(:comment,
94
- "",
95
- s(:module,
96
- s(:const_ref, s(:@const, "Foo", s(1, 7))),
97
- s(:bodystmt,
98
- s(:stmts, s(:void_stmt, s(1, 11))), nil, nil, nil),
99
- s(1, 0)))))
100
- end
101
-
102
- it "is not confused by a symbol containing a keyword" do
103
- result = parse_with_builder ":class; def foo; end"
104
- _(result).must_equal s(:program,
105
- s(:stmts,
106
- s(:symbol_literal, s(:symbol, s(:@kw, "class", s(1, 1)))),
107
- s(:comment,
108
- "",
109
- s(:def,
110
- s(:@ident, "foo", s(1, 12)),
111
- empty_params_list,
112
- s(:bodystmt,
113
- s(:stmts, s(:void_stmt, s(1, 20))), nil, nil, nil),
114
- s(1, 8)))))
115
- end
116
-
117
- it "is not confused by a dynamic symbol" do
118
- result = parse_with_builder ":'foo'; def bar; end"
119
- _(result).must_equal s(:program,
120
- s(:stmts,
121
- s(:dyna_symbol,
122
- s(dsym_string_type,
123
- s(:@tstring_content, "foo", s(1, 2), ":'"))),
124
- s(:comment,
125
- "",
126
- s(:def,
127
- s(:@ident, "bar", s(1, 12)),
128
- empty_params_list,
129
- s(:bodystmt,
130
- s(:stmts, s(:void_stmt, s(1, 20))), nil, nil, nil),
131
- s(1, 8)))))
132
- end
133
-
134
- it "is not confused by a dynamic symbol containing a class definition" do
135
- result = parse_with_builder ":\"foo\#{class Bar;end}\""
136
- _(result).must_equal s(:program,
137
- s(:stmts,
138
- s(:dyna_symbol,
139
- s(dsym_string_type,
140
- s(:@tstring_content, "foo", s(1, 2), ':"'),
141
- s(:string_embexpr,
142
- s(:stmts,
143
- s(:comment,
144
- "",
145
- s(:class,
146
- s(:const_ref, s(:@const, "Bar", s(1, 13))),
147
- nil,
148
- s(:bodystmt,
149
- s(:stmts, s(:void_stmt, s(1, 17))),
150
- nil, nil, nil),
151
- s(1, 7)))))))))
152
- end
153
-
154
- it "turns an embedded document into a comment node" do
155
- result = parse_with_builder "=begin Hello\nthere\n=end\nclass Foo; end"
156
- _(result).must_equal s(:program,
157
- s(:stmts,
158
- s(:comment,
159
- "=begin Hello\nthere\n=end\n",
160
- s(:class,
161
- s(:const_ref, s(:@const, "Foo", s(4, 6))),
162
- nil,
163
- s(:bodystmt,
164
- s(:stmts, s(:void_stmt, s(4, 10))), nil, nil, nil),
165
- s(4, 0)))))
166
- end
167
- end
168
-
169
- describe "handling syntax errors" do
170
- it "raises an error for an incomplete source" do
171
- _(proc { parse_with_builder "def foo" }).must_raise RipperRubyParser::SyntaxError
172
- end
173
-
174
- it "raises an error for an invalid class name" do
175
- _(proc { parse_with_builder "class foo; end" })
176
- .must_raise RipperRubyParser::SyntaxError
177
- end
178
-
179
- it "raises an error aliasing $1 as foo" do
180
- _(proc { parse_with_builder "alias foo $1" }).must_raise RipperRubyParser::SyntaxError
181
- end
182
-
183
- it "raises an error aliasing foo as $1" do
184
- _(proc { parse_with_builder "alias $1 foo" }).must_raise RipperRubyParser::SyntaxError
185
- end
186
-
187
- it "raises an error aliasing $2 as $1" do
188
- _(proc { parse_with_builder "alias $1 $2" }).must_raise RipperRubyParser::SyntaxError
189
- end
190
-
191
- it "raises an error assigning to $1" do
192
- _(proc { parse_with_builder "$1 = foo" }).must_raise RipperRubyParser::SyntaxError
193
- end
194
-
195
- it "raises an error using an invalid parameter name" do
196
- _(proc { parse_with_builder "def foo(BAR); end" })
197
- .must_raise RipperRubyParser::SyntaxError
198
- end
199
- end
200
- end