Skip to content

Commit

Permalink
[Fix rubocop#10016] Fix conflict between `Style/SoleNestedConditional…
Browse files Browse the repository at this point in the history
…` and `Style/NegatedIf`/`Style/NegatedUnless`.
  • Loading branch information
dvandersluis committed Aug 16, 2021
1 parent dcc4df5 commit 3c97e4a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog/fix_fix_conflict_between.md
@@ -0,0 +1 @@
* [#10016](https://github.com/rubocop/rubocop/issues/10016): Fix conflict between `Style/SoleNestedConditional` and `Style/NegatedIf`/`Style/NegatedUnless`. ([@dvandersluis][])
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, Style::NegatedUnless]
end

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

Expand Down
34 changes: 34 additions & 0 deletions spec/rubocop/cli/autocorrect_spec.rb
Expand Up @@ -2192,4 +2192,38 @@ def foo(a)
end
RUBY
end

it 'properly corrects when `Style/SoleNestedConditional` and one of '\
'`Style/NegatedIf` or `Style/NegatedUnless` detect offenses' do
source_file = Pathname('example.rb')
create_file(source_file, <<~RUBY)
if !foo?
if bar?
do_something
end
end
unless !foo?
if bar?
do_something
end
end
RUBY

status = cli.run(
%w[--auto-correct --only] << %w[
NegatedIf NegatedUnless SoleNestedConditional
].join(',')
)
expect(status).to eq(0)
expect(source_file.read).to eq(<<~RUBY)
if !foo? && bar?
do_something
end
if !!foo? && bar?
do_something
end
RUBY
end
end

0 comments on commit 3c97e4a

Please sign in to comment.