diff --git a/Library/Homebrew/rubocops/class.rb b/Library/Homebrew/rubocops/class.rb index 4a2a408ac8..5e1fa9ddf7 100644 --- a/Library/Homebrew/rubocops/class.rb +++ b/Library/Homebrew/rubocops/class.rb @@ -10,6 +10,8 @@ module RuboCop # # @api private class ClassName < FormulaCop + extend AutoCorrector + DEPRECATED_CLASSES = %w[ GithubGistFormula ScriptFileFormula @@ -20,12 +22,8 @@ module RuboCop parent_class = class_name(parent_class_node) return unless DEPRECATED_CLASSES.include?(parent_class) - problem "#{parent_class} is deprecated, use Formula instead" - end - - def autocorrect(node) - lambda do |corrector| - corrector.replace(node.source_range, "Formula") + problem "#{parent_class} is deprecated, use Formula instead" do |corrector| + corrector.replace(parent_class_node.source_range, "Formula") end end end @@ -34,6 +32,8 @@ module RuboCop # # @api private class Test < FormulaCop + extend AutoCorrector + def audit_formula(_node, _class_node, _parent_class_node, body_node) test = find_block(body_node, :test) return unless test @@ -49,31 +49,17 @@ module RuboCop p1, p2 = params if match = string_content(p1).match(%r{(/usr/local/(s?bin))}) offending_node(p1) - problem "use \#{#{match[2]}} instead of #{match[1]} in #{node}" + problem "use \#{#{match[2]}} instead of #{match[1]} in #{node}" do |corrector| + corrector.replace(p1.source_range, p1.source.sub(match[1], "\#{#{match[2]}}")) + end end if node == :shell_output && node_equals?(p2, 0) offending_node(p2) - problem "Passing 0 to shell_output() is redundant" - end - end - end - - def autocorrect(node) - lambda do |corrector| - case node.type - when :str, :dstr - # Rubocop: intentionally outputted non-interpolated strings - corrector.replace(node.source_range, - node.source.to_s.sub(%r{(/usr/local/(s?bin))}, - '#{\2}')) # rubocop:disable Lint/InterpolationCheck - when :int - corrector.remove( - range_with_surrounding_comma( - range_with_surrounding_space(range: node.source_range, - side: :left), - ), - ) + problem "Passing 0 to shell_output() is redundant" do |corrector| + corrector.remove(range_with_surrounding_comma(range_with_surrounding_space(range: p2.source_range, + side: :left))) + end end end end diff --git a/Library/Homebrew/test/rubocops/class_spec.rb b/Library/Homebrew/test/rubocops/class_spec.rb index 1f0c84ab27..15c793b229 100644 --- a/Library/Homebrew/test/rubocops/class_spec.rb +++ b/Library/Homebrew/test/rubocops/class_spec.rb @@ -6,55 +6,47 @@ require "rubocops/class" describe RuboCop::Cop::FormulaAudit::ClassName do subject(:cop) { described_class.new } - it "reports an offense when using ScriptFileFormula" do + corrected_source = <<~RUBY + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + end + RUBY + + it "reports and corrects an offense when using ScriptFileFormula" do expect_offense(<<~RUBY) class Foo < ScriptFileFormula ^^^^^^^^^^^^^^^^^ ScriptFileFormula is deprecated, use Formula instead url 'https://brew.sh/foo-1.0.tgz' end RUBY + expect_correction(corrected_source) end - it "reports an offense when using GithubGistFormula" do + it "reports and corrects an offense when using GithubGistFormula" do expect_offense(<<~RUBY) class Foo < GithubGistFormula ^^^^^^^^^^^^^^^^^ GithubGistFormula is deprecated, use Formula instead url 'https://brew.sh/foo-1.0.tgz' end RUBY + expect_correction(corrected_source) end - it "reports an offense when using AmazonWebServicesFormula" do + it "reports and corrects an offense when using AmazonWebServicesFormula" do expect_offense(<<~RUBY) class Foo < AmazonWebServicesFormula ^^^^^^^^^^^^^^^^^^^^^^^^ AmazonWebServicesFormula is deprecated, use Formula instead url 'https://brew.sh/foo-1.0.tgz' end RUBY - end - - it "supports auto-correcting deprecated parent classes" do - source = <<~RUBY - class Foo < AmazonWebServicesFormula - url 'https://brew.sh/foo-1.0.tgz' - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) + expect_correction(corrected_source) end end describe RuboCop::Cop::FormulaAudit::Test do subject(:cop) { described_class.new } - it "reports an offense when /usr/local/bin is found in test calls" do + it "reports and corrects an offense when /usr/local/bin is found in test calls" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -65,9 +57,19 @@ describe RuboCop::Cop::FormulaAudit::Test do end end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + + test do + system "\#{bin}/test" + end + end + RUBY end - it "reports an offense when passing 0 as the second parameter to shell_output" do + it "reports and corrects an offense when passing 0 as the second parameter to shell_output" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -78,6 +80,16 @@ describe RuboCop::Cop::FormulaAudit::Test do end end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + + test do + shell_output("\#{bin}/test") + end + end + RUBY end it "reports an offense when there is an empty test block" do @@ -104,31 +116,6 @@ describe RuboCop::Cop::FormulaAudit::Test do end RUBY end - - it "supports auto-correcting test calls" do - source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - - test do - shell_output("/usr/local/sbin/test", 0) - end - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - - test do - shell_output("\#{sbin}/test") - end - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) - end end describe RuboCop::Cop::FormulaAuditStrict::TestPresent do