Skip to content

Commit

Permalink
[Fix #12242] Support AllowModifiersOnAttrs option for `Style/Access…
Browse files Browse the repository at this point in the history
…ModifierDeclarations`
  • Loading branch information
krororo committed Dec 16, 2023
1 parent 079dffa commit 88ea9fd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 0 deletions.
@@ -0,0 +1 @@
* [#12242](https://github.com/rubocop/rubocop/issues/12242): Support `AllowModifiersOnAttrs` option for `Style/AccessModifierDeclarations`. ([@krororo][])
1 change: 1 addition & 0 deletions config/default.yml
Expand Up @@ -3056,6 +3056,7 @@ Style/AccessModifierDeclarations:
- inline
- group
AllowModifiersOnSymbols: true
AllowModifiersOnAttrs: true
SafeAutoCorrect: false

Style/AccessorGrouping:
Expand Down
33 changes: 33 additions & 0 deletions lib/rubocop/cop/style/access_modifier_declarations.rb
Expand Up @@ -67,6 +67,28 @@ module Style
# private :bar, :baz
#
# end
#
# @example AllowModifiersOnAttrs: true (default)
# # good
# class Foo
#
# public attr_reader :bar
# protected attr_writer :baz
# private attr_accessor :qux
# private attr :quux
#
# end
#
# @example AllowModifiersOnAttrs: false
# # bad
# class Foo
#
# public attr_reader :bar
# protected attr_writer :baz
# private attr_accessor :qux
# private attr :quux
#
# end
class AccessModifierDeclarations < Base
extend AutoCorrector

Expand All @@ -92,10 +114,17 @@ class AccessModifierDeclarations < Base
(send nil? {:private :protected :public :module_function} (sym _))
PATTERN

# @!method access_modifier_with_attr?(node)
def_node_matcher :access_modifier_with_attr?, <<~PATTERN
(send nil? {:private :protected :public :module_function}
(send nil? {:attr :attr_reader :attr_writer :attr_accessor} _))
PATTERN

def on_send(node)
return unless node.access_modifier?
return if ALLOWED_NODE_TYPES.include?(node.parent&.type)
return if allow_modifiers_on_symbols?(node)
return if allow_modifiers_on_attrs?(node)

if offense?(node)
add_offense(node.loc.selector) do |corrector|
Expand Down Expand Up @@ -128,6 +157,10 @@ def allow_modifiers_on_symbols?(node)
cop_config['AllowModifiersOnSymbols'] && access_modifier_with_symbol?(node)
end

def allow_modifiers_on_attrs?(node)
cop_config['AllowModifiersOnAttrs'] && access_modifier_with_attr?(node)
end

def offense?(node)
(group_style? && access_modifier_is_inlined?(node) &&
!right_siblings_same_inline_method?(node)) ||
Expand Down
40 changes: 40 additions & 0 deletions spec/rubocop/cop/style/access_modifier_declarations_spec.rb
Expand Up @@ -39,6 +39,21 @@ class Foo
expect_no_corrections
end
end

context 'allow access modifiers on attrs' do
let(:cop_config) { { 'AllowModifiersOnAttrs' => true } }

it 'accepts when argument to #{access_modifier} is a attr_*' do
expect_no_offenses(<<~RUBY)
class Foo
#{access_modifier} attr_reader :foo
#{access_modifier} attr_writer :bar
#{access_modifier} attr_accessor :baz
#{access_modifier} attr :qux
end
RUBY
end
end
end

context 'when `group` is configured' do
Expand Down Expand Up @@ -201,6 +216,31 @@ class Test
end
end

%w[attr attr_reader attr_writer attr_accessor].each do |attr_method|
context "when method is modified by inline modifier with disallowed #{attr_method}" do
let(:cop_config) do
{ 'AllowModifiersOnAttrs' => false }
end

it 'registers and autocorrects an offense' do
expect_offense(<<~RUBY, access_modifier: access_modifier)
class Test
#{access_modifier} #{attr_method} :foo
^{access_modifier} `#{access_modifier}` should not be inlined in method definitions.
end
RUBY

expect_correction(<<~RUBY)
class Test
#{access_modifier}
#{attr_method} :foo
end
RUBY
end
end
end

context 'when method is modified by inline modifier where group modifier already exists' do
it 'registers and autocorrects an offense' do
expect_offense(<<~RUBY, access_modifier: access_modifier)
Expand Down

0 comments on commit 88ea9fd

Please sign in to comment.