Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make custom cop inherit RuboCop::Cop::Base #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions .rubocop.yml
Expand Up @@ -6,6 +6,10 @@ AllCops:
TargetRubyVersion: 2.5
NewCops: enable

# rubocop-i18n does not have config/default.
InternalAffairs/UndefinedConfig:
Enabled: false

Metrics/BlockLength:
Exclude:
# Exclude the spec directory because the rspec DSL results in long blocks
Expand Down
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
21 changes: 9 additions & 12 deletions lib/rubocop/cop/i18n/gettext/decorate_string.rb
Expand Up @@ -4,7 +4,7 @@ module RuboCop
module Cop
module I18n
module GetText
# This cop is looks for strings that appear to be sentences but are not decorated.
# Looks for strings that appear to be sentences but are not decorated.
# Sentences are determined by the STRING_REGEXP. (Upper case character, at least one space,
# and sentence punctuation at the end)
#
Expand All @@ -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
4 changes: 2 additions & 2 deletions lib/rubocop/cop/i18n/rails_i18n/decorate_string.rb
Expand Up @@ -4,7 +4,7 @@ module RuboCop
module Cop
module I18n
module RailsI18n
# This cop is looks for strings that appear to be sentences but are not decorated.
# Looks for strings that appear to be sentences but are not decorated.
# Sentences are determined by the SENTENCE_REGEXP. (Upper case character, at least one space,
# and sentence punctuation at the end)
#
Expand Down 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
4 changes: 4 additions & 0 deletions rubocop-i18n.gemspec
Expand Up @@ -23,6 +23,10 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = '>= 2.5.8'

spec.metadata = {
'rubygems_mfa_required' => 'true'
}

spec.add_development_dependency 'bundler', '>= 1.17.3'
spec.add_development_dependency 'pry', '~> 0.13.1'
spec.add_development_dependency 'rake', '>= 12.3.3'
Expand Down
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
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -10,7 +10,7 @@
# to individual examples or groups you care about by tagging them with
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
# get run.
unless defined?(::TestQueue)
unless defined?(TestQueue)
# See. https://github.com/tmm1/test-queue/issues/60#issuecomment-281948929
config.filter_run :focus
config.run_all_when_everything_filtered = true
Expand All @@ -22,7 +22,7 @@
Kernel.srand config.seed

broken_filter = lambda do |v|
v.is_a?(Symbol) ? RUBY_ENGINE == v.to_s : v
v.is_a?(Symbol) ? v.to_s == RUBY_ENGINE : v
end
config.filter_run_excluding broken: broken_filter

Expand Down