Merge pull request #18019 from Homebrew/safe-try-new

This commit is contained in:
Mike McQuaid 2024-08-12 14:21:44 +01:00 committed by GitHub
commit 6b3cac7b89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -51,22 +51,29 @@ module Cask
# Loads a cask from a string.
class FromContentLoader < AbstractContentLoader
sig {
params(ref: T.any(Pathname, String, Cask, URI::Generic), warn: T::Boolean)
.returns(T.nilable(T.attached_class))
}
def self.try_new(ref, warn: false)
return false unless ref.respond_to?(:to_str)
case ref
when Cask, URI::Generic
# do nothing
else
content = ref.to_str
content = T.unsafe(ref).to_str
# Cache compiled regex
@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
# Cache compiled regex
@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
return unless content.match?(@regex)
new(content)
end
return unless content.match?(@regex)
new(content)
end
sig { params(content: String, tap: Tap).void }