rubocops: use Style/UnlessLogicalOperators

Replaces `Style/UnlessMultipleConditions`
This commit is contained in:
Seeker 2021-03-03 01:39:11 -08:00
parent 13508eebaa
commit 7e7274aaeb
4 changed files with 5 additions and 165 deletions

View File

@ -460,6 +460,11 @@ Style/StringLiteralsInInterpolation:
Style/TernaryParentheses: Style/TernaryParentheses:
EnforcedStyle: require_parentheses_when_complex EnforcedStyle: require_parentheses_when_complex
# `unless ... ||` and `unless ... &&` are hard to mentally parse
Style/UnlessLogicalOperators:
Enabled: true
EnforcedStyle: forbid_logical_operators
# a bit confusing to non-Rubyists but useful for longer arrays # a bit confusing to non-Rubyists but useful for longer arrays
Style/WordArray: Style/WordArray:
MinSize: 4 MinSize: 4

View File

@ -12,8 +12,6 @@ require "rubocop-rails"
require "rubocop-rspec" require "rubocop-rspec"
require "rubocop-sorbet" require "rubocop-sorbet"
require "rubocops/unless_multiple_conditions"
require "rubocops/formula_desc" require "rubocops/formula_desc"
require "rubocops/components_order" require "rubocops/components_order"
require "rubocops/components_redundancy" require "rubocops/components_redundancy"

View File

@ -1,35 +0,0 @@
# typed: strict
# frozen_string_literal: true
module RuboCop
module Cop
module Style
# This cop checks that `unless` is not used with multiple conditions.
#
# @api private
class UnlessMultipleConditions < Base
extend T::Sig
extend AutoCorrector
MSG = "Avoid using `unless` with multiple conditions."
sig { params(node: RuboCop::AST::IfNode).void }
def on_if(node)
return if !node.unless? || (!node.condition.and_type? && !node.condition.or_type?)
add_offense(node.condition.source_range.with(begin_pos: node.loc.keyword.begin_pos)) do |corrector|
corrector.replace(node.loc.keyword, "if")
corrector.replace(node.condition.loc.operator, node.condition.inverse_operator)
[node.condition.lhs, node.condition.rhs].each do |subcondition|
if !subcondition.source.start_with?("(") || !subcondition.source.end_with?(")")
corrector.insert_before(subcondition, "(")
corrector.insert_after(subcondition, ")")
end
corrector.insert_before(subcondition, "!")
end
end
end
end
end
end
end

View File

@ -1,128 +0,0 @@
# typed: false
# frozen_string_literal: true
require "rubocops/unless_multiple_conditions"
describe RuboCop::Cop::Style::UnlessMultipleConditions do
subject(:cop) { described_class.new }
it "reports an offense when using `unless` with multiple `and` conditions" do
expect_offense <<~RUBY
unless foo && bar
^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
something
end
RUBY
expect_offense <<~RUBY
something unless foo && bar
^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
RUBY
end
it "reports an offense when using `unless` with multiple `or` conditions" do
expect_offense <<~RUBY
unless foo || bar
^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
something
end
RUBY
expect_offense <<~RUBY
something unless foo || bar
^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
RUBY
end
it "reports no offenses when using `if` with multiple `and` conditions" do
expect_no_offenses <<~RUBY
if !foo && !bar
something
end
RUBY
expect_no_offenses <<~RUBY
something if !foo && !bar
RUBY
end
it "reports no offenses when using `if` with multiple `or` conditions" do
expect_no_offenses <<~RUBY
if !foo || !bar
something
end
RUBY
expect_no_offenses <<~RUBY
something if !foo || !bar
RUBY
end
it "reports no offenses when using `unless` with single condition" do
expect_no_offenses <<~RUBY
unless foo
something
end
RUBY
expect_no_offenses <<~RUBY
something unless foo
RUBY
end
it "auto-corrects `unless` with multiple `and` conditions" do
source = <<~RUBY
unless foo && (bar || baz)
something
end
RUBY
corrected_source = <<~RUBY
if !(foo) || !(bar || baz)
something
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
source = <<~RUBY
something unless foo && bar
RUBY
corrected_source = <<~RUBY
something if !(foo) || !(bar)
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end
it "auto-corrects `unless` with multiple `or` conditions" do
source = <<~RUBY
unless foo || (bar && baz)
something
end
RUBY
corrected_source = <<~RUBY
if !(foo) && !(bar && baz)
something
end
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
source = <<~RUBY
something unless foo || bar
RUBY
corrected_source = <<~RUBY
something if !(foo) && !(bar)
RUBY
new_source = autocorrect_source(source)
expect(new_source).to eq(corrected_source)
end
end