Skip to content

Commit

Permalink
Merge pull request #9178 from dvandersluis/issue/9174
Browse files Browse the repository at this point in the history
[Fix #9174] Handle send nodes with unparenthesized arguments in `Style/SoleNestedConditional`
  • Loading branch information
koic committed Dec 7, 2020
2 parents 16cbd40 + 26c07cb commit 17a937f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
1 change: 1 addition & 0 deletions changelog/fix_handle_send_nodes_with_unparenthesized.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9174](https://github.com/rubocop-hq/rubocop/issues/9174): Handle send nodes with unparenthesized arguments in `Style/SoleNestedConditional`. ([@dvandersluis][])
13 changes: 9 additions & 4 deletions lib/rubocop/cop/style/sole_nested_conditional.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ def autocorrect(corrector, node, if_branch)

and_operator = if_branch.unless? ? ' && !' : ' && '
if if_branch.modifier_form?
correct_for_gurad_condition_style(corrector, node, if_branch, and_operator)
correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
else
correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
end

correct_for_comment(corrector, node, if_branch)
end

def correct_for_gurad_condition_style(corrector, node, if_branch, and_operator)
def correct_for_guard_condition_style(corrector, node, if_branch, and_operator)
condition = if_branch.condition
corrector.insert_after(node.condition, replacement_condition(and_operator, condition))

Expand All @@ -95,7 +95,7 @@ def correct_for_basic_condition_style(corrector, node, if_branch, and_operator)
)
corrector.replace(range, and_operator)
corrector.remove(range_by_whole_lines(node.loc.end, include_final_newline: true))
corrector.wrap(if_branch.condition, '(', ')') if if_branch.condition.or_type?
corrector.wrap(if_branch.condition, '(', ')') if wrap_condition?(if_branch.condition)
end

def correct_for_comment(corrector, node, if_branch)
Expand All @@ -105,8 +105,13 @@ def correct_for_comment(corrector, node, if_branch)
corrector.insert_before(node.loc.keyword, comment_text) unless comments.empty?
end

def wrap_condition?(node)
node.or_type? ||
(node.send_type? && node.arguments.any? && !node.parenthesized?)
end

def replacement_condition(and_operator, condition)
if condition.or_type?
if wrap_condition?(condition)
"#{and_operator}(#{condition.source})"
else
"#{and_operator}#{condition.source}"
Expand Down
76 changes: 76 additions & 0 deletions spec/rubocop/cop/style/sole_nested_conditional_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,82 @@
RUBY
end

context 'when the inner condition has a send node without parens' do
context 'in guard style' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
if foo
do_something if ok? bar
^^ Consider merging nested conditions into outer `if` conditions.
end
RUBY

expect_correction(<<~RUBY)
if foo && (ok? bar)
do_something
end
RUBY
end
end

context 'in modifier style' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
if foo
if ok? bar
^^ Consider merging nested conditions into outer `if` conditions.
do_something
end
end
RUBY

expect_correction(<<~RUBY)
if foo && (ok? bar)
do_something
end
RUBY
end
end
end

context 'when the inner condition has a send node with parens' do
context 'in guard style' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
if foo
do_something if ok?(bar)
^^ Consider merging nested conditions into outer `if` conditions.
end
RUBY

expect_correction(<<~RUBY)
if foo && ok?(bar)
do_something
end
RUBY
end
end

context 'in modifier style' do
it 'registers an offense and corrects' do
expect_offense(<<~RUBY)
if foo
if ok?(bar)
^^ Consider merging nested conditions into outer `if` conditions.
do_something
end
end
RUBY

expect_correction(<<~RUBY)
if foo && ok?(bar)
do_something
end
RUBY
end
end
end

context 'when AllowModifier is true' do
let(:cop_config) do
{ 'AllowModifier' => true }
Expand Down

0 comments on commit 17a937f

Please sign in to comment.