Class: Rubocop::Cop::Style::BracesAroundHashParameters

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

Overview

This cop checks for braces in method calls with hash parameters.

Constant Summary

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, non_rails, rails?, style?, #support_autocorrect?, #warning

Constructor Details

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

Instance Method Details

#on_send(node) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubocop/cop/style/braces_around_hash_parameters.rb', line 8

def on_send(node)
  _receiver, method_name, *args = *node

  # discard attr writer methods.
  return if method_name.to_s.end_with?('=')
  # discard operator methods
  return if OPERATOR_METHODS.include?(method_name)

  # we care only for the first argument
  arg = args.last
  return unless arg && arg.type == :hash && arg.children.any?

  has_braces = !arg.loc.begin.nil?
  all_hashes = args.length > 1 && args.all? { |a| a.type == :hash }

  if style == :no_braces && has_braces && !all_hashes
    convention(arg,
               :expression,
               'Redundant curly braces around a hash parameter.')
  elsif style == :braces && !has_braces
    convention(arg,
               :expression,
               'Missing curly braces around a hash parameter.')
  end
end