Class: Rubocop::Cop::HashSyntax

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

Constant Summary collapse

ERROR_MESSAGE =
'Ruby 1.8 hash syntax detected'

Instance Attribute Summary

Attributes inherited from Cop

#correlations, #debug, #disabled_lines, #offences

Instance Method Summary collapse

Methods inherited from Cop

#add_offence, cop_name, #has_report?, inherited, #initialize, #name

Constructor Details

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

Instance Method Details

#inspect(file, source, tokens, sexp) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rubocop/cop/hash_syntax.rb', line 8

def inspect(file, source, tokens, sexp)
  each(:assoclist_from_args, sexp) do |assoclist_from_args|
    keys = assoclist_from_args[1].map { |assoc_new| assoc_new[1][0] }
    # If at least one of the keys in the hash is neither a symbol (:a)
    # nor a label (a:), we can't require the new syntax.
    return if keys.any? do |key|
      ![:symbol_literal, :@label].include?(key)
    end
  end
  each(:assoc_new, sexp) do |assoc_new|
    if assoc_new[1][0] == :symbol_literal
      add_offence(:convention, assoc_new[1][1][1][-1].lineno,
                  ERROR_MESSAGE)
    end
  end
end