From 00249bda44e2a89ce6841c74dc062a1371ddc628 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Sat, 5 Apr 2025 15:03:41 -0400 Subject: [PATCH] MacOSVersion: enable strong typing --- Library/Homebrew/ast_constants.rb | 2 +- Library/Homebrew/cask/dsl.rb | 2 +- Library/Homebrew/cask/dsl/depends_on.rb | 9 +++++---- Library/Homebrew/macos_version.rb | 19 ++++++++++++------- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Library/Homebrew/ast_constants.rb b/Library/Homebrew/ast_constants.rb index fa31aef6ed..c6ec8eb61c 100644 --- a/Library/Homebrew/ast_constants.rb +++ b/Library/Homebrew/ast_constants.rb @@ -52,4 +52,4 @@ FORMULA_COMPONENT_PRECEDENCE_LIST = T.let([ [{ name: :caveats, type: :method_definition }], [{ name: :plist_options, type: :method_call }, { name: :plist, type: :method_definition }], [{ name: :test, type: :block_call }], -].freeze, T::Array[[{ name: Symbol, type: Symbol }]]) +].freeze, T::Array[T::Array[{ name: Symbol, type: Symbol }]]) diff --git a/Library/Homebrew/cask/dsl.rb b/Library/Homebrew/cask/dsl.rb index 7b2d7f1bb3..22a55c4f1f 100644 --- a/Library/Homebrew/cask/dsl.rb +++ b/Library/Homebrew/cask/dsl.rb @@ -486,7 +486,7 @@ module Cask def add_implicit_macos_dependency return if (cask_depends_on = @depends_on).present? && cask_depends_on.macos.present? - depends_on macos: ">= :#{MacOSVersion::SYMBOLS.key MacOSVersion::SYMBOLS.values.min}" + depends_on macos: ">= #{MacOSVersion.new(HOMEBREW_MACOS_OLDEST_ALLOWED).to_sym.inspect}" end # Declare conflicts that keep a cask from installing or working correctly. diff --git a/Library/Homebrew/cask/dsl/depends_on.rb b/Library/Homebrew/cask/dsl/depends_on.rb index 7a5756f9c0..ddaf48698f 100644 --- a/Library/Homebrew/cask/dsl/depends_on.rb +++ b/Library/Homebrew/cask/dsl/depends_on.rb @@ -52,16 +52,17 @@ module Cask raise "Only a single 'depends_on macos' is allowed." if defined?(@macos) # workaround for https://github.com/sorbet/sorbet/issues/6860 - first_arg = args.first&.to_s + first_arg = args.first + first_arg_s = first_arg&.to_s begin @macos = if args.count > 1 MacOSRequirement.new([args], comparator: "==") - elsif MacOSVersion::SYMBOLS.key?(args.first) + elsif first_arg.is_a?(Symbol) && MacOSVersion::SYMBOLS.key?(first_arg) MacOSRequirement.new([args.first], comparator: "==") - elsif (md = /^\s*(?<|>|[=<>]=)\s*:(?\S+)\s*$/.match(first_arg)) + elsif (md = /^\s*(?<|>|[=<>]=)\s*:(?\S+)\s*$/.match(first_arg_s)) MacOSRequirement.new([T.must(md[:version]).to_sym], comparator: md[:comparator]) - elsif (md = /^\s*(?<|>|[=<>]=)\s*(?\S+)\s*$/.match(first_arg)) + elsif (md = /^\s*(?<|>|[=<>]=)\s*(?\S+)\s*$/.match(first_arg_s)) MacOSRequirement.new([md[:version]], comparator: md[:comparator]) # This is not duplicate of the first case: see `args.first` and a different comparator. else # rubocop:disable Lint/DuplicateBranch diff --git a/Library/Homebrew/macos_version.rb b/Library/Homebrew/macos_version.rb index f71a2e6e73..6d1b8239ed 100644 --- a/Library/Homebrew/macos_version.rb +++ b/Library/Homebrew/macos_version.rb @@ -1,4 +1,4 @@ -# typed: true # rubocop:todo Sorbet/StrictSigil +# typed: strong # frozen_string_literal: true require "version" @@ -10,6 +10,7 @@ class MacOSVersion < Version sig { returns(T.nilable(T.any(String, Symbol))) } attr_reader :version + sig { params(version: T.nilable(T.any(String, Symbol))).void } def initialize(version) @version = version super "unknown or unsupported macOS version: #{version.inspect}" @@ -18,7 +19,7 @@ class MacOSVersion < Version # NOTE: When removing symbols here, ensure that they are added # to `DEPRECATED_MACOS_VERSIONS` in `MacOSRequirement`. - SYMBOLS = { + SYMBOLS = T.let({ tahoe: "26", sequoia: "15", sonoma: "14", @@ -30,7 +31,7 @@ class MacOSVersion < Version high_sierra: "10.13", sierra: "10.12", el_capitan: "10.11", - }.freeze + }.freeze, T::Hash[Symbol, String]) sig { params(macos_version: MacOSVersion).returns(Version) } def self.kernel_major_version(macos_version) @@ -57,7 +58,9 @@ class MacOSVersion < Version super(T.must(version)) - @comparison_cache = {} + @comparison_cache = T.let({}, T::Hash[T.untyped, T.nilable(Integer)]) + @pretty_name = T.let(nil, T.nilable(String)) + @sym = T.let(nil, T.nilable(Symbol)) end sig { override.params(other: T.untyped).returns(T.nilable(Integer)) } @@ -95,7 +98,7 @@ class MacOSVersion < Version sig { returns(Symbol) } def to_sym - return @sym if defined?(@sym) + return @sym if @sym sym = SYMBOLS.invert.fetch(strip_patch.to_s, :dunno) @@ -106,7 +109,7 @@ class MacOSVersion < Version sig { returns(String) } def pretty_name - return @pretty_name if defined?(@pretty_name) + return @pretty_name if @pretty_name pretty_name = to_sym.to_s.split("_").map(&:capitalize).join(" ").freeze @@ -154,5 +157,7 @@ class MacOSVersion < Version # Represents the absence of a version. # # NOTE: Constructor needs to called with an arbitrary macOS-like version which is then set to `nil`. - NULL = MacOSVersion.new("10.0").tap { |v| v.instance_variable_set(:@version, nil) }.freeze + NULL = T.let(MacOSVersion.new("10.0").tap do |v| + T.let(v, MacOSVersion).instance_variable_set(:@version, nil) + end.freeze, MacOSVersion) end