Skip to content

Commit

Permalink
[Fix rubocop#10920] Fix an incorrect autocorrect for `Style/SoleNeste…
Browse files Browse the repository at this point in the history
…dConditional`

Fixes rubocop#10920.

This PR fixes an incorrect autocorrect for `Style/SoleNestedConditional`
when using nested conditional and branch contains a comment.

It removes `return if config.for_cop('Style/IfUnlessModifier')['Enabled']` that introduced in rubocop#9195.
That logic is now unnecessary due to rubocop@3fea162 change.
  • Loading branch information
koic committed Aug 15, 2022
1 parent feac1b8 commit e742ae6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
@@ -0,0 +1 @@
* [#10920](https://github.com/rubocop/rubocop/issues/10920): Fix an incorrect autocorrect for `Style/SoleNestedConditional` when using nested conditional and branch contains a comment. ([@koic][])
2 changes: 0 additions & 2 deletions lib/rubocop/cop/style/sole_nested_conditional.rb
Expand Up @@ -173,8 +173,6 @@ def correct_for_outer_condition_modify_form_style(corrector, node, if_branch)
end

def correct_for_comment(corrector, node, if_branch)
return if config.for_cop('Style/IfUnlessModifier')['Enabled']

comments = processed_source.ast_with_comments[if_branch]
comment_text = comments.map(&:text).join("\n") << "\n"

Expand Down
40 changes: 40 additions & 0 deletions spec/rubocop/cop/style/sole_nested_conditional_spec.rb
Expand Up @@ -382,6 +382,46 @@ def foo
RUBY
end

it 'registers an offense and corrects when using nested conditional and branch contains a comment' do
expect_offense(<<~RUBY)
if foo
# Comment.
if bar
^^ Consider merging nested conditions into outer `if` conditions.
do_something
end
end
RUBY

expect_correction(<<~RUBY)
# Comment.
if foo && bar
do_something
end
RUBY
end

it 'registers an offense and corrects when there are outer and inline comments' do
expect_offense(<<~RUBY)
# Outer comment.
if foo
# Comment.
if bar # nested condition
^^ Consider merging nested conditions into outer `if` conditions.
do_something
end
end
RUBY

expect_correction(<<~RUBY)
# Outer comment.
# Comment.
if foo && bar # nested condition
do_something
end
RUBY
end

context 'when disabling `Style/IfUnlessModifier`' do
let(:config) { RuboCop::Config.new('Style/IfUnlessModifier' => { 'Enabled' => false }) }

Expand Down

0 comments on commit e742ae6

Please sign in to comment.