Skip to content

Commit

Permalink
[Fix rubocop#10016] Fix an incorrect auto-correct for `Style/SoleNest…
Browse files Browse the repository at this point in the history
…edConditional`

Fixes rubocop#10016.

This PR fixes an incorrect auto-correct for `Style/SoleNestedConditional`
with `Style/NegatedIf`.
  • Loading branch information
koic committed Aug 16, 2021
1 parent dcc4df5 commit 446b824
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
@@ -0,0 +1 @@
* [#10016](https://github.com/rubocop/rubocop/issues/10016): Fix an incorrect auto-correct for `Style/SoleNestedConditional` with `Style/NegatedIf`. ([@koic][])
4 changes: 4 additions & 0 deletions lib/rubocop/cop/style/sole_nested_conditional.rb
Expand Up @@ -38,6 +38,10 @@ class SoleNestedConditional < Base

MSG = 'Consider merging nested conditions into outer `%<conditional_type>s` conditions.'

def self.autocorrect_incompatible_with
[Style::NegatedIf]
end

def on_if(node)
return if node.ternary? || node.else? || node.elsif?

Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -326,6 +326,26 @@ def foo
RUBY
end

it 'corrects `Style/SoleNestedConditional` with `Style/NegatedIf`' do
source = <<~RUBY
if !foo.nil?
if foo.do_something == bar
baz
end
end
RUBY
create_file('example.rb', source)
expect(cli.run([
'--auto-correct-all',
'--only', 'Style/NegatedIf,Style/SoleNestedConditional'
])).to eq(0)
expect(File.read('example.rb')).to eq(<<~RUBY)
if !foo.nil? && (foo.do_something == bar)
baz
end
RUBY
end

it 'corrects `Style/SoleNestedConditional` with `Style/InverseMethods` and `Style/IfUnlessModifier`' do
source = <<~RUBY
unless foo.to_s == 'foo'
Expand Down

0 comments on commit 446b824

Please sign in to comment.