Sha256: f4f1c5a755c4533c8ad1d08e3eadbed7e1f548c6c846b345d8237eabedd231d8

Contents?: true

Size: 1.43 KB

Versions: 49

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop looks for *unless* expressions with *else* clauses.
      #
      # @example
      #   # bad
      #   unless foo_bar.nil?
      #     # do something...
      #   else
      #     # do a different thing...
      #   end
      #
      #   # good
      #   if foo_bar.present?
      #     # do something...
      #   else
      #     # do a different thing...
      #   end
      class UnlessElse < Cop
        include RangeHelp

        MSG = 'Do not use `unless` with `else`. Rewrite these with the ' \
              'positive case first.'

        def on_if(node)
          return unless node.unless? && node.else?

          add_offense(node)
        end

        def autocorrect(node)
          body_range = range_between_condition_and_else(node, node.condition)
          else_range = range_between_else_and_end(node)

          lambda do |corrector|
            corrector.replace(node.loc.keyword, 'if')
            corrector.replace(body_range, else_range.source)
            corrector.replace(else_range, body_range.source)
          end
        end

        def range_between_condition_and_else(node, condition)
          range_between(condition.source_range.end_pos, node.loc.else.begin_pos)
        end

        def range_between_else_and_end(node)
          range_between(node.loc.else.end_pos, node.loc.end.begin_pos)
        end
      end
    end
  end
end

Version data entries

49 entries across 30 versions & 3 rubygems

Version Path
grape-extra_validators-2.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/unless_else.rb
rubocop-0.84.0 lib/rubocop/cop/style/unless_else.rb
rubocop-0.83.0 lib/rubocop/cop/style/unless_else.rb
rubocop-0.82.0 lib/rubocop/cop/style/unless_else.rb
rubocop-0.81.0 lib/rubocop/cop/style/unless_else.rb
rubocop-0.80.1 lib/rubocop/cop/style/unless_else.rb
rubocop-0.80.0 lib/rubocop/cop/style/unless_else.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.6.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/unless_else.rb
grape-extra_validators-1.0.0 vendor/bundle/ruby/2.4.0/gems/rubocop-0.79.0/lib/rubocop/cop/style/unless_else.rb
rubocop-0.79.0 lib/rubocop/cop/style/unless_else.rb
rubocop-0.78.0 lib/rubocop/cop/style/unless_else.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.1/lib/rubocop/cop/style/unless_else.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/unless_else.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.76.0/lib/rubocop/cop/style/unless_else.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.77.0/lib/rubocop/cop/style/unless_else.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/unless_else.rb
zuora_connect_ui-0.10.0 vendor/ruby/2.6.0/gems/rubocop-0.72.0/lib/rubocop/cop/style/unless_else.rb
rubocop-0.77.0 lib/rubocop/cop/style/unless_else.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.74.0/lib/rubocop/cop/style/unless_else.rb
zuora_connect_ui-0.9.2 vendor/ruby/2.6.0/gems/rubocop-0.75.0/lib/rubocop/cop/style/unless_else.rb