Skip to content

Commit

Permalink
Remove casts
Browse files Browse the repository at this point in the history
  • Loading branch information
dduugg committed Oct 15, 2024
1 parent ced4a3a commit 0d7f150
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
4 changes: 1 addition & 3 deletions Library/Homebrew/sorbet/tapioca/compilers/attrables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ class Attrables < Tapioca::Dsl::Compiler
ConstantType = type_member { { fixed: Module } }

sig { override.returns(T::Enumerable[Module]) }
def self.gather_constants
ObjectSpace.each_object(Attrable).map { |obj| obj.name.nil? ? obj.attached_object : obj }
end
def self.gather_constants = Homebrew::Tapioca::Utils.named_objects_with_module(Attrable)

sig { override.void }
def decorate
Expand Down
12 changes: 1 addition & 11 deletions Library/Homebrew/sorbet/tapioca/compilers/forwardables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,12 @@ class Forwardables < Tapioca::Dsl::Compiler

sig { override.returns(T::Enumerable[Module]) }
def self.gather_constants
objects = T.cast(ObjectSpace.each_object(Forwardable), T::Enumerator[Module])
objects.map { |obj| named_object(obj) }.uniq.reject do |obj|
Homebrew::Tapioca::Utils.named_objects_with_module(Forwardable).reject do |obj|
# Avoid duplicate stubs for forwardables that are defined in vendored gems
Object.const_source_location(T.must(obj.name))&.first&.include?("vendor/bundle/ruby")
end
end

sig { params(obj: Module).returns(Module) }
def self.named_object(obj)
if obj.is_a?(Class) && obj.name.nil?
T.cast(obj.attached_object, Module)
else
obj
end
end

sig { override.void }
def decorate
root.create_path(constant) do |klass|
Expand Down
14 changes: 14 additions & 0 deletions Library/Homebrew/sorbet/tapioca/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ def self.methods_from_file(mod, file_name, class_methods: false)
end
methods.select { _1.source_location&.first&.end_with?(file_name) }
end

sig { params(mod: Module).returns(T::Array[Module]) }
def self.named_objects_with_module(mod)
ObjectSpace.each_object(mod).map do |obj|
case obj
when Class
obj.name ? obj : T.cast(obj.attached_object, Module)
when Module
obj
else
raise "Unsupported object: #{obj}"
end
end.uniq
end
end
end
end

0 comments on commit 0d7f150

Please sign in to comment.