rubocops/conflicts: use rubocop v1 API

This commit is contained in:
Jonathan Chang 2021-01-12 16:15:52 +11:00
parent 92e07f5c0b
commit f57c96465c
2 changed files with 35 additions and 58 deletions

View File

@ -9,6 +9,8 @@ module RuboCop
module FormulaAudit module FormulaAudit
# This cop audits versioned formulae for `conflicts_with`. # This cop audits versioned formulae for `conflicts_with`.
class Conflicts < FormulaCop class Conflicts < FormulaCop
extend AutoCorrector
MSG = "Versioned formulae should not use `conflicts_with`. " \ MSG = "Versioned formulae should not use `conflicts_with`. " \
"Use `keg_only :versioned_formula` instead." "Use `keg_only :versioned_formula` instead."
@ -19,31 +21,29 @@ module RuboCop
reason = parameters(conflicts_with_call).last.values.first reason = parameters(conflicts_with_call).last.values.first
offending_node(reason) offending_node(reason)
name = Regexp.new(@formula_name, Regexp::IGNORECASE) name = Regexp.new(@formula_name, Regexp::IGNORECASE)
reason = string_content(reason).sub(name, "") reason_text = string_content(reason).sub(name, "")
first_word = reason.split.first first_word = reason_text.split.first
if reason.match?(/\A[A-Z]/) if reason_text.match?(/\A[A-Z]/)
problem "'#{first_word}' from the `conflicts_with` reason should be '#{first_word.downcase}'." problem "'#{first_word}' from the `conflicts_with` reason "\
"should be '#{first_word.downcase}'." do |corrector|
reason_text[0] = reason_text[0].downcase
corrector.replace(reason.source_range, "\"#{reason_text}\"")
end
end end
next unless reason_text.end_with?(".")
problem "`conflicts_with` reason should not end with a period." if reason.end_with?(".") problem "`conflicts_with` reason should not end with a period." do |corrector|
corrector.replace(reason.source_range, "\"#{reason_text.chop}\"")
end
end end
return unless versioned_formula? return unless versioned_formula?
problem MSG if !tap_style_exception?(:versioned_formulae_conflicts_allowlist) && if !tap_style_exception?(:versioned_formulae_conflicts_allowlist) && method_called_ever?(body_node,
method_called_ever?(body_node, :conflicts_with) :conflicts_with)
end problem MSG do |corrector|
corrector.replace(@offensive_node.source_range, "keg_only :versioned_formula")
def autocorrect(node)
lambda do |corrector|
if versioned_formula?
corrector.replace(node.source_range, "keg_only :versioned_formula")
else
reason = string_content(node)
reason[0] = reason[0].downcase
reason = reason.delete_suffix(".")
corrector.replace(node.source_range, "\"#{reason}\"")
end end
end end
end end

View File

@ -7,7 +7,7 @@ describe RuboCop::Cop::FormulaAudit::Conflicts do
subject(:cop) { described_class.new } subject(:cop) { described_class.new }
context "when auditing `conflicts_with`" do context "when auditing `conflicts_with`" do
it "reports an offense if reason is capitalized" do it "reports and corrects an offense if reason is capitalized" do
expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb") expect_offense(<<~RUBY, "/homebrew-core/Formula/foo.rb")
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
@ -16,9 +16,17 @@ describe RuboCop::Cop::FormulaAudit::Conflicts do
conflicts_with "baz", :because => "Foo is the formula name which does not require downcasing" conflicts_with "baz", :because => "Foo is the formula name which does not require downcasing"
end end
RUBY RUBY
expect_correction(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
conflicts_with "bar", :because => "reason"
conflicts_with "baz", :because => "Foo is the formula name which does not require downcasing"
end
RUBY
end end
it "reports an offense if reason ends with a period" do it "reports and corrects an offense if reason ends with a period" do
expect_offense(<<~RUBY) expect_offense(<<~RUBY)
class Foo < Formula class Foo < Formula
url "https://brew.sh/foo-1.0.tgz" url "https://brew.sh/foo-1.0.tgz"
@ -26,6 +34,13 @@ describe RuboCop::Cop::FormulaAudit::Conflicts do
^^^^^^^^^ `conflicts_with` reason should not end with a period. ^^^^^^^^^ `conflicts_with` reason should not end with a period.
end end
RUBY RUBY
expect_correction(<<~RUBY)
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
conflicts_with "bar", "baz", :because => "reason"
end
RUBY
end end
it "reports an offense if it is present in a versioned formula" do it "reports an offense if it is present in a versioned formula" do
@ -46,43 +61,5 @@ describe RuboCop::Cop::FormulaAudit::Conflicts do
end end
RUBY RUBY
end end
it "auto-corrects capitalized reason" do
source = <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
conflicts_with "bar", :because => "Reason"
end
RUBY
corrected_source = <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
conflicts_with "bar", :because => "reason"
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end
it "auto-corrects trailing period" do
source = <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
conflicts_with "bar", :because => "reason."
end
RUBY
corrected_source = <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
conflicts_with "bar", :because => "reason"
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end
end end
end end