-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
node_spec.rb
744 lines (608 loc) · 17.2 KB
/
node_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
# frozen_string_literal: true
RSpec.describe RuboCop::AST::Node do
let(:ast) { parse_source(src).node }
let(:node) { ast }
describe '#value_used?' do
before :all do
module RuboCop # rubocop:disable Lint/ConstantDefinitionInBlock
module AST
# Patch Node
class Node
# Let's make our predicate matchers read better
def used?
value_used?
end
end
end
end
end
context 'at the top level' do
let(:src) { 'expr' }
it 'is false' do
expect(node).not_to be_used
end
end
context 'within a method call node' do
let(:src) { 'obj.method(arg1, arg2, arg3)' }
it 'is always true' do
expect(node.child_nodes).to all(be_used)
end
end
context 'at the end of a block' do
let(:src) { 'obj.method { blah; expr }' }
it 'is always true' do
expect(node.children.last).to be_used
end
end
context 'within a class definition node' do
let(:src) { 'class C < Super; def a; 1; end; self; end' }
it 'is always true' do
expect(node.child_nodes).to all(be_used)
end
end
context 'within a module definition node' do
let(:src) { 'module M; def method; end; 1; end' }
it 'is always true' do
expect(node.child_nodes).to all(be_used)
end
end
context 'within a singleton class node' do
let(:src) { 'class << obj; 1; 2; end' }
it 'is always true' do
expect(node.child_nodes).to all(be_used)
end
end
context 'within an if...else..end node' do
context 'nested in a method call' do
let(:src) { 'obj.method(if a then b else c end)' }
it 'is always true' do
if_node = node.children[2]
expect(if_node.child_nodes).to all(be_used)
end
end
context 'at the top level' do
let(:src) { 'if a then b else c end' }
it 'is true only for the condition' do
expect(node.condition).to be_used
expect(node.if_branch).not_to be_used
expect(node.else_branch).not_to be_used
end
end
end
context 'within an array literal' do
context 'assigned to an ivar' do
let(:src) { '@var = [a, b, c]' }
it 'is always true' do
ary_node = node.children[1]
expect(ary_node.child_nodes).to all(be_used)
end
end
context 'at the top level' do
let(:src) { '[a, b, c]' }
it 'is always false' do
expect(node.child_nodes.map(&:used?)).to all(be false)
end
end
end
context 'within a while node' do
let(:src) { 'while a; b; end' }
it 'is true only for the condition' do
expect(node.condition).to be_used
expect(node.body).not_to be_used
end
end
end
describe '#recursive_basic_literal?' do
shared_examples 'literal' do |source|
let(:src) { source }
it "returns true for `#{source}`" do
expect(node).to be_recursive_literal
end
end
it_behaves_like 'literal', '!true'
it_behaves_like 'literal', '"#{2}"'
it_behaves_like 'literal', '(1)'
it_behaves_like 'literal', '(false && true)'
it_behaves_like 'literal', '(false <=> true)'
it_behaves_like 'literal', '(false or true)'
it_behaves_like 'literal', '[1, 2, 3]'
it_behaves_like 'literal', '{ :a => 1, :b => 2 }'
it_behaves_like 'literal', '{ a: 1, b: 2 }'
it_behaves_like 'literal', '/./'
it_behaves_like 'literal', '%r{abx}ixo'
it_behaves_like 'literal', '1.0'
it_behaves_like 'literal', '1'
it_behaves_like 'literal', 'false'
it_behaves_like 'literal', 'nil'
it_behaves_like 'literal', "'str'"
shared_examples 'non literal' do |source|
let(:src) { source }
it "returns false for `#{source}`" do
expect(node).not_to be_recursive_literal
end
end
it_behaves_like 'non literal', '(x && false)'
it_behaves_like 'non literal', '(x == false)'
it_behaves_like 'non literal', '(x or false)'
it_behaves_like 'non literal', '[some_method_call]'
it_behaves_like 'non literal', '{ :sym => some_method_call }'
it_behaves_like 'non literal', '{ some_method_call => :sym }'
it_behaves_like 'non literal', '/.#{some_method_call}/'
it_behaves_like 'non literal', '%r{abx#{foo}}ixo'
it_behaves_like 'non literal', 'some_method_call'
it_behaves_like 'non literal', 'some_method_call(x, y)'
end
describe '#pure?' do
context 'for a method call' do
let(:src) { 'obj.method(arg1, arg2)' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'for an integer literal' do
let(:src) { '100' }
it 'returns true' do
expect(node).to be_pure
end
end
context 'for an array literal' do
context 'with only literal children' do
let(:src) { '[1..100, false, :symbol, "string", 1.0]' }
it 'returns true' do
expect(node).to be_pure
end
end
context 'which contains a method call' do
let(:src) { '[1, 2, 3, 3 + 4]' }
it 'returns false' do
expect(node).not_to be_pure
end
end
end
context 'for a hash literal' do
context 'with only literal children' do
let(:src) { '{range: 1..100, bool: false, str: "string", float: 1.0}' }
it 'returns true' do
expect(node).to be_pure
end
end
context 'which contains a method call' do
let(:src) { '{a: 1, b: 2, c: Kernel.exit}' }
it 'returns false' do
expect(node).not_to be_pure
end
end
end
context 'for a nested if' do
context 'where the innermost descendants are local vars and literals' do
let(:src) do
['lvar1, lvar2 = method1, method2',
'if $global',
' if @initialized',
' [lvar1, lvar2, true]',
' else',
' :symbol',
' end',
'else',
' lvar1',
'end'].join("\n")
end
it 'returns true' do
if_node = node.children[1]
expect(if_node.type).to be :if
expect(if_node).to be_pure
end
end
context 'where one branch contains a method call' do
let(:src) { 'if $DEBUG then puts "hello" else nil end' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'where one branch contains an assignment statement' do
let(:src) { 'if @a then 1 else $global = "str" end' }
it 'returns false' do
expect(node).not_to be_pure
end
end
end
context 'for an ivar assignment' do
let(:src) { '@var = 1' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'for a gvar assignment' do
let(:src) { '$var = 1' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'for a cvar assignment' do
let(:src) { '@@var = 1' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'for an lvar assignment' do
let(:src) { 'var = 1' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'for a class definition' do
let(:src) { 'class C < Super; def method; end end' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'for a module definition' do
let(:src) { 'module M; def method; end end' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'for a regexp' do
let(:opts) { '' }
let(:body) { '' }
let(:src) { "/#{body}/#{opts}" }
context 'with interpolated segments' do
let(:body) { '#{x}' }
it 'returns false' do
expect(node).not_to be_pure
end
end
context 'with no interpolation' do
let(:src) { URI::DEFAULT_PARSER.make_regexp.inspect }
it 'returns true' do
expect(node).to be_pure
end
end
context 'with options' do
let(:opts) { 'oix' }
it 'returns true' do
expect(node).to be_pure
end
end
end
end
describe 'sibling_access' do
let(:src) { '[0, 1, 2, 3, 4, 5]' }
it 'returns trivial values for a root node' do
expect(node.sibling_index).to eq nil
expect(node.left_sibling).to eq nil
expect(node.right_sibling).to eq nil
expect(node.left_siblings).to eq []
expect(node.right_siblings).to eq []
end
context 'for a node with siblings' do
let(:node) { ast.children[2] }
it 'returns the expected values' do
expect(node.sibling_index).to eq 2
expect(node.left_sibling.value).to eq 1
expect(node.right_sibling.value).to eq 3
expect(node.left_siblings.map(&:value)).to eq [0, 1]
expect(node.right_siblings.map(&:value)).to eq [3, 4, 5]
end
end
context 'for a single child' do
let(:src) { '[0]' }
let(:node) { ast.children[0] }
it 'returns the expected values' do
expect(node.sibling_index).to eq 0
expect(node.left_sibling).to eq nil
expect(node.right_sibling).to eq nil
expect(node.left_siblings.map(&:value)).to eq []
expect(node.right_siblings.map(&:value)).to eq []
end
end
end
describe '#argument_type?' do
context 'block arguments' do
let(:src) { 'bar { |a, b = 42, *c, d: 42, **e| nil }' }
it 'returns true for all argument types' do
expect(node.arguments.children).to all be_argument_type
expect(node.arguments).not_to be_argument_type
end
end
context 'method arguments' do
let(:src) { 'def method_name(a = 0, *b, c: 42, **d); end' }
it 'returns true for all argument types' do
expect(node.arguments.children).to all be_argument_type
expect(node.arguments).not_to be_argument_type
end
end
end
describe '#class_constructor?' do
context 'class definition with a block' do
let(:src) { 'Class.new { a = 42 }' }
it 'matches' do
expect(node).to be_class_constructor
end
end
context 'module definition with a block' do
let(:src) { 'Module.new { a = 42 }' }
it 'matches' do
expect(node).to be_class_constructor
end
end
context 'class definition' do
let(:src) { 'class Foo; a = 42; end' }
it 'does not match' do
expect(node.class_constructor?).to eq(nil)
end
end
context 'class definition on outer scope' do
let(:src) { '::Class.new { a = 42 }' }
it 'matches' do
expect(node).to be_class_constructor
end
end
end
describe '#struct_constructor?' do
context 'struct definition with a block' do
let(:src) { 'Struct.new { a = 42 }' }
it 'matches' do
expect(node.struct_constructor?).to eq(node.body)
end
end
context 'struct definition without block' do
let(:src) { 'Struct.new(:foo, :bar)' }
it 'does not match' do
expect(node.struct_constructor?).to eq(nil)
end
end
context '::Struct' do
let(:src) { '::Struct.new { a = 42 }' }
it 'matches' do
expect(node.struct_constructor?).to eq(node.body)
end
end
end
describe '#class_definition?' do
context 'without inheritance' do
let(:src) { 'class Foo; a = 42; end' }
it 'matches' do
expect(node.class_definition?).to eq(node.body)
end
end
context 'with inheritance' do
let(:src) { 'class Foo < Bar; a = 42; end' }
it 'matches' do
expect(node.class_definition?).to eq(node.body)
end
end
context 'with ::ClassName' do
let(:src) { 'class ::Foo < Bar; a = 42; end' }
it 'matches' do
expect(node.class_definition?).to eq(node.body)
end
end
context 'with Struct' do
let(:src) do
<<~RUBY
Person = Struct.new(:name, :age) do
a = 2
def details; end
end
RUBY
end
it 'matches' do
class_node = node.children.last
expect(class_node.class_definition?).to eq(class_node.body)
end
end
context 'constant defined as Struct without block' do
let(:src) { 'Person = Struct.new(:name, :age)' }
it 'does not match' do
expect(node.class_definition?).to eq(nil)
end
end
context 'with Class.new' do
let(:src) do
<<~RUBY
Person = Class.new do
a = 2
def details; end
end
RUBY
end
it 'matches' do
class_node = node.children.last
expect(class_node.class_definition?).to eq(class_node.body)
end
end
context 'namespaced class' do
let(:src) do
<<~RUBY
class Foo::Bar::Baz
BAZ = 2
def variables; end
end
RUBY
end
it 'matches' do
expect(node.class_definition?).to eq(node.body)
end
end
context 'with self singleton class' do
let(:src) do
<<~RUBY
class << self
BAZ = 2
def variables; end
end
RUBY
end
it 'matches' do
expect(node.class_definition?).to eq(node.body)
end
end
context 'with object singleton class' do
let(:src) do
<<~RUBY
class << foo
BAZ = 2
def variables; end
end
RUBY
end
it 'matches' do
expect(node.class_definition?).to eq(node.body)
end
end
end
describe '#module_definition?' do
context 'using module keyword' do
let(:src) { 'module Foo; A = 42; end' }
it 'matches' do
expect(node.module_definition?).to eq(node.body)
end
end
context 'with ::ModuleName' do
let(:src) { 'module ::Foo; A = 42; end' }
it 'matches' do
expect(node.module_definition?).to eq(node.body)
end
end
context 'with Module.new' do
let(:src) do
<<~RUBY
Person = Module.new do
a = 2
def details; end
end
RUBY
end
it 'matches' do
module_node = node.children.last
expect(module_node.module_definition?).to eq(module_node.body)
end
end
context 'prepend Module.new' do
let(:src) do
<<~RUBY
prepend(Module.new do
a = 2
def details; end
end)
RUBY
end
it 'matches' do
module_node = node.children.last
expect(module_node.module_definition?).to eq(module_node.body)
end
end
context 'nested modules' do
let(:src) do
<<~RUBY
module Foo
module Bar
BAZ = 2
def variables; end
end
end
RUBY
end
it 'matches' do
expect(node.module_definition?).to eq(node.body)
end
end
context 'namespaced modules' do
let(:src) do
<<~RUBY
module Foo::Bar::Baz
BAZ = 2
def variables; end
end
RUBY
end
it 'matches' do
expect(node.module_definition?).to eq(node.body)
end
end
context 'included module definition' do
let(:src) do
<<~RUBY
include(Module.new do
BAZ = 2
def variables; end
end)
RUBY
end
it 'matches' do
module_node = node.children.last
expect(module_node.module_definition?).to eq(module_node.body)
end
end
end
describe '#parent_module_name' do
subject(:parent_module_name) { node.parent_module_name }
context 'when node on top level' do
let(:src) { 'def config; end' }
it { is_expected.to eq 'Object' }
end
context 'when node on module' do
let(:src) do
<<~RUBY
module Foo
>>attr_reader :config<<
end
RUBY
end
it { is_expected.to eq 'Foo' }
end
context 'when node on singleton class' do
let(:src) do
<<~RUBY
module Foo
class << self
>>attr_reader :config<<
end
end
RUBY
end
it { is_expected.to eq 'Foo::#<Class:Foo>' }
end
context 'when node on class in singleton class' do
let(:src) do
<<~RUBY
module Foo
class << self
class Bar
>>attr_reader :config<<
end
end
end
RUBY
end
it { is_expected.to eq 'Foo::#<Class:Foo>::Bar' }
end
context 'when node nested in an unknown block' do
let(:src) do
<<~RUBY
module Foo
foo do
class Bar
>>attr_reader :config<<
end
end
end
RUBY
end
it { is_expected.to eq nil }
end
context 'when node nested in a class << exp' do
let(:src) do
<<~RUBY
class A
class << expr
>>attr_reader :config<<
end
end
RUBY
end
it { is_expected.to eq nil }
end
end
end