-
-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12648 from bevanjkay/cask-comma-separator-style
rubocops (cask/url): add rubocop to use csv instead of before|after_comma
- Loading branch information
Showing
6 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
38
Library/Homebrew/rubocops/cask/url_legacy_comma_separators.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
Library/Homebrew/test/rubocops/cask/url_legacy_comma_separators_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require "rubocops/rubocop-cask" | ||
require "test/rubocops/cask/shared_examples/cask_cop" | ||
|
||
describe RuboCop::Cop::Cask::UrlLegacyCommaSeparators do | ||
include CaskCop | ||
|
||
subject(:cop) { described_class.new } | ||
|
||
context "when url version interpolation does not include version.before_comma or version.after_comma" do | ||
let(:source) do | ||
<<-CASK.undent | ||
cask 'foo' do | ||
version '1.1' | ||
url 'https://foo.brew.sh/foo-\#{version}.dmg' | ||
end | ||
CASK | ||
end | ||
|
||
include_examples "does not report any offenses" | ||
end | ||
|
||
context "when the url uses csv" do | ||
let(:source) do | ||
<<-CASK.undent | ||
cask 'foo' do | ||
version '1.1,111' | ||
url 'https://foo.brew.sh/foo-\#{version.csv.first}.dmg' | ||
end | ||
CASK | ||
end | ||
|
||
include_examples "does not report any offenses" | ||
end | ||
|
||
context "when the url uses version.before_comma" do | ||
let(:source) do | ||
<<-CASK.undent | ||
cask 'foo' do | ||
version '1.1,111' | ||
url 'https://foo.brew.sh/foo-\#{version.before_comma}.dmg' | ||
end | ||
CASK | ||
end | ||
let(:correct_source) do | ||
<<-CASK.undent | ||
cask 'foo' do | ||
version '1.1,111' | ||
url 'https://foo.brew.sh/foo-\#{version.csv.first}.dmg' | ||
end | ||
CASK | ||
end | ||
let(:expected_offenses) do | ||
[{ | ||
message: "Use 'version.csv.first' instead of 'version.before_comma' "\ | ||
"and 'version.csv.second' instead of 'version.after_comma'", | ||
severity: :convention, | ||
line: 3, | ||
column: 6, | ||
source: "'https://foo.brew.sh/foo-\#{version.before_comma}.dmg'", | ||
}] | ||
end | ||
|
||
include_examples "reports offenses" | ||
|
||
include_examples "autocorrects source" | ||
end | ||
|
||
context "when the url uses version.after_comma" do | ||
let(:source) do | ||
<<-CASK.undent | ||
cask 'foo' do | ||
version '1.1,111' | ||
url 'https://foo.brew.sh/foo-\#{version.after_comma}.dmg' | ||
end | ||
CASK | ||
end | ||
let(:correct_source) do | ||
<<-CASK.undent | ||
cask 'foo' do | ||
version '1.1,111' | ||
url 'https://foo.brew.sh/foo-\#{version.csv.second}.dmg' | ||
end | ||
CASK | ||
end | ||
let(:expected_offenses) do | ||
[{ | ||
message: "Use 'version.csv.first' instead of 'version.before_comma' "\ | ||
"and 'version.csv.second' instead of 'version.after_comma'", | ||
severity: :convention, | ||
line: 3, | ||
column: 6, | ||
source: "'https://foo.brew.sh/foo-\#{version.after_comma}.dmg'", | ||
}] | ||
end | ||
|
||
include_examples "reports offenses" | ||
|
||
include_examples "autocorrects source" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters