Class: RuboCop::Cop::Operandi::ArgumentTypeRequired

Inherits:
Base
  • Object
show all
Defined in:
lib/operandi/rubocop/cop/operandi/argument_type_required.rb

Overview

Ensures that all arg declarations in Operandi include a type: option.

Examples:

# bad
arg :user_id
arg :params, default: {}
arg :name, optional: true

# good
arg :user_id, type: Integer
arg :params, type: Hash, default: {}
arg :name, type: String, optional: true

Constant Summary collapse

MSG =
"Argument `%<name>s` must have a `type:` option."
RESTRICT_ON_SEND =
[:arg].freeze

Instance Method Summary collapse

Instance Method Details

#arg_call?(node) ⇒ Object



25
26
27
# File 'lib/operandi/rubocop/cop/operandi/argument_type_required.rb', line 25

def_node_matcher :arg_call?, <<~PATTERN
  (send nil? :arg (sym $_) ...)
PATTERN

#on_send(node) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/operandi/rubocop/cop/operandi/argument_type_required.rb', line 29

def on_send(node)
  arg_call?(node) do |name|
    return if has_type_option?(node)

    add_offense(node, message: format(MSG, name: name))
  end
end