Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #10016] Fix conflict between Style/SoleNestedConditional and Style/NegatedIf or Style/NegatedUnless #10021

Merged
merged 1 commit into from Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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