Skip to content

Commit

Permalink
Fix an error for Style/IfInsideElse
Browse files Browse the repository at this point in the history
This PR fixes the following error for `Style/IfInsideElse` when
a deep nested multiline `if...then...elsif...else...end`:

```console
$ cat example.rb
if cond
else
  if nested_one
  else
    if c
      if d
      else
        if e
        end
      end
    end
  end
end
```

```console
$ bundle exec rubocop --only Style/IfInsideElse -a
Inspecting 1 file
An error occurred while Style/IfInsideElse cop was inspecting
/Users/koic/src/github.com/koic/rubocop-issues/if_inside_else/example.rb:6:6.
To see the complete backtrace run rubocop -d.
C

Offenses:

example.rb:3:3: C: [Corrected] Style/IfInsideElse: Convert if nested inside else to elsif.
  if nested_one
  ^^
example.rb:5:5: C: [Corrected] Style/IfInsideElse: Convert if nested inside else to elsif.
    if c
    ^^
example.rb:6:9: C: [Corrected] Style/IfInsideElse: Convert if nested inside else to elsif.
        if e
        ^^

1 file inspected, 3 offenses detected, 3 offenses corrected

1 error occurred:
An error occurred while Style/IfInsideElse cop was inspecting
/Users/koic/src/github.com/koic/rubocop-issues/if_inside_else/example.rb:6:6.
Errors are usually caused by RuboCop bugs.
Please, report your problems to RuboCop's issue tracker.
https://github.com/rubocop/rubocop/issues

Mention the following information in the issue report:
1.50.2 (using Parser 3.2.2.1, rubocop-ast 1.28.1, running on ruby 3.2.1) [x86_64-darwin19]
```

The autocorrection is repeated until all warnings are eliminated,
so it is effectively compatible with the previous autocorrection.
  • Loading branch information
koic authored and bbatsov committed May 11, 2023
1 parent d9efbe2 commit de330b3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/fix_an_error_for_style_if_inside_else.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#11967](https://github.com/rubocop/rubocop/pull/11967): Fix error for `Style/IfInsideElse` when a deep nested multiline `if...then...elsif...else...end`. ([@koic][])
6 changes: 6 additions & 0 deletions lib/rubocop/cop/style/if_inside_else.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ module Style
# end
#
class IfInsideElse < Base
include IgnoredNode
include RangeHelp
extend AutoCorrector

MSG = 'Convert `if` nested inside `else` to `elsif`.'

# rubocop:disable Metrics/CyclomaticComplexity
def on_if(node)
return if node.ternary? || node.unless?

Expand All @@ -73,9 +75,13 @@ def on_if(node)
return if allow_if_modifier_in_else_branch?(else_branch)

add_offense(else_branch.loc.keyword) do |corrector|
next if part_of_ignored_node?(node)

autocorrect(corrector, else_branch)
ignore_node(node)
end
end
# rubocop:enable Metrics/CyclomaticComplexity

private

Expand Down
47 changes: 44 additions & 3 deletions spec/rubocop/cop/style/if_inside_else_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,11 @@
expect_correction(<<~RUBY)
if x
'x'
elsif y
else
if y
'y'
end
end
RUBY
end

Expand Down Expand Up @@ -183,11 +185,13 @@
expect_correction(<<~RUBY)
if x
'x'
elsif y
else
if y
'y'
elsif z
'z'
end
end
RUBY
end

Expand Down Expand Up @@ -229,12 +233,49 @@
expect_correction(<<~RUBY)
if x
'x'
elsif y
else
if y
'y'
elsif z
'z'
else
'a'
end
end
RUBY
end

it 'handles a deep nested multiline `if...then...elsif...else...end`' do
expect_offense(<<~RUBY)
if cond
else
if nested_one
^^ Convert `if` nested inside `else` to `elsif`.
else
if c
^^ Convert `if` nested inside `else` to `elsif`.
if d
else
if e
^^ Convert `if` nested inside `else` to `elsif`.
end
end
end
end
end
RUBY

expect_correction(<<~RUBY)
if cond
elsif nested_one
else
if c
if d
else
if e
end
end
end
end
RUBY
end
Expand Down

0 comments on commit de330b3

Please sign in to comment.