Sha256: 0bc4a909490b1ae56496d3d997271fad833395e844a32ebb567f5b399753cbdb

Contents?: true

Size: 1.47 KB

Versions: 5

Compression:

Stored size: 1.47 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)
              convention(node, :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)
          return unless node.loc.respond_to?(:keyword)
          return if node.loc.keyword.is?('elsif')

          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

5 entries across 5 versions & 1 rubygems

Version Path
rubocop-0.15.0 lib/rubocop/cop/style/favor_unless_over_negated_if.rb
rubocop-0.14.1 lib/rubocop/cop/style/favor_unless_over_negated_if.rb
rubocop-0.14.0 lib/rubocop/cop/style/favor_unless_over_negated_if.rb
rubocop-0.13.1 lib/rubocop/cop/style/favor_unless_over_negated_if.rb
rubocop-0.13.0 lib/rubocop/cop/style/favor_unless_over_negated_if.rb