brew/Library/Homebrew/rubocops/cask/homepage_url_trailing_slash.rb

46 lines
1.3 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: true
# frozen_string_literal: true
require "forwardable"
require "uri"
module RuboCop
module Cop
module Cask
# This cop checks that a cask's homepage ends with a slash
# if it does not have a path component.
2021-01-12 18:25:05 +11:00
class HomepageUrlTrailingSlash < Base
include OnHomepageStanza
2022-04-27 17:15:03 +01:00
include HelperFunctions
2021-01-12 18:25:05 +11:00
extend AutoCorrector
2019-10-03 08:50:45 +02:00
MSG_NO_SLASH = "'%<url>s' must have a slash after the domain."
def on_homepage_stanza(stanza)
url_node = stanza.stanza_node.first_argument
2019-10-16 19:06:39 +02:00
url = if url_node.dstr_type?
# Remove quotes from interpolated string.
url_node.source[1..-2]
else
url_node.str_content
end
2019-10-16 18:37:20 +02:00
return unless url&.match?(%r{^.+://[^/]+$})
2022-04-27 17:15:03 +01:00
domain = URI(string_content(url_node, strip_dynamic: true)).host
return if domain.blank?
# This also takes URLs like 'https://example.org?path'
# and 'https://example.org#path' into account.
2021-01-12 18:25:05 +11:00
corrected_source = url_node.source.sub("://#{domain}", "://#{domain}/")
2024-03-07 16:20:20 +00:00
add_offense(url_node.loc.expression, message: format(MSG_NO_SLASH, url:)) do |corrector|
2021-01-12 18:25:05 +11:00
corrector.replace(url_node.source_range, corrected_source)
end
end
end
end
end
end