ripper_ruby_parser 1.7.2 → 1.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +50 -0
- data/README.md +4 -4
- data/lib/ripper_ruby_parser/commenting_ripper_parser.rb +24 -12
- data/lib/ripper_ruby_parser/sexp_handlers/assignment.rb +2 -2
- data/lib/ripper_ruby_parser/sexp_handlers/blocks.rb +47 -53
- data/lib/ripper_ruby_parser/sexp_handlers/conditionals.rb +17 -19
- data/lib/ripper_ruby_parser/sexp_handlers/helper_methods.rb +34 -1
- data/lib/ripper_ruby_parser/sexp_handlers/literals.rb +1 -1
- data/lib/ripper_ruby_parser/sexp_handlers/method_calls.rb +9 -5
- data/lib/ripper_ruby_parser/sexp_handlers/methods.rb +17 -15
- data/lib/ripper_ruby_parser/sexp_handlers/operators.rb +3 -3
- data/lib/ripper_ruby_parser/sexp_handlers/string_literals.rb +24 -28
- data/lib/ripper_ruby_parser/sexp_processor.rb +5 -18
- data/lib/ripper_ruby_parser/unescape.rb +63 -22
- data/lib/ripper_ruby_parser/version.rb +1 -1
- metadata +140 -79
- data/Rakefile +0 -33
- data/test/end_to_end/comments_test.rb +0 -59
- data/test/end_to_end/comparison_test.rb +0 -104
- data/test/end_to_end/lib_comparison_test.rb +0 -18
- data/test/end_to_end/line_numbering_test.rb +0 -31
- data/test/end_to_end/samples_comparison_test.rb +0 -13
- data/test/end_to_end/test_comparison_test.rb +0 -18
- data/test/pt_testcase/pt_test.rb +0 -44
- data/test/ripper_ruby_parser/commenting_ripper_parser_test.rb +0 -200
- data/test/ripper_ruby_parser/parser_test.rb +0 -576
- data/test/ripper_ruby_parser/sexp_handlers/assignment_test.rb +0 -597
- data/test/ripper_ruby_parser/sexp_handlers/blocks_test.rb +0 -717
- data/test/ripper_ruby_parser/sexp_handlers/conditionals_test.rb +0 -536
- data/test/ripper_ruby_parser/sexp_handlers/literals_test.rb +0 -165
- data/test/ripper_ruby_parser/sexp_handlers/loops_test.rb +0 -209
- data/test/ripper_ruby_parser/sexp_handlers/method_calls_test.rb +0 -237
- data/test/ripper_ruby_parser/sexp_handlers/methods_test.rb +0 -429
- data/test/ripper_ruby_parser/sexp_handlers/operators_test.rb +0 -405
- data/test/ripper_ruby_parser/sexp_handlers/string_literals_test.rb +0 -973
- data/test/ripper_ruby_parser/sexp_processor_test.rb +0 -327
- data/test/ripper_ruby_parser/version_test.rb +0 -7
- data/test/samples/assignment.rb +0 -22
- data/test/samples/comments.rb +0 -13
- data/test/samples/conditionals.rb +0 -23
- data/test/samples/lambdas.rb +0 -5
- data/test/samples/loops.rb +0 -36
- data/test/samples/misc.rb +0 -285
- data/test/samples/number.rb +0 -9
- data/test/samples/operators.rb +0 -18
- data/test/samples/strings.rb +0 -147
- data/test/test_helper.rb +0 -111
@@ -1,327 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require File.expand_path("../test_helper.rb", File.dirname(__FILE__))
|
4
|
-
|
5
|
-
class TestProcessor < RipperRubyParser::SexpProcessor
|
6
|
-
def process_foo(exp)
|
7
|
-
exp.shift
|
8
|
-
s(:foo_p)
|
9
|
-
end
|
10
|
-
|
11
|
-
def process_bar(exp)
|
12
|
-
exp.shift
|
13
|
-
s(:bar_p)
|
14
|
-
end
|
15
|
-
|
16
|
-
def process_baz(exp)
|
17
|
-
exp.shift
|
18
|
-
s(:baz_p)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe RipperRubyParser::SexpProcessor do
|
23
|
-
let :processor do
|
24
|
-
TestProcessor.new
|
25
|
-
end
|
26
|
-
|
27
|
-
describe "#process" do
|
28
|
-
describe "for a :program sexp" do
|
29
|
-
it "strips off the outer :program node" do
|
30
|
-
sexp = s(:program, s(:stmts, s(:foo)))
|
31
|
-
result = processor.process sexp
|
32
|
-
_(result).must_equal s(:foo_p)
|
33
|
-
end
|
34
|
-
|
35
|
-
it "transforms a multi-statement :program into a :block sexp" do
|
36
|
-
sexp = s(:program, s(:stmts, s(:foo), s(:bar)))
|
37
|
-
result = processor.process sexp
|
38
|
-
_(result).must_equal s(:block, s(:foo_p), s(:bar_p))
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "for a :string_literal sexp" do
|
43
|
-
it "transforms a simple sexp to :str" do
|
44
|
-
sexp = s(:string_literal,
|
45
|
-
s(:string_content,
|
46
|
-
s(:@tstring_content, "foo", s(1, 1), '"')))
|
47
|
-
result = processor.process sexp
|
48
|
-
_(result).must_equal s(:str, "foo")
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "for a :command sexp" do
|
53
|
-
it "transforms a sexp to a :call" do
|
54
|
-
sexp = s(:command, s(:@ident, "foo", s(1, 0)), s(:arglist, s(:foo)))
|
55
|
-
result = processor.process sexp
|
56
|
-
_(result).must_equal s(:call, nil, :foo, s(:foo_p))
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "for a :var_ref sexp" do
|
61
|
-
it "transforms the sexp to a :lvar sexp" do
|
62
|
-
sexp = s(:var_ref, s(:@ident, "bar", s(1, 4)))
|
63
|
-
result = processor.process sexp
|
64
|
-
_(result).must_equal s(:lvar, :bar)
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
describe "for a :vcall sexp" do
|
69
|
-
it "transforms the sexp to a :call sexp" do
|
70
|
-
sexp = s(:vcall, s(:@ident, "bar", s(1, 4)))
|
71
|
-
result = processor.process sexp
|
72
|
-
_(result).must_equal s(:call, nil, :bar)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe "for a :module sexp" do
|
77
|
-
it "does not create body elements for an empty definition" do
|
78
|
-
sexp = s(:module,
|
79
|
-
s(:const_ref, s(:@const, "Foo", s(1, 13))),
|
80
|
-
s(:bodystmt, s(:stmts, s(:void_stmt, s(2, 3))), nil, nil, nil))
|
81
|
-
result = processor.process sexp
|
82
|
-
_(result).must_equal s(:module, :Foo)
|
83
|
-
end
|
84
|
-
|
85
|
-
it "creates a single body element for a definition with one statement" do
|
86
|
-
sexp = s(:module,
|
87
|
-
s(:const_ref, s(:@const, "Foo", s(1, 13))),
|
88
|
-
s(:bodystmt, s(:stmts, s(:foo)), nil, nil, nil))
|
89
|
-
result = processor.process sexp
|
90
|
-
_(result).must_equal s(:module, :Foo, s(:foo_p))
|
91
|
-
end
|
92
|
-
|
93
|
-
it "creates multiple body elements for a definition with more than one statement" do
|
94
|
-
sexp = s(:module,
|
95
|
-
s(:const_ref, s(:@const, "Foo", s(1, 13))),
|
96
|
-
s(:bodystmt, s(:stmts, s(:foo), s(:bar)), nil, nil, nil))
|
97
|
-
result = processor.process sexp
|
98
|
-
_(result).must_equal s(:module, :Foo, s(:foo_p), s(:bar_p))
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
102
|
-
describe "for a :class sexp" do
|
103
|
-
it "does not create body eleents for an empty definition" do
|
104
|
-
sexp = s(:class,
|
105
|
-
s(:const_ref, s(:@const, "Foo", s(1, 13))), nil,
|
106
|
-
s(:bodystmt, s(:stmts, s(:void_stmt, s(2, 8))), nil, nil, nil))
|
107
|
-
result = processor.process sexp
|
108
|
-
_(result).must_equal s(:class, :Foo, nil)
|
109
|
-
end
|
110
|
-
|
111
|
-
it "creates a single body element for a definition with one statement" do
|
112
|
-
sexp = s(:class,
|
113
|
-
s(:const_ref, s(:@const, "Foo", s(1, 13))), nil,
|
114
|
-
s(:bodystmt, s(:stmts, s(:foo)), nil, nil, nil))
|
115
|
-
result = processor.process sexp
|
116
|
-
_(result).must_equal s(:class, :Foo, nil, s(:foo_p))
|
117
|
-
end
|
118
|
-
|
119
|
-
it "creates multiple body elements for a definition with more than one statement" do
|
120
|
-
sexp = s(:class,
|
121
|
-
s(:const_ref, s(:@const, "Foo", s(1, 13))), nil,
|
122
|
-
s(:bodystmt, s(:stmts, s(:foo), s(:bar)), nil, nil, nil))
|
123
|
-
result = processor.process sexp
|
124
|
-
_(result).must_equal s(:class, :Foo, nil, s(:foo_p), s(:bar_p))
|
125
|
-
end
|
126
|
-
|
127
|
-
it "passes on the given ancestor" do
|
128
|
-
sexp = s(:class,
|
129
|
-
s(:const_ref, s(:@const, "Foo", s(1, 13))),
|
130
|
-
s(:var_ref, s(:@const, "Bar", s(1, 12))),
|
131
|
-
s(:bodystmt, s(:stmts, s(:void_stmt, s(2, 7))), nil, nil, nil))
|
132
|
-
result = processor.process sexp
|
133
|
-
_(result).must_equal s(:class, :Foo, s(:const, :Bar))
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
describe "for a :bodystmt sexp" do
|
138
|
-
it "creates a :block sexp when multiple statements are present" do
|
139
|
-
sexp = s(:bodystmt, s(:stmts, s(:foo), s(:bar)), nil, nil, nil)
|
140
|
-
result = processor.process sexp
|
141
|
-
_(result).must_equal s(:block, s(:foo_p), s(:bar_p))
|
142
|
-
end
|
143
|
-
|
144
|
-
it "removes nested :void_stmt sexps" do
|
145
|
-
sexp = s(:bodystmt, s(:stmts, s(:void_stmt), s(:foo)), nil, nil, nil)
|
146
|
-
result = processor.process sexp
|
147
|
-
_(result).must_equal s(:foo_p)
|
148
|
-
end
|
149
|
-
end
|
150
|
-
|
151
|
-
describe "for a :def sexp" do
|
152
|
-
it "transforms the sexp for a basic function definition" do
|
153
|
-
sexp = s(:def,
|
154
|
-
s(:@ident, "foo", s(1, 4)),
|
155
|
-
s(:params, nil, nil, nil, nil, nil),
|
156
|
-
s(:bodystmt, s(:stmts, s(:void_stmt, s(2, 3))), nil, nil, nil))
|
157
|
-
result = processor.process sexp
|
158
|
-
_(result).must_equal s(:defn, :foo, s(:args), s(:nil))
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
describe "for a :params sexp" do
|
163
|
-
describe "with a normal arguments" do
|
164
|
-
it "creates :lvar sexps" do
|
165
|
-
sexp = s(:params, s(s(:@ident, "bar", s(1, 8))), nil, nil, nil, nil)
|
166
|
-
result = processor.process sexp
|
167
|
-
_(result).must_equal s(:args, s(:lvar, :bar))
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
describe "with a ruby 2.4-style doublesplat argument" do
|
172
|
-
it "creates :lvar sexps" do
|
173
|
-
sexp = s(:params,
|
174
|
-
nil, nil, nil, nil, nil,
|
175
|
-
s(:@ident, "bar", s(1, 8)),
|
176
|
-
nil)
|
177
|
-
result = processor.process sexp
|
178
|
-
_(result).must_equal s(:args, s(:dsplat, s(:lvar, :bar)))
|
179
|
-
end
|
180
|
-
end
|
181
|
-
describe "with a ruby 2.5-style kwrest argument" do
|
182
|
-
it "creates :lvar sexps" do
|
183
|
-
sexp = s(:params,
|
184
|
-
nil, nil, nil, nil, nil,
|
185
|
-
s(:kwrest_param, s(:@ident, "bar", s(1, 8))),
|
186
|
-
nil)
|
187
|
-
result = processor.process sexp
|
188
|
-
_(result).must_equal s(:args, s(:dsplat, s(:lvar, :bar)))
|
189
|
-
end
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
describe "for an :assign sexp" do
|
194
|
-
it "creates a :lasgn sexp" do
|
195
|
-
sexp = s(:assign,
|
196
|
-
s(:var_field, s(:@ident, "a", s(1, 0))),
|
197
|
-
s(:@int, "1", s(1, 4)))
|
198
|
-
result = processor.process sexp
|
199
|
-
_(result).must_equal s(:lasgn, :a, s(:lit, 1))
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
describe "for a :binary sexp" do
|
204
|
-
it "creates a :call sexp" do
|
205
|
-
sexp = s(:binary, s(:bar), :==, s(:foo))
|
206
|
-
result = processor.process sexp
|
207
|
-
_(result).must_equal s(:call, s(:bar_p), :==, s(:foo_p))
|
208
|
-
end
|
209
|
-
end
|
210
|
-
|
211
|
-
describe "for a :method_add_block sexp" do
|
212
|
-
it "creates an :iter sexp" do
|
213
|
-
sexp = s(:method_add_block,
|
214
|
-
s(:call, s(:foo), :".", s(:@ident, "baz", s(1, 2))),
|
215
|
-
s(:brace_block, nil, s(:stmts, s(:bar))))
|
216
|
-
result = processor.process sexp
|
217
|
-
_(result).must_equal s(:iter,
|
218
|
-
s(:call, s(:foo_p), :baz), 0,
|
219
|
-
s(:bar_p))
|
220
|
-
end
|
221
|
-
|
222
|
-
describe "with a block parameter" do
|
223
|
-
it "creates an :iter sexp with an :args sexp for the block parameter" do
|
224
|
-
sexp = s(:method_add_block,
|
225
|
-
s(:call, s(:foo), :".", s(:@ident, "baz", s(1, 2))),
|
226
|
-
s(:brace_block,
|
227
|
-
s(:block_var,
|
228
|
-
s(:params, s(s(:@ident, "i", s(1, 6))), nil, nil, nil, nil),
|
229
|
-
nil),
|
230
|
-
s(:stmts, s(:bar))))
|
231
|
-
result = processor.process sexp
|
232
|
-
_(result).must_equal s(:iter,
|
233
|
-
s(:call, s(:foo_p), :baz),
|
234
|
-
s(:args, :i),
|
235
|
-
s(:bar_p))
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
describe "for an :if sexp" do
|
241
|
-
describe "with a single statement in the if body" do
|
242
|
-
it "uses the statement sexp as the body" do
|
243
|
-
sexp = s(:if, s(:foo), s(:stmts, s(:bar)), nil)
|
244
|
-
result = processor.process sexp
|
245
|
-
_(result).must_equal s(:if, s(:foo_p), s(:bar_p), nil)
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
describe "with multiple statements in the if body" do
|
250
|
-
it "uses a block containing the statement sexps as the body" do
|
251
|
-
sexp = s(:if, s(:foo), s(:stmts, s(:bar), s(:baz)), nil)
|
252
|
-
result = processor.process sexp
|
253
|
-
_(result).must_equal s(:if, s(:foo_p), s(:block, s(:bar_p), s(:baz_p)), nil)
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
describe "for an :array sexp" do
|
259
|
-
it "pulls up the element sexps" do
|
260
|
-
sexp = s(:array, s(:words, s(:foo), s(:bar), s(:baz)))
|
261
|
-
result = processor.process sexp
|
262
|
-
_(result).must_equal s(:array, s(:foo_p), s(:bar_p), s(:baz_p))
|
263
|
-
end
|
264
|
-
end
|
265
|
-
|
266
|
-
describe "for a :const_path_ref sexp" do
|
267
|
-
it "returns a :colon2 sexp" do
|
268
|
-
sexp = s(:const_path_ref,
|
269
|
-
s(:var_ref, s(:@const, "Foo", s(1, 0))),
|
270
|
-
s(:@const, "Bar", s(1, 5)))
|
271
|
-
result = processor.process sexp
|
272
|
-
_(result).must_equal s(:colon2, s(:const, :Foo), :Bar)
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
describe "for a :when sexp" do
|
277
|
-
it "turns nested :when clauses into a list" do
|
278
|
-
sexp = s(:when, s(:args, s(:foo)), s(:stmts, s(:bar)),
|
279
|
-
s(:when, s(:args, s(:foo)), s(:stmts, s(:bar)),
|
280
|
-
s(:when, s(:args, s(:foo)), s(:stmts, s(:bar)), nil)))
|
281
|
-
result = processor.process sexp
|
282
|
-
_(result).must_equal s(s(:when, s(:array, s(:foo_p)), s(:bar_p)),
|
283
|
-
s(:when, s(:array, s(:foo_p)), s(:bar_p)),
|
284
|
-
s(:when, s(:array, s(:foo_p)), s(:bar_p)),
|
285
|
-
nil)
|
286
|
-
end
|
287
|
-
end
|
288
|
-
|
289
|
-
it "processes a Ruby 2.5 style period Sexp" do
|
290
|
-
sexp = s(:call,
|
291
|
-
s(:vcall, s(:@ident, "foo", s(1, 0))),
|
292
|
-
:'.',
|
293
|
-
s(:@ident, "bar", s(1, 4)))
|
294
|
-
_(processor.process(sexp)).must_equal s(:call, s(:call, nil, :foo), :bar)
|
295
|
-
end
|
296
|
-
|
297
|
-
it "processes a Ruby 2.6 style period Sexp" do
|
298
|
-
sexp = s(:call,
|
299
|
-
s(:vcall, s(:@ident, "foo", s(1, 0))),
|
300
|
-
s(:@period, ".", s(1, 3)),
|
301
|
-
s(:@ident, "bar", s(1, 4)))
|
302
|
-
_(processor.process(sexp)).must_equal s(:call, s(:call, nil, :foo), :bar)
|
303
|
-
end
|
304
|
-
|
305
|
-
it "raises an error for an unknown call operator" do
|
306
|
-
sexp = s(:call,
|
307
|
-
s(:vcall, s(:@ident, "foo", s(1, 0))),
|
308
|
-
:'>.',
|
309
|
-
s(:@ident, "bar", s(1, 4)))
|
310
|
-
_(-> { processor.process(sexp) }).must_raise KeyError
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
describe "#extract_node_symbol" do
|
315
|
-
it "processes an lvar sexp to a bare symbol" do
|
316
|
-
sexp = s(:lvar, "foo")
|
317
|
-
result = processor.send :extract_node_symbol, sexp
|
318
|
-
_(result).must_equal :foo
|
319
|
-
end
|
320
|
-
|
321
|
-
it "processes a const sexp to a bare symbol" do
|
322
|
-
sexp = s(:const, "Foo")
|
323
|
-
result = processor.send :extract_node_symbol, sexp
|
324
|
-
_(result).must_equal :Foo
|
325
|
-
end
|
326
|
-
end
|
327
|
-
end
|
data/test/samples/assignment.rb
DELETED
@@ -1,22 +0,0 @@
|
|
1
|
-
# Samples of assignment
|
2
|
-
foo[bar] = baz
|
3
|
-
foo[bar] += baz
|
4
|
-
foo[bar] ||= foo bar
|
5
|
-
foo[bar] ||= foo(bar)
|
6
|
-
foo[bar] ||= baz.qux quuz
|
7
|
-
foo[bar] ||= baz.qux(quuz)
|
8
|
-
|
9
|
-
# Destructuring assignments
|
10
|
-
foo, bar = baz
|
11
|
-
(foo, bar) = baz
|
12
|
-
((foo, bar)) = baz
|
13
|
-
(((foo, bar))) = baz
|
14
|
-
foo, (bar, baz) = qux
|
15
|
-
foo, ((bar, baz)) = qux
|
16
|
-
foo, (((bar, baz))) = qux
|
17
|
-
foo, (bar, (baz, qux)) = quuz
|
18
|
-
|
19
|
-
# Safe attribute assignment
|
20
|
-
foo&.bar = baz
|
21
|
-
foo[bar]&.baz = qux
|
22
|
-
foo[bar, baz]&.qux = quuz
|
data/test/samples/comments.rb
DELETED
data/test/samples/lambdas.rb
DELETED
data/test/samples/loops.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
# Loop samples
|
2
|
-
|
3
|
-
def for_samples
|
4
|
-
for foo in bar
|
5
|
-
end
|
6
|
-
|
7
|
-
for foo, bar in baz
|
8
|
-
end
|
9
|
-
|
10
|
-
for foo, in bar
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
# begin..end in conditions
|
15
|
-
#
|
16
|
-
def while_until_samples
|
17
|
-
while begin foo end
|
18
|
-
bar
|
19
|
-
end
|
20
|
-
|
21
|
-
until begin foo end
|
22
|
-
bar
|
23
|
-
end
|
24
|
-
|
25
|
-
while begin foo end do
|
26
|
-
bar
|
27
|
-
end
|
28
|
-
|
29
|
-
until begin foo end do
|
30
|
-
bar
|
31
|
-
end
|
32
|
-
|
33
|
-
foo while begin bar end
|
34
|
-
|
35
|
-
foo until begin bar end
|
36
|
-
end
|