Skip to content

Commit

Permalink
Fix Style/AccessorGrouping to not register offense for accessor wit…
Browse files Browse the repository at this point in the history
…h comment

Closes rubocop#8289
  • Loading branch information
tejasbubane committed Jul 10, 2020
1 parent e80b278 commit 6eb897c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@
* [#7777](https://github.com/rubocop-hq/rubocop/issues/7777): Fix crash for `Layout/MultilineArrayBraceLayout` when comment is present after last element. ([@shekhar-patil][])
* [#7776](https://github.com/rubocop-hq/rubocop/issues/7776): Fix crash for `Layout/MultilineMethodCallBraceLayout` when comment is present before closing braces. ([@shekhar-patil][])
* [#8282](https://github.com/rubocop-hq/rubocop/issues/8282): Fix `Style/IfUnlessModifier` bad precedence detection. ([@tejasbubane][])
* [#8289](https://github.com/rubocop-hq/rubocop/issues/8289): Fix `Style/AccessorGrouping` to not register offense for accessor with comment. ([@tejasbubane][])

## 0.87.1 (2020-07-07)

Expand Down
9 changes: 8 additions & 1 deletion lib/rubocop/cop/style/accessor_grouping.rb
Expand Up @@ -58,6 +58,10 @@ def autocorrect(node)

private

def previous_line_comment?(node)
comment_line?(processed_source[node.first_line - 2])
end

def class_send_elements(class_node)
class_def = class_node.body

Expand All @@ -75,6 +79,8 @@ def accessor?(send_node)
end

def check(send_node)
return if previous_line_comment?(send_node)

if grouped_style? && sibling_accessors(send_node).size > 1
add_offense(send_node)
elsif separated_style? && send_node.arguments.size > 1
Expand All @@ -94,7 +100,8 @@ def sibling_accessors(send_node)
send_node.parent.each_child_node(:send).select do |sibling|
accessor?(sibling) &&
sibling.method?(send_node.method_name) &&
node_visibility(sibling) == node_visibility(send_node)
node_visibility(sibling) == node_visibility(send_node) &&
!previous_line_comment?(sibling)
end
end

Expand Down
50 changes: 50 additions & 0 deletions spec/rubocop/cop/style/accessor_grouping_spec.rb
Expand Up @@ -123,6 +123,47 @@ class Foo
end
RUBY
end

it 'does not register offense for accessors with comments' do
expect_no_offenses(<<~RUBY)
class Foo
# @return [String] value of foo
attr_reader :one, :two
attr_reader :four
end
RUBY
end

it 'registers offense and corrects if atleast two separate accessors without comments' do
expect_offense(<<~RUBY)
class Foo
# @return [String] value of foo
attr_reader :one, :two
# [String] Some bar value return
attr_reader :three
attr_reader :four
^^^^^^^^^^^^^^^^^ Group together all `attr_reader` attributes.
attr_reader :five
^^^^^^^^^^^^^^^^^ Group together all `attr_reader` attributes.
end
RUBY

expect_correction(<<~RUBY)
class Foo
# @return [String] value of foo
attr_reader :one, :two
# [String] Some bar value return
attr_reader :three
attr_reader :four, :five
end
RUBY
end
end

context 'when EnforcedStyle is separated' do
Expand Down Expand Up @@ -236,5 +277,14 @@ class Foo
end
RUBY
end

it 'does not register an offense for grouped accessors with comments' do
expect_no_offenses(<<~RUBY)
class Foo
# Some comment
attr_reader :one, :two
end
RUBY
end
end
end

0 comments on commit 6eb897c

Please sign in to comment.