Skip to content

Commit

Permalink
unless_multiple_conditions: add autocorrect
Browse files Browse the repository at this point in the history
  • Loading branch information
SeekingMeaning committed Jan 8, 2021
1 parent b9854bd commit 2a80427
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
19 changes: 18 additions & 1 deletion Library/Homebrew/rubocops/unless_multiple_conditions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,24 @@ class UnlessMultipleConditions < Cop

sig { params(node: RuboCop::AST::IfNode).void }
def on_if(node)
add_offense(node) if node.unless? && (node.condition.and_type? || node.condition.or_type?)
return if !node.unless? || (!node.condition.and_type? && !node.condition.or_type?)

add_offense(node, location: node.condition.source_range.with(begin_pos: node.loc.keyword.begin_pos))
end

sig { params(node: RuboCop::AST::IfNode).returns(T.proc.params(arg0: RuboCop::Cop::Corrector).void) }
def autocorrect(node)
lambda 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
Expand Down
60 changes: 58 additions & 2 deletions Library/Homebrew/test/rubocops/unless_multiple_conditions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

expect_offense <<~RUBY
something unless foo && bar
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
RUBY
end

Expand All @@ -30,7 +30,7 @@

expect_offense <<~RUBY
something unless foo || bar
^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
RUBY
end

Expand Down Expand Up @@ -69,4 +69,60 @@
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

0 comments on commit 2a80427

Please sign in to comment.