From d197cc498584214ae4c727575960b022bd5ee384 Mon Sep 17 00:00:00 2001 From: Magne Land Date: Thu, 7 Jan 2021 17:58:36 -0800 Subject: [PATCH] Add specs that demonstrate bug in Style/SoleNestedConditional The first spec succeeds. The second spec fails: -if !(foo || bar) && baz +if (!foo || bar) && baz --- .../cop/style/sole_nested_conditional_spec.rb | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/spec/rubocop/cop/style/sole_nested_conditional_spec.rb b/spec/rubocop/cop/style/sole_nested_conditional_spec.rb index 4e0ad4bceb53..d3aadf73ea29 100644 --- a/spec/rubocop/cop/style/sole_nested_conditional_spec.rb +++ b/spec/rubocop/cop/style/sole_nested_conditional_spec.rb @@ -178,6 +178,38 @@ RUBY end + it 'registers an offense and corrects when using `unless` and `||` and parens in the outer condition' \ + 'and nested modifier condition ' do + expect_offense(<<~RUBY) + unless (foo || bar) + do_something if baz + ^^ Consider merging nested conditions into outer `unless` conditions. + end + RUBY + + expect_correction(<<~RUBY) + if !(foo || bar) && baz + do_something + end + RUBY + end + + it 'registers an offense and corrects when using `unless` and `||` without parens in the outer condition' \ + 'and nested modifier condition ' do + expect_offense(<<~RUBY) + unless foo || bar + do_something if baz + ^^ Consider merging nested conditions into outer `unless` conditions. + end + RUBY + + expect_correction(<<~RUBY) + if !(foo || bar) && baz + do_something + end + RUBY + end + it 'registers an offense and corrects for multiple nested conditionals' do expect_offense(<<~RUBY) if foo