Skip to content

Commit

Permalink
[rubocop#3666] Add AllowNamesEndingInNumbers & enable by default
Browse files Browse the repository at this point in the history
  • Loading branch information
garettarrowood committed Dec 29, 2017
1 parent 76aad6c commit cf21688
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 66 deletions.
26 changes: 0 additions & 26 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,6 @@ Naming/PredicateName:
- def_node_matcher
- def_node_search

Naming/UncommunicativeBlockParamName:
AllowedNames:
- 'child_node1'
- 'child_node2'
- 'expression1'
- 'expression2'
- 'node1'
- 'node2'
- 'operator1'
- 'operator2'
- 'range1'
- 'range2'
- 'token1'
- 'token2'
- 'var1'
- 'var2'

Naming/UncommunicativeMethodArgName:
AllowedNames:
- 'node1'
- 'node2'
- 'range1'
- 'range2'
- 'token1'
- 'token2'

Style/FrozenStringLiteralComment:
EnforcedStyle: always

Expand Down
12 changes: 8 additions & 4 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -684,13 +684,17 @@ Naming/PredicateName:

Naming/UncommunicativeBlockParamName:
# Parameter names may be equal to or greater than this value
MinParamNameLength: 1
AllowedNames:
MinNameLength: 1
AllowNamesEndingInNumbers: true
# Whitelisted names that will not register an offense
AllowedNames: []

Naming/UncommunicativeMethodArgName:
# Argrument names may be equal to or greater than this value
MinArgNameLength: 3
AllowedNames:
MinNameLength: 3
AllowNamesEndingInNumbers: true
# Whitelisted names that will not register an offense
AllowedNames: []

Naming/VariableName:
EnforcedStyle: snake_case
Expand Down
39 changes: 27 additions & 12 deletions lib/rubocop/cop/mixin/uncommunicative_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,24 @@ module UncommunicativeName
LENGTH_MSG = '%<name_type>s must be longer than %<min>s ' \
'characters.'.freeze

def check(node, args, min:)
def check(node, args)
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)
length_offense(node, range, min) unless long_enough?(source, min)
name = arg.children.first.to_s
next if allowed_names.include?(name)
range = arg_range(arg, name.size)
issue_offenses(node, range, name)
end
end

private

def issue_offenses(node, range, name)
case_offense(node, range) if uppercase?(name)
length_offense(node, range) unless long_enough?(name)
return if allow_nums
num_offense(node, range) if ends_with_num?(name)
end

def case_offense(node, range)
add_offense(node, location: range,
message: format(CASE_MSG, name_type: name_type(node)))
Expand Down Expand Up @@ -47,15 +54,15 @@ def ends_with_num?(name)
name[-1] =~ /\d/
end

def length_offense(node, range, min)
def length_offense(node, range)
add_offense(node, location: range,
message: format(LENGTH_MSG,
name_type: name_type(node).capitalize,
min: min))
min: min_length))
end

def long_enough?(name, min)
name.size >= min
def long_enough?(name)
name.size >= min_length
end

def arg_range(arg, length)
Expand All @@ -66,7 +73,15 @@ def arg_range(arg, length)
end

def allowed_names
cop_config['AllowedNames'] || []
cop_config['AllowedNames']
end

def allow_nums
cop_config['AllowNamesEndingInNumbers']
end

def min_length
cop_config['MinNameLength']
end
end
end
Expand Down
8 changes: 1 addition & 7 deletions lib/rubocop/cop/naming/uncommunicative_block_param_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ class UncommunicativeBlockParamName < Cop

def on_block(node)
return unless node.arguments?
check(node, node.arguments, min: min_length)
end

private

def min_length
cop_config['MinParamNameLength']
check(node, node.arguments)
end
end
end
Expand Down
8 changes: 1 addition & 7 deletions lib/rubocop/cop/naming/uncommunicative_method_arg_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,9 @@ class UncommunicativeMethodArgName < Cop

def on_def(node)
return unless node.arguments?
check(node, node.arguments, min: min_length)
check(node, node.arguments)
end
alias on_defs on_def

private

def min_length
cop_config['MinArgNameLength']
end
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions manual/cops_naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -399,8 +399,9 @@ baz { |age, height, gender| do_stuff(age, height, gender) }
Name | Default value | Configurable values
--- | --- | ---
MinParamNameLength | `1` | Integer
AllowedNames | `<none>` |
MinNameLength | `1` | Integer
AllowNamesEndingInNumbers | `true` | Boolean
AllowedNames | `[]` | Array
## Naming/UncommunicativeMethodArgName
Expand Down Expand Up @@ -446,8 +447,9 @@ end

Name | Default value | Configurable values
--- | --- | ---
MinArgNameLength | `3` | Integer
AllowedNames | `<none>` |
MinNameLength | `3` | Integer
AllowNamesEndingInNumbers | `true` | Boolean
AllowedNames | `[]` | Array

## Naming/VariableName

Expand Down
3 changes: 1 addition & 2 deletions spec/rubocop/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1516,12 +1516,11 @@ def method(foo, bar, qux, fred, arg5, f) end #{'#' * 45}
expect($stdout.string).to eq(<<-RESULT.strip_indent)
== example/example1.rb ==
C: 1: 11: Metrics/ParameterLists: Avoid parameter lists longer than 5 parameters. [6/5]
C: 1: 33: Naming/UncommunicativeMethodArgName: Do not end method argument with a number.
C: 1: 39: Naming/UncommunicativeMethodArgName: Method argument must be longer than 3 characters.
C: 1: 46: Style/CommentedKeyword: Do not place comments on the same line as the def keyword.
E: 1: 81: Metrics/LineLength: Line is too long. [90/80]
1 file inspected, 5 offenses detected
1 file inspected, 4 offenses detected
RESULT
expect($stderr.string).to eq('')
end
Expand Down
24 changes: 22 additions & 2 deletions spec/rubocop/cop/naming/uncommunicative_block_param_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
RSpec.describe RuboCop::Cop::Naming::UncommunicativeBlockParamName, :config do
subject(:cop) { described_class.new(config) }

let(:cop_config) { { 'MinParamNameLength' => 2 } }
let(:cop_config) do
{
'MinNameLength' => 2,
'AllowNamesEndingInNumbers' => false
}
end

it 'does not register for block without parameters' do
expect_no_offenses(<<-RUBY.strip_indent)
Expand Down Expand Up @@ -65,7 +70,8 @@
context 'with AllowedNames' do
let(:cop_config) do
{
'AllowedNames' => %w[foo1 foo2]
'AllowedNames' => %w[foo1 foo2],
'AllowNamesEndingInNumbers' => false
}
end

Expand All @@ -82,4 +88,18 @@
RUBY
end
end

context 'with AllowNamesEndingInNumbers' do
let(:cop_config) do
{
'AllowNamesEndingInNumbers' => true
}
end

it 'accept params that end in numbers' do
expect_no_offenses(<<-RUBY.strip_indent)
something { |foo1, bar2, qux3| do_that_stuff }
RUBY
end
end
end
26 changes: 24 additions & 2 deletions spec/rubocop/cop/naming/uncommunicative_method_arg_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
RSpec.describe RuboCop::Cop::Naming::UncommunicativeMethodArgName, :config do
subject(:cop) { described_class.new(config) }

let(:cop_config) { { 'MinParamNameLength' => 3 } }
let(:cop_config) do
{
'MinNameLength' => 3,
'AllowNamesEndingInNumbers' => false
}
end

it 'does not register for method without arguments' do
expect_no_offenses(<<-RUBY.strip_indent)
Expand Down Expand Up @@ -116,7 +121,8 @@ def self.something(y, num1, oFo)
context 'with AllowedNames' do
let(:cop_config) do
{
'AllowedNames' => %w[foo1 foo2]
'AllowedNames' => %w[foo1 foo2],
'AllowNamesEndingInNumbers' => false
}
end

Expand All @@ -137,4 +143,20 @@ def quux(bar, bar1)
RUBY
end
end

context 'with AllowNamesEndingInNumbers' do
let(:cop_config) do
{
'AllowNamesEndingInNumbers' => true
}
end

it 'accept arguments that end in numbers' do
expect_no_offenses(<<-RUBY.strip_indent)
def something(foo1, bar2, qux3)
do_stuff
end
RUBY
end
end
end

0 comments on commit cf21688

Please sign in to comment.