Class: RuboCop::Cop::Airbnb::OptArgParameters

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/airbnb/opt_arg_parameters.rb

Overview

Cop to enforce use of options hash over default arguments github.com/airbnb/ruby#no-default-args

Constant Summary collapse

MSG =
'Do not use default positional arguments. '\
'Use keyword arguments or an options hash instead.'.freeze

Instance Method Summary collapse

Instance Method Details

#on_args(node) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/cop/airbnb/opt_arg_parameters.rb', line 11

def on_args(node)
  *but_last, last_arg = *node

  if last_arg && last_arg.blockarg_type?
    last_arg = but_last.pop
  end

  but_last.each do |arg|
    next unless arg.optarg_type?
    add_offense(arg, message: MSG)
  end
  return if last_arg.nil?

  return unless last_arg.optarg_type?

  _arg_name, default_value = *last_arg
  if default_value.hash_type?
    # asserting default value is empty hash
    *key_value_pairs = *default_value
    return if key_value_pairs.empty?
  end

  add_offense(last_arg, message: MSG)
end