Skip to content

Commit

Permalink
Fix empty line after guard clause with and return and heredoc
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Eres authored and bbatsov committed Jun 14, 2021
1 parent f313e6f commit 76fc82b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_empty_line_after_guard_clause_with.md
@@ -0,0 +1 @@
* [#9876](https://github.com/rubocop/rubocop/pull/9876): Fix empty line after guard clause with `and return` and heredoc. ([@AndreiEres][])
28 changes: 13 additions & 15 deletions lib/rubocop/cop/layout/empty_line_after_guard_clause.rb
Expand Up @@ -45,8 +45,7 @@ class EmptyLineAfterGuardClause < Base
def on_if(node)
return if correct_style?(node)

if node.modifier_form? && last_argument_is_heredoc?(node)
heredoc_node = last_heredoc_argument(node)
if node.modifier_form? && (heredoc_node = last_heredoc_argument(node))
return if next_line_empty_or_enable_directive_comment?(heredoc_line(node, heredoc_node))

add_offense(heredoc_node.loc.heredoc_end) do |corrector|
Expand All @@ -62,7 +61,7 @@ def on_if(node)
private

def autocorrect(corrector, node)
node_range = if node.respond_to?(:heredoc?) && node.heredoc?
node_range = if heredoc?(node)
range_by_whole_lines(node.loc.heredoc_body)
else
range_by_whole_lines(node.source_range)
Expand Down Expand Up @@ -125,19 +124,8 @@ def next_sibling_empty_or_guard_clause?(node)
next_sibling.if_type? && contains_guard_clause?(next_sibling)
end

def last_argument_is_heredoc?(node)
last_children = node.if_branch
return false unless last_children&.send_type?

heredoc?(last_heredoc_argument(node))
end

def last_heredoc_argument(node)
n = if node.respond_to?(:if_branch)
node.if_branch.children.last
else
node
end
n = last_heredoc_argument_node(node)

return n if heredoc?(n)
return unless n.respond_to?(:arguments)
Expand All @@ -150,6 +138,16 @@ def last_heredoc_argument(node)
return last_heredoc_argument(n.receiver) if n.respond_to?(:receiver)
end

def last_heredoc_argument_node(node)
return node unless node.respond_to?(:if_branch)

if node.if_branch.and_type?
node.if_branch.children.first
else
node.if_branch.children.last
end
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
Expand Down
37 changes: 37 additions & 0 deletions spec/rubocop/cop/layout/empty_line_after_guard_clause_spec.rb
Expand Up @@ -523,4 +523,41 @@ def foo
end
RUBY
end

it 'registers no offenses using heredoc with `and return` before guard condition with empty line' do
expect_no_offenses(<<~RUBY)
def foo
puts(<<~MSG) and return if bar
A multiline
message
MSG
baz
end
RUBY
end

it 'registers an offense and corrects using heredoc with `and return` before guard condition' do
expect_offense(<<~RUBY)
def foo
puts(<<~MSG) and return if bar
A multiline
message
MSG
^^^^^ Add empty line after guard clause.
baz
end
RUBY

expect_correction(<<~RUBY)
def foo
puts(<<~MSG) and return if bar
A multiline
message
MSG
baz
end
RUBY
end
end

0 comments on commit 76fc82b

Please sign in to comment.