class Rubocop::Cop::Style::HashSyntax

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.

Constants

MSG

Public Instance Methods

on_hash(node) click to toggle source
Calls superclass method
# File lib/rubocop/cop/style/hash_syntax.rb, line 13
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

  super
end

Private Instance Methods

word_symbol_pair?(pair) click to toggle source
# File lib/rubocop/cop/style/hash_syntax.rb, line 33
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