class RuboCop::Cop::Style::OptionHash

This cop checks for options hashes and discourages them if the current Ruby version supports keyword arguments.

@example

Instead of:

def fry(options = {})
  temperature = options.fetch(:temperature, 300)
  ...
end

Prefer:

def fry(temperature: 300)
  ...
end

Constants

MSG

Public Instance Methods

on_args(node) click to toggle source
# File lib/rubocop/cop/style/option_hash.rb, line 25
def on_args(node)
  *_but_last, last_arg = *node

  # asserting that there was an argument at all
  return unless last_arg

  # asserting last argument is an optional argument
  return unless last_arg.optarg_type?

  arg, default_value = *last_arg

  # asserting default value is a hash
  return unless default_value.hash_type?

  # asserting default value is empty hash
  *key_value_pairs = *default_value
  return unless key_value_pairs.empty?

  # Check for suspicious argument names
  return unless name_in_suspicious_param_names?(arg)

  add_offense(last_arg, :expression, MSG)
end
validate_config() click to toggle source
# File lib/rubocop/cop/style/option_hash.rb, line 49
def validate_config
  if target_ruby_version < 2.0
    raise ValidationError, 'The `Style/OptionHash` cop is only '                                    'compatible with Ruby 2.0 and up, but the '                                    'target Ruby version for your project is '                                    "1.9.\nPlease disable this cop or adjust "                                    'the `TargetRubyVersion` parameter in your '                                    'configuration.'
  end
end

Private Instance Methods

name_in_suspicious_param_names?(arg_name) click to toggle source
# File lib/rubocop/cop/style/option_hash.rb, line 62
def name_in_suspicious_param_names?(arg_name)
  cop_config.key?('SuspiciousParamNames') &&
    cop_config['SuspiciousParamNames'].include?(arg_name.to_s)
end