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

39 lines
1.1 KiB
Ruby
Raw Normal View History

# typed: true
# frozen_string_literal: true
require "forwardable"
require "uri"
module RuboCop
module Cop
module Cask
# This cop checks for version.before_comma and version.after_comma
class UrlLegacyCommaSeparators < Base
include OnUrlStanza
extend AutoCorrector
MSG_CSV = "Use 'version.csv.first' instead of 'version.before_comma' " \
2022-01-10 21:34:51 +11:00
"and 'version.csv.second' instead of 'version.after_comma'."
2022-01-10 21:34:51 +11:00
def on_url_stanza(stanza)
2022-01-10 21:27:20 +11:00
return if stanza.stanza_node.type == :block
2022-01-10 21:34:51 +11:00
url_node = stanza.stanza_node.first_argument
legacy_comma_separator_pattern = /version.(before|after)_comma/
url = url_node.source
return unless url.match?(legacy_comma_separator_pattern)
corrected_url = url.sub("before_comma", "csv.first")&.sub("after_comma", "csv.second")
add_offense(url_node.loc.expression, message: format(MSG_CSV, url: url)) do |corrector|
corrector.replace(url_node.source_range, corrected_url)
end
end
end
end
end
end