Sha256: a124b88a96849b2365847a3db79e7bd7955ebb948caffb750633f394b01ecafc

Contents?: true

Size: 1.14 KB

Versions: 6

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Style
      # This cop looks for *unless* expressions with *else* clauses.
      class UnlessElse < Cop
        MSG = 'Do not use `unless` with `else`. Rewrite these with the ' \
              'positive case first.'.freeze

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

          add_offense(node, :expression)
        end

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

          lambda do |corrector|
            corrector.replace(node.loc.keyword, 'if'.freeze)
            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

6 entries across 6 versions & 1 rubygems

Version Path
rubocop-0.49.1 lib/rubocop/cop/style/unless_else.rb
rubocop-0.49.0 lib/rubocop/cop/style/unless_else.rb
rubocop-0.48.1 lib/rubocop/cop/style/unless_else.rb
rubocop-0.48.0 lib/rubocop/cop/style/unless_else.rb
rubocop-0.47.1 lib/rubocop/cop/style/unless_else.rb
rubocop-0.47.0 lib/rubocop/cop/style/unless_else.rb