Sha256: b8c6e8b046ec5ccd73794c2a697cf920edd4e6d517327ff69b00ea19325bafdf

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for multi-line ternary op expressions.
      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)

          convention(node, :expression) if loc.line != loc.colon.line
        end
      end

      # This cop checks for nested ternary op expressions.
      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|
              convention(c, :expression) if c.loc.respond_to?(:question)
            end
          end
        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/ternary_operator.rb
rubocop-0.14.1 lib/rubocop/cop/style/ternary_operator.rb
rubocop-0.14.0 lib/rubocop/cop/style/ternary_operator.rb
rubocop-0.13.1 lib/rubocop/cop/style/ternary_operator.rb
rubocop-0.13.0 lib/rubocop/cop/style/ternary_operator.rb