-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new
Workit/RedundantBooleanConditional
cop
- Loading branch information
Showing
5 changed files
with
172 additions
and
0 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
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,66 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Workit | ||
# Checks for redundant boolean conditions. | ||
# | ||
# @example | ||
# # bad | ||
# true if x == y | ||
# | ||
# # good | ||
# x == y | ||
# | ||
class RedundantBooleanConditional < Base | ||
extend AutoCorrector | ||
|
||
MSG = "This conditional expression can just be replaced by `%<replaced>s`." | ||
|
||
# @!method true_or_false?(node) | ||
def_node_matcher :true_or_false?, <<~RUBY | ||
({:true :false}) | ||
RUBY | ||
|
||
def on_if(node) | ||
return unless redundant?(node) | ||
|
||
add_offense(node, message: offense_message(node)) do |corrector| | ||
corrector.replace(node, replacement_condition(node)) | ||
end | ||
end | ||
|
||
private | ||
|
||
def offense_message(node) | ||
replacement = replacement_condition(node) | ||
replaced = node.elsif? ? "\n#{replacement}" : replacement | ||
|
||
format(MSG, replaced: replaced) | ||
end | ||
|
||
def redundant?(node) | ||
return false if node.else? || node.elsif? || node.elsif_conditional? || node.ternary? | ||
|
||
node.if_branch.true_type? || node.if_branch.false_type? | ||
end | ||
|
||
def replacement_condition(node) | ||
condition = node.condition.source | ||
expression = invert_expression?(node) ? "!(#{condition})" : condition | ||
|
||
node.elsif? ? indented_else_node(expression, node) : expression | ||
end | ||
|
||
def invert_expression?(node) | ||
(!node.elsif_conditional? && (node.if? && node.if_branch.false_type?)) || | ||
(node.unless? && node.if_branch.true_type?) | ||
end | ||
|
||
def indented_else_node(expression, node) | ||
"else\n#{indentation(node)}#{expression}" | ||
end | ||
end | ||
end | ||
end | ||
end |
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
99 changes: 99 additions & 0 deletions
99
spec/rubocop/cop/workit/redundant_boolean_conditional_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,99 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Workit::RedundantBooleanConditional, :config do | ||
it "registers an offense for if with true results" do | ||
expect_offense(<<~RUBY) | ||
true if x == y | ||
^^^^^^^^^^^^^^ This conditional expression can just be replaced by `x == y`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
x == y | ||
RUBY | ||
end | ||
|
||
it "registers an offense for if with false results" do | ||
expect_offense(<<~RUBY) | ||
false if x == y | ||
^^^^^^^^^^^^^^^ This conditional expression can just be replaced by `!(x == y)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
!(x == y) | ||
RUBY | ||
end | ||
|
||
it "registers an offense for unless with true results" do | ||
expect_offense(<<~RUBY) | ||
true unless x == y | ||
^^^^^^^^^^^^^^^^^^ This conditional expression can just be replaced by `!(x == y)`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
!(x == y) | ||
RUBY | ||
end | ||
|
||
it "registers an offense for unless with false results" do | ||
expect_offense(<<~RUBY) | ||
false unless x == y | ||
^^^^^^^^^^^^^^^^^^^ This conditional expression can just be replaced by `x == y`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
x == y | ||
RUBY | ||
end | ||
|
||
it "registers an offense for if block with true results" do | ||
expect_offense(<<~RUBY) | ||
if x == y | ||
^^^^^^^^^ This conditional expression can just be replaced by `x == y`. | ||
true | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
x == y | ||
RUBY | ||
end | ||
|
||
it "registers an offense for if block with false results" do | ||
expect_offense(<<~RUBY) | ||
if x == y | ||
^^^^^^^^^ This conditional expression can just be replaced by `!(x == y)`. | ||
false | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
!(x == y) | ||
RUBY | ||
end | ||
|
||
it "registers an offense for unless block with true results" do | ||
expect_offense(<<~RUBY) | ||
unless x == y | ||
^^^^^^^^^^^^^ This conditional expression can just be replaced by `!(x == y)`. | ||
true | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
!(x == y) | ||
RUBY | ||
end | ||
|
||
it "registers an offense for unless block with false results" do | ||
expect_offense(<<~RUBY) | ||
unless x == y | ||
^^^^^^^^^^^^^ This conditional expression can just be replaced by `x == y`. | ||
false | ||
end | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
x == y | ||
RUBY | ||
end | ||
end |