rubocops/conflicts: use rubocop v1 API
This commit is contained in:
parent
92e07f5c0b
commit
f57c96465c
@ -9,6 +9,8 @@ module RuboCop
|
||||
module FormulaAudit
|
||||
# This cop audits versioned formulae for `conflicts_with`.
|
||||
class Conflicts < FormulaCop
|
||||
extend AutoCorrector
|
||||
|
||||
MSG = "Versioned formulae should not use `conflicts_with`. " \
|
||||
"Use `keg_only :versioned_formula` instead."
|
||||
|
||||
@ -19,31 +21,29 @@ module RuboCop
|
||||
reason = parameters(conflicts_with_call).last.values.first
|
||||
offending_node(reason)
|
||||
name = Regexp.new(@formula_name, Regexp::IGNORECASE)
|
||||
reason = string_content(reason).sub(name, "")
|
||||
first_word = reason.split.first
|
||||
reason_text = string_content(reason).sub(name, "")
|
||||
first_word = reason_text.split.first
|
||||
|
||||
if reason.match?(/\A[A-Z]/)
|
||||
problem "'#{first_word}' from the `conflicts_with` reason should be '#{first_word.downcase}'."
|
||||
if reason_text.match?(/\A[A-Z]/)
|
||||
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
|
||||
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
|
||||
|
||||
return unless versioned_formula?
|
||||
|
||||
problem MSG if !tap_style_exception?(:versioned_formulae_conflicts_allowlist) &&
|
||||
method_called_ever?(body_node, :conflicts_with)
|
||||
end
|
||||
|
||||
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}\"")
|
||||
if !tap_style_exception?(:versioned_formulae_conflicts_allowlist) && method_called_ever?(body_node,
|
||||
:conflicts_with)
|
||||
problem MSG do |corrector|
|
||||
corrector.replace(@offensive_node.source_range, "keg_only :versioned_formula")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -7,7 +7,7 @@ describe RuboCop::Cop::FormulaAudit::Conflicts do
|
||||
subject(:cop) { described_class.new }
|
||||
|
||||
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")
|
||||
class Foo < Formula
|
||||
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"
|
||||
end
|
||||
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
|
||||
|
||||
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)
|
||||
class Foo < Formula
|
||||
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.
|
||||
end
|
||||
RUBY
|
||||
|
||||
expect_correction(<<~RUBY)
|
||||
class Foo < Formula
|
||||
url "https://brew.sh/foo-1.0.tgz"
|
||||
conflicts_with "bar", "baz", :because => "reason"
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
|
||||
it "reports an offense if it is present in a versioned formula" do
|
||||
@ -46,43 +61,5 @@ describe RuboCop::Cop::FormulaAudit::Conflicts do
|
||||
end
|
||||
RUBY
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user