synvert-core 1.3.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -4
  3. data/CHANGELOG.md +16 -0
  4. data/Gemfile +0 -3
  5. data/Guardfile +0 -9
  6. data/README.md +30 -12
  7. data/Rakefile +1 -15
  8. data/lib/synvert/core/engine/erb.rb +1 -1
  9. data/lib/synvert/core/engine.rb +1 -1
  10. data/lib/synvert/core/node_ext.rb +0 -466
  11. data/lib/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action.rb +20 -17
  12. data/lib/synvert/core/rewriter/condition/if_exist_condition.rb +1 -1
  13. data/lib/synvert/core/rewriter/condition/unless_exist_condition.rb +1 -1
  14. data/lib/synvert/core/rewriter/instance.rb +89 -135
  15. data/lib/synvert/core/rewriter/scope/query_scope.rb +2 -2
  16. data/lib/synvert/core/rewriter/scope/within_scope.rb +4 -4
  17. data/lib/synvert/core/rewriter.rb +0 -10
  18. data/lib/synvert/core/version.rb +1 -1
  19. data/lib/synvert/core.rb +4 -6
  20. data/spec/synvert/core/engine/erb_spec.rb +3 -3
  21. data/spec/synvert/core/node_ext_spec.rb +0 -795
  22. data/spec/synvert/core/rewriter/action/replace_erb_stmt_with_expr_action_spec.rb +21 -1
  23. data/spec/synvert/core/rewriter/instance_spec.rb +49 -112
  24. data/spec/synvert/core/rewriter/scope/goto_scope_spec.rb +1 -4
  25. data/spec/synvert/core/rewriter/scope/query_scope_spec.rb +8 -4
  26. data/spec/synvert/core/rewriter/scope/within_scope_spec.rb +1 -4
  27. data/synvert-core-ruby.gemspec +4 -2
  28. metadata +44 -61
  29. data/lib/synvert/core/array_ext.rb +0 -48
  30. data/lib/synvert/core/node_query/compiler/array.rb +0 -34
  31. data/lib/synvert/core/node_query/compiler/attribute.rb +0 -39
  32. data/lib/synvert/core/node_query/compiler/attribute_list.rb +0 -24
  33. data/lib/synvert/core/node_query/compiler/boolean.rb +0 -23
  34. data/lib/synvert/core/node_query/compiler/comparable.rb +0 -86
  35. data/lib/synvert/core/node_query/compiler/dynamic_attribute.rb +0 -51
  36. data/lib/synvert/core/node_query/compiler/expression.rb +0 -58
  37. data/lib/synvert/core/node_query/compiler/float.rb +0 -23
  38. data/lib/synvert/core/node_query/compiler/identifier.rb +0 -41
  39. data/lib/synvert/core/node_query/compiler/integer.rb +0 -23
  40. data/lib/synvert/core/node_query/compiler/invalid_operator_error.rb +0 -7
  41. data/lib/synvert/core/node_query/compiler/nil.rb +0 -23
  42. data/lib/synvert/core/node_query/compiler/parse_error.rb +0 -7
  43. data/lib/synvert/core/node_query/compiler/regexp.rb +0 -37
  44. data/lib/synvert/core/node_query/compiler/selector.rb +0 -138
  45. data/lib/synvert/core/node_query/compiler/simple_selector.rb +0 -29
  46. data/lib/synvert/core/node_query/compiler/string.rb +0 -23
  47. data/lib/synvert/core/node_query/compiler/symbol.rb +0 -23
  48. data/lib/synvert/core/node_query/compiler.rb +0 -25
  49. data/lib/synvert/core/node_query/lexer.rex +0 -108
  50. data/lib/synvert/core/node_query/lexer.rex.rb +0 -321
  51. data/lib/synvert/core/node_query/parser.racc.rb +0 -322
  52. data/lib/synvert/core/node_query/parser.y +0 -62
  53. data/lib/synvert/core/node_query.rb +0 -37
  54. data/lib/synvert/core/rewriter/action/append_action.rb +0 -28
  55. data/lib/synvert/core/rewriter/action/delete_action.rb +0 -32
  56. data/lib/synvert/core/rewriter/action/insert_action.rb +0 -34
  57. data/lib/synvert/core/rewriter/action/insert_after_action.rb +0 -22
  58. data/lib/synvert/core/rewriter/action/prepend_action.rb +0 -44
  59. data/lib/synvert/core/rewriter/action/remove_action.rb +0 -53
  60. data/lib/synvert/core/rewriter/action/replace_action.rb +0 -33
  61. data/lib/synvert/core/rewriter/action/replace_with_action.rb +0 -36
  62. data/lib/synvert/core/rewriter/action/wrap_action.rb +0 -37
  63. data/lib/synvert/core/rewriter/action.rb +0 -102
  64. data/spec/synvert/core/node_query/lexer_spec.rb +0 -640
  65. data/spec/synvert/core/node_query/parser_spec.rb +0 -382
  66. data/spec/synvert/core/rewriter/action/append_action_spec.rb +0 -70
  67. data/spec/synvert/core/rewriter/action/delete_action_spec.rb +0 -26
  68. data/spec/synvert/core/rewriter/action/insert_action_spec.rb +0 -70
  69. data/spec/synvert/core/rewriter/action/insert_after_action_spec.rb +0 -26
  70. data/spec/synvert/core/rewriter/action/prepend_action_spec.rb +0 -175
  71. data/spec/synvert/core/rewriter/action/remove_action_spec.rb +0 -26
  72. data/spec/synvert/core/rewriter/action/replace_action_spec.rb +0 -28
  73. data/spec/synvert/core/rewriter/action/replace_with_action_spec.rb +0 -59
  74. data/spec/synvert/core/rewriter/action/wrap_action_spec.rb +0 -31
  75. data/spec/synvert/core/rewriter/action_spec.rb +0 -14
@@ -3,482 +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
6
  describe '#match?' do
483
7
  it 'matches class name' do
484
8
  source = 'class Synvert; end'
@@ -606,325 +130,6 @@ describe Parser::AST::Node do
606
130
  end
607
131
  end
608
132
 
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
133
  describe '#strip_curly_braces' do
929
134
  context 'hash node' do
930
135
  it 'removes curly braces' do