From c18ffa84ca8d1500ebd403955da61530db50f4f3 Mon Sep 17 00:00:00 2001 From: Jonathan Chang Date: Tue, 12 Jan 2021 11:14:12 +1100 Subject: [PATCH] rubocops/deprecate_disable: use rubocop v1 API --- .../Homebrew/rubocops/deprecate_disable.rb | 38 ++- .../test/rubocops/deprecate_disable_spec.rb | 276 ++++++++---------- 2 files changed, 135 insertions(+), 179 deletions(-) diff --git a/Library/Homebrew/rubocops/deprecate_disable.rb b/Library/Homebrew/rubocops/deprecate_disable.rb index 2c8cc4b7db..140e75e244 100644 --- a/Library/Homebrew/rubocops/deprecate_disable.rb +++ b/Library/Homebrew/rubocops/deprecate_disable.rb @@ -8,6 +8,8 @@ module RuboCop module FormulaAudit # This cop audits `deprecate!` and `disable!` dates. class DeprecateDisableDate < FormulaCop + extend AutoCorrector + def audit_formula(_node, _class_node, _parent_class_node, body_node) [:deprecate!, :disable!].each do |method| node = find_node_method_by_name(body_node, method) @@ -19,18 +21,13 @@ module RuboCop rescue ArgumentError fixed_date_string = Date.parse(string_content(date_node)).iso8601 offending_node(date_node) - problem "Use `#{fixed_date_string}` to comply with ISO 8601" + problem "Use `#{fixed_date_string}` to comply with ISO 8601" do |corrector| + corrector.replace(date_node.source_range, "\"#{fixed_date_string}\"") + end end end end - def autocorrect(node) - lambda do |corrector| - fixed_fixed_date_string = Date.parse(string_content(node)).iso8601 - corrector.replace(node.source_range, "\"#{fixed_fixed_date_string}\"") - end - end - def_node_search :date, <<~EOS (pair (sym :date) $str) EOS @@ -38,6 +35,8 @@ module RuboCop # This cop audits `deprecate!` and `disable!` reasons. class DeprecateDisableReason < FormulaCop + extend AutoCorrector + PUNCTUATION_MARKS = %w[. ! ?].freeze def audit_formula(_node, _class_node, _parent_class_node, body_node) @@ -54,9 +53,17 @@ module RuboCop offending_node(reason_node) reason_string = string_content(reason_node) - problem "Do not start the reason with `it`" if reason_string.start_with?("it ") + if reason_string.start_with?("it ") + problem "Do not start the reason with `it`" do |corrector| + corrector.replace(@offensive_node.source_range, "\"#{reason_string[3..]}\"") + end + end - problem "Do not end the reason with a punctuation mark" if PUNCTUATION_MARKS.include?(reason_string[-1]) + if PUNCTUATION_MARKS.include?(reason_string[-1]) + problem "Do not end the reason with a punctuation mark" do |corrector| + corrector.replace(@offensive_node.source_range, "\"#{reason_string.chop}\"") + end + end end next if reason_found @@ -70,17 +77,6 @@ module RuboCop end end - def autocorrect(node) - return unless node.str_type? - - lambda do |corrector| - reason = string_content(node) - reason = reason[3..] if reason.start_with?("it ") - reason.chop! if PUNCTUATION_MARKS.include?(reason[-1]) - corrector.replace(node.source_range, "\"#{reason}\"") - end - end - def_node_search :reason, <<~EOS (pair (sym :because) ${str sym}) EOS diff --git a/Library/Homebrew/test/rubocops/deprecate_disable_spec.rb b/Library/Homebrew/test/rubocops/deprecate_disable_spec.rb index 58572a91b1..59c40133d7 100644 --- a/Library/Homebrew/test/rubocops/deprecate_disable_spec.rb +++ b/Library/Homebrew/test/rubocops/deprecate_disable_spec.rb @@ -7,7 +7,7 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableDate do subject(:cop) { described_class.new } context "when auditing `deprecate!`" do - it "reports an offense if `date` is not ISO 8601 compliant" do + it "reports and corrects an offense if `date` is not ISO 8601 compliant" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -15,9 +15,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableDate do ^^^^^^^^^^^^^^^ Use `2020-06-25` to comply with ISO 8601 end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + deprecate! date: "2020-06-25" + end + RUBY end - it "reports an offense if `date` is not ISO 8601 compliant (with `reason`)" do + it "reports and corrects an offense if `date` is not ISO 8601 compliant (with `reason`)" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -25,6 +32,13 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableDate do ^^^^^^^^^^^^^^^ Use `2020-06-25` to comply with ISO 8601 end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + deprecate! because: "is broken", date: "2020-06-25" + end + RUBY end it "reports no offenses if `date` is ISO 8601 compliant" do @@ -62,48 +76,10 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableDate do end RUBY end - - it "auto-corrects `date` to ISO 8601 format" do - source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - deprecate! date: "June 25, 2020" - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - deprecate! date: "2020-06-25" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) - end - - it "auto-corrects `date` to ISO 8601 format (with `reason`)" do - source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - deprecate! because: "is broken", date: "June 25, 2020" - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - deprecate! because: "is broken", date: "2020-06-25" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) - end end context "when auditing `disable!`" do - it "reports an offense if `date` is not ISO 8601 compliant" do + it "reports and corrects an offense if `date` is not ISO 8601 compliant" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -111,9 +87,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableDate do ^^^^^^^^^^^^^^^ Use `2020-06-25` to comply with ISO 8601 end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + disable! date: "2020-06-25" + end + RUBY end - it "reports an offense if `date` is not ISO 8601 compliant (with `reason`)" do + it "reports and corrects an offense if `date` is not ISO 8601 compliant (with `reason`)" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -121,6 +104,13 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableDate do ^^^^^^^^^^^^^^^ Use `2020-06-25` to comply with ISO 8601 end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + disable! because: "is broken", date: "2020-06-25" + end + RUBY end it "reports no offenses if `date` is ISO 8601 compliant" do @@ -158,44 +148,6 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableDate do end RUBY end - - it "auto-corrects `date` to ISO 8601 format" do - source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - disable! date: "June 25, 2020" - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - disable! date: "2020-06-25" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) - end - - it "auto-corrects `date` to ISO 8601 format (with `reason`)" do - source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - disable! because: "is broken", date: "June 25, 2020" - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - disable! because: "is broken", date: "2020-06-25" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) - end end end @@ -259,7 +211,7 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do RUBY end - it "reports an offense if `reason` starts with 'it'" do + it "reports and corrects an offense if `reason` starts with 'it'" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -267,9 +219,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^^^ Do not start the reason with `it` end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + deprecate! because: "is broken" + end + RUBY end - it "reports an offense if `reason` starts with 'it' (with `date`)" do + it "reports and corrects an offense if `reason` starts with 'it' (with `date`)" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -277,9 +236,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^^^ Do not start the reason with `it` end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + deprecate! date: "2020-08-28", because: "is broken" + 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' @@ -287,9 +253,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^ Do not end the reason with a punctuation mark end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + deprecate! because: "is broken" + end + RUBY end - it "reports an offense if `reason` ends with an exclamation point" do + it "reports and corrects an offense if `reason` ends with an exclamation point" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -297,9 +270,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^ Do not end the reason with a punctuation mark end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + deprecate! because: "is broken" + end + RUBY end - it "reports an offense if `reason` ends with a question mark" do + it "reports and corrects an offense if `reason` ends with a question mark" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -307,9 +287,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^ Do not end the reason with a punctuation mark end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + deprecate! because: "is broken" + end + RUBY end - it "reports an offense if `reason` ends with a period (with `date`)" do + it "reports and corrects an offense if `reason` ends with a period (with `date`)" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -317,44 +304,13 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^ Do not end the reason with a punctuation mark end RUBY - end - it "auto-corrects `reason` to remove 'it'" do - source = <<~RUBY + expect_correction(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' - deprecate! because: "it is broken" + deprecate! date: "2020-08-28", because: "is broken" end RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - deprecate! because: "is broken" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) - end - - it "auto-corrects `reason` to remove punctuation" do - source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - deprecate! because: "is broken." - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - deprecate! because: "is broken" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) end end @@ -415,7 +371,7 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do RUBY end - it "reports an offense if `reason` starts with 'it'" do + it "reports and corrects an offense if `reason` starts with 'it'" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -423,9 +379,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^^^ Do not start the reason with `it` end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + disable! because: "is broken" + end + RUBY end - it "reports an offense if `reason` starts with 'it' (with `date`)" do + it "reports and corrects an offense if `reason` starts with 'it' (with `date`)" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -433,9 +396,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^^^ Do not start the reason with `it` end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + disable! date: "2020-08-28", because: "is broken" + 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' @@ -443,9 +413,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^ Do not end the reason with a punctuation mark end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + disable! because: "is broken" + end + RUBY end - it "reports an offense if `reason` ends with an exclamation point" do + it "reports and corrects an offense if `reason` ends with an exclamation point" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -453,9 +430,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^ Do not end the reason with a punctuation mark end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + disable! because: "is broken" + end + RUBY end - it "reports an offense if `reason` ends with a question mark" do + it "reports and corrects an offense if `reason` ends with a question mark" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -463,9 +447,16 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^ Do not end the reason with a punctuation mark end RUBY + + expect_correction(<<~RUBY) + class Foo < Formula + url 'https://brew.sh/foo-1.0.tgz' + disable! because: "is broken" + end + RUBY end - it "reports an offense if `reason` ends with a period (with `date`)" do + it "reports and corrects an offense if `reason` ends with a period (with `date`)" do expect_offense(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' @@ -473,44 +464,13 @@ describe RuboCop::Cop::FormulaAudit::DeprecateDisableReason do ^^^^^^^^^^^^ Do not end the reason with a punctuation mark end RUBY - end - it "auto-corrects to remove 'it'" do - source = <<~RUBY + expect_correction(<<~RUBY) class Foo < Formula url 'https://brew.sh/foo-1.0.tgz' - disable! because: "it is broken" + disable! date: "2020-08-28", because: "is broken" end RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - disable! because: "is broken" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) - end - - it "auto-corrects to remove punctuation" do - source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - disable! because: "is broken." - end - RUBY - - corrected_source = <<~RUBY - class Foo < Formula - url 'https://brew.sh/foo-1.0.tgz' - disable! because: "is broken" - end - RUBY - - new_source = autocorrect_source(source) - expect(new_source).to eq(corrected_source) end end end