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'

Constants inherited from Cop

Cop::OPERATOR_METHODS

Instance Attribute Summary

Attributes inherited from Cop

#config, #corrections, #offences, #processed_source

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, all, #autocorrect?, #autocorrect_action, #convention, #cop_config, #cop_name, cop_name, cop_type, #debug?, #ignore_node, inherited, #initialize, lint?, #message, rails?, style?, #warning

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?('=>')
        convention(nil,
                   pair.loc.expression.begin.join(pair.loc.operator),
                   MSG)
      end
    end
  end
end