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?, #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

#autocorrect_action(node) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/rubocop/cop/style/hash_syntax.rb', line 28

def autocorrect_action(node)
  @corrections << lambda do |corrector|
    replacement = node.loc.expression.source[1..-1]
      .sub(/\s*=>\s*/, ': ')
    corrector.replace(node.loc.expression, replacement)
  end
end

#on_hash(node) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# 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(pair,
                   pair.loc.expression.begin.join(pair.loc.operator))
      end
    end
  end
end