Sha256: 93cc067dfb058b0cc811fcad2d804e1975b21479adc64d8cdd2001b4593681d4

Contents?: true

Size: 951 Bytes

Versions: 4

Compression:

Stored size: 951 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    class MultilineTernaryOperator < Cop
      MSG =
        'Avoid multi-line ?: (the ternary operator); use if/unless instead.'

      def on_if(node)
        loc = node.loc

        # discard non-ternary ops
        return unless loc.respond_to?(:question)

        add_offence(:convention, loc.line, MSG) if loc.line != loc.colon.line

        super
      end
    end

    class NestedTernaryOperator < Cop
      MSG = 'Ternary operators must not be nested. Prefer if/else ' +
          'constructs instead.'

      def on_if(node)
        loc = node.loc

        # discard non-ternary ops
        return unless loc.respond_to?(:question)

        node.children.each do |child|
          on_node(:if, child) do |c|
            if c.loc.respond_to?(:question)
              add_offence(:convention, c.loc.line, MSG)
            end
          end
        end

        super
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.8.3 lib/rubocop/cop/ternary_operator.rb
rubocop-0.8.2 lib/rubocop/cop/ternary_operator.rb
rubocop-0.8.1 lib/rubocop/cop/ternary_operator.rb
rubocop-0.8.0 lib/rubocop/cop/ternary_operator.rb