Cask: Initialize yet more instance variables

I previously added some instance variables in `Cask::DSL` to its
`initialize` method but I missed some last time, so we still see
warnings like `The class Cask::DSL reached 8 shape variations,
instance variables accesses will be slower and memory usage increased.
It is recommended to define instance variables in a consistent order,
for instance by eagerly defining them all in the #initialize method.`

This initializes more instance variables in `Cask` classes to resolve
other situations where this warning may occur. I've been testing this
for a while and haven't see any warnings with these changes but
there's always a chance that there's still more work to be done.
This commit is contained in:
Sam Ford 2025-06-13 08:32:56 -04:00
parent e049ee3d3b
commit 4a4f7a541a
No known key found for this signature in database
GPG Key ID: 7AF5CBEE1DD6F76D
4 changed files with 24 additions and 7 deletions

View File

@ -139,7 +139,11 @@ module Cask
def initialize(cask, *dsl_args) def initialize(cask, *dsl_args)
@cask = cask @cask = cask
@dirmethod = nil
@dsl_args = dsl_args.deep_dup @dsl_args = dsl_args.deep_dup
@dsl_key = nil
@english_article = nil
@english_name = nil
end end
def config def config

View File

@ -41,7 +41,9 @@ module Cask
super super
target = target_hash[:target] target = target_hash[:target]
@source = nil
@source_string = source.to_s @source_string = source.to_s
@target = nil
@target_string = target.to_s @target_string = target.to_s
end end

View File

@ -69,7 +69,6 @@ module Cask
].freeze ].freeze
DSL_METHODS = Set.new([ DSL_METHODS = Set.new([
:appcast,
:arch, :arch,
:artifacts, :artifacts,
:auto_updates, :auto_updates,
@ -123,15 +122,21 @@ module Cask
sig { params(cask: Cask).void } sig { params(cask: Cask).void }
def initialize(cask) def initialize(cask)
# NOTE: Variables set by `set_unique_stanza` must be initialized to `nil`. # NOTE: `:"@#{stanza}"` variables set by `set_unique_stanza` must be
@auto_updates = T.let(nil, T.nilable(T::Boolean)) # initialized to `nil`.
@arch = T.let(nil, T.nilable(String)) @arch = T.let(nil, T.nilable(String))
@arch_set_in_block = T.let(false, T::Boolean)
@artifacts = T.let(ArtifactSet.new, ArtifactSet) @artifacts = T.let(ArtifactSet.new, ArtifactSet)
@auto_updates = T.let(nil, T.nilable(T::Boolean))
@auto_updates_set_in_block = T.let(false, T::Boolean)
@autobump = T.let(true, T::Boolean)
@called_in_on_system_block = T.let(false, T::Boolean) @called_in_on_system_block = T.let(false, T::Boolean)
@cask = T.let(cask, Cask) @cask = T.let(cask, Cask)
@caveats = T.let(DSL::Caveats.new(cask), DSL::Caveats) @caveats = T.let(DSL::Caveats.new(cask), DSL::Caveats)
@conflicts_with = T.let(nil, T.nilable(DSL::ConflictsWith)) @conflicts_with = T.let(nil, T.nilable(DSL::ConflictsWith))
@conflicts_with_set_in_block = T.let(false, T::Boolean)
@container = T.let(nil, T.nilable(DSL::Container)) @container = T.let(nil, T.nilable(DSL::Container))
@container_set_in_block = T.let(false, T::Boolean)
@depends_on = T.let(DSL::DependsOn.new, DSL::DependsOn) @depends_on = T.let(DSL::DependsOn.new, DSL::DependsOn)
@depends_on_set_in_block = T.let(false, T::Boolean) @depends_on_set_in_block = T.let(false, T::Boolean)
@deprecated = T.let(false, T::Boolean) @deprecated = T.let(false, T::Boolean)
@ -140,27 +145,32 @@ module Cask
@deprecation_replacement_cask = T.let(nil, T.nilable(String)) @deprecation_replacement_cask = T.let(nil, T.nilable(String))
@deprecation_replacement_formula = T.let(nil, T.nilable(String)) @deprecation_replacement_formula = T.let(nil, T.nilable(String))
@desc = T.let(nil, T.nilable(String)) @desc = T.let(nil, T.nilable(String))
@desc_set_in_block = T.let(false, T::Boolean)
@disable_date = T.let(nil, T.nilable(Date)) @disable_date = T.let(nil, T.nilable(Date))
@disable_reason = T.let(nil, T.nilable(T.any(String, Symbol))) @disable_reason = T.let(nil, T.nilable(T.any(String, Symbol)))
@disable_replacement_cask = T.let(nil, T.nilable(String)) @disable_replacement_cask = T.let(nil, T.nilable(String))
@disable_replacement_formula = T.let(nil, T.nilable(String)) @disable_replacement_formula = T.let(nil, T.nilable(String))
@disabled = T.let(false, T::Boolean) @disabled = T.let(false, T::Boolean)
@homepage = T.let(nil, T.nilable(String)) @homepage = T.let(nil, T.nilable(String))
@homepage_set_in_block = T.let(false, T::Boolean)
@language_blocks = T.let({}, T::Hash[T::Array[String], Proc]) @language_blocks = T.let({}, T::Hash[T::Array[String], Proc])
@language_eval = T.let(nil, T.nilable(String)) @language_eval = T.let(nil, T.nilable(String))
@livecheck = T.let(Livecheck.new(cask), Livecheck) @livecheck = T.let(Livecheck.new(cask), Livecheck)
@livecheck_defined = T.let(false, T::Boolean) @livecheck_defined = T.let(false, T::Boolean)
@name = T.let([], T::Array[String]) @name = T.let([], T::Array[String])
@autobump = T.let(true, T::Boolean)
@no_autobump_defined = T.let(false, T::Boolean) @no_autobump_defined = T.let(false, T::Boolean)
@on_system_blocks_exist = T.let(false, T::Boolean) @on_system_blocks_exist = T.let(false, T::Boolean)
@os = T.let(nil, T.nilable(String))
@on_system_block_min_os = T.let(nil, T.nilable(MacOSVersion)) @on_system_block_min_os = T.let(nil, T.nilable(MacOSVersion))
@os = T.let(nil, T.nilable(String))
@os_set_in_block = T.let(false, T::Boolean)
@sha256 = T.let(nil, T.nilable(T.any(Checksum, Symbol))) @sha256 = T.let(nil, T.nilable(T.any(Checksum, Symbol)))
@sha256_set_in_block = T.let(false, T::Boolean)
@staged_path = T.let(nil, T.nilable(Pathname)) @staged_path = T.let(nil, T.nilable(Pathname))
@token = T.let(cask.token, String) @token = T.let(cask.token, String)
@url = T.let(nil, T.nilable(URL)) @url = T.let(nil, T.nilable(URL))
@url_set_in_block = T.let(false, T::Boolean)
@version = T.let(nil, T.nilable(DSL::Version)) @version = T.let(nil, T.nilable(DSL::Version))
@version_set_in_block = T.let(false, T::Boolean)
end end
sig { returns(T::Boolean) } sig { returns(T::Boolean) }
@ -216,7 +226,7 @@ module Cask
raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.") raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.")
end end
if instance_variable_defined?(:"@#{stanza}_set_in_block") && @called_in_on_system_block if instance_variable_get(:"@#{stanza}_set_in_block") && @called_in_on_system_block
raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only be overridden once.") raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only be overridden once.")
end end
end end

View File

@ -44,7 +44,8 @@ module Homebrew
titleized_repository = tap.repository.dup titleized_repository = tap.repository.dup
titleized_user[0] = titleized_user[0].upcase titleized_user[0] = titleized_user[0].upcase
titleized_repository[0] = titleized_repository[0].upcase titleized_repository[0] = titleized_repository[0].upcase
root_url = GitHubPackages.root_url(tap.user, "homebrew-#{tap.repository}") if args.github_packages? # Duplicate assignment to silence `assigned but unused variable` warning
root_url = root_url = GitHubPackages.root_url(tap.user, "homebrew-#{tap.repository}") if args.github_packages?
(tap.path/"Formula").mkpath (tap.path/"Formula").mkpath