Sha256: 02fd046a69f4487b1f767e92d7d80cdd10496ed03dee674bac27cbdc4d8b2376

Contents?: true

Size: 922 Bytes

Versions: 16

Compression:

Stored size: 922 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module TernaryOperator
      def inspect(file, source, tokens, sexp)
        each(:ifop, sexp) do |ifop|
          if offends?(ifop)
            add_offence(:convention, all_positions(ifop).first.lineno,
                        error_message)
          end
        end
      end
    end

    class MultilineTernaryOperator < Cop
      include TernaryOperator

      def error_message
        'Avoid multi-line ?: (the ternary operator); use if/unless instead.'
      end

      def offends?(ifop)
        all_positions(ifop).map(&:lineno).uniq.size > 1
      end
    end

    class NestedTernaryOperator < Cop
      include TernaryOperator

      def error_message
        'Ternary operators must not be nested. Prefer if/else constructs ' +
          'instead.'
      end

      def offends?(ifop)
        ifop.flatten[1..-1].include?(:ifop)
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

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