Skip to content

Commit

Permalink
Fix a false positive for Layout/EmptyLinesAroundAttributeAccessor
Browse files Browse the repository at this point in the history
This PR fixes the following false positive for `Layout/EmptyLinesAroundAttributeAccessor`
when using attribute accessors in `if` ... `else` branches.

```ruby
if condition
  attr_reader :foo
else
  do_something
end
```

```console
% bundle exec rubocop --only Layout/EmptyLinesAroundAttributeAccessor
(snip)
Inspecting 1 file
C

Offenses:

example.rb:2:3: C: Layout/EmptyLinesAroundAttributeAccessor: Add an
empty line after attribute accessor.
  attr_reader :foo
  ^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```
  • Loading branch information
koic authored and bbatsov committed Jun 14, 2020
1 parent 6e2d4a3 commit 928abea
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
* [#8124](https://github.com/rubocop-hq/rubocop/issues/8124): Fix a false positive for `Lint/FormatParameterMismatch` when using named parameters with escaped `%`. ([@koic][])
* [#7979](https://github.com/rubocop-hq/rubocop/issues/7979): Fix "uninitialized constant DidYouMean::SpellChecker" exception. ([@bquorning][])
* [#8098](https://github.com/rubocop-hq/rubocop/issues/8098): Fix a false positive for `Style/RedundantRegexpCharacterClass` when using interpolations. ([@owst][])
* [#8150](https://github.com/rubocop-hq/rubocop/pull/8150): Fix a false positive for `Layout/EmptyLinesAroundAttributeAccessor` when using attribute accessors in `if` ... `else` branches. ([@koic][])

### Changes

Expand Down
Expand Up @@ -96,6 +96,8 @@ def require_empty_line?(node)
end

def next_line_node(node)
return if node.parent.if_type?

node.parent.children[node.sibling_index + 1]
end

Expand Down
Expand Up @@ -75,6 +75,16 @@ class Foo
RUBY
end

it 'does not registers an offense and corrects when using `if` ... `else` branches' do
expect_no_offenses(<<~RUBY)
if condition
attr_reader :foo
else
do_something
end
RUBY
end

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

0 comments on commit 928abea

Please sign in to comment.