unless_multiple_conditions: add autocorrect
This commit is contained in:
		
							parent
							
								
									b9854bd4cf
								
							
						
					
					
						commit
						2a80427755
					
				@ -14,7 +14,24 @@ module RuboCop
 | 
			
		||||
 | 
			
		||||
        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
 | 
			
		||||
 | 
			
		||||
@ -16,7 +16,7 @@ describe RuboCop::Cop::Style::UnlessMultipleConditions do
 | 
			
		||||
 | 
			
		||||
    expect_offense <<~RUBY
 | 
			
		||||
      something unless foo && bar
 | 
			
		||||
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
 | 
			
		||||
                ^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
 | 
			
		||||
    RUBY
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@ -30,7 +30,7 @@ describe RuboCop::Cop::Style::UnlessMultipleConditions do
 | 
			
		||||
 | 
			
		||||
    expect_offense <<~RUBY
 | 
			
		||||
      something unless foo || bar
 | 
			
		||||
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
 | 
			
		||||
                ^^^^^^^^^^^^^^^^^ Avoid using `unless` with multiple conditions.
 | 
			
		||||
    RUBY
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
@ -69,4 +69,60 @@ describe RuboCop::Cop::Style::UnlessMultipleConditions do
 | 
			
		||||
      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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user