Skip to content

Commit

Permalink
[Fix rubocop#8406] Improve Style/AccessorGrouping's auto-correction
Browse files Browse the repository at this point in the history
## Summary

Fixes rubocop#8406.

This PR improves `Style/AccessorGrouping`'s auto-correction to remove
redundant blank lines.

## Other Information

This PR doesn't update to `Cop::Base` to make it clear the difference
between the changes. Will be updated with other `Style` cops in different PR.
  • Loading branch information
koic committed Jul 28, 2020
1 parent e5566c8 commit 8fa11a8
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -24,6 +24,7 @@
* [#8375](https://github.com/rubocop-hq/rubocop/pull/8375): Fix an infinite loop error for `Style/EmptyMethod`. ([@koic][])
* [#8385](https://github.com/rubocop-hq/rubocop/pull/8385): Remove auto-correction for `Lint/EnsureReturn`. ([@marcandre][])
* [#8391](https://github.com/rubocop-hq/rubocop/issues/8391): Mark `Style/ArrayCoercion` as not safe. ([@marcandre][])
* [#8406](https://github.com/rubocop-hq/rubocop/issues/8406): Improve `Style/AccessorGrouping`'s auto-correction to remove redundant blank lines. ([@koic][])

### Changes

Expand Down
16 changes: 9 additions & 7 deletions lib/rubocop/cop/style/accessor_grouping.rb
Expand Up @@ -33,6 +33,7 @@ module Style
#
class AccessorGrouping < Cop
include ConfigurableEnforcedStyle
include RangeHelp
include VisibilityHelp

GROUPED_MSG = 'Group together all `%<accessor>s` attributes.'
Expand All @@ -52,7 +53,12 @@ def on_class(node)

def autocorrect(node)
lambda do |corrector|
corrector.replace(node, correction(node))
if (preferred_accessors = preferred_accessors(node))
corrector.replace(node, preferred_accessors)
else
range = range_with_surrounding_space(range: node.loc.expression, side: :left)
corrector.remove(range)
end
end
end

Expand Down Expand Up @@ -110,14 +116,10 @@ def message(send_node)
format(msg, accessor: send_node.method_name)
end

def correction(node)
def preferred_accessors(node)
if grouped_style?
accessors = sibling_accessors(node)
if node == accessors.first
group_accessors(node, accessors)
else
''
end
group_accessors(node, accessors) if node == accessors.first
else
separate_accessors(node)
end
Expand Down
8 changes: 0 additions & 8 deletions spec/rubocop/cop/style/accessor_grouping_spec.rb
Expand Up @@ -25,9 +25,7 @@ class Foo
expect_correction(<<~RUBY)
class Foo
attr_reader :bar1, :bar2, :bar3, :bar4
attr_accessor :quux
other_macro :zoo
end
RUBY
Expand Down Expand Up @@ -61,18 +59,15 @@ class Foo
expect_correction(<<~RUBY)
class Foo
attr_reader :bar1, :bar2, :bar3
protected
attr_accessor :quux
private
attr_reader :baz1, :baz2, :baz4
attr_writer :baz3
public
other_macro :zoo
end
RUBY
Expand Down Expand Up @@ -105,12 +100,10 @@ class Foo
class << self
attr_reader :baz1, :baz2, :baz3
private
attr_reader :quux1, :quux2
end
end
RUBY
Expand Down Expand Up @@ -160,7 +153,6 @@ class Foo
attr_reader :three
attr_reader :four, :five
end
RUBY
end
Expand Down

0 comments on commit 8fa11a8

Please sign in to comment.