diff --git a/Library/.rubocop.yml b/Library/.rubocop.yml index f9d7a1cd9a..e15f7840a6 100644 --- a/Library/.rubocop.yml +++ b/Library/.rubocop.yml @@ -460,6 +460,11 @@ Style/StringLiteralsInInterpolation: Style/TernaryParentheses: 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 Style/WordArray: MinSize: 4 diff --git a/Library/Homebrew/rubocops.rb b/Library/Homebrew/rubocops.rb index 794be945d0..0d17d82478 100644 --- a/Library/Homebrew/rubocops.rb +++ b/Library/Homebrew/rubocops.rb @@ -12,8 +12,6 @@ require "rubocop-rails" require "rubocop-rspec" require "rubocop-sorbet" -require "rubocops/unless_multiple_conditions" - require "rubocops/formula_desc" require "rubocops/components_order" require "rubocops/components_redundancy" diff --git a/Library/Homebrew/rubocops/unless_multiple_conditions.rb b/Library/Homebrew/rubocops/unless_multiple_conditions.rb deleted file mode 100644 index 4e4d673e47..0000000000 --- a/Library/Homebrew/rubocops/unless_multiple_conditions.rb +++ /dev/null @@ -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 diff --git a/Library/Homebrew/test/rubocops/unless_multiple_conditions_spec.rb b/Library/Homebrew/test/rubocops/unless_multiple_conditions_spec.rb deleted file mode 100644 index 186c419044..0000000000 --- a/Library/Homebrew/test/rubocops/unless_multiple_conditions_spec.rb +++ /dev/null @@ -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