Skip to content

Commit

Permalink
Fix false positive for Style/EmptyLineAfterGuardClause
Browse files Browse the repository at this point in the history
Related rubocop#5679.

This PR fixes a false positive for Style/EmptyLineAfterGuardClause when
guard clause is after heredoc.

## Reproduction code

```ruby
def foo
  raise ArgumentError, <<-MSG unless path
    Must be called with mount point
  MSG

  bar
end
```

:memo: I found this false positivity below.
https://github.com/rails/rails/blob/v5.2.0.rc2/actionpack/lib/action_dispatch/routing/mapper.rb#L614-L622

## Expected behavior

No offenses.

## Actual behavior and Steps to reproduce the problem

```console
% rubocop /tmp/example.rb --only Style/EmptyLineAfterGuardClause
Inspecting 1 file
C

Offenses:

/tmp/example.rb:2:3: C: Style/EmptyLineAfterGuardClause: Add empty line
after guard clause.
  raise ArgumentError, <<-MSG unless path
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1 file inspected, 1 offense detected
```

## RuboCop version

```console
% rubocop -V
0.54.0 (using Parser 2.5.0.4, running on ruby 2.5.0 x86_64-darwin17)
```

This PR fixes the above false positive.
  • Loading branch information
koic committed Mar 31, 2018
1 parent 3c05076 commit b4a79d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Changes

* [#5734](https://github.com/bbatsov/rubocop/pull/5734): Add `by`, `on`, `in` and `at` to allowed names of `Naming/UncommunicativeMethodParamName` cop in default config. ([@AlexWayfer][])
* [#5720](https://github.com/bbatsov/rubocop/pull/5720): Fix false positive for `Style/EmptyLineAfterGuardClause` when guard clause is after heredoc. ([@koic][])

## 0.54.0 (2018-03-21)

Expand Down
22 changes: 15 additions & 7 deletions lib/rubocop/cop/style/empty_line_after_guard_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ class EmptyLineAfterGuardClause < Cop
MSG = 'Add empty line after guard clause.'.freeze

def on_if(node)
return unless contains_guard_clause?(node)

return if next_line_rescue_or_ensure?(node)
return if next_sibling_parent_empty_or_else?(node)
return if next_sibling_empty_or_guard_clause?(node)

return if next_line_empty?(node)
return if correct_style?(node)

add_offense(node)
end
Expand All @@ -61,6 +55,15 @@ def autocorrect(node)

private

def correct_style?(node)
!contains_guard_clause?(node) ||
next_line_rescue_or_ensure?(node) ||
next_sibling_parent_empty_or_else?(node) ||
next_sibling_empty_or_guard_clause?(node) ||
last_argument_is_heredoc?(node) ||
next_line_empty?(node)
end

def contains_guard_clause?(node)
node.if_branch && node.if_branch.guard_clause?
end
Expand Down Expand Up @@ -89,6 +92,11 @@ def next_sibling_empty_or_guard_clause?(node)

next_sibling.if_type? && contains_guard_clause?(next_sibling)
end

def last_argument_is_heredoc?(node)
node.children.last && node.children.last.send_type? &&
node.children.last.last_argument.heredoc?
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/empty_line_after_guard_clause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ def foo
RUBY
end

it 'does not register offence when guard clause is after heredoc' do
expect_no_offenses(<<-RUBY.strip_indent)
def foo
raise ArgumentError, <<-MSG unless path
Must be called with mount point
MSG
bar
end
RUBY
end

it 'registers an offence for methods starting with end_' do
expect_offense(<<-RUBY.strip_indent)
def foo
Expand Down

0 comments on commit b4a79d5

Please sign in to comment.