2023-03-04 22:23:32 +00:00
|
|
|
# typed: true
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RuboCop
|
|
|
|
module Cop
|
|
|
|
module Cask
|
|
|
|
# This cop checks that a cask's `url` stanza is formatted correctly.
|
|
|
|
#
|
|
|
|
# @example
|
|
|
|
# # bad
|
2023-04-02 15:29:17 +01:00
|
|
|
# url "https://example.com/download/foo.dmg",
|
2023-04-02 16:38:33 +01:00
|
|
|
# verified: "https://example.com/download"
|
2023-03-04 22:23:32 +00:00
|
|
|
#
|
|
|
|
#
|
|
|
|
# # good
|
2023-04-02 15:29:17 +01:00
|
|
|
# url "https://example.com/download/foo.dmg",
|
2023-04-02 16:38:33 +01:00
|
|
|
# verified: "example.com/download/"
|
2023-03-04 22:23:32 +00:00
|
|
|
#
|
|
|
|
class Url < Base
|
|
|
|
extend AutoCorrector
|
|
|
|
extend Forwardable
|
|
|
|
include OnUrlStanza
|
|
|
|
|
|
|
|
def on_url_stanza(stanza)
|
|
|
|
return if stanza.stanza_node.block_type?
|
|
|
|
|
2023-04-02 19:39:23 +01:00
|
|
|
url_string = stanza.stanza_node.first_argument.str_content
|
2023-03-04 22:23:32 +00:00
|
|
|
hash_node = stanza.stanza_node.last_argument
|
|
|
|
return unless hash_node.hash_type?
|
|
|
|
|
|
|
|
hash_node.each_pair do |key_node, value_node|
|
|
|
|
next unless key_node.source == "verified"
|
|
|
|
next unless value_node.str_type?
|
2023-04-02 15:29:17 +01:00
|
|
|
|
|
|
|
if value_node.source.start_with?(%r{^"https?://})
|
|
|
|
add_offense(
|
|
|
|
value_node.source_range,
|
|
|
|
message: "Verified URL parameter value should not start with https:// or http://.",
|
|
|
|
) do |corrector|
|
|
|
|
corrector.replace(value_node.source_range, value_node.source.gsub(%r{^"https?://}, "\""))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-02 19:39:23 +01:00
|
|
|
# Skip if the URL and the verified value are the same.
|
|
|
|
next if value_node.str_content == url_string.delete_prefix("https://").delete_prefix("http://")
|
|
|
|
# Skip if the verified value ends with a slash.
|
2023-04-02 15:29:17 +01:00
|
|
|
next if value_node.str_content.end_with?("/")
|
2023-03-04 22:23:32 +00:00
|
|
|
|
|
|
|
add_offense(
|
|
|
|
value_node.source_range,
|
2023-04-02 15:29:17 +01:00
|
|
|
message: "Verified URL parameter value should end with a /.",
|
2023-03-04 22:23:32 +00:00
|
|
|
) do |corrector|
|
2023-04-02 15:29:17 +01:00
|
|
|
corrector.replace(value_node.source_range, value_node.source.gsub(/"$/, "/\""))
|
2023-03-04 22:23:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|