Sha256: 3b1b36dbd0af0d220197eb64ba707aec7746ff92c6ebd90ce97f40fa299d8d09

Contents?: true

Size: 938 Bytes

Versions: 3

Compression:

Stored size: 938 Bytes

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for uses of *and* and *or*.
      class AndOr < Cop
        MSG = 'Use %s instead of %s.'

        OPS = { 'and' => '&&', 'or' => '||' }

        def on_and(node)
          process_logical_op(node)

          super
        end

        def on_or(node)
          process_logical_op(node)

          super
        end

        private

        def process_logical_op(node)
          op = node.loc.operator.source
          op_type = node.type.to_s

          if op == op_type
            add_offence(:convention,
                        node.loc.operator,
                        sprintf(MSG, OPS[op], op))
            do_autocorrect(node)
          end
        end

        def autocorrect_action(node)
          replacement = (node.type == :and ? '&&' : '||')
          replace(node.loc.operator, replacement)
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
rubocop-0.9.1 lib/rubocop/cop/style/and_or.rb
sabat-rubocop-0.9.0 lib/rubocop/cop/style/and_or.rb
rubocop-0.9.0 lib/rubocop/cop/style/and_or.rb