Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Style/NegatedIfElseCondition
cop
- Loading branch information
Showing
10 changed files
with
306 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#8967](https://github.com/rubocop-hq/rubocop/pull/8967): Add new `Style/NegatedIfElseCondition` cop. ([@fatkodima][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# This cop checks for uses of `if-else` and ternary operators with a negated condition | ||
# which can be simplified by inverting condition and swapping branches. | ||
# | ||
# @example | ||
# # bad | ||
# if !x | ||
# do_something | ||
# else | ||
# do_something_else | ||
# end | ||
# | ||
# # good | ||
# if x | ||
# do_something_else | ||
# else | ||
# do_something | ||
# end | ||
# | ||
# # bad | ||
# !x ? do_something : do_something_else | ||
# | ||
# # good | ||
# x ? do_something_else : do_something | ||
# | ||
class NegatedIfElseCondition < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Invert the negated condition and swap the %<type>s branches.' | ||
|
||
NEGATED_EQUALITY_METHODS = %i[!= !~].freeze | ||
|
||
def self.autocorrect_incompatible_with | ||
[Style::InverseMethods, Style::Not] | ||
end | ||
|
||
def on_new_investigation | ||
@corrected_nodes = nil | ||
end | ||
|
||
def on_if(node) | ||
return unless if_else?(node) | ||
|
||
condition = node.condition | ||
return unless negated_condition?(condition) | ||
|
||
type = node.ternary? ? 'ternary' : 'if-else' | ||
add_offense(node, message: format(MSG, type: type)) do |corrector| | ||
unless corrected_ancestor?(node) | ||
correct_negated_condition(corrector, condition) | ||
swap_branches(corrector, node) | ||
|
||
@corrected_nodes ||= Set.new.compare_by_identity | ||
@corrected_nodes.add(node) | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def if_else?(node) | ||
else_branch = node.else_branch | ||
!node.elsif? && else_branch && (!else_branch.if_type? || !else_branch.elsif?) | ||
end | ||
|
||
def negated_condition?(node) | ||
node.send_type? && | ||
(node.negation_method? || NEGATED_EQUALITY_METHODS.include?(node.method_name)) | ||
end | ||
|
||
def corrected_ancestor?(node) | ||
node.each_ancestor(:if).any? { |ancestor| @corrected_nodes&.include?(ancestor) } | ||
end | ||
|
||
def correct_negated_condition(corrector, node) | ||
receiver, method_name, rhs = *node | ||
replacement = | ||
if node.negation_method? | ||
receiver.source | ||
else | ||
inverted_method = method_name.to_s.sub('!', '=') | ||
"#{receiver.source} #{inverted_method} #{rhs.source}" | ||
end | ||
|
||
corrector.replace(node, replacement) | ||
end | ||
|
||
def swap_branches(corrector, node) | ||
corrector.replace(node.if_branch, node.else_branch.source) | ||
corrector.replace(node.else_branch, node.if_branch.source) | ||
end | ||
end | ||
end | ||
end | ||
end |
147 changes: 147 additions & 0 deletions
147
spec/rubocop/cop/style/negated_if_else_condition_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::NegatedIfElseCondition do | ||
subject(:cop) { described_class.new } | ||
|
||
it 'registers an offense and corrects when negating condition with `!` for `if-else`' do | ||
expect_offense(<<~RUBY) | ||
if !x | ||
^^^^^ Invert the negated condition and swap the if-else branches. | ||
do_something | ||
else | ||
do_something_else | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
if x | ||
do_something_else | ||
else | ||
do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when negating condition with `not` for `if-else`' do | ||
expect_offense(<<~RUBY) | ||
if not x | ||
^^^^^^^^ Invert the negated condition and swap the if-else branches. | ||
do_something | ||
else | ||
do_something_else | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
if x | ||
do_something_else | ||
else | ||
do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when negating condition with `not` for ternary' do | ||
expect_offense(<<~RUBY) | ||
!x ? do_something : do_something_else | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the ternary branches. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
x ? do_something_else : do_something | ||
RUBY | ||
end | ||
|
||
shared_examples 'negation method' do |method, inverted_method| | ||
it "registers an offense and corrects when negating condition with `#{method}` for `if-else`" do | ||
expect_offense(<<~RUBY, method: method) | ||
if x %{method} y | ||
^^^^^^{method}^^ Invert the negated condition and swap the if-else branches. | ||
do_something | ||
else | ||
do_something_else | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
if x #{inverted_method} y | ||
do_something_else | ||
else | ||
do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it "registers an offense and corrects when negating condition with `#{method}` for ternary" do | ||
expect_offense(<<~RUBY, method: method) | ||
x %{method} y ? do_something : do_something_else | ||
^^^{method}^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Invert the negated condition and swap the ternary branches. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
x #{inverted_method} y ? do_something_else : do_something | ||
RUBY | ||
end | ||
end | ||
|
||
it_behaves_like('negation method', '!=', '==') | ||
it_behaves_like('negation method', '!~', '=~') | ||
|
||
it 'registers an offense and corrects nested `if-else` with negated condition' do | ||
expect_offense(<<~RUBY) | ||
if !x | ||
^^^^^ Invert the negated condition and swap the if-else branches. | ||
do_something | ||
else | ||
if !y | ||
^^^^^ Invert the negated condition and swap the if-else branches. | ||
do_something_else_1 | ||
else | ||
do_something_else_2 | ||
end | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
if x | ||
if y | ||
do_something_else_2 | ||
else | ||
do_something_else_1 | ||
end | ||
else | ||
do_something | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when negating condition for `if-elsif`' do | ||
expect_no_offenses(<<~RUBY) | ||
if !x | ||
do_something | ||
elsif !y | ||
do_something_else | ||
else | ||
do_another_thing | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when not whe whole condition is negated' do | ||
expect_no_offenses(<<~RUBY) | ||
if !x && y | ||
do_something | ||
else | ||
do_another_thing | ||
end | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when `if` with negated condition has no `else` branch' do | ||
expect_no_offenses(<<~RUBY) | ||
if !x | ||
do_something | ||
end | ||
RUBY | ||
end | ||
end |