Skip to content

Commit

Permalink
Merge pull request #19288 from Homebrew/cask-delegation
Browse files Browse the repository at this point in the history
Use delegation to create Cask DSL methods
  • Loading branch information
dduugg authored Feb 11, 2025
2 parents 1ff098b + d8c790d commit 2e211a5
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 82 deletions.
4 changes: 1 addition & 3 deletions Library/Homebrew/cask/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ def refresh
@dsl.language_eval
end

::Cask::DSL::DSL_METHODS.each do |method_name|
define_method(method_name) { |*args, &block| @dsl.send(method_name, *args, &block) }
end
def_delegators :@dsl, *::Cask::DSL::DSL_METHODS

sig { params(caskroom_path: Pathname).returns(T::Array[[String, String]]) }
def timestamped_versions(caskroom_path: self.caskroom_path)
Expand Down
67 changes: 0 additions & 67 deletions Library/Homebrew/cask/cask.rbi

This file was deleted.

186 changes: 186 additions & 0 deletions Library/Homebrew/sorbet/rbi/dsl/cask/cask.rbi

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Library/Homebrew/sorbet/tapioca/compilers/delegators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module Tapioca
module Compilers
# A compiler for subclasses of Delegator.
# To add a new delegator: require it above add add it to the DELEGATIONS hash below.
# To add a new delegator: require it above and add it to the DELEGATIONS hash below.
class Delegators < Tapioca::Dsl::Compiler
# Mapping of delegator classes to the classes they delegate to (as defined in `__getobj__`).
DELEGATIONS = T.let({
Expand Down
26 changes: 15 additions & 11 deletions Library/Homebrew/sorbet/tapioca/compilers/forwardables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class Forwardables < Tapioca::Dsl::Compiler
ARRAY_METHODS = T.let(["to_a", "to_ary"].freeze, T::Array[String])
HASH_METHODS = T.let(["to_h", "to_hash"].freeze, T::Array[String])
STRING_METHODS = T.let(["to_s", "to_str", "to_json"].freeze, T::Array[String])
# Use this to override the default return type of a forwarded method:
RETURN_TYPE_OVERRIDES = T.let({
"::Cask::Cask" => {
"on_system_block_min_os" => "T.nilable(MacOSVersion)",
"url" => "T.nilable(::Cask::URL)",
},
}.freeze, T::Hash[String, T::Hash[String, String]])

ConstantType = type_member { { fixed: Module } }

Expand All @@ -38,7 +45,7 @@ def decorate
sig { params(klass: RBI::Scope, method: T.any(Method, UnboundMethod), class_method: T::Boolean).void }
def compile_forwardable_method(klass, method, class_method: false)
name = method.name.to_s
return_type = return_type(name)
return_type = return_type(klass.to_s, name)
klass.create_method(
name,
parameters: [
Expand All @@ -50,16 +57,13 @@ def compile_forwardable_method(klass, method, class_method: false)
)
end

sig { params(name: String).returns(String) }
def return_type(name)
if name.end_with?("?")
"T::Boolean"
elsif ARRAY_METHODS.include?(name)
"Array"
elsif HASH_METHODS.include?(name)
"Hash"
elsif STRING_METHODS.include?(name)
"String"
sig { params(klass: String, name: String).returns(String) }
def return_type(klass, name)
if (override = RETURN_TYPE_OVERRIDES.dig(klass, name)) then override
elsif name.end_with?("?") then "T::Boolean"
elsif ARRAY_METHODS.include?(name) then "Array"
elsif HASH_METHODS.include?(name) then "Hash"
elsif STRING_METHODS.include?(name) then "String"
else
"T.untyped"
end
Expand Down

0 comments on commit 2e211a5

Please # to comment.