Skip to content

Commit

Permalink
Allow nil return, and fix typing
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Aug 28, 2024
1 parent 7b8c18e commit ca7a40e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
11 changes: 5 additions & 6 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -619,13 +619,12 @@ 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::Array[T.type_parameter(:T)])
type: T::Class[T.all(T.type_parameter(:T), Entry)],
).returns(T.nilable(T::Array[T.type_parameter(:T)]))
end
def entries_for(path, type = nil)
type = Entry if type.nil?

Array(@files_to_entries[path]).grep(type)
def entries_for(path, type)
entries = @files_to_entries[path]
entries&.grep(type)
end

private
Expand Down
4 changes: 2 additions & 2 deletions lib/ruby_indexer/test/index_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1853,15 +1853,15 @@ def self.my_singleton_def; end
end
RUBY

entries = @index.entries_for("/fake/path/foo.rb")
entries = @index.entries_for("/fake/path/foo.rb", Entry)
assert_equal(["Foo", "Bar", "my_def", "Bar::<Class:Bar>", "my_singleton_def"], entries.map(&:name))

entries = @index.entries_for("/fake/path/foo.rb", RubyIndexer::Entry::Namespace)
assert_equal(["Foo", "Bar", "Bar::<Class:Bar>"], entries.map(&:name))
end

def test_entries_for_returns_nil_if_no_matches
assert_empty(@index.entries_for("non_existing_file.rb"))
assert_nil(@index.entries_for("non_existing_file.rb", Entry::Namespace))
end
end
end

0 comments on commit ca7a40e

Please sign in to comment.