diff --git a/config/default.yml b/config/default.yml index 0b888553efd7..5fac0a0aa0fa 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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 diff --git a/lib/rubocop/cop/mixin/uncommunicative_name.rb b/lib/rubocop/cop/mixin/uncommunicative_name.rb index f6db26f865f2..509e76a25d3f 100644 --- a/lib/rubocop/cop/mixin/uncommunicative_name.rb +++ b/lib/rubocop/cop/mixin/uncommunicative_name.rb @@ -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) @@ -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 diff --git a/spec/rubocop/cop/naming/uncommunicative_block_param_name_spec.rb b/spec/rubocop/cop/naming/uncommunicative_block_param_name_spec.rb index 6ab7f3c8adf0..56955f9e814b 100644 --- a/spec/rubocop/cop/naming/uncommunicative_block_param_name_spec.rb +++ b/spec/rubocop/cop/naming/uncommunicative_block_param_name_spec.rb @@ -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 } } @@ -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 diff --git a/spec/rubocop/cop/naming/uncommunicative_method_arg_name_spec.rb b/spec/rubocop/cop/naming/uncommunicative_method_arg_name_spec.rb index acb1df03454c..dcad8dc487c6 100644 --- a/spec/rubocop/cop/naming/uncommunicative_method_arg_name_spec.rb +++ b/spec/rubocop/cop/naming/uncommunicative_method_arg_name_spec.rb @@ -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 } } @@ -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