ruby-lsp 0.26.5 → 0.26.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,790 +0,0 @@
1
- # typed: true
2
- # frozen_string_literal: true
3
-
4
- require_relative "test_case"
5
-
6
- module RubyIndexer
7
- class ClassesAndModulesTest < TestCase
8
- def test_empty_statements_class
9
- index(<<~RUBY)
10
- class Foo
11
- end
12
- RUBY
13
-
14
- assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
15
- end
16
-
17
- def test_conditional_class
18
- index(<<~RUBY)
19
- class Foo
20
- end if condition
21
- RUBY
22
-
23
- assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
24
- end
25
-
26
- def test_class_with_statements
27
- index(<<~RUBY)
28
- class Foo
29
- def something; end
30
- end
31
- RUBY
32
-
33
- assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:2-3")
34
- end
35
-
36
- def test_colon_colon_class
37
- index(<<~RUBY)
38
- class ::Foo
39
- end
40
- RUBY
41
-
42
- assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
43
- end
44
-
45
- def test_colon_colon_class_inside_class
46
- index(<<~RUBY)
47
- class Bar
48
- class ::Foo
49
- end
50
- end
51
- RUBY
52
-
53
- assert_entry("Bar", Entry::Class, "/fake/path/foo.rb:0-0:3-3")
54
- assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:1-2:2-5")
55
- end
56
-
57
- def test_namespaced_class
58
- index(<<~RUBY)
59
- class Foo::Bar
60
- end
61
- RUBY
62
-
63
- assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
64
- end
65
-
66
- def test_dynamically_namespaced_class
67
- index(<<~RUBY)
68
- class self::Bar
69
- end
70
- RUBY
71
-
72
- assert_entry("self::Bar", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
73
- end
74
-
75
- def test_dynamically_namespaced_class_does_not_affect_other_classes
76
- index(<<~RUBY)
77
- class Foo
78
- class self::Bar
79
- end
80
-
81
- class Bar
82
- end
83
- end
84
- RUBY
85
-
86
- refute_entry("self::Bar")
87
- assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:6-3")
88
- assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:4-2:5-5")
89
- end
90
-
91
- def test_empty_statements_module
92
- index(<<~RUBY)
93
- module Foo
94
- end
95
- RUBY
96
-
97
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
98
- end
99
-
100
- def test_conditional_module
101
- index(<<~RUBY)
102
- module Foo
103
- end if condition
104
- RUBY
105
-
106
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
107
- end
108
-
109
- def test_module_with_statements
110
- index(<<~RUBY)
111
- module Foo
112
- def something; end
113
- end
114
- RUBY
115
-
116
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:2-3")
117
- end
118
-
119
- def test_colon_colon_module
120
- index(<<~RUBY)
121
- module ::Foo
122
- end
123
- RUBY
124
-
125
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
126
- end
127
-
128
- def test_namespaced_module
129
- index(<<~RUBY)
130
- module Foo::Bar
131
- end
132
- RUBY
133
-
134
- assert_entry("Foo::Bar", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
135
- end
136
-
137
- def test_dynamically_namespaced_module
138
- index(<<~RUBY)
139
- module self::Bar
140
- end
141
- RUBY
142
-
143
- assert_entry("self::Bar", Entry::Module, "/fake/path/foo.rb:0-0:1-3")
144
- end
145
-
146
- def test_dynamically_namespaced_module_does_not_affect_other_modules
147
- index(<<~RUBY)
148
- module Foo
149
- class self::Bar
150
- end
151
-
152
- module Bar
153
- end
154
- end
155
- RUBY
156
-
157
- assert_entry("Foo::self::Bar", Entry::Class, "/fake/path/foo.rb:1-2:2-5")
158
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:6-3")
159
- assert_entry("Foo::Bar", Entry::Module, "/fake/path/foo.rb:4-2:5-5")
160
- end
161
-
162
- def test_nested_modules_and_classes_with_multibyte_characters
163
- index(<<~RUBY)
164
- module A動物
165
- class Bねこ; end
166
- end
167
- RUBY
168
-
169
- assert_entry("A動物", Entry::Module, "/fake/path/foo.rb:0-0:2-3")
170
- assert_entry("A動物::Bねこ", Entry::Class, "/fake/path/foo.rb:1-2:1-16")
171
- end
172
-
173
- def test_nested_modules_and_classes
174
- index(<<~RUBY)
175
- module Foo
176
- class Bar
177
- end
178
-
179
- module Baz
180
- class Qux
181
- class Something
182
- end
183
- end
184
- end
185
- end
186
- RUBY
187
-
188
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:10-3")
189
- assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:1-2:2-5")
190
- assert_entry("Foo::Baz", Entry::Module, "/fake/path/foo.rb:4-2:9-5")
191
- assert_entry("Foo::Baz::Qux", Entry::Class, "/fake/path/foo.rb:5-4:8-7")
192
- assert_entry("Foo::Baz::Qux::Something", Entry::Class, "/fake/path/foo.rb:6-6:7-9")
193
- end
194
-
195
- def test_deleting_from_index_based_on_file_path
196
- index(<<~RUBY)
197
- class Foo
198
- end
199
- RUBY
200
-
201
- assert_entry("Foo", Entry::Class, "/fake/path/foo.rb:0-0:1-3")
202
-
203
- @index.delete(URI::Generic.from_path(path: "/fake/path/foo.rb"))
204
- refute_entry("Foo")
205
-
206
- assert_no_indexed_entries
207
- end
208
-
209
- def test_comments_can_be_attached_to_a_class
210
- index(<<~RUBY)
211
- # This is method comment
212
- def foo; end
213
- # This is a Foo comment
214
- # This is another Foo comment
215
- class Foo
216
- # This should not be attached
217
- end
218
-
219
- # Ignore me
220
-
221
- # This Bar comment has 1 line padding
222
-
223
- class Bar; end
224
- RUBY
225
-
226
- foo_entry = @index["Foo"] #: as !nil
227
- .first #: as !nil
228
- assert_equal("This is a Foo comment\nThis is another Foo comment", foo_entry.comments)
229
-
230
- bar_entry = @index["Bar"] #: as !nil
231
- .first #: as !nil
232
- assert_equal("This Bar comment has 1 line padding", bar_entry.comments)
233
- end
234
-
235
- def test_skips_comments_containing_invalid_encodings
236
- index(<<~RUBY)
237
- # comment \xBA
238
- class Foo
239
- end
240
- RUBY
241
- assert(@index["Foo"]&.first)
242
- end
243
-
244
- def test_comments_can_be_attached_to_a_namespaced_class
245
- index(<<~RUBY)
246
- # This is a Foo comment
247
- # This is another Foo comment
248
- class Foo
249
- # This is a Bar comment
250
- class Bar; end
251
- end
252
- RUBY
253
-
254
- foo_entry = @index["Foo"] #: as !nil
255
- .first #: as !nil
256
- assert_equal("This is a Foo comment\nThis is another Foo comment", foo_entry.comments)
257
-
258
- bar_entry = @index["Foo::Bar"] #: as !nil
259
- .first #: as !nil
260
- assert_equal("This is a Bar comment", bar_entry.comments)
261
- end
262
-
263
- def test_comments_can_be_attached_to_a_reopened_class
264
- index(<<~RUBY)
265
- # This is a Foo comment
266
- class Foo; end
267
-
268
- # This is another Foo comment
269
- class Foo; end
270
- RUBY
271
-
272
- first_foo_entry, second_foo_entry = @index["Foo"] #: as !nil
273
- assert_equal("This is a Foo comment", first_foo_entry&.comments)
274
- assert_equal("This is another Foo comment", second_foo_entry&.comments)
275
- end
276
-
277
- def test_comments_removes_the_leading_pound_and_space
278
- index(<<~RUBY)
279
- # This is a Foo comment
280
- class Foo; end
281
-
282
- #This is a Bar comment
283
- class Bar; end
284
- RUBY
285
-
286
- first_foo_entry = @index["Foo"] #: as !nil
287
- .first #: as !nil
288
- assert_equal("This is a Foo comment", first_foo_entry.comments)
289
-
290
- second_foo_entry = @index["Bar"] #: as !nil
291
- .first #: as !nil
292
- assert_equal("This is a Bar comment", second_foo_entry.comments)
293
- end
294
-
295
- def test_private_class_and_module_indexing
296
- index(<<~RUBY)
297
- class A
298
- class B; end
299
- private_constant(:B)
300
-
301
- module C; end
302
- private_constant("C")
303
-
304
- class D; end
305
- end
306
- RUBY
307
-
308
- b_const = @index["A::B"] #: as !nil
309
- .first
310
- assert_predicate(b_const, :private?)
311
-
312
- c_const = @index["A::C"] #: as !nil
313
- .first
314
- assert_predicate(c_const, :private?)
315
-
316
- d_const = @index["A::D"] #: as !nil
317
- .first
318
- assert_predicate(d_const, :public?)
319
- end
320
-
321
- def test_keeping_track_of_super_classes
322
- index(<<~RUBY)
323
- class Foo < Bar
324
- end
325
-
326
- class Baz
327
- end
328
-
329
- module Something
330
- class Baz
331
- end
332
-
333
- class Qux < ::Baz
334
- end
335
- end
336
-
337
- class FinalThing < Something::Baz
338
- end
339
- RUBY
340
-
341
- foo = @index["Foo"] #: as !nil
342
- .first #: as Entry::Class
343
- assert_equal("Bar", foo.parent_class)
344
-
345
- baz = @index["Baz"] #: as !nil
346
- .first #: as Entry::Class
347
- assert_equal("::Object", baz.parent_class)
348
-
349
- qux = @index["Something::Qux"] #: as !nil
350
- .first #: as Entry::Class
351
- assert_equal("::Baz", qux.parent_class)
352
-
353
- final_thing = @index["FinalThing"] #: as !nil
354
- .first #: as Entry::Class
355
- assert_equal("Something::Baz", final_thing.parent_class)
356
- end
357
-
358
- def test_keeping_track_of_included_modules
359
- index(<<~RUBY)
360
- class Foo
361
- # valid syntaxes that we can index
362
- include A1
363
- self.include A2
364
- include A3, A4
365
- self.include A5, A6
366
-
367
- # valid syntaxes that we cannot index because of their dynamic nature
368
- include some_variable_or_method_call
369
- self.include some_variable_or_method_call
370
-
371
- def something
372
- include A7 # We should not index this because of this dynamic nature
373
- end
374
-
375
- # Valid inner class syntax definition with its own modules included
376
- class Qux
377
- include Corge
378
- self.include Corge
379
- include Baz
380
-
381
- include some_variable_or_method_call
382
- end
383
- end
384
-
385
- class ConstantPathReferences
386
- include Foo::Bar
387
- self.include Foo::Bar2
388
-
389
- include dynamic::Bar
390
- include Foo::
391
- end
392
- RUBY
393
-
394
- foo = @index["Foo"] #: as !nil
395
- .first #: as Entry::Class
396
- assert_equal(["A1", "A2", "A3", "A4", "A5", "A6"], foo.mixin_operation_module_names)
397
-
398
- qux = @index["Foo::Qux"] #: as !nil
399
- .first #: as Entry::Class
400
- assert_equal(["Corge", "Corge", "Baz"], qux.mixin_operation_module_names)
401
-
402
- constant_path_references = @index["ConstantPathReferences"] #: as !nil
403
- .first #: as Entry::Class
404
- assert_equal(["Foo::Bar", "Foo::Bar2"], constant_path_references.mixin_operation_module_names)
405
- end
406
-
407
- def test_keeping_track_of_prepended_modules
408
- index(<<~RUBY)
409
- class Foo
410
- # valid syntaxes that we can index
411
- prepend A1
412
- self.prepend A2
413
- prepend A3, A4
414
- self.prepend A5, A6
415
-
416
- # valid syntaxes that we cannot index because of their dynamic nature
417
- prepend some_variable_or_method_call
418
- self.prepend some_variable_or_method_call
419
-
420
- def something
421
- prepend A7 # We should not index this because of this dynamic nature
422
- end
423
-
424
- # Valid inner class syntax definition with its own modules prepended
425
- class Qux
426
- prepend Corge
427
- self.prepend Corge
428
- prepend Baz
429
-
430
- prepend some_variable_or_method_call
431
- end
432
- end
433
-
434
- class ConstantPathReferences
435
- prepend Foo::Bar
436
- self.prepend Foo::Bar2
437
-
438
- prepend dynamic::Bar
439
- prepend Foo::
440
- end
441
- RUBY
442
-
443
- foo = @index["Foo"] #: as !nil
444
- .first #: as Entry::Class
445
- assert_equal(["A1", "A2", "A3", "A4", "A5", "A6"], foo.mixin_operation_module_names)
446
-
447
- qux = @index["Foo::Qux"] #: as !nil
448
- .first #: as Entry::Class
449
- assert_equal(["Corge", "Corge", "Baz"], qux.mixin_operation_module_names)
450
-
451
- constant_path_references = @index["ConstantPathReferences"] #: as !nil
452
- .first #: as Entry::Class
453
- assert_equal(["Foo::Bar", "Foo::Bar2"], constant_path_references.mixin_operation_module_names)
454
- end
455
-
456
- def test_keeping_track_of_extended_modules
457
- index(<<~RUBY)
458
- class Foo
459
- # valid syntaxes that we can index
460
- extend A1
461
- self.extend A2
462
- extend A3, A4
463
- self.extend A5, A6
464
-
465
- # valid syntaxes that we cannot index because of their dynamic nature
466
- extend some_variable_or_method_call
467
- self.extend some_variable_or_method_call
468
-
469
- def something
470
- extend A7 # We should not index this because of this dynamic nature
471
- end
472
-
473
- # Valid inner class syntax definition with its own modules prepended
474
- class Qux
475
- extend Corge
476
- self.extend Corge
477
- extend Baz
478
-
479
- extend some_variable_or_method_call
480
- end
481
- end
482
-
483
- class ConstantPathReferences
484
- extend Foo::Bar
485
- self.extend Foo::Bar2
486
-
487
- extend dynamic::Bar
488
- extend Foo::
489
- end
490
- RUBY
491
-
492
- foo = @index["Foo::<Class:Foo>"] #: as !nil
493
- .first #: as Entry::Class
494
- assert_equal(["A1", "A2", "A3", "A4", "A5", "A6"], foo.mixin_operation_module_names)
495
-
496
- qux = @index["Foo::Qux::<Class:Qux>"] #: as !nil
497
- .first #: as Entry::Class
498
- assert_equal(["Corge", "Corge", "Baz"], qux.mixin_operation_module_names)
499
-
500
- constant_path_references = @index["ConstantPathReferences::<Class:ConstantPathReferences>"] #: as !nil
501
- .first #: as Entry::Class
502
- assert_equal(["Foo::Bar", "Foo::Bar2"], constant_path_references.mixin_operation_module_names)
503
- end
504
-
505
- def test_tracking_singleton_classes
506
- index(<<~RUBY)
507
- class Foo; end
508
- class Foo
509
- # Some extra comments
510
- class << self
511
- end
512
- end
513
- RUBY
514
-
515
- foo = @index["Foo::<Class:Foo>"] #: as !nil
516
- .first #: as Entry::SingletonClass
517
- assert_equal(4, foo.location.start_line)
518
- assert_equal("Some extra comments", foo.comments)
519
- end
520
-
521
- def test_dynamic_singleton_class_blocks
522
- index(<<~RUBY)
523
- class Foo
524
- # Some extra comments
525
- class << bar
526
- end
527
- end
528
- RUBY
529
-
530
- singleton = @index["Foo::<Class:bar>"] #: as !nil
531
- .first #: as Entry::SingletonClass
532
-
533
- # Even though this is not correct, we consider any dynamic singleton class block as a regular singleton class.
534
- # That pattern cannot be properly analyzed statically and assuming that it's always a regular singleton simplifies
535
- # the implementation considerably.
536
- assert_equal(3, singleton.location.start_line)
537
- assert_equal("Some extra comments", singleton.comments)
538
- end
539
-
540
- def test_namespaces_inside_singleton_blocks
541
- index(<<~RUBY)
542
- class Foo
543
- class << self
544
- class Bar
545
- end
546
- end
547
- end
548
- RUBY
549
-
550
- assert_entry("Foo::<Class:Foo>::Bar", Entry::Class, "/fake/path/foo.rb:2-4:3-7")
551
- end
552
-
553
- def test_name_location_points_to_constant_path_location
554
- index(<<~RUBY)
555
- class Foo
556
- def foo; end
557
- end
558
-
559
- module Bar
560
- def bar; end
561
- end
562
- RUBY
563
-
564
- foo = @index["Foo"] #: as !nil
565
- .first #: as Entry::Class
566
- refute_equal(foo.location, foo.name_location)
567
-
568
- name_location = foo.name_location
569
- assert_equal(1, name_location.start_line)
570
- assert_equal(1, name_location.end_line)
571
- assert_equal(6, name_location.start_column)
572
- assert_equal(9, name_location.end_column)
573
-
574
- bar = @index["Bar"] #: as !nil
575
- .first #: as Entry::Module
576
- refute_equal(bar.location, bar.name_location)
577
-
578
- name_location = bar.name_location
579
- assert_equal(5, name_location.start_line)
580
- assert_equal(5, name_location.end_line)
581
- assert_equal(7, name_location.start_column)
582
- assert_equal(10, name_location.end_column)
583
- end
584
-
585
- def test_indexing_namespaces_inside_top_level_references
586
- index(<<~RUBY)
587
- module ::Foo
588
- class Bar
589
- end
590
- end
591
- RUBY
592
-
593
- # We want to explicitly verify that we didn't introduce the leading `::` by accident, but `Index#[]` deletes the
594
- # prefix when we use `refute_entry`
595
- entries = @index.instance_variable_get(:@entries)
596
- refute(entries.key?("::Foo"))
597
- refute(entries.key?("::Foo::Bar"))
598
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:3-3")
599
- assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:1-2:2-5")
600
- end
601
-
602
- def test_indexing_singletons_inside_top_level_references
603
- index(<<~RUBY)
604
- module ::Foo
605
- class Bar
606
- class << self
607
- end
608
- end
609
- end
610
- RUBY
611
-
612
- # We want to explicitly verify that we didn't introduce the leading `::` by accident, but `Index#[]` deletes the
613
- # prefix when we use `refute_entry`
614
- entries = @index.instance_variable_get(:@entries)
615
- refute(entries.key?("::Foo"))
616
- refute(entries.key?("::Foo::Bar"))
617
- refute(entries.key?("::Foo::Bar::<Class:Bar>"))
618
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:0-0:5-3")
619
- assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:1-2:4-5")
620
- assert_entry("Foo::Bar::<Class:Bar>", Entry::SingletonClass, "/fake/path/foo.rb:2-4:3-7")
621
- end
622
-
623
- def test_indexing_namespaces_inside_nested_top_level_references
624
- index(<<~RUBY)
625
- class Baz
626
- module ::Foo
627
- class Bar
628
- end
629
-
630
- class ::Qux
631
- end
632
- end
633
- end
634
- RUBY
635
-
636
- refute_entry("Baz::Foo")
637
- refute_entry("Baz::Foo::Bar")
638
- assert_entry("Baz", Entry::Class, "/fake/path/foo.rb:0-0:8-3")
639
- assert_entry("Foo", Entry::Module, "/fake/path/foo.rb:1-2:7-5")
640
- assert_entry("Foo::Bar", Entry::Class, "/fake/path/foo.rb:2-4:3-7")
641
- assert_entry("Qux", Entry::Class, "/fake/path/foo.rb:5-4:6-7")
642
- end
643
-
644
- def test_lazy_comment_fetching_uses_correct_line_breaks_for_rendering
645
- uri = URI::Generic.from_path(
646
- load_path_entry: "#{Dir.pwd}/lib",
647
- path: "#{Dir.pwd}/lib/ruby_lsp/node_context.rb",
648
- )
649
-
650
- @index.index_file(uri, collect_comments: false)
651
-
652
- entry = @index["RubyLsp::NodeContext"] #: as !nil
653
- .first #: as !nil
654
-
655
- assert_equal(<<~COMMENTS.chomp, entry.comments)
656
- This class allows listeners to access contextual information about a node in the AST, such as its parent,
657
- its namespace nesting, and the surrounding CallNode (e.g. a method call).
658
- COMMENTS
659
- end
660
-
661
- def test_lazy_comment_fetching_does_not_fail_if_file_gets_deleted
662
- uri = URI::Generic.from_path(
663
- load_path_entry: "#{Dir.pwd}/lib",
664
- path: "#{Dir.pwd}/lib/ruby_lsp/does_not_exist.rb",
665
- )
666
-
667
- @index.index_single(uri, <<~RUBY, collect_comments: false)
668
- class Foo
669
- end
670
- RUBY
671
-
672
- entry = @index["Foo"]&.first #: as !nil
673
- assert_empty(entry.comments)
674
- end
675
-
676
- def test_singleton_inside_compact_namespace
677
- index(<<~RUBY)
678
- module Foo::Bar
679
- class << self
680
- def baz; end
681
- end
682
- end
683
- RUBY
684
-
685
- # Verify we didn't index the incorrect name
686
- assert_nil(@index["Foo::Bar::<Class:Foo::Bar>"])
687
-
688
- # Verify we indexed the correct name
689
- assert_entry("Foo::Bar::<Class:Bar>", Entry::SingletonClass, "/fake/path/foo.rb:1-2:3-5")
690
-
691
- method = @index["baz"]&.first #: as Entry::Method
692
- assert_equal("Foo::Bar::<Class:Bar>", method.owner&.name)
693
- end
694
-
695
- def test_lazy_comments_with_spaces_are_properly_attributed
696
- path = File.join(Dir.pwd, "lib", "foo.rb")
697
- source = <<~RUBY
698
- require "whatever"
699
-
700
- # These comments belong to the declaration below
701
- # They have to be associated with it
702
-
703
- class Foo
704
- end
705
- RUBY
706
- File.write(path, source)
707
- @index.index_single(URI::Generic.from_path(path: path), source, collect_comments: false)
708
-
709
- entry = @index["Foo"]&.first #: as !nil
710
-
711
- begin
712
- assert_equal(<<~COMMENTS.chomp, entry.comments)
713
- These comments belong to the declaration below
714
- They have to be associated with it
715
- COMMENTS
716
- ensure
717
- FileUtils.rm(path)
718
- end
719
- end
720
-
721
- def test_lazy_comments_with_no_spaces_are_properly_attributed
722
- path = File.join(Dir.pwd, "lib", "foo.rb")
723
- source = <<~RUBY
724
- require "whatever"
725
-
726
- # These comments belong to the declaration below
727
- # They have to be associated with it
728
- class Foo
729
- end
730
- RUBY
731
- File.write(path, source)
732
- @index.index_single(URI::Generic.from_path(path: path), source, collect_comments: false)
733
-
734
- entry = @index["Foo"]&.first #: as !nil
735
-
736
- begin
737
- assert_equal(<<~COMMENTS.chomp, entry.comments)
738
- These comments belong to the declaration below
739
- They have to be associated with it
740
- COMMENTS
741
- ensure
742
- FileUtils.rm(path)
743
- end
744
- end
745
-
746
- def test_lazy_comments_with_two_extra_spaces_are_properly_ignored
747
- path = File.join(Dir.pwd, "lib", "foo.rb")
748
- source = <<~RUBY
749
- require "whatever"
750
-
751
- # These comments don't belong to the declaration below
752
- # They will not be associated with it
753
-
754
-
755
- class Foo
756
- end
757
- RUBY
758
- File.write(path, source)
759
- @index.index_single(URI::Generic.from_path(path: path), source, collect_comments: false)
760
-
761
- entry = @index["Foo"]&.first #: as !nil
762
-
763
- begin
764
- assert_empty(entry.comments)
765
- ensure
766
- FileUtils.rm(path)
767
- end
768
- end
769
-
770
- def test_lazy_comments_ignores_magic_comments
771
- path = File.join(Dir.pwd, "lib", "foo.rb")
772
- source = <<~RUBY
773
- # frozen_string_literal: true
774
-
775
- class Foo
776
- end
777
- RUBY
778
- File.write(path, source)
779
- @index.index_single(URI::Generic.from_path(path: path), source, collect_comments: false)
780
-
781
- entry = @index["Foo"]&.first #: as !nil
782
-
783
- begin
784
- assert_empty(entry.comments)
785
- ensure
786
- FileUtils.rm(path)
787
- end
788
- end
789
- end
790
- end