From 574795c68a4cb6cb9b1425fb7b62dc30611bcd81 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Wed, 24 Jul 2024 16:33:21 -0400 Subject: [PATCH 1/3] Support RBS signature aliases --- lib/ruby_indexer/lib/ruby_indexer/entry.rb | 2 +- .../lib/ruby_indexer/rbs_indexer.rb | 19 +++++++++++++++++++ lib/ruby_indexer/test/rbs_indexer_test.rb | 17 +++++++++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/entry.rb b/lib/ruby_indexer/lib/ruby_indexer/entry.rb index 1e4cc445c..13d663a27 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/entry.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/entry.rb @@ -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 diff --git a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb index 713175bc8..57f6c2da8 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb @@ -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_signature_alias(member, entry) end end end @@ -243,5 +245,22 @@ 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_signature_alias(member, owner_entry) + file_path = member.location.buffer.name + comments = Array(member.comment&.string) + + 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 end end diff --git a/lib/ruby_indexer/test/rbs_indexer_test.rb b/lib/ruby_indexer/test/rbs_indexer_test.rb index 45471636a..f47ab52a2 100644 --- a/lib/ruby_indexer/test/rbs_indexer_test.rb +++ b/lib/ruby_indexer/test/rbs_indexer_test.rb @@ -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 }) + + 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) From f965b8c4f9b7a6a937816880d8091095ad7dbdda Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:33:05 -0400 Subject: [PATCH 2/3] Extract `comments_to_string` --- .../lib/ruby_indexer/rbs_indexer.rb | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb index 57f6c2da8..0c39dd414 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb @@ -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) @@ -111,7 +111,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 @@ -242,14 +242,14 @@ 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 = Array(member.comment&.string) + comments = comments_to_string(member) entry = Entry::UnresolvedMethodAlias.new( member.new_name.to_s, @@ -262,5 +262,18 @@ def handle_signature_alias(member, owner_entry) @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]) + end + def comments_to_string(declaration) + Array(declaration.comment&.string) + end end end From c25bd71060649acd35c3b57efbf962ff3c038bf2 Mon Sep 17 00:00:00 2001 From: Andy Waite <13400+andyw8@users.noreply.github.com> Date: Thu, 25 Jul 2024 10:35:04 -0400 Subject: [PATCH 3/3] Update comments --- lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb | 4 +++- lib/ruby_indexer/test/rbs_indexer_test.rb | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb index 0c39dd414..89cca8642 100644 --- a/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb +++ b/lib/ruby_indexer/lib/ruby_indexer/rbs_indexer.rb @@ -69,7 +69,9 @@ 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 + 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 diff --git a/lib/ruby_indexer/test/rbs_indexer_test.rb b/lib/ruby_indexer/test/rbs_indexer_test.rb index f47ab52a2..bba982c5c 100644 --- a/lib/ruby_indexer/test/rbs_indexer_test.rb +++ b/lib/ruby_indexer/test/rbs_indexer_test.rb @@ -332,8 +332,8 @@ def self?.open: (String name, ?String mode, ?Integer perm) -> IO? 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. + # 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 })