synvert-core 1.4.0 → 1.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/main.yml +0 -1
- data/.gitignore +0 -5
- data/CHANGELOG.md +12 -0
- data/Gemfile +0 -3
- data/Gemfile.lock +101 -0
- data/Guardfile +0 -9
- data/README.md +31 -13
- data/Rakefile +1 -15
- data/lib/synvert/core/engine/erb.rb +1 -1
- data/lib/synvert/core/engine.rb +1 -1
- data/lib/synvert/core/node_ext.rb +0 -639
- data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +20 -17
- data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -5
- data/lib/synvert/core/rewriter/condition/if_only_exist_condition.rb +1 -1
- data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -5
- data/lib/synvert/core/rewriter/condition.rb +5 -1
- data/lib/synvert/core/rewriter/instance.rb +91 -140
- data/lib/synvert/core/rewriter/scope/query_scope.rb +8 -6
- data/lib/synvert/core/rewriter/scope/within_scope.rb +4 -87
- data/lib/synvert/core/rewriter.rb +0 -10
- data/lib/synvert/core/version.rb +1 -1
- data/lib/synvert/core.rb +4 -6
- data/spec/synvert/core/engine/erb_spec.rb +3 -3
- data/spec/synvert/core/node_ext_spec.rb +0 -965
- data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +21 -1
- data/spec/synvert/core/rewriter/instance_spec.rb +64 -131
- data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +1 -4
- data/spec/synvert/core/rewriter/scope/query_scope_spec.rb +1 -16
- data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +22 -13
- data/synvert-core-ruby.gemspec +5 -3
- metadata +46 -62
- data/lib/synvert/core/array_ext.rb +0 -48
- data/lib/synvert/core/node_query/compiler/array.rb +0 -34
- data/lib/synvert/core/node_query/compiler/attribute.rb +0 -39
- data/lib/synvert/core/node_query/compiler/attribute_list.rb +0 -24
- data/lib/synvert/core/node_query/compiler/basic_selector.rb +0 -28
- data/lib/synvert/core/node_query/compiler/boolean.rb +0 -23
- data/lib/synvert/core/node_query/compiler/comparable.rb +0 -86
- data/lib/synvert/core/node_query/compiler/dynamic_attribute.rb +0 -51
- data/lib/synvert/core/node_query/compiler/expression.rb +0 -41
- data/lib/synvert/core/node_query/compiler/float.rb +0 -23
- data/lib/synvert/core/node_query/compiler/identifier.rb +0 -41
- data/lib/synvert/core/node_query/compiler/integer.rb +0 -23
- data/lib/synvert/core/node_query/compiler/invalid_operator_error.rb +0 -7
- data/lib/synvert/core/node_query/compiler/nil.rb +0 -23
- data/lib/synvert/core/node_query/compiler/parse_error.rb +0 -7
- data/lib/synvert/core/node_query/compiler/regexp.rb +0 -37
- data/lib/synvert/core/node_query/compiler/selector.rb +0 -113
- data/lib/synvert/core/node_query/compiler/string.rb +0 -23
- data/lib/synvert/core/node_query/compiler/symbol.rb +0 -23
- data/lib/synvert/core/node_query/compiler.rb +0 -25
- data/lib/synvert/core/node_query/lexer.rex +0 -99
- data/lib/synvert/core/node_query/lexer.rex.rb +0 -299
- data/lib/synvert/core/node_query/parser.racc.rb +0 -306
- data/lib/synvert/core/node_query/parser.y +0 -60
- data/lib/synvert/core/node_query.rb +0 -36
- data/lib/synvert/core/rewriter/action/append_action.rb +0 -28
- data/lib/synvert/core/rewriter/action/delete_action.rb +0 -34
- data/lib/synvert/core/rewriter/action/insert_action.rb +0 -34
- data/lib/synvert/core/rewriter/action/insert_after_action.rb +0 -22
- data/lib/synvert/core/rewriter/action/prepend_action.rb +0 -44
- data/lib/synvert/core/rewriter/action/remove_action.rb +0 -56
- data/lib/synvert/core/rewriter/action/replace_action.rb +0 -33
- data/lib/synvert/core/rewriter/action/replace_with_action.rb +0 -36
- data/lib/synvert/core/rewriter/action/wrap_action.rb +0 -37
- data/lib/synvert/core/rewriter/action.rb +0 -102
- data/spec/synvert/core/node_query/lexer_spec.rb +0 -580
- data/spec/synvert/core/node_query/parser_spec.rb +0 -337
- data/spec/synvert/core/rewriter/action/append_action_spec.rb +0 -70
- data/spec/synvert/core/rewriter/action/delete_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/insert_action_spec.rb +0 -70
- data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/prepend_action_spec.rb +0 -175
- data/spec/synvert/core/rewriter/action/remove_action_spec.rb +0 -26
- data/spec/synvert/core/rewriter/action/replace_action_spec.rb +0 -28
- data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +0 -59
- data/spec/synvert/core/rewriter/action/wrap_action_spec.rb +0 -31
- data/spec/synvert/core/rewriter/action_spec.rb +0 -14
@@ -3,928 +3,6 @@
|
|
3
3
|
require 'spec_helper'
|
4
4
|
|
5
5
|
describe Parser::AST::Node do
|
6
|
-
describe '#parent' do
|
7
|
-
it 'gets parent node' do
|
8
|
-
node = parse('FactoryBot.create(:user)')
|
9
|
-
child_node = node.children.first
|
10
|
-
expect(child_node.parent).to eq node
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
describe '#siblings' do
|
15
|
-
it 'gets sibling nodes' do
|
16
|
-
node = parse(<<~EOS)
|
17
|
-
def foobar; end
|
18
|
-
def foo; end
|
19
|
-
def bar; end
|
20
|
-
EOS
|
21
|
-
expect(node.children.first.siblings).to eq [node.children.second, node.children.last]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
describe '#name' do
|
26
|
-
it 'gets for class node' do
|
27
|
-
node = parse('class Synvert; end')
|
28
|
-
expect(node.name).to eq parse('Synvert')
|
29
|
-
|
30
|
-
node = parse('class Synvert::Rewriter::Instance; end')
|
31
|
-
expect(node.name).to eq parse('Synvert::Rewriter::Instance')
|
32
|
-
end
|
33
|
-
|
34
|
-
it 'gets for module node' do
|
35
|
-
node = parse('module Synvert; end')
|
36
|
-
expect(node.name).to eq parse('Synvert')
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'gets for def node' do
|
40
|
-
node = parse('def current_node; end')
|
41
|
-
expect(node.name).to eq :current_node
|
42
|
-
end
|
43
|
-
|
44
|
-
it 'gets for defs node' do
|
45
|
-
node = parse('def self.current_node; end')
|
46
|
-
expect(node.name).to eq :current_node
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'gets for arg node' do
|
50
|
-
node = parse('def test(foo); end')
|
51
|
-
expect(node.arguments.first.name).to eq :foo
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'gets for blockarg node' do
|
55
|
-
node = parse('def test(&block); end')
|
56
|
-
expect(node.arguments.first.name).to eq :block
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'gets for const node' do
|
60
|
-
node = parse('Synvert')
|
61
|
-
expect(node.name).to eq :Synvert
|
62
|
-
end
|
63
|
-
|
64
|
-
it 'gets for lvar node' do
|
65
|
-
node = parse("foo = 'bar'\nfoo").children[1]
|
66
|
-
expect(node.name).to eq :foo
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'gets for ivar node' do
|
70
|
-
node = parse("@foo = 'bar'\n@foo").children[1]
|
71
|
-
expect(node.name).to eq :@foo
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'gets for cvar node' do
|
75
|
-
node = parse("@@foo = 'bar'\n@@foo").children[1]
|
76
|
-
expect(node.name).to eq :@@foo
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'gets for restarg node' do
|
80
|
-
node = parse('object.each { |*entry| }')
|
81
|
-
expect(node.arguments.first.name).to eq :entry
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe '#parent_class' do
|
86
|
-
it 'gets for class node' do
|
87
|
-
node = parse('class Post < ActiveRecord::Base; end')
|
88
|
-
expect(node.parent_class).to eq parse('ActiveRecord::Base')
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe '#receiver' do
|
93
|
-
it 'gets for send node' do
|
94
|
-
node = parse('FactoryGirl.create :post')
|
95
|
-
expect(node.receiver).to eq parse('FactoryGirl')
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'gets for csend node' do
|
99
|
-
node = parse('user&.update(name: name)')
|
100
|
-
expect(node.receiver).to eq parse('user')
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
describe '#message' do
|
105
|
-
it 'gets for send node' do
|
106
|
-
node = parse('FactoryGirl.create :post')
|
107
|
-
expect(node.message).to eq :create
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'gets for csend node' do
|
111
|
-
node = parse('user&.update(name: name)')
|
112
|
-
expect(node.message).to eq :update
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
describe '#parent_const' do
|
117
|
-
it 'gets for const node' do
|
118
|
-
node = parse('Synvert::Node')
|
119
|
-
expect(node.parent_const).to eq parse('Synvert')
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'gets for const node at the root' do
|
123
|
-
node = parse('::Node')
|
124
|
-
expect(node.parent_const.type).to eq :cbase
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'gets for const node with no parent' do
|
128
|
-
node = parse('Node')
|
129
|
-
expect(node.parent_const).to eq nil
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
describe '#arguments' do
|
134
|
-
it 'gets for def node' do
|
135
|
-
node = parse('def test(foo, bar); foo + bar; end')
|
136
|
-
expect(node.arguments.map(&:type)).to eq [:arg, :arg]
|
137
|
-
end
|
138
|
-
|
139
|
-
it 'gets for defs node' do
|
140
|
-
node = parse('def self.test(foo, bar); foo + bar; end')
|
141
|
-
expect(node.arguments.map(&:type)).to eq [:arg, :arg]
|
142
|
-
end
|
143
|
-
|
144
|
-
it 'gets for block node' do
|
145
|
-
node = parse('RSpec.configure do |config|; end')
|
146
|
-
expect(node.arguments.map(&:type)).to eq [:arg]
|
147
|
-
end
|
148
|
-
|
149
|
-
it 'gets for send node' do
|
150
|
-
node = parse("FactoryGirl.create :post, title: 'post'")
|
151
|
-
expect(node.arguments).to eq parse("[:post, title: 'post']").children
|
152
|
-
end
|
153
|
-
|
154
|
-
it 'gets for csend node' do
|
155
|
-
node = parse('user&.update(name: name)')
|
156
|
-
expect(node.arguments).to eq parse('[name: name]').children
|
157
|
-
end
|
158
|
-
|
159
|
-
it 'gets for defined? node' do
|
160
|
-
node = parse('defined?(Bundler)')
|
161
|
-
expect(node.arguments).to eq [parse('Bundler')]
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
describe '#caller' do
|
166
|
-
it 'gets for block node' do
|
167
|
-
node = parse('RSpec.configure do |config|; end')
|
168
|
-
expect(node.caller).to eq parse('RSpec.configure')
|
169
|
-
end
|
170
|
-
end
|
171
|
-
|
172
|
-
describe '#body' do
|
173
|
-
it 'gets one line for block node' do
|
174
|
-
node = parse('RSpec.configure do |config|; include EmailSpec::Helpers; end')
|
175
|
-
expect(node.body).to eq [parse('include EmailSpec::Helpers')]
|
176
|
-
end
|
177
|
-
|
178
|
-
it 'gets multiple lines for block node' do
|
179
|
-
node = parse('RSpec.configure do |config|; include EmailSpec::Helpers; include EmailSpec::Matchers; end')
|
180
|
-
expect(node.body).to eq [parse('include EmailSpec::Helpers'), parse('include EmailSpec::Matchers')]
|
181
|
-
end
|
182
|
-
|
183
|
-
it 'gets empty for class node' do
|
184
|
-
node = parse('class User; end')
|
185
|
-
expect(node.body).to be_empty
|
186
|
-
end
|
187
|
-
|
188
|
-
it 'gets empty for module node' do
|
189
|
-
node = parse('module Admin; end')
|
190
|
-
expect(node.body).to be_empty
|
191
|
-
end
|
192
|
-
|
193
|
-
it 'gets one line for class node' do
|
194
|
-
node = parse('class User; attr_accessor :email; end')
|
195
|
-
expect(node.body).to eq [parse('attr_accessor :email')]
|
196
|
-
end
|
197
|
-
|
198
|
-
it 'gets one line for class node' do
|
199
|
-
node = parse('class User; attr_accessor :email; attr_accessor :username; end')
|
200
|
-
expect(node.body).to eq [parse('attr_accessor :email'), parse('attr_accessor :username')]
|
201
|
-
end
|
202
|
-
|
203
|
-
it 'gets for begin node' do
|
204
|
-
node = parse('foo; bar')
|
205
|
-
expect(node.body).to eq [parse('foo'), parse('bar')]
|
206
|
-
end
|
207
|
-
|
208
|
-
it 'gets for def node' do
|
209
|
-
node = parse('def test; foo; bar; end')
|
210
|
-
expect(node.body).to eq [parse('foo'), parse('bar')]
|
211
|
-
end
|
212
|
-
|
213
|
-
it 'gets for def node with empty body' do
|
214
|
-
node = parse('def test; end')
|
215
|
-
expect(node.body).to eq []
|
216
|
-
end
|
217
|
-
|
218
|
-
it 'gets for defs node' do
|
219
|
-
node = parse('def self.test; foo; bar; end')
|
220
|
-
expect(node.body).to eq [parse('foo'), parse('bar')]
|
221
|
-
end
|
222
|
-
|
223
|
-
it 'gets for def node with empty body' do
|
224
|
-
node = parse('def self.test; end')
|
225
|
-
expect(node.body).to eq []
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
describe '#keys' do
|
230
|
-
it 'gets for hash node' do
|
231
|
-
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
232
|
-
expect(node.keys).to eq [parse(':foo'), parse("'foo'")]
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
describe '#values' do
|
237
|
-
it 'gets for hash node' do
|
238
|
-
node = parse("{:foo => :bar, 'foo' => 'bar'}")
|
239
|
-
expect(node.values).to eq [parse(':bar'), parse("'bar'")]
|
240
|
-
end
|
241
|
-
end
|
242
|
-
|
243
|
-
describe '#key?' do
|
244
|
-
it 'gets true if key exists' do
|
245
|
-
node = parse('{:foo => :bar}')
|
246
|
-
expect(node.key?(:foo)).to be_truthy
|
247
|
-
end
|
248
|
-
|
249
|
-
it 'gets false if key does not exist' do
|
250
|
-
node = parse('{:foo => :bar}')
|
251
|
-
expect(node.key?('foo')).to be_falsey
|
252
|
-
end
|
253
|
-
end
|
254
|
-
|
255
|
-
describe '#hash_value' do
|
256
|
-
it 'gets value of specified key' do
|
257
|
-
node = parse('{:foo => :bar}')
|
258
|
-
expect(node.hash_value(:foo)).to eq parse(':bar')
|
259
|
-
end
|
260
|
-
|
261
|
-
it 'gets nil if key does not exist' do
|
262
|
-
node = parse('{:foo => :bar}')
|
263
|
-
expect(node.hash_value(:bar)).to be_nil
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
describe '#key' do
|
268
|
-
it 'gets for pair node' do
|
269
|
-
node = parse("{:foo => 'bar'}").children[0]
|
270
|
-
expect(node.key).to eq parse(':foo')
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
describe '#value' do
|
275
|
-
it 'gets for hash node' do
|
276
|
-
node = parse("{:foo => 'bar'}").children[0]
|
277
|
-
expect(node.value).to eq parse("'bar'")
|
278
|
-
end
|
279
|
-
end
|
280
|
-
|
281
|
-
describe '#condition' do
|
282
|
-
it 'gets for if node' do
|
283
|
-
node = parse('if defined?(Bundler); end')
|
284
|
-
expect(node.condition).to eq parse('defined?(Bundler)')
|
285
|
-
end
|
286
|
-
end
|
287
|
-
|
288
|
-
describe '#left_value' do
|
289
|
-
it 'gets for masgn' do
|
290
|
-
node = parse('a, b = 1, 2')
|
291
|
-
expect(node.left_value.to_source).to eq 'a, b'
|
292
|
-
end
|
293
|
-
|
294
|
-
it 'gets for lvasgn' do
|
295
|
-
node = parse('a = 1')
|
296
|
-
expect(node.left_value).to eq :a
|
297
|
-
end
|
298
|
-
|
299
|
-
it 'gets for ivasgn' do
|
300
|
-
node = parse('@a = 1')
|
301
|
-
expect(node.left_value).to eq :@a
|
302
|
-
end
|
303
|
-
|
304
|
-
it 'gets for cvasgn' do
|
305
|
-
node = parse('@@a = 1')
|
306
|
-
expect(node.left_value).to eq :@@a
|
307
|
-
end
|
308
|
-
|
309
|
-
it 'gets for or_asgn' do
|
310
|
-
node = parse('a ||= 1')
|
311
|
-
expect(node.left_value).to eq :a
|
312
|
-
end
|
313
|
-
|
314
|
-
it 'gets for and' do
|
315
|
-
node = parse('foo && bar')
|
316
|
-
expect(node.left_value).to eq parse('foo')
|
317
|
-
end
|
318
|
-
|
319
|
-
it 'gets for or' do
|
320
|
-
node = parse('foo || bar')
|
321
|
-
expect(node.left_value).to eq parse('foo')
|
322
|
-
end
|
323
|
-
end
|
324
|
-
|
325
|
-
describe '#right_value' do
|
326
|
-
it 'gets for masgn' do
|
327
|
-
node = parse('a, b = 1, 2')
|
328
|
-
expect(node.right_value).to eq parse('[1, 2]')
|
329
|
-
end
|
330
|
-
|
331
|
-
it 'gets for masgn' do
|
332
|
-
node = parse('a, b = params')
|
333
|
-
expect(node.right_value).to eq parse('params')
|
334
|
-
end
|
335
|
-
|
336
|
-
it 'gets for lvasgn' do
|
337
|
-
node = parse('a = 1')
|
338
|
-
expect(node.right_value).to eq parse('1')
|
339
|
-
end
|
340
|
-
|
341
|
-
it 'gets for ivasgn' do
|
342
|
-
node = parse('@a = 1')
|
343
|
-
expect(node.right_value).to eq parse('1')
|
344
|
-
end
|
345
|
-
|
346
|
-
it 'gets for cvasgn' do
|
347
|
-
node = parse('@@a = 1')
|
348
|
-
expect(node.right_value).to eq parse('1')
|
349
|
-
end
|
350
|
-
|
351
|
-
it 'gets for or_asgn' do
|
352
|
-
node = parse('a ||= 1')
|
353
|
-
expect(node.right_value).to eq parse('1')
|
354
|
-
end
|
355
|
-
|
356
|
-
it 'gets for and' do
|
357
|
-
node = parse('foo && bar')
|
358
|
-
expect(node.right_value).to eq parse('bar')
|
359
|
-
end
|
360
|
-
|
361
|
-
it 'gets for or' do
|
362
|
-
node = parse('foo || bar')
|
363
|
-
expect(node.right_value).to eq parse('bar')
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
describe '#to_value' do
|
368
|
-
it 'gets for int' do
|
369
|
-
node = parse('1')
|
370
|
-
expect(node.to_value).to eq 1
|
371
|
-
end
|
372
|
-
|
373
|
-
it 'gets for float' do
|
374
|
-
node = parse('1.5')
|
375
|
-
expect(node.to_value).to eq 1.5
|
376
|
-
end
|
377
|
-
|
378
|
-
it 'gets for string' do
|
379
|
-
node = parse("'str'")
|
380
|
-
expect(node.to_value).to eq 'str'
|
381
|
-
end
|
382
|
-
|
383
|
-
it 'gets for symbol' do
|
384
|
-
node = parse(':str')
|
385
|
-
expect(node.to_value).to eq :str
|
386
|
-
end
|
387
|
-
|
388
|
-
it 'gets for boolean' do
|
389
|
-
node = parse('true')
|
390
|
-
expect(node.to_value).to be_truthy
|
391
|
-
node = parse('false')
|
392
|
-
expect(node.to_value).to be_falsey
|
393
|
-
end
|
394
|
-
|
395
|
-
it 'gets for nil' do
|
396
|
-
node = parse('nil')
|
397
|
-
expect(node.to_value).to be_nil
|
398
|
-
end
|
399
|
-
|
400
|
-
it 'gets for irange' do
|
401
|
-
node = parse('(1..10)')
|
402
|
-
expect(node.to_value).to eq(1..10)
|
403
|
-
end
|
404
|
-
|
405
|
-
it 'gets for erange' do
|
406
|
-
node = parse('(1...10)')
|
407
|
-
expect(node.to_value).to eq(1...10)
|
408
|
-
end
|
409
|
-
|
410
|
-
it 'gets for array' do
|
411
|
-
node = parse("['str', :str]")
|
412
|
-
expect(node.to_value).to eq ['str', :str]
|
413
|
-
end
|
414
|
-
end
|
415
|
-
|
416
|
-
describe '#filename' do
|
417
|
-
it 'gets file name' do
|
418
|
-
source = 'foobar'
|
419
|
-
node = parse(source)
|
420
|
-
expect(node.filename).to eq '(string)'
|
421
|
-
end
|
422
|
-
end
|
423
|
-
|
424
|
-
describe '#to_source' do
|
425
|
-
it 'gets for node' do
|
426
|
-
source = 'params[:user][:email]'
|
427
|
-
node = parse(source)
|
428
|
-
expect(node.to_source).to eq 'params[:user][:email]'
|
429
|
-
end
|
430
|
-
end
|
431
|
-
|
432
|
-
describe '#column' do
|
433
|
-
it 'gets column number' do
|
434
|
-
node = parse(' FactoryGirl.create :post')
|
435
|
-
expect(node.column).to eq 2
|
436
|
-
end
|
437
|
-
end
|
438
|
-
|
439
|
-
describe '#line' do
|
440
|
-
it 'gets line number' do
|
441
|
-
node = parse('foobar')
|
442
|
-
expect(node.line).to eq 1
|
443
|
-
end
|
444
|
-
end
|
445
|
-
|
446
|
-
describe 'key value by method_missing' do
|
447
|
-
it 'gets for key value' do
|
448
|
-
node = parse('{:foo => :bar}')
|
449
|
-
expect(node.foo_value).to eq :bar
|
450
|
-
|
451
|
-
node = parse("{'foo' => 'bar'}")
|
452
|
-
expect(node.foo_value).to eq 'bar'
|
453
|
-
|
454
|
-
expect(node.bar_value).to be_nil
|
455
|
-
end
|
456
|
-
end
|
457
|
-
|
458
|
-
describe 'key value source by method_missing' do
|
459
|
-
it 'gets for key value source' do
|
460
|
-
node = parse('{:foo => :bar}')
|
461
|
-
expect(node.foo_source).to eq ':bar'
|
462
|
-
|
463
|
-
node = parse("{'foo' => 'bar'}")
|
464
|
-
expect(node.foo_source).to eq "'bar'"
|
465
|
-
|
466
|
-
expect(node.bar_source).to be_nil
|
467
|
-
end
|
468
|
-
end
|
469
|
-
|
470
|
-
describe '#recursive_children' do
|
471
|
-
it 'iterates all children recursively' do
|
472
|
-
node = parse('class Synvert; def current_node; @node; end; end')
|
473
|
-
children = []
|
474
|
-
node.recursive_children { |child| children << child.type }
|
475
|
-
expect(children).to be_include :const
|
476
|
-
expect(children).to be_include :def
|
477
|
-
expect(children).to be_include :args
|
478
|
-
expect(children).to be_include :ivar
|
479
|
-
end
|
480
|
-
end
|
481
|
-
|
482
|
-
describe '#match?' do
|
483
|
-
it 'matches class name' do
|
484
|
-
source = 'class Synvert; end'
|
485
|
-
node = parse(source)
|
486
|
-
expect(node).to be_match(type: 'class', name: 'Synvert')
|
487
|
-
end
|
488
|
-
|
489
|
-
it 'matches message with regexp' do
|
490
|
-
source = 'User.find_by_login(login)'
|
491
|
-
node = parse(source)
|
492
|
-
expect(node).to be_match(type: 'send', message: /^find_by_/)
|
493
|
-
end
|
494
|
-
|
495
|
-
it 'matches arguments with symbol' do
|
496
|
-
source = 'params[:user]'
|
497
|
-
node = parse(source)
|
498
|
-
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: [:user])
|
499
|
-
end
|
500
|
-
|
501
|
-
it 'matches pair key with symbol' do
|
502
|
-
source = '{ type: :model }'
|
503
|
-
node = parse(source).children[0]
|
504
|
-
expect(node).to be_match(type: 'pair', key: :type)
|
505
|
-
end
|
506
|
-
|
507
|
-
it 'matches assign number' do
|
508
|
-
source = 'at_least(0)'
|
509
|
-
node = parse(source)
|
510
|
-
expect(node).to be_match(type: 'send', arguments: [0])
|
511
|
-
end
|
512
|
-
|
513
|
-
it 'matches assign float' do
|
514
|
-
source = 'at_least(1.5)'
|
515
|
-
node = parse(source)
|
516
|
-
expect(node).to be_match(type: 'send', arguments: [1.5])
|
517
|
-
end
|
518
|
-
|
519
|
-
it 'matches arguments with string' do
|
520
|
-
source = 'params["user"]'
|
521
|
-
node = parse(source)
|
522
|
-
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ['user'])
|
523
|
-
end
|
524
|
-
|
525
|
-
it 'matches arguments with string 2' do
|
526
|
-
source = 'params["user"]'
|
527
|
-
node = parse(source)
|
528
|
-
expect(node).to be_match(type: 'send', receiver: 'params', message: '[]', arguments: ["'user'"])
|
529
|
-
end
|
530
|
-
|
531
|
-
it 'matches arguments with string 3' do
|
532
|
-
source = "{ notice: 'Welcome' }"
|
533
|
-
node = parse(source)
|
534
|
-
expect(node).to be_match(type: 'hash', notice_value: "'Welcome'")
|
535
|
-
end
|
536
|
-
|
537
|
-
it 'matches arguments any' do
|
538
|
-
source = 'config.middleware.insert_after ActiveRecord::QueryCache, Lifo::Cache, page_cache: false'
|
539
|
-
node = parse(source)
|
540
|
-
expect(node).to be_match(type: 'send', arguments: { any: 'Lifo::Cache' })
|
541
|
-
end
|
542
|
-
|
543
|
-
it 'matches arguments with nested hash' do
|
544
|
-
source = '{ user_id: user.id }'
|
545
|
-
node = parse(source)
|
546
|
-
expect(node).to be_match(
|
547
|
-
type: 'hash',
|
548
|
-
user_id_value: {
|
549
|
-
type: 'send',
|
550
|
-
receiver: { type: 'send', message: 'user' },
|
551
|
-
message: 'id'
|
552
|
-
}
|
553
|
-
)
|
554
|
-
end
|
555
|
-
|
556
|
-
it 'matches arguments contain' do
|
557
|
-
source = 'def slow(foo, bar, &block); end'
|
558
|
-
node = parse(source)
|
559
|
-
expect(node).to be_match(type: 'def', arguments: { contain: '&block' })
|
560
|
-
end
|
561
|
-
|
562
|
-
it 'matches not' do
|
563
|
-
source = 'class Synvert; end'
|
564
|
-
node = parse(source)
|
565
|
-
expect(node).not_to be_match(type: 'class', name: { not: 'Synvert' })
|
566
|
-
end
|
567
|
-
|
568
|
-
it 'matches in' do
|
569
|
-
source = 'FactoryBot.create(:user)'
|
570
|
-
node = parse(source)
|
571
|
-
expect(node).to be_match(type: 'send', message: { in: %i[create build] })
|
572
|
-
end
|
573
|
-
|
574
|
-
it 'matches not_in' do
|
575
|
-
source = 'FactoryBot.create(:user)'
|
576
|
-
node = parse(source)
|
577
|
-
expect(node).not_to be_match(type: 'send', message: { not_in: %i[create build] })
|
578
|
-
end
|
579
|
-
|
580
|
-
it 'matches gt' do
|
581
|
-
source = 'foobar(foo, bar)'
|
582
|
-
node = parse(source)
|
583
|
-
expect(node).to be_match(type: 'send', arguments: { size: { gt: 1 } })
|
584
|
-
expect(node).not_to be_match(type: 'send', arguments: { size: { gt: 2 } })
|
585
|
-
end
|
586
|
-
|
587
|
-
it 'matches gte' do
|
588
|
-
source = 'foobar(foo, bar)'
|
589
|
-
node = parse(source)
|
590
|
-
expect(node).to be_match(type: 'send', arguments: { size: { gte: 2 } })
|
591
|
-
expect(node).not_to be_match(type: 'send', arguments: { size: { gte: 3 } })
|
592
|
-
end
|
593
|
-
|
594
|
-
it 'matches lt' do
|
595
|
-
source = 'foobar(foo, bar)'
|
596
|
-
node = parse(source)
|
597
|
-
expect(node).to be_match(type: 'send', arguments: { size: { lt: 3 } })
|
598
|
-
expect(node).not_to be_match(type: 'send', arguments: { size: { lt: 2 } })
|
599
|
-
end
|
600
|
-
|
601
|
-
it 'matches lte' do
|
602
|
-
source = 'foobar(foo, bar)'
|
603
|
-
node = parse(source)
|
604
|
-
expect(node).to be_match(type: 'send', arguments: { size: { lte: 2 } })
|
605
|
-
expect(node).not_to be_match(type: 'send', arguments: { size: { lte: 1 } })
|
606
|
-
end
|
607
|
-
end
|
608
|
-
|
609
|
-
describe '#child_node_by_name' do
|
610
|
-
context 'block node' do
|
611
|
-
it 'checks caller' do
|
612
|
-
node = parse('Factory.define :user do |user|; end')
|
613
|
-
child_node = node.child_node_by_name(:caller)
|
614
|
-
expect(child_node).to eq node.caller
|
615
|
-
end
|
616
|
-
|
617
|
-
it 'checks arguments' do
|
618
|
-
node = parse('Factory.define :user do |user|; end')
|
619
|
-
child_node = node.child_node_by_name(:arguments)
|
620
|
-
expect(child_node).to eq node.arguments
|
621
|
-
end
|
622
|
-
|
623
|
-
it 'checks caller.receiver' do
|
624
|
-
node = parse('Factory.define :user do |user|; end')
|
625
|
-
child_node = node.child_node_by_name('caller.receiver')
|
626
|
-
expect(child_node).to eq node.caller.receiver
|
627
|
-
end
|
628
|
-
|
629
|
-
it 'checks caller.message' do
|
630
|
-
node = parse('Factory.define :user do |user|; end')
|
631
|
-
child_node = node.child_node_by_name('caller.message')
|
632
|
-
expect(child_node).to eq node.caller.message
|
633
|
-
end
|
634
|
-
end
|
635
|
-
|
636
|
-
context 'array' do
|
637
|
-
it 'checks array by index' do
|
638
|
-
node = parse('factory :admin, class: User do; end')
|
639
|
-
child_node = node.child_node_by_name('caller.arguments.2')
|
640
|
-
expect(child_node).to eq node.caller.arguments[1]
|
641
|
-
end
|
642
|
-
|
643
|
-
it 'checks array by method' do
|
644
|
-
node = parse('factory :admin, class: User do; end')
|
645
|
-
child_node = node.child_node_by_name('caller.arguments.second')
|
646
|
-
expect(child_node).to eq node.caller.arguments[1]
|
647
|
-
end
|
648
|
-
|
649
|
-
it "checks array' value" do
|
650
|
-
node = parse('factory :admin, class: User do; end')
|
651
|
-
child_node = node.child_node_by_name('caller.arguments.second.class_value')
|
652
|
-
expect(child_node).to eq node.caller.arguments[1].class_value
|
653
|
-
end
|
654
|
-
end
|
655
|
-
end
|
656
|
-
|
657
|
-
describe '#child_node_range' do
|
658
|
-
context 'block node' do
|
659
|
-
it 'checks caller' do
|
660
|
-
node = parse('Factory.define :user do |user|; end')
|
661
|
-
range = node.child_node_range(:caller)
|
662
|
-
expect(range.to_range).to eq(0...20)
|
663
|
-
end
|
664
|
-
|
665
|
-
it 'checks arguments' do
|
666
|
-
node = parse('Factory.define :user do |user|; end')
|
667
|
-
range = node.child_node_range(:arguments)
|
668
|
-
expect(range.to_range).to eq(25...29)
|
669
|
-
end
|
670
|
-
|
671
|
-
it 'checks pipes' do
|
672
|
-
node = parse('Factory.define :user do |user|; end')
|
673
|
-
range = node.child_node_range(:pipes)
|
674
|
-
expect(range.to_range).to eq(24...30)
|
675
|
-
end
|
676
|
-
|
677
|
-
it 'checks caller.receiver' do
|
678
|
-
node = parse('Factory.define :user do |user|; end')
|
679
|
-
range = node.child_node_range('caller.receiver')
|
680
|
-
expect(range.to_range).to eq(0...7)
|
681
|
-
end
|
682
|
-
|
683
|
-
it 'checks caller.message' do
|
684
|
-
node = parse('Factory.define :user do |user|; end')
|
685
|
-
range = node.child_node_range('caller.message')
|
686
|
-
expect(range.to_range).to eq(8...14)
|
687
|
-
end
|
688
|
-
end
|
689
|
-
|
690
|
-
context 'class node' do
|
691
|
-
it 'checks name' do
|
692
|
-
node = parse('class Post < ActiveRecord::Base; end')
|
693
|
-
range = node.child_node_range(:name)
|
694
|
-
expect(range.to_range).to eq(6...10)
|
695
|
-
end
|
696
|
-
|
697
|
-
it 'checks parent_class' do
|
698
|
-
node = parse('class Post < ActiveRecord::Base; end')
|
699
|
-
range = node.child_node_range(:parent_class)
|
700
|
-
expect(range.to_range).to eq(13...31)
|
701
|
-
|
702
|
-
node = parse('class Post; end')
|
703
|
-
range = node.child_node_range(:parent_class)
|
704
|
-
expect(range).to be_nil
|
705
|
-
end
|
706
|
-
end
|
707
|
-
|
708
|
-
context 'csend node' do
|
709
|
-
it 'checks receiver' do
|
710
|
-
node = parse('foo&.bar(test)')
|
711
|
-
range = node.child_node_range(:receiver)
|
712
|
-
expect(range.to_range).to eq(0...3)
|
713
|
-
end
|
714
|
-
|
715
|
-
it 'checks dot' do
|
716
|
-
node = parse('foo&.bar(test)')
|
717
|
-
range = node.child_node_range(:dot)
|
718
|
-
expect(range.to_range).to eq(3...5)
|
719
|
-
end
|
720
|
-
|
721
|
-
it 'checks message' do
|
722
|
-
node = parse('foo&.bar(test)')
|
723
|
-
range = node.child_node_range(:message)
|
724
|
-
expect(range.to_range).to eq(5...8)
|
725
|
-
|
726
|
-
node = parse('foo&.bar = test')
|
727
|
-
range = node.child_node_range(:message)
|
728
|
-
expect(range.to_range).to eq(5...10)
|
729
|
-
end
|
730
|
-
|
731
|
-
it 'checks arguments' do
|
732
|
-
node = parse('foo&.bar(test)')
|
733
|
-
range = node.child_node_range(:arguments)
|
734
|
-
expect(range.to_range).to eq(9...13)
|
735
|
-
|
736
|
-
node = parse('foo&.bar')
|
737
|
-
range = node.child_node_range(:arguments)
|
738
|
-
expect(range).to be_nil
|
739
|
-
end
|
740
|
-
|
741
|
-
it 'checks parentheses' do
|
742
|
-
node = parse('foo&.bar(test)')
|
743
|
-
range = node.child_node_range(:parentheses)
|
744
|
-
expect(range.to_range).to eq(8...14)
|
745
|
-
|
746
|
-
node = parse('foo&.bar')
|
747
|
-
range = node.child_node_range(:parentheses)
|
748
|
-
expect(range).to be_nil
|
749
|
-
end
|
750
|
-
end
|
751
|
-
|
752
|
-
context 'def node' do
|
753
|
-
it 'checks name' do
|
754
|
-
node = parse('def foo(bar); end')
|
755
|
-
range = node.child_node_range(:name)
|
756
|
-
expect(range.to_range).to eq(4...7)
|
757
|
-
end
|
758
|
-
|
759
|
-
it 'checks arguments' do
|
760
|
-
node = parse('def foo(bar); end')
|
761
|
-
range = node.child_node_range(:arguments)
|
762
|
-
expect(range.to_range).to eq(8...11)
|
763
|
-
end
|
764
|
-
|
765
|
-
it 'checks parentheses' do
|
766
|
-
node = parse('def foo(bar); end')
|
767
|
-
range = node.child_node_range(:parentheses)
|
768
|
-
expect(range.to_range).to eq(7...12)
|
769
|
-
end
|
770
|
-
end
|
771
|
-
|
772
|
-
context 'defs node' do
|
773
|
-
it 'checks self' do
|
774
|
-
node = parse('def self.foo(bar); end')
|
775
|
-
range = node.child_node_range(:self)
|
776
|
-
expect(range.to_range).to eq(4...8)
|
777
|
-
end
|
778
|
-
|
779
|
-
it 'checks dot' do
|
780
|
-
node = parse('def self.foo(bar); end')
|
781
|
-
range = node.child_node_range(:dot)
|
782
|
-
expect(range.to_range).to eq(8...9)
|
783
|
-
end
|
784
|
-
|
785
|
-
it 'checks name' do
|
786
|
-
node = parse('def self.foo(bar); end')
|
787
|
-
range = node.child_node_range(:name)
|
788
|
-
expect(range.to_range).to eq(9...12)
|
789
|
-
end
|
790
|
-
|
791
|
-
it 'checks arguments' do
|
792
|
-
node = parse('def self.foo(bar); end')
|
793
|
-
range = node.child_node_range(:arguments)
|
794
|
-
expect(range.to_range).to eq(13...16)
|
795
|
-
end
|
796
|
-
|
797
|
-
it 'checks parentheses' do
|
798
|
-
node = parse('def self.foo(bar); end')
|
799
|
-
range = node.child_node_range(:parentheses)
|
800
|
-
expect(range.to_range).to eq(12...17)
|
801
|
-
end
|
802
|
-
end
|
803
|
-
|
804
|
-
context 'send node' do
|
805
|
-
it 'checks receiver' do
|
806
|
-
node = parse('foo.bar(test)')
|
807
|
-
range = node.child_node_range(:receiver)
|
808
|
-
expect(range.to_range).to eq(0...3)
|
809
|
-
|
810
|
-
node = parse('foobar(test)')
|
811
|
-
range = node.child_node_range(:receiver)
|
812
|
-
expect(range).to be_nil
|
813
|
-
end
|
814
|
-
|
815
|
-
it 'checks dot' do
|
816
|
-
node = parse('foo.bar(test)')
|
817
|
-
range = node.child_node_range(:dot)
|
818
|
-
expect(range.to_range).to eq(3...4)
|
819
|
-
|
820
|
-
node = parse('foobar(test)')
|
821
|
-
range = node.child_node_range(:dot)
|
822
|
-
expect(range).to be_nil
|
823
|
-
end
|
824
|
-
|
825
|
-
it 'checks message' do
|
826
|
-
node = parse('foo.bar(test)')
|
827
|
-
range = node.child_node_range(:message)
|
828
|
-
expect(range.to_range).to eq(4...7)
|
829
|
-
|
830
|
-
node = parse('foo.bar = test')
|
831
|
-
range = node.child_node_range(:message)
|
832
|
-
expect(range.to_range).to eq(4...9)
|
833
|
-
|
834
|
-
node = parse('foobar(test)')
|
835
|
-
range = node.child_node_range(:message)
|
836
|
-
expect(range.to_range).to eq(0...6)
|
837
|
-
end
|
838
|
-
|
839
|
-
it 'checks arguments' do
|
840
|
-
node = parse('foo.bar(test)')
|
841
|
-
range = node.child_node_range(:arguments)
|
842
|
-
expect(range.to_range).to eq(8...12)
|
843
|
-
|
844
|
-
node = parse('foobar(test)')
|
845
|
-
range = node.child_node_range(:arguments)
|
846
|
-
expect(range.to_range).to eq(7...11)
|
847
|
-
|
848
|
-
node = parse('foo.bar')
|
849
|
-
range = node.child_node_range(:arguments)
|
850
|
-
expect(range).to be_nil
|
851
|
-
end
|
852
|
-
|
853
|
-
it 'checks parentheses' do
|
854
|
-
node = parse('foo.bar(test)')
|
855
|
-
range = node.child_node_range(:parentheses)
|
856
|
-
expect(range.to_range).to eq(7...13)
|
857
|
-
|
858
|
-
node = parse('foobar(test)')
|
859
|
-
range = node.child_node_range(:parentheses)
|
860
|
-
expect(range.to_range).to eq(6...12)
|
861
|
-
|
862
|
-
node = parse('foo.bar')
|
863
|
-
range = node.child_node_range(:parentheses)
|
864
|
-
expect(range).to be_nil
|
865
|
-
end
|
866
|
-
end
|
867
|
-
|
868
|
-
context 'array' do
|
869
|
-
it 'checks array by index' do
|
870
|
-
node = parse('factory :admin, class: User do; end')
|
871
|
-
range = node.child_node_range('caller.arguments.2')
|
872
|
-
expect(range.to_range).to eq(16...27)
|
873
|
-
end
|
874
|
-
|
875
|
-
it 'checks array by method' do
|
876
|
-
node = parse('factory :admin, class: User do; end')
|
877
|
-
range = node.child_node_range('caller.arguments.second')
|
878
|
-
expect(range.to_range).to eq(16...27)
|
879
|
-
end
|
880
|
-
|
881
|
-
it "checks array's value" do
|
882
|
-
node = parse('factory :admin, class: User do; end')
|
883
|
-
range = node.child_node_range('caller.arguments.second.class_value')
|
884
|
-
expect(range.to_range).to eq(23...27)
|
885
|
-
end
|
886
|
-
end
|
887
|
-
end
|
888
|
-
|
889
|
-
describe '#rewritten_source' do
|
890
|
-
it 'does not rewrite with unknown method' do
|
891
|
-
source = 'class Synvert; end'
|
892
|
-
node = parse(source)
|
893
|
-
expect(node.rewritten_source('{{foobar}}')).to eq '{{foobar}}'
|
894
|
-
end
|
895
|
-
|
896
|
-
it 'rewrites with node known method' do
|
897
|
-
source = 'class Synvert; end'
|
898
|
-
node = parse(source)
|
899
|
-
expect(node.rewritten_source('{{name}}')).to eq 'Synvert'
|
900
|
-
end
|
901
|
-
|
902
|
-
it 'rewrites for arguments' do
|
903
|
-
source = 'test { |a, b| }'
|
904
|
-
node = parse(source)
|
905
|
-
expect(node.rewritten_source('{{arguments}}')).to eq 'a, b'
|
906
|
-
end
|
907
|
-
|
908
|
-
it 'rewrites array with multi line given as argument for method' do
|
909
|
-
source = <<~EOS.strip
|
910
|
-
long_name_method([
|
911
|
-
1,
|
912
|
-
2,
|
913
|
-
3
|
914
|
-
])
|
915
|
-
EOS
|
916
|
-
|
917
|
-
node = parse(source)
|
918
|
-
expect(node.rewritten_source('{{arguments}}')).to eq <<~EOS.strip
|
919
|
-
[
|
920
|
-
1,
|
921
|
-
2,
|
922
|
-
3
|
923
|
-
]
|
924
|
-
EOS
|
925
|
-
end
|
926
|
-
end
|
927
|
-
|
928
6
|
describe '#strip_curly_braces' do
|
929
7
|
context 'hash node' do
|
930
8
|
it 'removes curly braces' do
|
@@ -1027,47 +105,4 @@ describe Parser::AST::Node do
|
|
1027
105
|
end
|
1028
106
|
end
|
1029
107
|
end
|
1030
|
-
|
1031
|
-
describe '#to_hash' do
|
1032
|
-
it 'gets hash' do
|
1033
|
-
node = parse(<<~EOS)
|
1034
|
-
class Synvert
|
1035
|
-
def foobar(foo, bar)
|
1036
|
-
{ foo => bar }
|
1037
|
-
end
|
1038
|
-
end
|
1039
|
-
EOS
|
1040
|
-
expect(node.to_hash).to eq(
|
1041
|
-
{
|
1042
|
-
type: :class,
|
1043
|
-
parent_class: nil,
|
1044
|
-
name: {
|
1045
|
-
type: :const,
|
1046
|
-
parent_const: nil,
|
1047
|
-
name: :Synvert
|
1048
|
-
},
|
1049
|
-
body: [
|
1050
|
-
{
|
1051
|
-
type: :def,
|
1052
|
-
name: :foobar,
|
1053
|
-
arguments: [
|
1054
|
-
{ type: :arg, name: :foo },
|
1055
|
-
{ type: :arg, name: :bar }
|
1056
|
-
],
|
1057
|
-
body: [
|
1058
|
-
{
|
1059
|
-
type: :hash,
|
1060
|
-
pairs: {
|
1061
|
-
type: :pair,
|
1062
|
-
key: { type: :lvar, name: :foo },
|
1063
|
-
value: { type: :lvar, name: :bar }
|
1064
|
-
}
|
1065
|
-
}
|
1066
|
-
]
|
1067
|
-
}
|
1068
|
-
]
|
1069
|
-
}
|
1070
|
-
)
|
1071
|
-
end
|
1072
|
-
end
|
1073
108
|
end
|