Skip to content

Commit

Permalink
[rubocop#3666] Add AllowedNames configuration to UncommunicativeName …
Browse files Browse the repository at this point in the history
…cops
  • Loading branch information
garettarrowood committed Dec 28, 2017
1 parent 3f65be3 commit 7e82e5f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -685,11 +685,12 @@ Naming/PredicateName:
Naming/UncommunicativeBlockParamName:
# Parameter names may be equal to or greater than this value
MinParamNameLength: 1
AllowedNames:

Naming/UncommunicativeMethodArgName:
# Argrument names may be equal to or greater than this value
MinArgNameLength: 3

AllowedNames:

Naming/VariableName:
EnforcedStyle: snake_case
Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/cop/mixin/uncommunicative_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module UncommunicativeName
def check(node, args, min:)
args.each do |arg|
source = arg.children.first.to_s
next if allowed_names.include?(source)
range = arg_range(arg, source.size)
case_offense(node, range) if uppercase?(source)
num_offense(node, range) if ends_with_num?(source)
Expand Down Expand Up @@ -63,6 +64,10 @@ def arg_range(arg, length)
begin_pos,
begin_pos + length)
end

def allowed_names
cop_config['AllowedNames'] || []
end
end
end
end
23 changes: 22 additions & 1 deletion spec/rubocop/cop/naming/uncommunicative_block_param_name_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe RuboCop::Cop::Naming::UncommunicativeBlockParamName, :config do
RSpec.describe RuboCop::Cop::Naming::UncommunicativeBlockParamName, :config do
subject(:cop) { described_class.new(config) }

let(:cop_config) { { 'MinParamNameLength' => 2 } }
Expand Down Expand Up @@ -61,4 +61,25 @@
'Only use lowercase characters for block parameter.'
]
end

context 'with AllowedNames' do
let(:cop_config) do
{
'AllowedNames' => %w[foo1 foo2]
}
end

it 'accepts specified block param names' do
expect_no_offenses(<<-RUBY.strip_indent)
something { |foo1, foo2| do_things }
RUBY
end

it 'registers unlisted offensive names' do
expect_offense(<<-RUBY.strip_indent)
something { |bar, bar1| do_things }
^^^^ Do not end block parameter with a number.
RUBY
end
end
end
27 changes: 26 additions & 1 deletion spec/rubocop/cop/naming/uncommunicative_method_arg_name_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

describe RuboCop::Cop::Naming::UncommunicativeMethodArgName, :config do
RSpec.describe RuboCop::Cop::Naming::UncommunicativeMethodArgName, :config do
subject(:cop) { described_class.new(config) }

let(:cop_config) { { 'MinParamNameLength' => 3 } }
Expand Down Expand Up @@ -112,4 +112,29 @@ def self.something(y, num1, oFo)
'Only use lowercase characters for method argument.'
]
end

context 'with AllowedNames' do
let(:cop_config) do
{
'AllowedNames' => %w[foo1 foo2]
}
end

it 'accepts specified block param names' do
expect_no_offenses(<<-RUBY.strip_indent)
def quux(foo1, foo2)
do_stuff
end
RUBY
end

it 'registers unlisted offensive names' do
expect_offense(<<-RUBY.strip_indent)
def quux(bar, bar1)
^^^^ Do not end method argument with a number.
do_stuff
end
RUBY
end
end
end

0 comments on commit 7e82e5f

Please sign in to comment.