From 3d2ca2e4e3197e97bb00ffb94de5e3a14c1f6298 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Wed, 28 Aug 2024 11:21:16 -0400 Subject: [PATCH] Return empty array instead of nil if no matches --- lib/ruby_indexer/lib/ruby_indexer/index.rb | 6 +++--- lib/ruby_indexer/test/index_test.rb | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/index.rb b/lib/ruby_indexer/lib/ruby_indexer/index.rb index d14f2812b9..aba69d098d 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/index.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/index.rb @@ -617,13 +617,13 @@ def existing_or_new_singleton_class(name) sig do type_parameters(:T).params( path: String, - type: T.nilable(T::Class[T.all(T.type_parameter(:T), Entry)]), - ).returns(T.nilable(T::Array[T.type_parameter(:T)])) + type: T::Class[T.all(T.type_parameter(:T), Entry)], + ).returns(T::Array[T.type_parameter(:T)]) end def entries_for(path, type = nil) type = Entry if type.nil? - @files_to_entries[path]&.grep(type)&.grep_v(RubyIndexer::Entry::SingletonClass) + Array(@files_to_entries[path]).grep(type).grep_v(RubyIndexer::Entry::SingletonClass) end private diff --git a/lib/ruby_indexer/test/index_test.rb b/lib/ruby_indexer/test/index_test.rb index 41b383fc39..3b0925ed98 100644 --- a/lib/ruby_indexer/test/index_test.rb +++ b/lib/ruby_indexer/test/index_test.rb @@ -1839,5 +1839,9 @@ def self.my_singleton_def; end entries = @index.entries_for("/fake/path/foo.rb", RubyIndexer::Entry::Namespace) assert_equal(["Foo", "Bar"], entries.map(&:name)) end + + def test_entries_for_returns__nil_if_no_matches + assert_empty(@index.entries_for("non_existing_file.rb")) + end end end