Sha256: 0f2f876dfa64207ab659ba5977c7a408c273327a48bfd553e47421c34c9377b3

Contents?: true

Size: 1.28 KB

Versions: 14

Compression:

Stored size: 1.28 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module FavorOtherKeywordOverNegation
      private
      def check(grammar_part, sexp)
        each(grammar_part, sexp) do |s|
          # Don't complain about negative if/else. We don't want unless/else.
          next if s[3] && [:else, :elsif].include?(s[3][0])

          condition = s[1]
          condition = condition[1][0] while condition[0] == :paren

          if condition[0] == :unary && [:!, :not].include?(condition[1])
            add_offence(:convention, all_positions(s).first.lineno,
                        error_message)
          end
        end
      end
    end

    class FavorUnlessOverNegatedIf < Cop
      include FavorOtherKeywordOverNegation

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

      def inspect(file, source, tokens, sexp)
        [:if, :if_mod].each { |grammar_part| check(grammar_part, sexp) }
      end
    end

    class FavorUntilOverNegatedWhile < Cop
      include FavorOtherKeywordOverNegation

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

      def inspect(file, source, tokens, sexp)
        [:while, :while_mod].each { |grammar_part| check(grammar_part, sexp) }
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rubocop-0.7.2 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.7.1 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.7.0 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.6.1 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.6.0 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.5.0 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.4.6 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.4.5 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.4.4 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.4.3 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.4.2 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.4.1 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.4.0 lib/rubocop/cop/favor_unless_over_negated_if.rb
rubocop-0.3.2 lib/rubocop/cop/favor_unless_over_negated_if.rb