Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix an incorrect autocorrect for Style/AccessModifierDeclarations when multiple groupable access modifiers are defined #11530

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11530](https://github.com/rubocop/rubocop/pull/11530): Fix an incorrect autocorrect for `Style/AccessModifierDeclarations` when multiple groupable access modifiers are defined. ([@ydah][])
9 changes: 8 additions & 1 deletion lib/rubocop/cop/style/access_modifier_declarations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ def allow_modifiers_on_symbols?(node)
end

def offense?(node)
(group_style? && access_modifier_is_inlined?(node)) ||
(group_style? && access_modifier_is_inlined?(node) &&
!right_siblings_same_inline_method?(node)) ||
(inline_style? && access_modifier_is_not_inlined?(node))
end

Expand All @@ -149,6 +150,12 @@ def access_modifier_is_not_inlined?(node)
!access_modifier_is_inlined?(node)
end

def right_siblings_same_inline_method?(node)
node.right_siblings.any? do |sibling|
sibling.method?(node.method_name) && !sibling.arguments.empty?
end
end

def message(range)
access_modifier = range.source

Expand Down
38 changes: 38 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2785,4 +2785,42 @@ module Foo#{trailing_whitespace}
end
RUBY
end

it 'corrects `Style/AccessModifierDeclarations` offenses when multiple groupable access modifiers are defined' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
class Test
private def foo; end
private def bar; end
def baz; end
end
RUBY
status = cli.run(%w[--autocorrect-all --only Style/AccessModifierDeclarations])
expect($stdout.string).to eq(<<~RESULT)
Inspecting 1 file
C

Offenses:

example.rb:2:3: C: [Corrected] Style/AccessModifierDeclarations: private should not be inlined in method definitions.
private def foo; end
^^^^^^^
example.rb:3:3: C: [Corrected] Style/AccessModifierDeclarations: private should not be inlined in method definitions.
private def bar; end
^^^^^^^

1 file inspected, 2 offenses detected, 2 offenses corrected
RESULT
expect(status).to eq(0)
expect(source_file.read).to eq(<<~RUBY)
class Test
def baz; end
private

def foo; end

def bar; end
end
RUBY
end
end
22 changes: 22 additions & 0 deletions spec/rubocop/cop/style/access_modifier_declarations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,28 @@ def foo

include_examples 'always accepted', access_modifier
end

it 'offends when multiple groupable access modifiers are defined' do
expect_offense(<<~RUBY)
class Test
private def foo; end
private def bar; end
^^^^^^^ `private` should not be inlined in method definitions.
def baz; end
end
RUBY

expect_correction(<<~RUBY)
class Test
def baz; end
private

def foo; end

def bar; end
end
RUBY
end
end

context 'when `inline` is configured' do
Expand Down