Sha256: bc55ee1945e9d9a98e0006fe7cb701533b90ed209c29fe685af2e473867444b4

Contents?: true

Size: 1.38 KB

Versions: 2

Compression:

Stored size: 1.38 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # Some common code shared between the two cops.
      module FavorOtherKeywordOverNegation
        def check(node)
          condition, _body, _rest = *node

          # Look at last expression of contents if there's a parenthesis
          # around condition.
          condition = condition.children.last while condition.type == :begin

          if condition.type == :send
            _object, method = *condition
            if method == :! && !(node.loc.respond_to?(:else) && node.loc.else)
              add_offence(:convention, node.loc.expression, error_message)
            end
          end
        end
      end

      # Checks for uses of if with a negated condition. Only ifs
      # without else are considered.
      class FavorUnlessOverNegatedIf < Cop
        include FavorOtherKeywordOverNegation

        def on_if(node)
          check(node)
        end

        def error_message
          'Favor unless (or control flow or) over if for negative conditions.'
        end
      end

      # Checks for uses of while with a negated condition.
      class FavorUntilOverNegatedWhile < Cop
        include FavorOtherKeywordOverNegation

        def on_while(node)
          check(node)
        end

        def error_message
          'Favor until over while for negative conditions.'
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rubocop-0.11.0 lib/rubocop/cop/style/favor_unless_over_negated_if.rb
rubocop-0.10.0 lib/rubocop/cop/style/favor_unless_over_negated_if.rb