Skip to content

Commit

Permalink
[Fix #7991] Fix an error for Layout/EmptyLinesAroundAttributeAccessor
Browse files Browse the repository at this point in the history
Fixes #7991.

This PR fixes an error for `Layout/EmptyLinesAroundAttributeAccessor`
when attribute method is method chained.
  • Loading branch information
koic authored and bbatsov committed May 19, 2020
1 parent 8376d3d commit 3651d6b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
* [#7967](https://github.com/rubocop-hq/rubocop/pull/7967): `Style/SlicingWithRange` cop now supports any expression as its first index. ([@zverok][])
* [#7972](https://github.com/rubocop-hq/rubocop/issues/7972): Fix an incorrect autocrrect for `Style/HashSyntax` when using a return value uses `return`. ([@koic][])
* [#7886](https://github.com/rubocop-hq/rubocop/issues/7886): Fix a bug in `AllowComments` logic in `Lint/SuppressedException`. ([@jonas054][])
* [#7991](https://github.com/rubocop-hq/rubocop/issues/7991): Fix an error for `Layout/EmptyLinesAroundAttributeAccessor` when attribute method is method chained. ([@koic][])

### Changes

Expand Down
Expand Up @@ -70,7 +70,7 @@ def on_send(node)
return if next_line_empty?(node.last_line)

next_line_node = next_line_node(node)
return if next_line_node.nil? || allow_alias?(next_line_node) || attribute_or_allowed_method?(next_line_node)
return unless require_empty_line?(next_line_node)

add_offense(node)
end
Expand All @@ -89,6 +89,12 @@ def next_line_empty?(line)
processed_source[line].blank?
end

def require_empty_line?(node)
return false unless node&.respond_to?(:type)

!allow_alias?(node) && !attribute_or_allowed_method?(node)
end

def next_line_node(node)
node.parent.children[node.sibling_index + 1]
end
Expand Down
Expand Up @@ -67,6 +67,14 @@ class Foo
RUBY
end

it 'accepts code when attribute method is method chained' do
expect_no_offenses(<<~RUBY)
class Foo
attr.foo
end
RUBY
end

context 'when `AllowAliasSyntax: true`' do
let(:cop_config) do
{ 'AllowAliasSyntax' => true }
Expand Down

0 comments on commit 3651d6b

Please sign in to comment.