Skip to content

Commit

Permalink
Fix an incorrect autocorrect for Style/AccessModifierDeclarations w…
Browse files Browse the repository at this point in the history
…hen multiple groupable access modifiers are defined

This PR is fix an incorrect autocorrect for `Style/AccessModifierDeclarations` when multiple groupable access modifiers are defined.

## Code to reproduce

```ruby
class Test
  private def foo; end
  private def bar; end
  def baz; end
end
```

## Expected behavior

Automatically corrected as follows:

```ruby
class Test
  def baz; end

  private

  def foo; end

  def bar; end
end
```

## Actual behavior

Automatically corrected as follows:

```ruby
class Test
  def baz; end
private

def bar; end # <--- The order is changing
private # <--- Duplicate

def foo; end # <--- The order is changing
end
```
  • Loading branch information
ydah committed Feb 2, 2023
1 parent 62c0e90 commit a3d4077
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
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

0 comments on commit a3d4077

Please sign in to comment.