Skip to content

Commit

Permalink
Merge pull request #10923 from koic/fix_an_incorrect_autocorrect_for_…
Browse files Browse the repository at this point in the history
…style_sole_nested_conditional

[Fix #10920] Fix an incorrect autocorrect for `Style/SoleNestedConditional`
  • Loading branch information
koic committed Aug 16, 2022
2 parents feac1b8 + e742ae6 commit 5d35825
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 5d35825

Please sign in to comment.