Skip to content

Commit

Permalink
Fix an error for Layout/EmptyLineAfterMultilineCondition when condi…
Browse files Browse the repository at this point in the history
…tional is at the top level
  • Loading branch information
fatkodima authored and bbatsov committed Sep 9, 2020
1 parent 12385fe commit c1f1690
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@
### Bug fixes

* [#8627](https://github.com/rubocop-hq/rubocop/issues/8627): Fix a false positive for `Lint/DuplicateRequire` when same feature argument but different require method. ([@koic][])
* [#8674](https://github.com/rubocop-hq/rubocop/issues/8674): Fix an error for `Layout/EmptyLineAfterMultilineCondition` when conditional is at the top level. ([@fatkodima][])
* [#8658](https://github.com/rubocop-hq/rubocop/pull/8658): Fix a false positive for `Style/RedundantSelfAssignment` when calling coercion methods. ([@fatkodima][])
* [#8607](https://github.com/rubocop-hq/rubocop/issues/8607): Fix a false positive for `Lint/UnreachableLoop` when conditional branch includes continue statement preceding break statement. ([@fatkodima][])
* [#8572](https://github.com/rubocop-hq/rubocop/issues/8572): Fix a false positive for `Style/RedundantParentheses` when parentheses are used like method argument parentheses. ([@koic][])
Expand Down
11 changes: 6 additions & 5 deletions lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb
Expand Up @@ -62,7 +62,7 @@ def on_if(node)
return if node.ternary?

if node.modifier_form?
check_condition(node.condition) unless next_sibling_empty?(node)
check_condition(node.condition) if right_sibling(node)
else
check_condition(node.condition)
end
Expand All @@ -74,7 +74,7 @@ def on_while(node)
alias on_until on_while

def on_while_post(node)
return if next_sibling_empty?(node)
return unless right_sibling(node)

check_condition(node.condition)
end
Expand Down Expand Up @@ -116,9 +116,10 @@ def next_line_empty?(line)
processed_source[line].blank?
end

def next_sibling_empty?(node)
next_sibling = node.parent.children[node.sibling_index + 1]
next_sibling.nil?
def right_sibling(node)
return unless node.parent

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

def multiline_when_condition?(when_node)
Expand Down
Expand Up @@ -59,6 +59,12 @@ def m
RUBY
end

it 'does not register an offense when `if` at the top level' do
expect_no_offenses(<<~RUBY)
do_something if condition
RUBY
end

it 'registers an offense when no new line after `elsif` with multiline condition' do
expect_offense(<<~RUBY)
if condition
Expand Down

0 comments on commit c1f1690

Please sign in to comment.