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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
# frozen_string_literal: true
require "yarp_test_helper"
module YARP
class LocationTest < Test::Unit::TestCase
def test_AliasNode
assert_location(AliasNode, "alias foo bar")
end
def test_AlternationPatternNode
assert_location(AlternationPatternNode, "foo => bar | baz", 7...16, &:pattern)
end
def test_AndNode
assert_location(AndNode, "foo and bar")
assert_location(AndNode, "foo && bar")
end
def test_ArgumentsNode
assert_location(ArgumentsNode, "foo(bar, baz, qux)", 4...17, &:arguments)
end
def test_ArrayNode
assert_location(ArrayNode, "[foo, bar, baz]")
assert_location(ArrayNode, "%i[foo bar baz]")
assert_location(ArrayNode, "%I[foo bar baz]")
assert_location(ArrayNode, "%w[foo bar baz]")
assert_location(ArrayNode, "%W[foo bar baz]")
end
def test_ArrayPatternNode
assert_location(ArrayPatternNode, "foo => bar, baz", 7...15, &:pattern)
assert_location(ArrayPatternNode, "foo => [bar, baz]", 7...17, &:pattern)
assert_location(ArrayPatternNode, "foo => *bar", 7...11, &:pattern)
assert_location(ArrayPatternNode, "foo => []", 7...9, &:pattern)
assert_location(ArrayPatternNode, "foo => Foo[]", 7...12, &:pattern)
assert_location(ArrayPatternNode, "foo => Foo[bar]", 7...15, &:pattern)
end
def test_AssocNode
assert_location(AssocNode, "{ foo: :bar }", 2...11) { |node| node.elements.first }
assert_location(AssocNode, "{ :foo => :bar }", 2...14) { |node| node.elements.first }
assert_location(AssocNode, "foo(bar: :baz)", 4...13) { |node| node.arguments.arguments.first.elements.first }
end
def test_AssocSplatNode
assert_location(AssocSplatNode, "{ **foo }", 2...7) { |node| node.elements.first }
assert_location(AssocSplatNode, "foo(**bar)", 4...9) { |node| node.arguments.arguments.first.elements.first }
end
def test_BackReferenceReadNode
assert_location(BackReferenceReadNode, "$+")
end
def test_BeginNode
assert_location(BeginNode, "begin foo end")
assert_location(BeginNode, "begin foo rescue bar end")
assert_location(BeginNode, "begin foo; rescue bar\nelse baz end")
assert_location(BeginNode, "begin foo; rescue bar\nelse baz\nensure qux end")
assert_location(BeginNode, "class Foo\nrescue then end", 10..25, &:statements)
assert_location(BeginNode, "module Foo\nrescue then end", 11..26, &:statements)
end
def test_BlockArgumentNode
assert_location(BlockArgumentNode, "foo(&bar)", 4...8) { |node| node.arguments.arguments.last }
end
def test_BlockNode
assert_location(BlockNode, "foo {}", 4...6, &:block)
assert_location(BlockNode, "foo do end", 4...10, &:block)
end
def test_BlockParameterNode
assert_location(BlockParameterNode, "def foo(&bar) end", 8...12) { |node| node.parameters.block }
end
def test_BlockParametersNode
assert_location(BlockParametersNode, "foo { || }", 6...8) { |node| node.block.parameters }
assert_location(BlockParametersNode, "foo { |bar| baz }", 6...11) { |node| node.block.parameters }
assert_location(BlockParametersNode, "foo { |bar; baz| baz }", 6...16) { |node| node.block.parameters }
assert_location(BlockParametersNode, "-> () {}", 3...5, &:parameters)
assert_location(BlockParametersNode, "-> (bar) { baz }", 3...8, &:parameters)
assert_location(BlockParametersNode, "-> (bar; baz) { baz }", 3...13, &:parameters)
end
def test_BreakNode
assert_location(BreakNode, "break")
assert_location(BreakNode, "break foo")
assert_location(BreakNode, "break foo, bar")
assert_location(BreakNode, "break(foo)")
end
def test_CallNode
assert_location(CallNode, "foo")
assert_location(CallNode, "foo?")
assert_location(CallNode, "foo!")
assert_location(CallNode, "foo()")
assert_location(CallNode, "foo?()")
assert_location(CallNode, "foo!()")
assert_location(CallNode, "foo(bar)")
assert_location(CallNode, "foo?(bar)")
assert_location(CallNode, "foo!(bar)")
assert_location(CallNode, "!foo")
assert_location(CallNode, "~foo")
assert_location(CallNode, "+foo")
assert_location(CallNode, "-foo")
assert_location(CallNode, "not foo")
assert_location(CallNode, "not(foo)")
assert_location(CallNode, "not()")
assert_location(CallNode, "foo + bar")
assert_location(CallNode, "foo -\n bar")
assert_location(CallNode, "Foo()")
assert_location(CallNode, "Foo(bar)")
assert_location(CallNode, "Foo::Bar()")
assert_location(CallNode, "Foo::Bar(baz)")
assert_location(CallNode, "Foo::bar")
assert_location(CallNode, "Foo::bar()")
assert_location(CallNode, "Foo::bar(baz)")
assert_location(CallNode, "Foo.bar")
assert_location(CallNode, "Foo.bar()")
assert_location(CallNode, "Foo.bar(baz)")
assert_location(CallNode, "foo::bar")
assert_location(CallNode, "foo::bar()")
assert_location(CallNode, "foo::bar(baz)")
assert_location(CallNode, "foo.bar")
assert_location(CallNode, "foo.bar()")
assert_location(CallNode, "foo.bar(baz)")
assert_location(CallNode, "foo&.bar")
assert_location(CallNode, "foo&.bar()")
assert_location(CallNode, "foo&.bar(baz)")
assert_location(CallNode, "foo[]")
assert_location(CallNode, "foo[bar]")
assert_location(CallNode, "foo[bar, baz]")
assert_location(CallNode, "foo[] = 1")
assert_location(CallNode, "foo[bar] = 1")
assert_location(CallNode, "foo[bar, baz] = 1")
assert_location(CallNode, "foo.()")
assert_location(CallNode, "foo.(bar)")
assert_location(CallNode, "foo&.()")
assert_location(CallNode, "foo&.(bar)")
assert_location(CallNode, "foo::()")
assert_location(CallNode, "foo::(bar)")
assert_location(CallNode, "foo::(bar, baz)")
assert_location(CallNode, "foo bar baz")
assert_location(CallNode, "foo bar('baz')")
end
def test_CallOperatorAndWriteNode
assert_location(CallOperatorAndWriteNode, "foo.foo &&= bar")
assert_location(CallOperatorAndWriteNode, "foo[foo] &&= bar")
end
def test_CallOperatorWriteNode
assert_location(CallOperatorWriteNode, "foo.foo += bar")
assert_location(CallOperatorWriteNode, "foo[foo] += bar")
end
def test_CallOperatorOrWriteNode
assert_location(CallOperatorOrWriteNode, "foo.foo ||= bar")
assert_location(CallOperatorOrWriteNode, "foo[foo] ||= bar")
end
def test_CapturePatternNode
assert_location(CapturePatternNode, "case foo; in bar => baz; end", 13...23) do |node|
node.conditions.first.pattern
end
end
def test_CaseNode
assert_location(CaseNode, "case foo; when bar; end")
assert_location(CaseNode, "case foo; when bar; else; end")
assert_location(CaseNode, "case foo; when bar; when baz; end")
assert_location(CaseNode, "case foo; when bar; when baz; else; end")
end
def test_ClassNode
assert_location(ClassNode, "class Foo end")
assert_location(ClassNode, "class Foo < Bar end")
end
def test_ClassVariableOperatorAndWriteNode
assert_location(ClassVariableOperatorAndWriteNode, "@@foo &&= bar")
end
def test_ClassVariableOperatorWriteNode
assert_location(ClassVariableOperatorWriteNode, "@@foo += bar")
end
def test_ClassVariableOperatorOrWriteNode
assert_location(ClassVariableOperatorOrWriteNode, "@@foo ||= bar")
end
def test_ClassVariableReadNode
assert_location(ClassVariableReadNode, "@@foo")
end
def test_ClassVariableWriteNode
assert_location(ClassVariableWriteNode, "@@foo = bar")
end
def test_ConstantPathNode
assert_location(ConstantPathNode, "Foo::Bar")
assert_location(ConstantPathNode, "::Foo")
assert_location(ConstantPathNode, "::Foo::Bar")
end
def test_ConstantPathWriteNode
assert_location(ConstantPathWriteNode, "Foo::Bar = baz")
assert_location(ConstantPathWriteNode, "::Foo = bar")
assert_location(ConstantPathWriteNode, "::Foo::Bar = baz")
end
def test_ConstantPathOperatorAndWriteNode
assert_location(ConstantPathOperatorAndWriteNode, "Parent::Child &&= bar")
end
def test_ConstantPathOperatorWriteNode
assert_location(ConstantPathOperatorWriteNode, "Parent::Child += bar")
end
def test_ConstantPathOperatorOrWriteNode
assert_location(ConstantPathOperatorOrWriteNode, "Parent::Child ||= bar")
end
def test_ConstantOperatorAndWriteNode
assert_location(ConstantOperatorAndWriteNode, "Foo &&= bar")
end
def test_ConstantOperatorWriteNode
assert_location(ConstantOperatorWriteNode, "Foo += bar")
end
def test_ConstantOperatorOrWriteNode
assert_location(ConstantOperatorOrWriteNode, "Foo ||= bar")
end
def test_ConstantReadNode
assert_location(ConstantReadNode, "Foo")
assert_location(ConstantReadNode, "Foo::Bar", 5...8, &:child)
end
def test_DefNode
assert_location(DefNode, "def foo; bar; end")
assert_location(DefNode, "def foo = bar")
assert_location(DefNode, "def foo.bar; baz; end")
assert_location(DefNode, "def foo.bar = baz")
end
def test_DefinedNode
assert_location(DefinedNode, "defined? foo")
assert_location(DefinedNode, "defined?(foo)")
end
def test_ElseNode
assert_location(ElseNode, "if foo; bar; else; baz; end", 13...27, &:consequent)
assert_location(ElseNode, "foo ? bar : baz", 10...15, &:consequent)
end
def test_EmbeddedStatementsNode
assert_location(EmbeddedStatementsNode, '"foo #{bar} baz"', 5...11) { |node| node.parts[1] }
end
def test_EmbeddedVariableNode
assert_location(EmbeddedVariableNode, '"foo #@@bar baz"', 5...11) { |node| node.parts[1] }
end
def test_EnsureNode
assert_location(EnsureNode, "begin; foo; ensure; bar; end", 12...28, &:ensure_clause)
end
def test_FalseNode
assert_location(FalseNode, "false")
end
def test_FindPatternNode
assert_location(FindPatternNode, "case foo; in *, bar, *; end", 13...22) do |node|
node.conditions.first.pattern
end
end
def test_FloatNode
assert_location(FloatNode, "0.0")
assert_location(FloatNode, "1.0")
assert_location(FloatNode, "1.0e10")
assert_location(FloatNode, "1.0e-10")
end
def test_ForNode
assert_location(ForNode, "for foo in bar; end")
assert_location(ForNode, "for foo, bar in baz do end")
end
def test_ForwardingArgumentsNode
assert_location(ForwardingArgumentsNode, "def foo(...); bar(...); end", 18...21) do |node|
node.statements.body.first.arguments.arguments.first
end
end
def test_ForwardingParameterNode
assert_location(ForwardingParameterNode, "def foo(...); end", 8...11) do |node|
node.parameters.keyword_rest
end
end
def test_ForwardingSuperNode
assert_location(ForwardingSuperNode, "super")
assert_location(ForwardingSuperNode, "super {}")
end
def test_GlobalVariableOperatorAndWriteNode
assert_location(GlobalVariableOperatorAndWriteNode, "$foo &&= bar")
end
def test_GlobalVariableOperatorWriteNode
assert_location(GlobalVariableOperatorWriteNode, "$foo += bar")
end
def test_GlobalVariableOperatorOrWriteNode
assert_location(GlobalVariableOperatorOrWriteNode, "$foo ||= bar")
end
def test_GlobalVariableReadNode
assert_location(GlobalVariableReadNode, "$foo")
end
def test_GlobalVariableWriteNode
assert_location(GlobalVariableWriteNode, "$foo = bar")
end
def test_HashNode
assert_location(HashNode, "{ foo: 2 }")
assert_location(HashNode, "{ \nfoo: 2, \nbar: 3 \n}")
end
def test_HashPatternNode
assert_location(HashPatternNode, "case foo; in bar: baz; end", 13...21) do |node|
node.conditions.first.pattern
end
end
def test_IfNode
assert_location(IfNode, "if type in 1;elsif type in B;end")
end
def test_ImaginaryNode
assert_location(ImaginaryNode, "1i")
assert_location(ImaginaryNode, "1ri")
end
def test_InNode
assert_location(InNode, "case foo; in bar; end", 10...16) do |node|
node.conditions.first
end
end
def test_InstanceVariableOperatorAndWriteNode
assert_location(InstanceVariableOperatorAndWriteNode, "@foo &&= bar")
end
def test_InstanceVariableOperatorWriteNode
assert_location(InstanceVariableOperatorWriteNode, "@foo += bar")
end
def test_InstanceVariableOperatorOrWriteNode
assert_location(InstanceVariableOperatorOrWriteNode, "@foo ||= bar")
end
def test_InstanceVariableReadNode
assert_location(InstanceVariableReadNode, "@foo")
end
def test_InstanceVariableWriteNode
assert_location(InstanceVariableWriteNode, "@foo = bar")
end
def test_IntegerNode
assert_location(IntegerNode, "0")
assert_location(IntegerNode, "1")
assert_location(IntegerNode, "1_000")
assert_location(IntegerNode, "0x1")
assert_location(IntegerNode, "0x1_000")
assert_location(IntegerNode, "0b1")
assert_location(IntegerNode, "0b1_000")
assert_location(IntegerNode, "0o1")
assert_location(IntegerNode, "0o1_000")
end
def test_InterpolatedRegularExpressionNode
assert_location(InterpolatedRegularExpressionNode, "/\#{foo}/")
end
def test_InterpolatedStringNode
assert_location(InterpolatedStringNode, "\"foo \#@bar baz\"")
assert_location(InterpolatedStringNode, "<<~A\nhello world\nA", 0...4)
end
def test_InterpolatedSymbolNode
assert_location(InterpolatedSymbolNode, ':"#{foo}bar"')
end
def test_InterpolatedXStringNode
assert_location(InterpolatedXStringNode, '`foo #{bar} baz`')
end
def test_KeywordHashNode
assert_location(KeywordHashNode, "foo(a, b: 1)", 7...11) { |node| node.arguments.arguments[1] }
end
def test_KeywordParameterNode
assert_location(KeywordParameterNode, "def foo(bar:); end", 8...12) do |node|
node.parameters.keywords.first
end
assert_location(KeywordParameterNode, "def foo(bar: nil); end", 8...16) do |node|
node.parameters.keywords.first
end
end
def test_KeywordRestParameterNode
assert_location(KeywordRestParameterNode, "def foo(**); end", 8...10) do |node|
node.parameters.keyword_rest
end
assert_location(KeywordRestParameterNode, "def foo(**bar); end", 8...13) do |node|
node.parameters.keyword_rest
end
end
def test_LambdaNode
assert_location(LambdaNode, "-> { foo }")
assert_location(LambdaNode, "-> do foo end")
end
def test_LocalVariableOperatorAndWriteNode
assert_location(LocalVariableOperatorAndWriteNode, "foo &&= bar")
assert_location(LocalVariableOperatorAndWriteNode, "foo = 1; foo &&= bar", 9...20)
end
def test_LocalVariableOperatorWriteNode
assert_location(LocalVariableOperatorWriteNode, "foo += bar")
assert_location(LocalVariableOperatorWriteNode, "foo = 1; foo += bar", 9...19)
end
def test_LocalVariableOperatorOrWriteNode
assert_location(LocalVariableOperatorOrWriteNode, "foo ||= bar")
assert_location(LocalVariableOperatorOrWriteNode, "foo = 1; foo ||= bar", 9...20)
end
def test_LocalVariableReadNode
assert_location(LocalVariableReadNode, "foo = 1; foo", 9...12)
end
def test_LocalVariableWriteNode
assert_location(LocalVariableWriteNode, "foo = bar")
end
def test_MatchPredicateNode
assert_location(MatchPredicateNode, "foo in bar")
end
def test_MatchRequiredNode
assert_location(MatchRequiredNode, "foo => bar")
end
def test_ModuleNode
assert_location(ModuleNode, "module Foo end")
end
def test_MultiWriteNode
assert_location(MultiWriteNode, "foo, bar = baz")
end
def test_NextNode
assert_location(NextNode, "next")
assert_location(NextNode, "next foo")
assert_location(NextNode, "next foo, bar")
assert_location(NextNode, "next(foo)")
end
def test_NilNode
assert_location(NilNode, "nil")
end
def test_NoKeywordsParameterNode
assert_location(NoKeywordsParameterNode, "def foo(**nil); end", 8...13) { |node| node.parameters.keyword_rest }
end
def test_NumberedReferenceReadNode
assert_location(NumberedReferenceReadNode, "$1")
end
def test_OptionalParameterNode
assert_location(OptionalParameterNode, "def foo(bar = nil); end", 8...17) do |node|
node.parameters.optionals.first
end
end
def test_OrNode
assert_location(OrNode, "foo || bar")
assert_location(OrNode, "foo or bar")
end
def test_ParametersNode
assert_location(ParametersNode, "def foo(bar, baz); end", 8...16, &:parameters)
end
def test_ParenthesesNode
assert_location(ParenthesesNode, "()")
assert_location(ParenthesesNode, "(foo)")
assert_location(ParenthesesNode, "foo (bar), baz", 4...9) { |node| node.arguments.arguments.first }
assert_location(ParenthesesNode, "def (foo).bar; end", 4...9, &:receiver)
end
def test_PinnedExpressionNode
assert_location(PinnedExpressionNode, "foo in ^(bar)", 7...13, &:pattern)
end
def test_PinnedVariableNode
assert_location(PinnedVariableNode, "foo in ^bar", 7...11, &:pattern)
end
def test_PostExecutionNode
assert_location(PostExecutionNode, "END {}")
assert_location(PostExecutionNode, "END { foo }")
end
def test_PreExecutionNode
assert_location(PreExecutionNode, "BEGIN {}")
assert_location(PreExecutionNode, "BEGIN { foo }")
end
def test_RangeNode
assert_location(RangeNode, "1..2")
assert_location(RangeNode, "1...2")
assert_location(RangeNode, "..2")
assert_location(RangeNode, "...2")
assert_location(RangeNode, "1..")
assert_location(RangeNode, "1...")
end
def test_RationalNode
assert_location(RationalNode, "1r")
assert_location(RationalNode, "1.0r")
end
def test_RedoNode
assert_location(RedoNode, "redo")
end
def test_RegularExpressionNode
assert_location(RegularExpressionNode, "/foo/")
end
def test_RequiredParameterNode
assert_location(RequiredParameterNode, "def foo(bar); end", 8...11) do |node|
node.parameters.requireds.first
end
end
def test_RequiredDestructuredParameterNode
assert_location(RequiredDestructuredParameterNode, "def foo((bar)); end", 8...13) do |node|
node.parameters.requireds.first
end
end
def test_RescueNode
code = <<~RUBY
begin
body
rescue TypeError
rescue ArgumentError
end
RUBY
assert_location(RescueNode, code, 13...50) { |node| node.rescue_clause }
assert_location(RescueNode, code, 30...50) { |node| node.rescue_clause.consequent }
end
def test_RescueModifierNode
assert_location(RescueModifierNode, "foo rescue bar")
end
def test_RestParameterNode
assert_location(RestParameterNode, "def foo(*bar); end", 8...12) do |node|
node.parameters.rest
end
end
def test_RetryNode
assert_location(RetryNode, "retry")
end
def test_ReturnNode
assert_location(ReturnNode, "return")
assert_location(ReturnNode, "return foo")
assert_location(ReturnNode, "return foo, bar")
assert_location(ReturnNode, "return(foo)")
end
def test_SelfNode
assert_location(SelfNode, "self")
end
def test_SingletonClassNode
assert_location(SingletonClassNode, "class << self; end")
end
def test_SourceEncodingNode
assert_location(SourceEncodingNode, "__ENCODING__")
end
def test_SourceFileNode
assert_location(SourceFileNode, "__FILE__")
end
def test_SourceLineNode
assert_location(SourceLineNode, "__LINE__")
end
def test_SplatNode
assert_location(SplatNode, "*foo = bar", 0...4) { |node| node.targets.first }
end
def test_StatementsNode
assert_location(StatementsNode, "foo { 1 }", 6...7) { |node| node.block.statements }
assert_location(StatementsNode, "(1)", 1...2, &:statements)
assert_location(StatementsNode, "def foo; 1; end", 9...10, &:statements)
assert_location(StatementsNode, "def foo = 1", 10...11, &:statements)
assert_location(StatementsNode, "def foo; 1\n2; end", 9...12, &:statements)
assert_location(StatementsNode, "if foo; bar; end", 8...11, &:statements)
assert_location(StatementsNode, "foo if bar", 0...3, &:statements)
assert_location(StatementsNode, "if foo; foo; elsif bar; bar; end", 24...27) { |node| node.consequent.statements }
assert_location(StatementsNode, "if foo; foo; else; bar; end", 19...22) { |node| node.consequent.statements }
assert_location(StatementsNode, "unless foo; bar; end", 12...15, &:statements)
assert_location(StatementsNode, "foo unless bar", 0...3, &:statements)
assert_location(StatementsNode, "case; when foo; bar; end", 16...19) { |node| node.conditions.first.statements }
assert_location(StatementsNode, "while foo; bar; end", 11...14, &:statements)
assert_location(StatementsNode, "foo while bar", 0...3, &:statements)
assert_location(StatementsNode, "until foo; bar; end", 11...14, &:statements)
assert_location(StatementsNode, "foo until bar", 0...3, &:statements)
assert_location(StatementsNode, "for foo in bar; baz; end", 16...19, &:statements)
assert_location(StatementsNode, "begin; foo; end", 7...10, &:statements)
assert_location(StatementsNode, "begin; rescue; foo; end", 15...18) { |node| node.rescue_clause.statements }
assert_location(StatementsNode, "begin; ensure; foo; end", 15...18) { |node| node.ensure_clause.statements }
assert_location(StatementsNode, "begin; rescue; else; foo; end", 21...24) { |node| node.else_clause.statements }
assert_location(StatementsNode, "class Foo; foo; end", 11...14, &:statements)
assert_location(StatementsNode, "module Foo; foo; end", 12...15, &:statements)
assert_location(StatementsNode, "class << self; foo; end", 15...18, &:statements)
assert_location(StatementsNode, "-> { foo }", 5...8, &:statements)
assert_location(StatementsNode, "BEGIN { foo }", 8...11, &:statements)
assert_location(StatementsNode, "END { foo }", 6...9, &:statements)
assert_location(StatementsNode, "\"\#{foo}\"", 3...6) { |node| node.parts.first.statements }
end
def test_StringConcatNode
assert_location(StringConcatNode, '"foo" "bar"')
end
def test_StringNode
assert_location(StringNode, '"foo"')
assert_location(StringNode, '%q[foo]')
end
def test_SuperNode
assert_location(SuperNode, "super foo")
assert_location(SuperNode, "super foo, bar")
assert_location(SuperNode, "super()")
assert_location(SuperNode, "super(foo)")
assert_location(SuperNode, "super(foo, bar)")
assert_location(SuperNode, "super() {}")
end
def test_SymbolNode
assert_location(SymbolNode, ":foo")
end
def test_TrueNode
assert_location(TrueNode, "true")
end
def test_UndefNode
assert_location(UndefNode, "undef foo")
assert_location(UndefNode, "undef foo, bar")
end
def test_UnlessNode
assert_location(UnlessNode, "foo unless bar")
assert_location(UnlessNode, "unless bar; foo; end")
end
def test_UntilNode
assert_location(UntilNode, "foo = bar until baz")
assert_location(UntilNode, "until bar;baz;end")
end
def test_WhenNode
assert_location(WhenNode, "case foo; when bar; end", 10...18) { |node| node.conditions.first }
end
def test_WhileNode
assert_location(WhileNode, "foo = bar while foo != baz")
assert_location(WhileNode, "while a;bar;baz;end")
end
def test_XStringNode
assert_location(XStringNode, "`foo`")
assert_location(XStringNode, "%x[foo]")
end
def test_YieldNode
assert_location(YieldNode, "yield")
assert_location(YieldNode, "yield foo")
assert_location(YieldNode, "yield foo, bar")
assert_location(YieldNode, "yield(foo)")
end
private
def assert_location(kind, source, expected = 0...source.length)
result = YARP.parse(source)
assert_equal [], result.comments
assert_equal [], result.errors
node = result.value.statements.body.last
node = yield node if block_given?
assert_kind_of kind, node
assert_equal expected.begin, node.location.start_offset
assert_equal expected.end, node.location.end_offset
end
end
end
|