Skip to content

Commit

Permalink
More effectively determine method ownership
Browse files Browse the repository at this point in the history
  • Loading branch information
egiurleo committed Apr 12, 2022
1 parent cadc05f commit 5b4bf06
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
31 changes: 30 additions & 1 deletion lib/tapioca/gem/listeners/methods.rb
Expand Up @@ -66,7 +66,7 @@ def compile_directly_owned_methods(tree, module_name, mod, for_visibility = [:pu
end
def compile_method(tree, symbol_name, constant, method, visibility = RBI::Public.new)
return unless method
return unless method.owner == constant
return unless method_defined_by_constant?(method, constant)
return if @pipeline.symbol_in_payload?(symbol_name) && !@pipeline.method_in_gem?(method)

signature = signature_of(method)
Expand Down Expand Up @@ -142,6 +142,35 @@ def compile_method(tree, symbol_name, constant, method, visibility = RBI::Public
tree << rbi_method
end

sig { params(method: UnboundMethod, constant: Module).returns(T::Boolean) }
def method_defined_by_constant?(method, constant)
# Check whether the method is defined on the constant.
#
# In most cases, it works to check that the constant is the method owner. However,
# in the case that a method is also defined in a module prepended to the constant, it
# will be owned by the prepended module, not the constant.
#
# This method implements a better way of checking whether a constant defines a method.
# It walks up the ancestor tree via the `super_method` method; if any of the super
# methods are owned by the constant, it means that the constant declares the method.

# Widen the type of `method` to be nilable
method = T.let(method, T.nilable(UnboundMethod))

loop do
# At the top of the loop, we are guaranteed to have a non-nilable method
method = T.must(method)

return true if method.owner == constant

method = method.super_method

break unless method
end

false
end

sig { params(mod: Module).returns(T::Hash[Symbol, T::Array[Symbol]]) }
def method_names_by_visibility(mod)
{
Expand Down
30 changes: 30 additions & 0 deletions spec/tapioca/gem/pipeline_spec.rb
Expand Up @@ -1210,6 +1210,36 @@ class Bar; end
assert_equal(output, compile)
end

it "compiles method that is also prepended" do
add_ruby_file("foo.rb", <<~RUBY)
module Foo
def bar
super
end
end
class Baz
prepend Foo
def bar; end
end
RUBY

output = template(<<~RBI)
class Baz
include ::Foo
def bar; end
end
module Foo
def bar; end
end
RBI

assert_equal(output, compile)
end

it "ignores methods on other objects" do
add_ruby_file("bar.rb", <<~RUBY)
class Bar
Expand Down

0 comments on commit 5b4bf06

Please sign in to comment.