Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rubocops (cask/url): add rubocop to use csv instead of before|after_comma #12648

Merged
merged 7 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Library/Homebrew/rubocops/cask/mixin/on_url_stanza.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# typed: false
# frozen_string_literal: true

module RuboCop
module Cop
module Cask
# Common functionality for checking url stanzas.
module OnUrlStanza
extend Forwardable
include CaskHelp

def on_cask(cask_block)
@cask_block = cask_block

toplevel_stanzas.select(&:url?).each do |stanza|
on_url_stanza(stanza)
end
end

private

attr_reader :cask_block

def_delegators :cask_block,
:toplevel_stanzas
end
end
end
end
38 changes: 38 additions & 0 deletions Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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'"\
"and 'version.csv.second' instead of 'version.after_comma'."

def on_url_stanza(stanza)
return if stanza.stanza_node.type == :block

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
2 changes: 2 additions & 0 deletions Library/Homebrew/rubocops/rubocop-cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
require_relative "cask/extend/node"
require_relative "cask/mixin/cask_help"
require_relative "cask/mixin/on_homepage_stanza"
require_relative "cask/mixin/on_url_stanza"
require_relative "cask/desc"
require_relative "cask/homepage_url_trailing_slash"
require_relative "cask/no_dsl_version"
require_relative "cask/stanza_order"
require_relative "cask/stanza_grouping"
require_relative "cask/url_legacy_comma_separators"
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
sha256 "a69e7357bea014f4c14ac9699274f559086844ffa46563c4619bf1addfd72ad9"

# rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1 was verified as official when first introduced to the cask
url "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1/app_versions/#{version.after_comma}?format=zip"
url "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1/app_versions/#{version.csv.second}?format=zip"
appcast "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1"
name "HockeyApp"
homepage "https://www.brew.sh/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
sha256 "a69e7357bea014f4c14ac9699274f559086844ffa46563c4619bf1addfd72ad9"

# rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1 was verified as official when first introduced to the cask
url "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1/app_versions/#{version.after_comma}?format=zip"
url "https://rink.hockeyapp.net/api/2/apps/67503a7926431872c4b6c1549f5bd6b1/app_versions/#{version.csv.second}?format=zip"
name "HockeyApp"
homepage "https://www.brew.sh/"

Expand Down