Class: RuboCop::Cop::Operandi::ReservedName

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

Overview

Ensures that arg, step, and output declarations do not use reserved names that would conflict with Operandi methods.

Examples:

# bad
arg :errors, type: Array
arg :output, type: Hash
step :call
output :success?, type: [TrueClass, FalseClass]

# good
arg :validation_errors, type: Array
arg :result_output, type: Hash
step :execute
output :succeeded, type: [TrueClass, FalseClass]

Constant Summary collapse

MSG =
"`%<name>s` is a reserved name and cannot be used as %<field_type>s. " \
"It conflicts with Operandi methods."
SEVERITY =
:error
RESTRICT_ON_SEND =
[:arg, :step, :output].freeze
FIELD_TYPE_NAMES =
{
  arg: "an argument",
  step: "a step",
  output: "an output",
}.freeze

Instance Method Summary collapse

Instance Method Details

#dsl_call?(node) ⇒ Object



41
42
43
# File 'lib/operandi/rubocop/cop/operandi/reserved_name.rb', line 41

def_node_matcher :dsl_call?, <<~PATTERN
  (send nil? ${:arg :step :output} (sym $_) ...)
PATTERN

#on_send(node) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/operandi/rubocop/cop/operandi/reserved_name.rb', line 45

def on_send(node)
  dsl_call?(node) do |method_name, name|
    return unless ::Operandi::ReservedNames::ALL.include?(name)

    field_type = FIELD_TYPE_NAMES[method_name]
    add_offense(node, message: format(MSG, name: name, field_type: field_type), severity: SEVERITY)
  end
end