Skip to content

Commit

Permalink
[Fix rubocop#9959] Make Style/IdenticalConditionalBranches able to …
Browse files Browse the repository at this point in the history
…handle ternary `if`s.
  • Loading branch information
dvandersluis committed Aug 2, 2021
1 parent d40dfe7 commit ac93839
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_make_styleidenticalconditionalbranches.md
@@ -0,0 +1 @@
* [#9959](https://github.com/rubocop/rubocop/issues/9959): Make `Style/IdenticalConditionalBranches` able to handle ternary `if`s. ([@dvandersluis][])
4 changes: 3 additions & 1 deletion lib/rubocop/cop/style/identical_conditional_branches.rb
Expand Up @@ -134,11 +134,13 @@ def duplicated_expressions?(expressions)
expressions.size > 1 && expressions.uniq.one?
end

def check_expressions(node, expressions, insert_position)
def check_expressions(node, expressions, insert_position) # rubocop:disable Metrics/MethodLength
inserted_expression = false

expressions.each do |expression|
add_offense(expression) do |corrector|
next if node.if_type? && node.ternary?

range = range_by_whole_lines(expression.source_range, include_final_newline: true)
corrector.remove(range)
next if inserted_expression
Expand Down
12 changes: 12 additions & 0 deletions spec/rubocop/cop/style/identical_conditional_branches_spec.rb
Expand Up @@ -382,4 +382,16 @@
RUBY
end
end

context 'with a ternary' do
it 'registers an offense' do
expect_offense(<<~RUBY)
x ? y : y
^ Move `y` out of the conditional.
^ Move `y` out of the conditional.
RUBY

expect_no_corrections
end
end
end

0 comments on commit ac93839

Please sign in to comment.