Skip to content

Commit

Permalink
Merge pull request #9375 from koic/fix_error_for_style_if_inside_else
Browse files Browse the repository at this point in the history
[Fix #9372] Fix an error for `Style/IfInsideElse`
  • Loading branch information
koic authored Jan 15, 2021
2 parents e5f926d + e234429 commit be0ff6a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 7 deletions.
1 change: 1 addition & 0 deletions changelog/fix_error_for_style_if_inside_else.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9372](https://github.com/rubocop-hq/rubocop/issues/9372): Fix an error for `Style/IfInsideElse` when nested `if` branch code is empty. ([@koic][])
21 changes: 14 additions & 7 deletions lib/rubocop/cop/style/if_inside_else.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ def autocorrect(corrector, node)
correct_to_elsif_from_if_inside_else_form(corrector, node, node.condition)
end
corrector.remove(range_by_whole_lines(find_end_range(node), include_final_newline: true))
corrector.remove(
range_by_whole_lines(node.if_branch.source_range, include_final_newline: true)
)
return unless (if_branch = node.if_branch)

range = range_by_whole_lines(if_branch.source_range, include_final_newline: true)
corrector.remove(range)
end

def correct_to_elsif_from_modifier_form(corrector, node)
Expand All @@ -101,10 +102,12 @@ def correct_to_elsif_from_modifier_form(corrector, node)

def correct_to_elsif_from_if_inside_else_form(corrector, node, condition)
corrector.replace(node.parent.loc.else, "elsif #{condition.source}")
if_condition_range = range_between(
node.loc.keyword.begin_pos, condition.source_range.end_pos
)
corrector.replace(if_condition_range, node.if_branch.source)
if_condition_range = if_condition_range(node, condition)
if (if_branch = node.if_branch)
corrector.replace(if_condition_range, if_branch.source)
else
corrector.remove(range_by_whole_lines(if_condition_range, include_final_newline: true))
end
corrector.remove(condition)
end

Expand All @@ -115,6 +118,10 @@ def find_end_range(node)
find_end_range(node.parent)
end

def if_condition_range(node, condition)
range_between(node.loc.keyword.begin_pos, condition.source_range.end_pos)
end

def allow_if_modifier_in_else_branch?(else_branch)
allow_if_modifier? && else_branch&.modifier_form?
end
Expand Down
25 changes: 25 additions & 0 deletions spec/rubocop/cop/style/if_inside_else_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,31 @@
RUBY
end

it 'catches an `if..else` nested inside an `else` and nested inside `if` branch code is empty' do
expect_offense(<<~RUBY)
if a
foo
else
if b
^^ Convert `if` nested inside `else` to `elsif`.
# TODO: comment.
else
bar
end
end
RUBY

expect_correction(<<~RUBY)
if a
foo
elsif b
# TODO: comment.
else
bar
end
RUBY
end

it 'catches an if..elsif..else nested inside an else' do
expect_offense(<<~RUBY)
if a
Expand Down

0 comments on commit be0ff6a

Please sign in to comment.