Skip to content

Commit

Permalink
[Fix #8229] Fix faulty calculation in UncommunicativeName
Browse files Browse the repository at this point in the history
`UncommunicativeName#check` does faulty calculation of location when a
parameter has a prefix like `_`, `*`.

As for the file below,

```ruby
def test1(_a); end

def test2(__a); end

def test3(*a); end

def test4(**a); end

def test5(**__a); end
```

it shows offenses with faulty locations.

```shell
$ rubocop test.rb
Inspecting 1 file
C

Offenses:

test.rb:3:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test1(_a); end
          ^
test.rb:5:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test2(__a); end
          ^
test.rb:7:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test3(*a); end
          ^
test.rb:9:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test4(**a); end
          ^
test.rb:11:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test5(**__a); end
          ^

1 file inspected, 5 offenses detected
```

This commit fixed the behavior and make it show the correct locations.

```shell
$ rubocop test.rb
Inspecting 1 file
C

Offenses:

test.rb:3:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test1(_a); end
          ^^
test.rb:5:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test2(__a); end
          ^^^
test.rb:7:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test3(*a); end
          ^^
test.rb:9:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test4(**a); end
          ^^^
test.rb:11:11: C: Naming/MethodParameterName: Method parameter must be at least 3 characters long.
def test5(**__a); end
          ^^^^^

1 file inspected, 5 offenses detected
```
  • Loading branch information
ohbarye authored and bbatsov committed Jan 9, 2021
1 parent 14858e4 commit 72c6b95
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -5350,3 +5350,4 @@
[@agargiulo]: https://github.com/agargiulo
[@muirdm]: https://github.com/muirdm
[@noon-ng]: https://github.com/noon-ng
[@ohbarye]: https://github.com/ohbarye
@@ -0,0 +1 @@
* [#8229](https://github.com/rubocop-hq/rubocop/issues/8229): Fix faulty calculation in UncommunicativeName. ([@ohbarye][])
6 changes: 5 additions & 1 deletion lib/rubocop/cop/mixin/uncommunicative_name.rb
Expand Up @@ -24,7 +24,11 @@ def check(node, args)
name = full_name.gsub(/\A(_+)/, '')
next if allowed_names.include?(name)

range = arg_range(arg, name.size)
length = full_name.size
length += 1 if arg.restarg_type?
length += 2 if arg.kwrestarg_type?

range = arg_range(arg, length)
issue_offenses(node, range, name)
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/naming/block_parameter_name_spec.rb
Expand Up @@ -44,6 +44,18 @@
RUBY
end

it 'registers offense when param with prefix is less than minimum length' do
expect_offense(<<~RUBY)
something do |_a, __b, *c, **__d|
^^ Block parameter must be at least 2 characters long.
^^^ Block parameter must be at least 2 characters long.
^^ Block parameter must be at least 2 characters long.
^^^^^ Block parameter must be at least 2 characters long.
do_stuff
end
RUBY
end

it 'registers offense when param contains uppercase characters' do
expect_offense(<<~RUBY)
something { |number_One| do_stuff }
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/naming/method_parameter_name_spec.rb
Expand Up @@ -99,6 +99,18 @@ def something(ab)
RUBY
end

it 'registers offense when parameter with prefix is less than minimum length' do
expect_offense(<<~RUBY)
def something(_a, __b, *c, **__d)
^^ Method parameter must be at least 3 characters long.
^^^ Method parameter must be at least 3 characters long.
^^ Method parameter must be at least 3 characters long.
^^^^^ Method parameter must be at least 3 characters long.
do_stuff
end
RUBY
end

it 'registers offense when parameter contains uppercase characters' do
expect_offense(<<~RUBY)
def something(number_One)
Expand Down

0 comments on commit 72c6b95

Please sign in to comment.