Skip to content

Commit

Permalink
Support the cases that there is a method call in each branch
Browse files Browse the repository at this point in the history
  • Loading branch information
nobuyo authored and bbatsov committed May 6, 2022
1 parent 070ffea commit 1b6bda4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 5 deletions.
46 changes: 41 additions & 5 deletions lib/rubocop/cop/style/redundant_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,15 @@ def synonymous_condition_and_branch?(node)
# else
# @value = another_value?
# end
branches_have_assignment?(node) && condition == if_branch.expression
return true if branches_have_assignment?(node) && condition == if_branch.expression

# e.g.
# if foo
# test.value = foo
# else
# test.value = another_value?
# end
branches_have_method?(node) && condition == if_branch.first_argument
end

def branches_have_assignment?(node)
Expand All @@ -122,18 +130,46 @@ def asgn_type?(node)
node.lvasgn_type? || node.ivasgn_type? || node.cvasgn_type? || node.gvasgn_type?
end

def branches_have_method?(node)
_condition, if_branch, else_branch = *node

return false unless if_branch && else_branch

if_branch.send_type? && if_branch.arguments.count == 1 &&
else_branch.send_type? && else_branch.arguments.count == 1 &&
if_branch.method?(else_branch.method_name)
end

def else_source(else_branch)
if require_parentheses?(else_branch)
if branches_have_method?(else_branch.parent)
else_source_if_has_method(else_branch)
elsif require_parentheses?(else_branch)
"(#{else_branch.source})"
elsif without_argument_parentheses_method?(else_branch)
"#{else_branch.method_name}(#{else_branch.arguments.map(&:source).join(', ')})"
elsif branches_have_assignment?(else_branch.parent)
else_branch.expression.source
else_source_if_has_assignment(else_branch)
else
else_branch.source
end
end

def else_source_if_has_method(else_branch)
if require_parentheses?(else_branch.first_argument)
"(#{else_branch.first_argument.source})"
else
else_branch.first_argument.source
end
end

def else_source_if_has_assignment(else_branch)
if require_parentheses?(else_branch.expression)
"(#{else_branch.expression.source})"
else
else_branch.expression.source
end
end

def make_ternary_form(node)
_condition, if_branch, else_branch = *node
ternary_form = [if_branch.source, else_source(else_branch)].join(' || ')
Expand Down Expand Up @@ -161,8 +197,8 @@ def require_parentheses?(node)
end

def without_argument_parentheses_method?(node)
node.send_type? &&
!node.arguments.empty? && !node.parenthesized? && !node.operator_method?
node.send_type? && !node.arguments.empty? &&
!node.parenthesized? && !node.operator_method? && !node.assignment_method?
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions spec/rubocop/cop/style/redundant_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,36 @@ def test
RUBY
end

it 'registers an offense and corrects when the branches contains assignment method' do
expect_offense(<<~RUBY)
if foo
^^^^^^ Use double pipes `||` instead.
test.bar = foo
else
test.bar = 'baz'
end
RUBY

expect_correction(<<~RUBY)
test.bar = foo || 'baz'
RUBY
end

it 'registers an offense and corrects when the branches contains method call' do
expect_offense(<<~RUBY)
if foo
^^^^^^ Use double pipes `||` instead.
bar foo
else
bar 1..2
end
RUBY

expect_correction(<<~RUBY)
bar foo || (1..2)
RUBY
end

it 'does not register offenses when using `nil?` and the branches contains assignment' do
expect_no_offenses(<<~RUBY)
if foo.nil?
Expand All @@ -281,6 +311,16 @@ def test
end
RUBY
end

it 'does not register offenses when using `nil?` and the branches contains method which has multiple arguments' do
expect_no_offenses(<<~RUBY)
if foo.nil?
test.bar foo, bar
else
test.bar = 'baz', 'quux'
end
RUBY
end
end
end

Expand Down

0 comments on commit 1b6bda4

Please sign in to comment.