Move nil check inside demodulize

This commit is contained in:
Douglas Eichelberger 2025-02-23 11:08:00 -08:00
parent f6d0146b1a
commit 936b9b5369
No known key found for this signature in database
GPG Key ID: F90193CBD547EB81
10 changed files with 17 additions and 12 deletions

View File

@ -31,7 +31,7 @@ module Homebrew
sig { params(strategy_class: T::Class[T.anything]).returns(String) } sig { params(strategy_class: T::Class[T.anything]).returns(String) }
private_class_method def self.livecheck_strategy_names(strategy_class) private_class_method def self.livecheck_strategy_names(strategy_class)
@livecheck_strategy_names ||= T.let({}, T.nilable(T::Hash[T::Class[T.anything], String])) @livecheck_strategy_names ||= T.let({}, T.nilable(T::Hash[T::Class[T.anything], String]))
@livecheck_strategy_names[strategy_class] ||= Utils.demodulize(T.must(strategy_class.name)) @livecheck_strategy_names[strategy_class] ||= Utils.demodulize(strategy_class.name)
end end
# Uses `formulae_and_casks_to_check` to identify taps in use other than # Uses `formulae_and_casks_to_check` to identify taps in use other than

View File

@ -47,7 +47,7 @@ module Homebrew
def self.find_versions(url:, regex: nil, provided_content: nil, **unused, &block) def self.find_versions(url:, regex: nil, provided_content: nil, **unused, &block)
if regex.present? && block.blank? if regex.present? && block.blank?
raise ArgumentError, raise ArgumentError,
"#{Utils.demodulize(T.must(name))} only supports a regex when using a `strategy` block" "#{Utils.demodulize(name)} only supports a regex when using a `strategy` block"
end end
Yaml.find_versions( Yaml.find_versions(

View File

@ -92,11 +92,9 @@ module Homebrew
def self.find_versions(cask:, url: nil, regex: nil, **_unused, &block) def self.find_versions(cask:, url: nil, regex: nil, **_unused, &block)
if regex.present? && block.blank? if regex.present? && block.blank?
raise ArgumentError, raise ArgumentError,
"#{Utils.demodulize(T.must(name))} only supports a regex when using a `strategy` block" "#{Utils.demodulize(name)} only supports a regex when using a `strategy` block"
end
unless T.unsafe(cask)
raise ArgumentError, "The #{Utils.demodulize(T.must(name))} strategy only supports casks."
end end
raise ArgumentError, "The #{Utils.demodulize(name)} strategy only supports casks." unless T.unsafe(cask)
match_data = { matches: {}, regex:, url: } match_data = { matches: {}, regex:, url: }

View File

@ -107,7 +107,7 @@ module Homebrew
).returns(T::Hash[Symbol, T.untyped]) ).returns(T::Hash[Symbol, T.untyped])
} }
def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **unused, &block) def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **unused, &block)
raise ArgumentError, "#{Utils.demodulize(T.must(name))} requires a `strategy` block" if block.blank? raise ArgumentError, "#{Utils.demodulize(name)} requires a `strategy` block" if block.blank?
match_data = { matches: {}, regex:, url: } match_data = { matches: {}, regex:, url: }
return match_data if url.blank? || block.blank? return match_data if url.blank? || block.blank?

View File

@ -91,7 +91,7 @@ module Homebrew
} }
def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **unused, &block) def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **unused, &block)
if regex.blank? && block.blank? if regex.blank? && block.blank?
raise ArgumentError, "#{Utils.demodulize(T.must(name))} requires a regex or `strategy` block" raise ArgumentError, "#{Utils.demodulize(name)} requires a regex or `strategy` block"
end end
match_data = { matches: {}, regex:, url: } match_data = { matches: {}, regex:, url: }

View File

@ -228,7 +228,7 @@ module Homebrew
def self.find_versions(url:, regex: nil, homebrew_curl: false, **unused, &block) def self.find_versions(url:, regex: nil, homebrew_curl: false, **unused, &block)
if regex.present? && block.blank? if regex.present? && block.blank?
raise ArgumentError, raise ArgumentError,
"#{Utils.demodulize(T.must(name))} only supports a regex when using a `strategy` block" "#{Utils.demodulize(name)} only supports a regex when using a `strategy` block"
end end
match_data = { matches: {}, regex:, url: } match_data = { matches: {}, regex:, url: }

View File

@ -147,7 +147,7 @@ module Homebrew
).returns(T::Hash[Symbol, T.untyped]) ).returns(T::Hash[Symbol, T.untyped])
} }
def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **unused, &block) def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **unused, &block)
raise ArgumentError, "#{Utils.demodulize(T.must(name))} requires a `strategy` block" if block.blank? raise ArgumentError, "#{Utils.demodulize(name)} requires a `strategy` block" if block.blank?
match_data = { matches: {}, regex:, url: } match_data = { matches: {}, regex:, url: }
return match_data if url.blank? || block.blank? return match_data if url.blank? || block.blank?

View File

@ -107,7 +107,7 @@ module Homebrew
).returns(T::Hash[Symbol, T.untyped]) ).returns(T::Hash[Symbol, T.untyped])
} }
def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **unused, &block) def self.find_versions(url:, regex: nil, provided_content: nil, homebrew_curl: false, **unused, &block)
raise ArgumentError, "#{Utils.demodulize(T.must(name))} requires a `strategy` block" if block.blank? raise ArgumentError, "#{Utils.demodulize(name)} requires a `strategy` block" if block.blank?
match_data = { matches: {}, regex:, url: } match_data = { matches: {}, regex:, url: }
return match_data if url.blank? || block.blank? return match_data if url.blank? || block.blank?

View File

@ -30,6 +30,10 @@ RSpec.describe Utils do
expect(described_class.demodulize("")).to eq("") expect(described_class.demodulize("")).to eq("")
expect(described_class.demodulize("::")).to eq("") expect(described_class.demodulize("::")).to eq("")
end end
it "raise an ArgumentError when passed nil" do
expect { described_class.demodulize(nil) }.to raise_error(ArgumentError)
end
end end
specify ".parse_author!" do specify ".parse_author!" do

View File

@ -97,8 +97,11 @@ module Utils
# See also #deconstantize. # See also #deconstantize.
# @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L230-L245 # @see https://github.com/rails/rails/blob/b0dd7c7/activesupport/lib/active_support/inflector/methods.rb#L230-L245
# `ActiveSupport::Inflector.demodulize` # `ActiveSupport::Inflector.demodulize`
sig { params(path: String).returns(String) } # @raise [ArgumentError] if the provided path is nil
sig { params(path: T.nilable(String)).returns(String) }
def self.demodulize(path) def self.demodulize(path)
raise ArgumentError, "No constant path provided" if path.nil?
if (i = path.rindex("::")) if (i = path.rindex("::"))
T.must(path[(i + 2)..]) T.must(path[(i + 2)..])
else else