From e252de5295aa2991c1e0b21af0ce7975766ad742 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Fri, 3 Jul 2020 14:35:32 -0400 Subject: [PATCH] style: improve keg_only style checks --- Library/Homebrew/rubocops/keg_only.rb | 18 ++++++-- .../Homebrew/test/rubocops/keg_only_spec.rb | 46 ++++++++++++++++++- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/Library/Homebrew/rubocops/keg_only.rb b/Library/Homebrew/rubocops/keg_only.rb index 0c3d76ff63..820bf79541 100644 --- a/Library/Homebrew/rubocops/keg_only.rb +++ b/Library/Homebrew/rubocops/keg_only.rb @@ -22,18 +22,28 @@ module RuboCop Firefox ].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) - reason = reason.sub(name, "") + reason = string_content(reason).sub(name, "") first_word = reason.split.first 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 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 diff --git a/Library/Homebrew/test/rubocops/keg_only_spec.rb b/Library/Homebrew/test/rubocops/keg_only_spec.rb index dd01dbb327..2f0af35aab 100644 --- a/Library/Homebrew/test/rubocops/keg_only_spec.rb +++ b/Library/Homebrew/test/rubocops/keg_only_spec.rb @@ -13,7 +13,7 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do homepage "https://brew.sh" keg_only "Because why not" - ^^^^^^^^^^^^^^^^^^^^^^^^^^ 'Because' from the keg_only reason should be 'because'. + ^^^^^^^^^^^^^^^^^ 'Because' from the `keg_only` reason should be 'because'. end RUBY end @@ -25,11 +25,53 @@ describe RuboCop::Cop::FormulaAudit::KegOnly do homepage "https://brew.sh" 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 RUBY 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 expect_no_offenses(<<~RUBY) class Foo < Formula