Skip to content

Commit

Permalink
[Fix #6300] Fix a false positive for Layout/EmptyLineAfterGuardClause
Browse files Browse the repository at this point in the history
Fixes #6300.

This PR fixes a false positive for `Layout/EmptyLineAfterGuardClause`
when guard clause including heredoc.
  • Loading branch information
koic authored and bbatsov committed Sep 18, 2018
1 parent edc2014 commit bf9b122
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug fixes

* [#6296](https://github.com/rubocop-hq/rubocop/issues/6296): Fix an auto-correct error for `Style/For` when setting `EnforcedStyle: each` and `for` dose not have `do` or semicolon. ([@autopp][])
* [#6300](https://github.com/rubocop-hq/rubocop/pull/6300): Fix a false positive for `Layout/EmptyLineAfterGuardClause` when guard clause including heredoc. ([@koic][])

## 0.59.1 (2018-09-15)

Expand Down
28 changes: 19 additions & 9 deletions lib/rubocop/cop/layout/empty_line_after_guard_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,16 @@ class EmptyLineAfterGuardClause < Cop
def on_if(node)
return if correct_style?(node)

if last_argument_is_heredoc?(node)
if node.modifier_form? && last_argument_is_heredoc?(node)
heredoc_node = last_argument(node)

heredoc_body = heredoc_node.loc.heredoc_body
num_of_heredoc_lines =
heredoc_body.last_line - heredoc_body.first_line

line = node.last_line + num_of_heredoc_lines + END_OF_HEREDOC_LINE

return if next_line_empty?(line)
return if next_line_empty?(heredoc_line(node, heredoc_node))

add_offense(heredoc_node, location: :heredoc_end)
else
return if next_line_empty?(node.last_line)

add_offense(node)
add_offense(node, location: offense_location(node))
end
end

Expand Down Expand Up @@ -126,6 +120,22 @@ def last_argument_is_heredoc?(node)
def last_argument(node)
node.if_branch.children.last
end

def heredoc_line(node, heredoc_node)
heredoc_body = heredoc_node.loc.heredoc_body
num_of_heredoc_lines =
heredoc_body.last_line - heredoc_body.first_line

node.last_line + num_of_heredoc_lines + END_OF_HEREDOC_LINE
end

def offense_location(node)
if node.loc && node.loc.end
:end
else
:expression
end
end
end
end
end
Expand Down
30 changes: 30 additions & 0 deletions spec/rubocop/cop/layout/empty_line_after_guard_clause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,36 @@ def foo
RUBY
end

it 'registers an offense for guard clause followed by empty line' \
'when guard clause including heredoc' do
expect_no_offenses(<<-RUBY.strip_indent)
def method
if truthy
raise <<-MSG
This is an error.
MSG
end
value
end
RUBY
end

it 'registers an offense for guard clause not followed by empty line' \
'when guard clause including heredoc' do
expect_offense(<<-RUBY.strip_indent)
def method
if truthy
raise <<-MSG
This is an error.
MSG
end
^^^ Add empty line after guard clause.
value
end
RUBY
end

it 'does not register offense for guard clause followed by end' do
expect_no_offenses(<<-RUBY.strip_indent)
def foo
Expand Down

0 comments on commit bf9b122

Please sign in to comment.