Class: Rubocop::Cop::Style::HashSyntax

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/style/hash_syntax.rb

Overview

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.

Constant Summary collapse

MSG =
'Ruby 1.8 hash syntax detected'

Instance Attribute Summary

Attributes inherited from Cop

#autocorrect, #corrections, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect_action, cop_name, cop_type, #do_autocorrect, #ignore_node, inherited, #initialize, lint?, #name, rails?, style?

Constructor Details

This class inherits a constructor from Rubocop::Cop::Cop

Instance Method Details

#on_hash(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 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
end