Class: RuboCop::Cop::Legion::Extension::DefinitionCallMismatched

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/legion/extension/definition_call_mismatched.rb

Overview

Detects ‘definition :method_name` DSL calls inside runner modules where no corresponding `def method_name` exists. A mismatched definition means the framework advertises a runner function that doesn’t exist, causing a NoMethodError at dispatch time.

Examples:

# bad
module Runners
  module Foo
    definition :do_work
    # missing: def do_work
  end
end

# good
module Runners
  module Foo
    definition :do_work

    def do_work
      { success: true }
    end
  end
end

Constant Summary collapse

MSG =
'`definition :%<name>s` has no matching `def %<name>s` method.'

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rubocop/cop/legion/extension/definition_call_mismatched.rb', line 34

def on_send(node)
  return unless node.method_name == :definition
  return unless node.receiver.nil?
  return unless node.first_argument&.sym_type?

  defined_name = node.first_argument.value
  enclosing = enclosing_module_or_class(node)
  return unless enclosing
  return if defines_method?(enclosing, defined_name)

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