-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new Style/InvertibleUnlessCondition
cop
#11432
Merged
bbatsov
merged 1 commit into
rubocop:master
from
fatkodima:invertible_unless_condition-cop
Jan 23, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ | ||
* [#11410](https://github.com/rubocop/rubocop/issues/11410): Add new `Style/InvertibleUnlessCondition` 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Style | ||
# Checks for usages of `unless` which can be replaced by `if` with inverted condition. | ||
# Code without `unless` is easier to read, but that is subjective, so this cop | ||
# is disabled by default. | ||
# | ||
# Methods that can be inverted should be defined in `InverseMethods`. Note that | ||
# the relationship of inverse methods needs to be defined in both directions. | ||
# For example, | ||
# InverseMethods: | ||
# :!=: :== | ||
# :even?: :odd? | ||
# :odd?: :even? | ||
# | ||
# will suggest both `even?` and `odd?` to be inverted, but only `!=` (and not `==`). | ||
# | ||
# @safety | ||
# This cop is unsafe because it cannot be guaranteed that the method | ||
# and its inverse method are both defined on receiver, and also are | ||
# actually inverse of each other. | ||
# | ||
# @example | ||
# # bad (simple condition) | ||
# foo unless !bar | ||
# foo unless x != y | ||
# foo unless x >= 10 | ||
# foo unless x.even? | ||
# | ||
# # good | ||
# foo if bar | ||
# foo if x == y | ||
# foo if x < 10 | ||
# foo if x.odd? | ||
# | ||
# # bad (complex condition) | ||
# foo unless x != y || x.even? | ||
# | ||
# # good | ||
# foo if x == y && x.odd? | ||
# | ||
# # good (if) | ||
# foo if !condition | ||
# | ||
class InvertibleUnlessCondition < Base | ||
extend AutoCorrector | ||
|
||
MSG = 'Favor `if` with inverted condition over `unless`.' | ||
|
||
def on_if(node) | ||
return unless node.unless? | ||
|
||
condition = node.condition | ||
return unless invertible?(condition) | ||
|
||
add_offense(node) do |corrector| | ||
corrector.replace(node.loc.keyword, node.inverse_keyword) | ||
autocorrect(corrector, condition) | ||
end | ||
end | ||
|
||
private | ||
|
||
def invertible?(node) | ||
case node.type | ||
when :begin | ||
invertible?(node.children.first) | ||
when :send | ||
return if inheritance_check?(node) | ||
|
||
node.method?(:!) || inverse_methods.key?(node.method_name) | ||
when :or, :and | ||
invertible?(node.lhs) && invertible?(node.rhs) | ||
else | ||
false | ||
end | ||
end | ||
|
||
def inheritance_check?(node) | ||
argument = node.first_argument | ||
node.method?(:<) && | ||
(argument.const_type? && argument.short_name.to_s.upcase != argument.short_name.to_s) | ||
end | ||
|
||
def autocorrect(corrector, node) | ||
case node.type | ||
when :begin | ||
autocorrect(corrector, node.children.first) | ||
when :send | ||
autocorrect_send_node(corrector, node) | ||
when :or, :and | ||
corrector.replace(node.loc.operator, node.inverse_operator) | ||
autocorrect(corrector, node.lhs) | ||
autocorrect(corrector, node.rhs) | ||
end | ||
end | ||
|
||
def autocorrect_send_node(corrector, node) | ||
if node.method?(:!) | ||
corrector.remove(node.loc.selector) | ||
else | ||
corrector.replace(node.loc.selector, inverse_methods[node.method_name]) | ||
end | ||
end | ||
|
||
def inverse_methods | ||
@inverse_methods ||= cop_config['InverseMethods'] | ||
end | ||
end | ||
end | ||
end | ||
end |
94 changes: 94 additions & 0 deletions
94
spec/rubocop/cop/style/invertible_unless_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,94 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Style::InvertibleUnlessCondition, :config do | ||
let(:cop_config) do | ||
{ | ||
'InverseMethods' => { | ||
:!= => :==, | ||
:even? => :odd?, | ||
:odd? => :even?, | ||
:>= => :<, | ||
:< => :>=, | ||
:zero? => :nonzero? | ||
} | ||
} | ||
end | ||
|
||
context 'when invertible `unless`' do | ||
it 'registers an offense and corrects when using `!` negation' do | ||
expect_offense(<<~RUBY) | ||
foo unless !x | ||
^^^^^^^^^^^^^ Favor `if` with inverted condition over `unless`. | ||
foo unless !!x | ||
^^^^^^^^^^^^^^ Favor `if` with inverted condition over `unless`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo if x | ||
foo if !x | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using simple operator condition' do | ||
expect_offense(<<~RUBY) | ||
foo unless x != y | ||
^^^^^^^^^^^^^^^^^ Favor `if` with inverted condition over `unless`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo if x == y | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using simple method condition' do | ||
expect_offense(<<~RUBY) | ||
foo unless x.odd? | ||
^^^^^^^^^^^^^^^^^ Favor `if` with inverted condition over `unless`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo if x.even? | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using simple bracketed condition' do | ||
expect_offense(<<~RUBY) | ||
foo unless ((x != y)) | ||
^^^^^^^^^^^^^^^^^^^^^ Favor `if` with inverted condition over `unless`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo if ((x == y)) | ||
RUBY | ||
end | ||
|
||
it 'registers an offense and corrects when using complex condition' do | ||
expect_offense(<<~RUBY) | ||
foo unless x != y && (((x.odd?) || (((y >= 5)))) || z.zero?) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Favor `if` with inverted condition over `unless`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
foo if x == y || (((x.even?) && (((y < 5)))) && z.nonzero?) | ||
RUBY | ||
end | ||
end | ||
|
||
it 'does not register an offense when using non invertible `unless`' do | ||
expect_no_offenses(<<~RUBY) | ||
foo unless x != y || x.awesome? | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when checking for inheritance' do | ||
expect_no_offenses(<<~RUBY) | ||
foo unless x < Foo | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when using invertible `if`' do | ||
expect_no_offenses(<<~RUBY) | ||
foo if !condition | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably it's a good idea to mention here that the idea is that code without
unless
is easier to read, but that's subjective that's why the cop is disabled by default.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.