Skip to content

Commit

Permalink
Make custom cop inherit RuboCop::Cop::Base
Browse files Browse the repository at this point in the history
Follow rubocop/rubocop#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
  • Loading branch information
koic committed Feb 11, 2023
1 parent 92ef8f4 commit 7418af9
Show file tree
Hide file tree
Showing 15 changed files with 53 additions and 56 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -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.
Expand Down
31 changes: 16 additions & 15 deletions lib/rubocop/cop/i18n/gettext/decorate_function_message.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 8 additions & 11 deletions lib/rubocop/cop/i18n/gettext/decorate_string.rb
Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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)
Expand Down
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb
Expand Up @@ -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
Expand Down
Expand Up @@ -20,7 +20,7 @@ module RailsI18n
#
# t("status.accepted")
#
class DecorateStringFormattingUsingInterpolation < Cop
class DecorateStringFormattingUsingInterpolation < Base
def on_send(node)
return unless node&.loc&.selector

Expand Down
12 changes: 6 additions & 6 deletions spec/rubocop/cop/i18n/gettext/decorate_function_message_spec.rb
Expand Up @@ -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|
Expand Down Expand Up @@ -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
Expand Down
Expand Up @@ -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|
Expand Down
Expand Up @@ -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|
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cop/i18n/gettext/decorate_string_spec.rb
Expand Up @@ -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
Expand Down
Expand Up @@ -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|
Expand Down
4 changes: 2 additions & 2 deletions spec/rubocop/cop/i18n/rails_i18n/decorate_string_spec.rb
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions spec/shared_examples.rb
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions spec/shared_functions.rb
Expand Up @@ -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

0 comments on commit 7418af9

Please sign in to comment.