version: enable strict typing

This commit is contained in:
Bo Anderson 2024-06-11 23:03:32 +01:00
parent 3f08c75e7b
commit 1962a37e94
No known key found for this signature in database
3 changed files with 20 additions and 28 deletions

View File

@ -107,7 +107,7 @@ module Homebrew
version = if HOMEBREW_BOTTLES_EXTNAME_REGEX.match?(to_s)
begin
Utils::Bottles.resolve_version(pathname)
Utils::Bottles.resolve_version(pathname).to_s
rescue
nil
end

View File

@ -1,7 +1,6 @@
# typed: true
# typed: strict
# frozen_string_literal: true
require "pkg_version"
require "version/parser"
# A formula's version.
@ -141,7 +140,7 @@ class Version
private_constant :NullToken
# Represents the absence of a token.
NULL_TOKEN = NullToken.new.freeze
NULL_TOKEN = T.let(NullToken.new.freeze, NullToken)
# A token string.
class StringToken < Token
@ -328,7 +327,7 @@ class Version
end
end
SCAN_PATTERN = Regexp.union(
SCAN_PATTERN = T.let(Regexp.union(
AlphaToken::PATTERN,
BetaToken::PATTERN,
PreToken::PATTERN,
@ -337,7 +336,7 @@ class Version
PostToken::PATTERN,
NumericToken::PATTERN,
StringToken::PATTERN,
).freeze
).freeze, Regexp)
private_constant :SCAN_PATTERN
sig { params(url: T.any(String, Pathname), specs: T.untyped).returns(Version) }
@ -345,7 +344,7 @@ class Version
parse(specs.fetch(:tag, url), detected_from_url: true)
end
sig { params(spec: T.any(String, Pathname), detected_from_url: T::Boolean).returns(T.attached_class) }
sig { params(spec: T.any(String, Pathname), detected_from_url: T::Boolean).returns(Version) }
def self.parse(spec, detected_from_url: false)
spec = CGI.unescape(spec.to_s) if detected_from_url
@ -359,22 +358,22 @@ class Version
NULL
end
NUMERIC_WITH_OPTIONAL_DOTS = /(?:\d+(?:\.\d+)*)/.source.freeze
NUMERIC_WITH_OPTIONAL_DOTS = T.let(/(?:\d+(?:\.\d+)*)/.source.freeze, String)
private_constant :NUMERIC_WITH_OPTIONAL_DOTS
NUMERIC_WITH_DOTS = /(?:\d+(?:\.\d+)+)/.source.freeze
NUMERIC_WITH_DOTS = T.let(/(?:\d+(?:\.\d+)+)/.source.freeze, String)
private_constant :NUMERIC_WITH_DOTS
MINOR_OR_PATCH = /(?:\d+(?:\.\d+){1,2})/.source.freeze
MINOR_OR_PATCH = T.let(/(?:\d+(?:\.\d+){1,2})/.source.freeze, String)
private_constant :MINOR_OR_PATCH
CONTENT_SUFFIX = /(?:[._-](?i:bin|dist|stable|src|sources?|final|full))/.source.freeze
CONTENT_SUFFIX = T.let(/(?:[._-](?i:bin|dist|stable|src|sources?|final|full))/.source.freeze, String)
private_constant :CONTENT_SUFFIX
PRERELEASE_SUFFIX = /(?:[._-]?(?i:alpha|beta|pre|rc)\.?\d{,2})/.source.freeze
PRERELEASE_SUFFIX = T.let(/(?:[._-]?(?i:alpha|beta|pre|rc)\.?\d{,2})/.source.freeze, String)
private_constant :PRERELEASE_SUFFIX
VERSION_PARSERS = [
VERSION_PARSERS = T.let([
# date-based versioning
# e.g. `2023-09-28.tar.gz`
# e.g. `ltopers-v2017-04-14.tar.gz`
@ -486,15 +485,15 @@ class Version
# e.g. `https://secure.php.net/get/php-7.1.10.tar.bz2/from/this/mirror`
UrlParser.new(/[-.vV]?(#{NUMERIC_WITH_DOTS}#{PRERELEASE_SUFFIX}?)/),
].freeze
].freeze, T::Array[Version::Parser])
private_constant :VERSION_PARSERS
sig { params(val: T.any(PkgVersion, String, Version), detected_from_url: T::Boolean).void }
sig { params(val: T.any(String, Version), detected_from_url: T::Boolean).void }
def initialize(val, detected_from_url: false)
version = val.to_str
raise ArgumentError, "Version must not be empty" if version.blank?
@version = version
@version = T.let(version, String)
@detected_from_url = detected_from_url
end
@ -760,7 +759,10 @@ class Version
sig { returns(T::Array[Token]) }
def tokens
@tokens ||= version&.scan(SCAN_PATTERN)&.map { |token| Token.create(T.cast(token, String)) } || []
@tokens ||= T.let(
version&.scan(SCAN_PATTERN)&.map { |token| Token.create(T.cast(token, String)) } || [],
T.nilable(T::Array[Token]),
)
end
private
@ -773,5 +775,5 @@ class Version
# Represents the absence of a version.
#
# NOTE: Constructor needs to called with an arbitrary non-empty version which is then set to `nil`.
NULL = Version.new("NULL").tap { |v| v.instance_variable_set(:@version, nil) }.freeze
NULL = T.let(Version.new("NULL").tap { |v| v.instance_variable_set(:@version, nil) }.freeze, Version)
end

View File

@ -1,10 +0,0 @@
# typed: strict
class Version
# For `alias eql? ==`
# See discussions:
# - https://github.com/sorbet/sorbet/pull/1443
# - https://github.com/sorbet/sorbet/issues/2378
sig { params(other: T.untyped).returns(T::Boolean) }
def ==(other); end
end