Sha256: 26325b22c089008f6090a99a9bdf1da9618eb1b8f1d18616a06052e33c3eec7a

Contents?: true

Size: 1.04 KB

Versions: 4

Compression:

Stored size: 1.04 KB

Contents

# encoding: utf-8

module Rubocop
  module Cop
    module Style
      # This cop checks for uses of the Ruby 1.8 hash literal syntax,
      # when the 1.9 syntax is applicable as well.
      #
      # A separate offence is registered for each problematic pair.
      class HashSyntax < Cop
        MSG = 'Ruby 1.8 hash syntax detected'

        def on_hash(node)
          pairs = *node

          sym_indices = pairs.all? { |p| word_symbol_pair?(p) }

          if sym_indices
            pairs.each do |pair|
              if pair.loc.operator && pair.loc.operator.is?('=>')
                add_offence(:convention,
                            pair.loc.expression.begin.join(pair.loc.operator),
                            MSG)
              end
            end
          end
        end

        private

        def word_symbol_pair?(pair)
          key, _value = *pair

          if key.type == :sym
            sym_name = key.to_a[0]

            sym_name =~ /\A\w+\z/
          else
            false
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rubocop-0.12.0 lib/rubocop/cop/style/hash_syntax.rb
rubocop-0.11.1 lib/rubocop/cop/style/hash_syntax.rb
rubocop-0.11.0 lib/rubocop/cop/style/hash_syntax.rb
rubocop-0.10.0 lib/rubocop/cop/style/hash_syntax.rb