Skip to content

Commit

Permalink
[Fix rubocop#6761] Make UncommunicativeMethodParamName account for un…
Browse files Browse the repository at this point in the history
…derscores.
  • Loading branch information
thomthom committed Apr 2, 2019
1 parent 7d66f85 commit 4359752
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@

### Bug fixes

* [#6761](https://github.com/rubocop-hq/rubocop/issues/6761): Make `Naming/UncommunicativeMethodParamName` account for param names prefixed with underscores. ([@thomthom][])
* [#6855](https://github.com/rubocop-hq/rubocop/pull/6855): Fix an exception in `Rails/RedundantReceiverInWithOptions` when the body is empty. ([@ericsullivan][])
* [#6856](https://github.com/rubocop-hq/rubocop/pull/6856): Fix auto-correction for `Style/BlockComments` when the file is missing a trailing blank line. ([@ericsullivan][])
* [#6858](https://github.com/rubocop-hq/rubocop/issues/6858): Fix an incorrect auto-correct for `Lint/ToJSON` when there are no `to_json` arguments. ([@koic][])
Expand Down Expand Up @@ -3895,3 +3896,4 @@
[@ericsullivan]: https://github.com/ericsullivan
[@aeroastro]: https://github.com/aeroastro
[@anuja-joshi]: https://github.com/anuja-joshi
[@thomthom]: https://github.com/thomthom
7 changes: 6 additions & 1 deletion lib/rubocop/cop/mixin/uncommunicative_name.rb
Expand Up @@ -13,7 +13,12 @@ module UncommunicativeName

def check(node, args)
args.each do |arg|
name = arg.children.first.to_s
# Argument names might be "_" or prefixed with "_" to indicate they
# are unused. Trim away this prefix and only analyse the basename.
full_name = arg.children.first.to_s
next if full_name == '_'

name = full_name.gsub(/\A([_]+)/, '')
next if (arg.restarg_type? || arg.kwrestarg_type?) && name.empty?
next if allowed_names.include?(name)

Expand Down
16 changes: 16 additions & 0 deletions spec/rubocop/cop/naming/uncommunicative_method_param_name_spec.rb
Expand Up @@ -147,6 +147,22 @@ def quux(foo1, foo2)
RUBY
end

it 'accepts param names prefixed with underscore' do
expect_no_offenses(<<-RUBY.strip_indent)
def quux(_foo1, _foo2)
do_stuff
end
RUBY
end

it 'accepts underscore param names' do
expect_no_offenses(<<-RUBY.strip_indent)
def quux(_)
do_stuff
end
RUBY
end

it 'registers unlisted offensive names' do
expect_offense(<<-RUBY.strip_indent)
def quux(bar, bar1)
Expand Down

0 comments on commit 4359752

Please sign in to comment.