Skip to content

Commit

Permalink
[Fix rubocop#7326] Fix a false positive for `Style/AccessModifierDecl…
Browse files Browse the repository at this point in the history
…arations`

Fixes rubocop#7326.

This PR fixes a false positive for `Style/AccessModifierDeclarations`
when access modifier name is used for hash literal value.
  • Loading branch information
koic authored and bbatsov committed Sep 5, 2019
1 parent dbf2f90 commit 8cb0b2d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -25,6 +25,7 @@
* [#5788](https://github.com/rubocop-hq/rubocop/issues/5788): Allow block arguments on separate lines if line would be too long in `Layout/MultilineBlockLayout`. ([@jonas054][])
* [#7305](https://github.com/rubocop-hq/rubocop/issues/7305): Register `Style/BlockDelimiters` offense when block result is assigned to an attribute. ([@mvz][])
* [#4802](https://github.com/rubocop-hq/rubocop/issues/4802): Don't leave any `Lint/UnneededCopEnableDirective` offenses undetected/uncorrected. ([@jonas054][])
* [#7326](https://github.com/rubocop-hq/rubocop/issues/7326): Fix a false positive for `Style/AccessModifierDeclarations` when access modifier name is used for hash literal value. ([@koic][])

### Changes

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/access_modifier_declarations.rb
Expand Up @@ -63,6 +63,7 @@ class AccessModifierDeclarations < Cop

def on_send(node)
return unless node.access_modifier?
return if node.parent.pair_type?

if offense?(node)
add_offense(node, location: :selector) do
Expand Down
15 changes: 15 additions & 0 deletions spec/rubocop/cop/style/access_modifier_declarations_spec.rb
Expand Up @@ -3,6 +3,17 @@
RSpec.describe RuboCop::Cop::Style::AccessModifierDeclarations, :config do
subject(:cop) { described_class.new(config) }

shared_examples 'always accepted' do |access_modifier|
it 'accepts when #{access_modifier} is a hash literal value' do
expect_no_offenses(<<~RUBY)
class Foo
foo
bar(key: #{access_modifier})
end
RUBY
end
end

context 'when `group` is configured' do
let(:cop_config) do
{
Expand Down Expand Up @@ -47,6 +58,8 @@ class Test
end
RUBY
end

include_examples 'always accepted', access_modifier
end
end

Expand Down Expand Up @@ -93,6 +106,8 @@ def foo; end
end
RUBY
end

include_examples 'always accepted', access_modifier
end
end
end

0 comments on commit 8cb0b2d

Please sign in to comment.