From 0b79eda05b489c6b0fee96cf5df175b5541e04a1 Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Thu, 9 Feb 2023 02:21:39 +0900 Subject: [PATCH] Use `Cop::Base` API Follow https://github.com/rubocop/rubocop/pull/7868. The legacy `Cop::Cop` API is soft deprecated and this PR use new `Cop::Base` API instead. > maintain any RuboCop extensions, as the legacy API will be removed in > RuboCop 2.0. https://metaredux.com/posts/2020/10/21/rubocop-1-0.html --- .rubocop_todo.yml | 2 +- .../i18n/gettext/decorate_function_message.rb | 31 ++++++++++--------- .../cop/i18n/gettext/decorate_string.rb | 19 +++++------- ...e_string_formatting_using_interpolation.rb | 2 +- ...ecorate_string_formatting_using_percent.rb | 2 +- .../cop/i18n/rails_i18n/decorate_string.rb | 2 +- ...e_string_formatting_using_interpolation.rb | 2 +- .../gettext/decorate_function_message_spec.rb | 12 +++---- ...ing_formatting_using_interpolation_spec.rb | 4 +-- ...te_string_formatting_using_percent_spec.rb | 4 +-- .../cop/i18n/gettext/decorate_string_spec.rb | 4 +-- ...ing_formatting_using_interpolation_spec.rb | 4 +-- .../i18n/rails_i18n/decorate_string_spec.rb | 4 +-- spec/shared_examples.rb | 11 +++---- spec/shared_functions.rb | 6 ++-- 15 files changed, 53 insertions(+), 56 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index a3bc6a4..0cb0482 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -27,7 +27,7 @@ Metrics/AbcSize: # Offense count: 1 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 108 + Max: 109 # Offense count: 5 # Configuration parameters: IgnoredMethods. diff --git a/lib/rubocop/cop/i18n/gettext/decorate_function_message.rb b/lib/rubocop/cop/i18n/gettext/decorate_function_message.rb index 5ab7b07..389b85e 100644 --- a/lib/rubocop/cop/i18n/gettext/decorate_function_message.rb +++ b/lib/rubocop/cop/i18n/gettext/decorate_function_message.rb @@ -4,7 +4,9 @@ module RuboCop module Cop module I18n module GetText - class DecorateFunctionMessage < Cop + class DecorateFunctionMessage < Base + extend AutoCorrector + def on_send(node) method_name = node.loc.selector.source return unless GetText.supported_method?(method_name) @@ -22,14 +24,6 @@ def on_send(node) end end - def autocorrect(node) - if node.str_type? - single_string_correct(node) - elsif interpolation_offense?(node) - # interpolation_correct(node) - end - end - private def already_decorated?(node, parent = nil) @@ -65,7 +59,17 @@ def detect_and_report(_node, message_section, method_name) error_message << 'message should be decorated. ' if error == :no_decoration end error_message = error_message.join('\n') - add_offense(message_section, message: error_message) + add_offense(message_section, message: error_message) do |corrector| + autocorrect(corrector, message_section) + end + end + + def autocorrect(corrector, node) + if node.str_type? + single_string_correct(corrector, node) + elsif interpolation_offense?(node) + # interpolation_correct(node) + end end def how_bad_is_it(message_section) @@ -106,11 +110,8 @@ def interpolation_offense?(node, parent = nil) node.children.any? { |child| interpolation_offense?(child, parent) } end - def single_string_correct(node) - lambda { |corrector| - corrector.insert_before(node.source_range, '_(') - corrector.insert_after(node.source_range, ')') - } + def single_string_correct(corrector, node) + corrector.wrap(node.source_range, '_(', ')') end def interpolation_correct(node) diff --git a/lib/rubocop/cop/i18n/gettext/decorate_string.rb b/lib/rubocop/cop/i18n/gettext/decorate_string.rb index 82526aa..f382cd0 100644 --- a/lib/rubocop/cop/i18n/gettext/decorate_string.rb +++ b/lib/rubocop/cop/i18n/gettext/decorate_string.rb @@ -19,7 +19,9 @@ module GetText # # good # # _("Result is good.") - class DecorateString < Cop + class DecorateString < Base + extend AutoCorrector + STRING_REGEXP = /^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/.freeze def on_dstr(node) @@ -37,10 +39,6 @@ def on_str(node) check_for_parent_decorator(node) end - def autocorrect(node) - single_string_correct(node) if node.str_type? - end - private def sentence?(node) @@ -70,14 +68,13 @@ def check_for_parent_decorator(node) elsif parent.respond_to?(:method_name) && parent.method?(:[]) return end - add_offense(node, message: 'decorator is missing around sentence') + add_offense(node, message: 'decorator is missing around sentence') do |corrector| + single_string_correct(corrector, node) if node.str_type? + end end - def single_string_correct(node) - lambda { |corrector| - corrector.insert_before(node.source_range, '_(') - corrector.insert_after(node.source_range, ')') - } + def single_string_correct(corrector, node) + corrector.wrap(node.source_range, '_(', ')') end end end diff --git a/lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation.rb b/lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation.rb index b3e329a..afb1216 100644 --- a/lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation.rb +++ b/lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation.rb @@ -22,7 +22,7 @@ module GetText # # _("result is %{detail}" % {detail: message}) # - class DecorateStringFormattingUsingInterpolation < Cop + class DecorateStringFormattingUsingInterpolation < Base def on_send(node) decorator_name = node.loc.selector.source return unless GetText.supported_decorator?(decorator_name) diff --git a/lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent.rb b/lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent.rb index 45c9b27..e71b83b 100644 --- a/lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent.rb +++ b/lib/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent.rb @@ -23,7 +23,7 @@ module GetText # # _("result is %{detail}" % {detail: message}) # - class DecorateStringFormattingUsingPercent < Cop + class DecorateStringFormattingUsingPercent < Base SUPPORTED_FORMATS = %w[b B d i o u x X e E f g G a A c p s].freeze def on_send(node) diff --git a/lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb b/lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb index 6ccfe6d..5add13c 100644 --- a/lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb +++ b/lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb @@ -70,7 +70,7 @@ module RailsI18n # "Any other string is fine now" # t("only_this_text") # - class DecorateString < Cop + class DecorateString < Base SENTENCE_REGEXP = /^\s*[[:upper:]][[:alpha:]]*[[:blank:]]+.*[.!?]$/.freeze FRAGMENTED_SENTENCE_REGEXP = /^\s*([[:upper:]][[:alpha:]]*[[:blank:]]+.*)|([[:alpha:]]*[[:blank:]]+.*[.!?])$/.freeze FRAGMENT_REGEXP = /^\s*[[:alpha:]]*[[:blank:]]+.*$/.freeze diff --git a/lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb b/lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb index 01a3087..53495e6 100644 --- a/lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb +++ b/lib/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation.rb @@ -20,7 +20,7 @@ module RailsI18n # # t("status.accepted") # - class DecorateStringFormattingUsingInterpolation < Cop + class DecorateStringFormattingUsingInterpolation < Base def on_send(node) return unless node&.loc&.selector diff --git a/spec/rubocop/cop/i18n/gettext/decorate_function_message_spec.rb b/spec/rubocop/cop/i18n/gettext/decorate_function_message_spec.rb index 0501177..e55b27e 100644 --- a/spec/rubocop/cop/i18n/gettext/decorate_function_message_spec.rb +++ b/spec/rubocop/cop/i18n/gettext/decorate_function_message_spec.rb @@ -2,9 +2,9 @@ require 'spec_helper' -describe RuboCop::Cop::I18n::GetText::DecorateFunctionMessage, :config, :config do +describe RuboCop::Cop::I18n::GetText::DecorateFunctionMessage, :config do before(:each) do - investigate(cop, source) + @offenses = investigate(cop, source) end RuboCop::Cop::I18n::GetText.supported_methods.each do |function| @@ -52,13 +52,13 @@ let(:source) { "raise(Puppet::ParseError, \"mysql_password(): Wrong number of arguments \" \\\n \"given (\#{args.size} for 1)\")" } it 'has the correct error message' do - expect(cop.offenses[0]).not_to be_nil - expect(cop.offenses[0].message).to match(/message should not be a multi-line string/) - expect(cop.offenses[0].message).to match(/message should use correctly formatted interpolation/) + expect(@offenses[0]).not_to be_nil + expect(@offenses[0].message).to match(/message should not be a multi-line string/) + expect(@offenses[0].message).to match(/message should use correctly formatted interpolation/) end it 'has the correct number of offenses' do - expect(cop.offenses.size).to eq(1) + expect(@offenses.size).to eq(1) end it 'autocorrects', broken: true do diff --git a/spec/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation_spec.rb b/spec/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation_spec.rb index 7beaf3a..ad38606 100644 --- a/spec/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation_spec.rb +++ b/spec/rubocop/cop/i18n/gettext/decorate_string_formatting_using_interpolation_spec.rb @@ -2,9 +2,9 @@ require 'spec_helper' -describe RuboCop::Cop::I18n::GetText::DecorateStringFormattingUsingInterpolation, :config, :config do +describe RuboCop::Cop::I18n::GetText::DecorateStringFormattingUsingInterpolation, :config do before(:each) do - investigate(cop, source) + @offenses = investigate(cop, source) end RuboCop::Cop::I18n::GetText.supported_decorators.each do |decorator| diff --git a/spec/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent_spec.rb b/spec/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent_spec.rb index 77b5782..27cea87 100644 --- a/spec/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent_spec.rb +++ b/spec/rubocop/cop/i18n/gettext/decorate_string_formatting_using_percent_spec.rb @@ -2,9 +2,9 @@ require 'spec_helper' -describe RuboCop::Cop::I18n::GetText::DecorateStringFormattingUsingPercent, :config, :config do +describe RuboCop::Cop::I18n::GetText::DecorateStringFormattingUsingPercent, :config do before(:each) do - investigate(cop, source) + @offenses = investigate(cop, source) end RuboCop::Cop::I18n::GetText.supported_decorators.each do |decorator| diff --git a/spec/rubocop/cop/i18n/gettext/decorate_string_spec.rb b/spec/rubocop/cop/i18n/gettext/decorate_string_spec.rb index 46ddb9b..4cd4400 100644 --- a/spec/rubocop/cop/i18n/gettext/decorate_string_spec.rb +++ b/spec/rubocop/cop/i18n/gettext/decorate_string_spec.rb @@ -2,9 +2,9 @@ require 'spec_helper' -describe RuboCop::Cop::I18n::GetText::DecorateString, :config, :config do +describe RuboCop::Cop::I18n::GetText::DecorateString, :config do before(:each) do - investigate(cop, source) + @offenses = investigate(cop, source) end context 'decoration needed for string' do diff --git a/spec/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation_spec.rb b/spec/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation_spec.rb index 6c72ca3..c605765 100644 --- a/spec/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation_spec.rb +++ b/spec/rubocop/cop/i18n/rails_i18n/decorate_string_formatting_using_interpolation_spec.rb @@ -2,9 +2,9 @@ require 'spec_helper' -describe RuboCop::Cop::I18n::RailsI18n::DecorateStringFormattingUsingInterpolation, :config, :config do +describe RuboCop::Cop::I18n::RailsI18n::DecorateStringFormattingUsingInterpolation, :config do before(:each) do - investigate(cop, source) + @offenses = investigate(cop, source) end RuboCop::Cop::I18n::RailsI18n.supported_decorators.each do |decorator| diff --git a/spec/rubocop/cop/i18n/rails_i18n/decorate_string_spec.rb b/spec/rubocop/cop/i18n/rails_i18n/decorate_string_spec.rb index f215a42..da93be3 100644 --- a/spec/rubocop/cop/i18n/rails_i18n/decorate_string_spec.rb +++ b/spec/rubocop/cop/i18n/rails_i18n/decorate_string_spec.rb @@ -2,9 +2,9 @@ require 'spec_helper' -describe RuboCop::Cop::I18n::RailsI18n::DecorateString, :config, :config do +describe RuboCop::Cop::I18n::RailsI18n::DecorateString, :config do before(:each) do - investigate(cop, source) + @offenses = investigate(cop, source) end context 'decoration needed for string' do diff --git a/spec/shared_examples.rb b/spec/shared_examples.rb index b4d4fa1..87e98a3 100644 --- a/spec/shared_examples.rb +++ b/spec/shared_examples.rb @@ -6,27 +6,26 @@ let(:source) { code } it 'does not register an offense' do - investigate(cop, source) - expect(cop.offenses).to be_empty + expect(@offenses).to be_empty end end shared_examples 'a_detecting_cop' do |unfixed, _function, expected_warning| let(:source) { unfixed.to_s } it 'has the correct rubocop warning' do - expect(cop.offenses[0]).not_to be_nil - expect(cop.offenses[0].message).to include(expected_warning) + expect(@offenses[0]).not_to be_nil + expect(@offenses[0].message).to include(expected_warning) end it 'has the correct number of offenses' do - expect(cop.offenses.size).to eq(1) + expect(@offenses.size).to eq(1) end end shared_examples 'a_no_cop_required' do |fixed, _function| let(:source) { fixed.to_s } it 'has no offenses found' do - expect(cop.offenses).to be_empty + expect(@offenses).to be_empty end end diff --git a/spec/shared_functions.rb b/spec/shared_functions.rb index a11729a..1eb5a64 100644 --- a/spec/shared_functions.rb +++ b/spec/shared_functions.rb @@ -2,7 +2,7 @@ def investigate(cop, src, filename = nil) processed_source = RuboCop::ProcessedSource.new(src, RUBY_VERSION.to_f, filename) - commissioner = RuboCop::Cop::Commissioner.new([cop], [], raise_error: true) - commissioner.investigate(processed_source) - commissioner + team = RuboCop::Cop::Team.new([cop], nil, raise_error: true) + report = team.investigate(processed_source) + report.offenses end