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

Implement Rubocop DSL RBI compiler #1046

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Next Next commit
Add extenders_of helper method
This method is similar to `descendants_of`, but returns all modules
which extend the given module. That is, all modules (including classes),
whose singleton class (or parent thereof) includes the given module.
  • Loading branch information
sambostock committed Jan 29, 2024
commit ef1eff576fcc1541a2bb21fe89e9ad33f3862806
27 changes: 27 additions & 0 deletions lib/tapioca/runtime/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,33 @@ def descendants_of(klass)
T.unsafe(result)
end

# Returns an array with all modules which extend the supplied module
# (i.e. all modules whose singleton class, or ancestor thereof, includes the supplied module).
#
# module M; end
# extenders_of(M) # => []
#
# module E
# extend M
# end
# extenders_of(M) # => [E]
#
# class P
# extend M
# end
# extenders_of(M) # => [E, P]
#
# class C < P; end
# extenders_of(M) # => [E, P, C]
sig { params(mod: Module).returns(T::Array[Module]) }
def extenders_of(mod)
result = ObjectSpace.each_object(Module).select do |m|
T.cast(m, Module).singleton_class.included_modules.include?(mod)
end

T.cast(result, T::Array[Module])
end

# Examines the call stack to identify the closest location where a "require" is performed
# by searching for the label "<top (required)>". If none is found, it returns the location
# labeled "<main>", which is the original call site.
Expand Down