Minor type safety improvements

This commit is contained in:
Douglas Eichelberger 2024-08-18 16:28:17 -07:00
parent 2374e988d4
commit fb2fdc5249
7 changed files with 22 additions and 27 deletions

View File

@ -56,24 +56,21 @@ module Cask
.returns(T.nilable(T.attached_class)) .returns(T.nilable(T.attached_class))
} }
def self.try_new(ref, warn: false) def self.try_new(ref, warn: false)
case ref return if ref.is_a?(Cask)
when Cask, URI::Generic
# do nothing
else
content = ref.to_str
# Cache compiled regex content = ref.to_str
@regex ||= begin
token = /(?:"[^"]*"|'[^']*')/
curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/
do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/
/\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m
end
return unless content.match?(@regex) # Cache compiled regex
@regex ||= begin
new(content) token = /(?:"[^"]*"|'[^']*')/
curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/
do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/
/\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m
end end
return unless content.match?(@regex)
new(content)
end end
sig { params(content: String, tap: Tap).void } sig { params(content: String, tap: Tap).void }

View File

@ -155,7 +155,7 @@ module Cask
# @api public # @api public
def method_missing(method, *args, &block) def method_missing(method, *args, &block)
if @dsl.respond_to?(method) if @dsl.respond_to?(method)
T.unsafe(@dsl).public_send(method, *args, &block) @dsl.public_send(method, *args, &block)
else else
super super
end end

View File

@ -36,7 +36,7 @@ module Homebrew
formula_or_cask.homepage formula_or_cask.homepage
end end
exec_browser(*T.unsafe(homepages)) exec_browser(*homepages)
end end
private private

View File

@ -50,9 +50,9 @@ module Homebrew
parallel = true parallel = true
files = if args.only only = args.only
# FIXME: This is safe once args are namespaced by command files = if only
test_name, line = T.unsafe(args.only).split(":", 2) test_name, line = only.split(":", 2)
if line.nil? if line.nil?
Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb") Dir.glob("test/{#{test_name},#{test_name}/**/*}_spec.rb")
@ -67,7 +67,7 @@ module Homebrew
end end
if files.blank? if files.blank?
raise UsageError, "The `--only` argument requires a valid file or folder name!" if args.only raise UsageError, "The `--only` argument requires a valid file or folder name!" if only
if args.changed? if args.changed?
opoo "No tests are directly associated with the changed files!" opoo "No tests are directly associated with the changed files!"

View File

@ -3124,7 +3124,7 @@ class Formula
removed = ENV.remove_cc_etc removed = ENV.remove_cc_etc
begin begin
T.unsafe(self).system("xcodebuild", *args) self.system("xcodebuild", *args)
ensure ensure
ENV.update(removed) ENV.update(removed)
end end

View File

@ -11,7 +11,7 @@ module Homebrew
attr_reader :column attr_reader :column
sig { params(line: Integer, column: T.nilable(Integer)).void } sig { params(line: Integer, column: T.nilable(Integer)).void }
def initialize(line, column = T.unsafe(nil)) def initialize(line, column = nil)
@line = line @line = line
@column = column @column = column
end end

View File

@ -731,12 +731,10 @@ class Version
def to_s = version.to_s def to_s = version.to_s
sig { params(options: T.untyped).returns(String) } sig { params(options: T.untyped).returns(String) }
def to_json(*options) def to_json(*options) = version.to_json(*options)
T.unsafe(version).to_json(*options)
end
sig { params(method: T.any(Symbol, String), include_all: T::Boolean).returns(T::Boolean) } sig { params(method: T.any(Symbol, String), include_all: T::Boolean).returns(T::Boolean) }
def respond_to?(method, include_all = T.unsafe(nil)) def respond_to?(method, include_all = false) # rubocop:disable Style/OptionalBooleanParameter (`Object` override)
return !null? if ["to_str", :to_str].include?(method) return !null? if ["to_str", :to_str].include?(method)
super super