Class: Ducalis::KeywordDefaults

Inherits:
RuboCop::Cop::Cop
  • Object
show all
Defined in:
lib/ducalis/cops/keyword_defaults.rb

Constant Summary collapse

OFFENSE =
<<-MESSAGE.gsub(/^ +\|\s/, '').strip
  | Prefer to use keyword arguments for defaults. It increases readability and reduces ambiguities.
  | It is ok if an argument is single and the name obvious from the function declaration.
MESSAGE

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ Object Also known as: on_defs



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ducalis/cops/keyword_defaults.rb', line 12

def on_def(node)
  args = node.type == :defs ? node.to_a[2] : node.to_a[1]

  return if args.to_a.one?

  args.children.each do |arg_node|
    next unless arg_node.type == :optarg

    add_offense(node, :expression, OFFENSE)
  end
end