Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RBS signature aliases #2359

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/ruby_indexer/lib/ruby_indexer/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ class UnresolvedMethodAlias < Entry
old_name: String,
owner: T.nilable(Entry::Namespace),
file_path: String,
location: Prism::Location,
location: T.any(Prism::Location, RubyIndexer::Location),
comments: T::Array[String],
).void
end
Expand Down
40 changes: 37 additions & 3 deletions lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def handle_class_or_module_declaration(declaration, pathname)
nesting = [declaration.name.name.to_s]
file_path = pathname.to_s
location = to_ruby_indexer_location(declaration.location)
comments = Array(declaration.comment&.string)
comments = comments_to_string(declaration)
entry = if declaration.is_a?(RBS::AST::Declarations::Class)
parent_class = declaration.super_class&.name&.name&.to_s
Entry::Class.new(nesting, file_path, location, location, comments, parent_class)
Expand All @@ -69,6 +69,10 @@ def handle_class_or_module_declaration(declaration, pathname)
handle_method(member, entry)
when RBS::AST::Declarations::Constant
handle_constant(member, nesting, file_path)
when RBS::AST::Members::Alias
# In RBS, an alias means that two methods have the same signature.
# It does not mean the same thing as a Ruby alias.
handle_signature_alias(member, entry)
end
end
end
Expand Down Expand Up @@ -109,7 +113,7 @@ def handle_method(member, owner)
name = member.name.name
file_path = member.location.buffer.name
location = to_ruby_indexer_location(member.location)
comments = Array(member.comment&.string)
comments = comments_to_string(member)

visibility = case member.visibility
when :private
Expand Down Expand Up @@ -240,8 +244,38 @@ def handle_constant(declaration, nesting, file_path)
fully_qualified_name,
file_path,
to_ruby_indexer_location(declaration.location),
Array(declaration.comment&.string),
comments_to_string(declaration),
))
end

sig { params(member: RBS::AST::Members::Alias, owner_entry: Entry::Namespace).void }
def handle_signature_alias(member, owner_entry)
file_path = member.location.buffer.name
comments = comments_to_string(member)

entry = Entry::UnresolvedMethodAlias.new(
member.new_name.to_s,
member.old_name.to_s,
owner_entry,
file_path,
to_ruby_indexer_location(member.location),
comments,
)

@index.add(entry)
end

sig do
params(declaration: T.any(
RBS::AST::Declarations::Class,
RBS::AST::Declarations::Module,
RBS::AST::Declarations::Constant,
RBS::AST::Members::MethodDefinition,
RBS::AST::Members::Alias,
)).returns(T::Array[String])
andyw8 marked this conversation as resolved.
Show resolved Hide resolved
end
def comments_to_string(declaration)
Array(declaration.comment&.string)
end
end
end
17 changes: 17 additions & 0 deletions lib/ruby_indexer/test/rbs_indexer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ def self?.open: (String name, ?String mode, ?Integer perm) -> IO?
assert_kind_of(Entry::BlockParameter, parameters[3])
end

def test_signature_alias
# In RBS, an alias means that two methods have the same signature.
# It does not mean the same thing as a Ruby alias.
any_entries = @index["any?"]

assert_equal(["Array", "Enumerable", "Hash"], any_entries.map { _1.owner.name })
andyw8 marked this conversation as resolved.
Show resolved Hide resolved

entry = any_entries.find { |entry| entry.owner.name == "Array" }

assert_kind_of(RubyIndexer::Entry::UnresolvedMethodAlias, entry)
assert_equal("any?", entry.name)
assert_equal("all?", entry.old_name)
assert_equal("Array", entry.owner.name)
assert(entry.file_path.end_with?("core/array.rbs"))
assert_includes(entry.comments[0], "Returns `true` if any element of `self` meets a given criterion.")
end

private

def parse_rbs_methods(rbs, method_name)
Expand Down
Loading