Skip to content

Commit

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

Fixes rubocop#13213.

This PR fixes false positives for `Style/AccessModifierDeclarations` when `AllowModifiersOnAttrs: true`
and using splat with a percent symbol array, or with a constant.
  • Loading branch information
koic committed Sep 27, 2024
1 parent 23cc315 commit 9873204
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#13213](https://github.com/rubocop/rubocop/issues/13213): Fix false positives for `Style/AccessModifierDeclarations` when `AllowModifiersOnAttrs: true` and using splat with a percent symbol array, or with a constant. ([@koic][])
14 changes: 12 additions & 2 deletions lib/rubocop/cop/style/access_modifier_declarations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ module Style
# class Foo
#
# private :bar, :baz
# private *%i[qux quux]
# private *METHOD_NAMES
#
# end
#
Expand All @@ -76,6 +78,8 @@ module Style
# class Foo
#
# private :bar, :baz
# private *%i[qux quux]
# private *METHOD_NAMES
#
# end
#
Expand Down Expand Up @@ -128,7 +132,9 @@ class AccessModifierDeclarations < Base

# @!method access_modifier_with_symbol?(node)
def_node_matcher :access_modifier_with_symbol?, <<~PATTERN
(send nil? {:private :protected :public :module_function} (sym _))
(send nil? {:private :protected :public :module_function}
{(sym _) (splat {#percent_symbol_array? const})}
)
PATTERN

# @!method access_modifier_with_attr?(node)
Expand Down Expand Up @@ -170,6 +176,10 @@ def autocorrect(corrector, node)
end
end

def percent_symbol_array?(node)
node.array_type? && node.percent_literal?(:symbol)
end

def allow_modifiers_on_symbols?(node)
cop_config['AllowModifiersOnSymbols'] && access_modifier_with_symbol?(node)
end
Expand Down Expand Up @@ -218,7 +228,7 @@ def message(range)

def find_corresponding_def_node(node)
if access_modifier_with_symbol?(node)
method_name = node.first_argument.value
method_name = node.first_argument.respond_to?(:value) && node.first_argument.value
node.parent.each_child_node(:def).find do |child|
child.method?(method_name)
end
Expand Down
42 changes: 42 additions & 0 deletions spec/rubocop/cop/style/access_modifier_declarations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ class Foo
end
RUBY
end

it 'accepts when argument to #{access_modifier} is splat with a `%i` array literal' do
expect_no_offenses(<<~RUBY)
class Foo
foo
#{access_modifier} *%i[bar baz]
end
RUBY
end

it 'accepts when argument to #{access_modifier} is splat with a constant' do
expect_no_offenses(<<~RUBY)
class Foo
foo
#{access_modifier} *METHOD_NAMES
end
RUBY
end
end

context 'do not allow access modifiers on symbols' do
Expand All @@ -38,6 +56,30 @@ class Foo

expect_no_corrections
end

it 'registers an offense when argument to #{access_modifier} is splat with a `%i` array literal' do
expect_offense(<<~RUBY, access_modifier: access_modifier)
class Foo
foo
%{access_modifier} *%i[bar baz]
^{access_modifier} `#{access_modifier}` should not be inlined in method definitions.
end
RUBY

expect_no_corrections
end

it 'registers an offense when argument to #{access_modifier} is splat with a constant' do
expect_offense(<<~RUBY, access_modifier: access_modifier)
class Foo
foo
%{access_modifier} *METHOD_NAMES
^{access_modifier} `#{access_modifier}` should not be inlined in method definitions.
end
RUBY

expect_no_corrections
end
end

context 'allow access modifiers on attrs' do
Expand Down

0 comments on commit 9873204

Please sign in to comment.