Merge pull request #7898 from Rylan12/keg-only-audit

style: improve keg_only style checks
This commit is contained in:
Rylan Polster 2020-07-04 10:12:05 -04:00 committed by GitHub
commit 4290789dc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 6 deletions

View File

@ -22,18 +22,28 @@ module RuboCop
Firefox Firefox
].freeze ].freeze
reason = string_content(parameters(keg_only_node).first) reason = parameters(keg_only_node).first
offending_node(reason)
name = Regexp.new(@formula_name, Regexp::IGNORECASE) name = Regexp.new(@formula_name, Regexp::IGNORECASE)
reason = reason.sub(name, "") reason = string_content(reason).sub(name, "")
first_word = reason.split.first first_word = reason.split.first
if reason =~ /\A[A-Z]/ && !reason.start_with?(*allowlist) if reason =~ /\A[A-Z]/ && !reason.start_with?(*allowlist)
problem "'#{first_word}' from the keg_only reason should be '#{first_word.downcase}'." problem "'#{first_word}' from the `keg_only` reason should be '#{first_word.downcase}'."
end end
return unless reason.end_with?(".") return unless reason.end_with?(".")
problem "keg_only reason should not end with a period." problem "`keg_only` reason should not end with a period."
end
def autocorrect(node)
lambda do |corrector|
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 end

View File

@ -13,7 +13,7 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do
homepage "https://brew.sh" homepage "https://brew.sh"
keg_only "Because why not" keg_only "Because why not"
^^^^^^^^^^^^^^^^^^^^^^^^^^ 'Because' from the keg_only reason should be 'because'. ^^^^^^^^^^^^^^^^^ 'Because' from the `keg_only` reason should be 'because'.
end end
RUBY RUBY
end end
@ -25,11 +25,53 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do
homepage "https://brew.sh" homepage "https://brew.sh"
keg_only "ending with a period." keg_only "ending with a period."
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ keg_only reason should not end with a period. ^^^^^^^^^^^^^^^^^^^^^^^ `keg_only` reason should not end with a period.
end end
RUBY RUBY
end end
specify "keg_only_autocorrects_downcasing" do
source = <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"
keg_only "Because why not"
end
RUBY
corrected_source = <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"
keg_only "because why not"
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end
specify "keg_only_autocorrects_redundant_period" do
source = <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"
keg_only "ending with a period."
end
RUBY
corrected_source = <<~RUBY
class Foo < Formula
url "https://brew.sh/foo-1.0.tgz"
homepage "https://brew.sh"
keg_only "ending with a period"
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end
specify "keg_only_handles_block_correctly" do specify "keg_only_handles_block_correctly" do
expect_no_offenses(<<~RUBY) expect_no_offenses(<<~RUBY)
class Foo < Formula class Foo < Formula