Skip to content

Commit

Permalink
Support RBS signature aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
andyw8 committed Jul 24, 2024
1 parent 77de05f commit a1cf814
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
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
20 changes: 20 additions & 0 deletions lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ 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 # NOTE: This is a signature alias, not a Ruby method alias
handle_alias(member, entry)
end
end
end
Expand Down Expand Up @@ -243,5 +245,23 @@ def handle_constant(declaration, nesting, file_path)
Array(declaration.comment&.string),
))
end

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

comments = Array(member.comment&.string)
entry = Entry::UnresolvedMethodAlias.new(
new_name.to_s,
old_name.to_s,
owner_entry,
file_path,
to_ruby_indexer_location(member.location),
comments,
)
@index.add(entry)
end
end
end
15 changes: 15 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,21 @@ 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?"]

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

assert_kind_of(RubyIndexer::Entry::UnresolvedMethodAlias, any_entry)
assert_equal("any?", any_entry.name)
assert_equal("all?", any_entry.old_name)
assert_equal("Array", any_entry.owner.name)
assert(any_entry.file_path.end_with?("core/array.rbs"))
assert_includes(any_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

0 comments on commit a1cf814

Please sign in to comment.