class RubbyCop::Cop::Style::MethodMissing

This cop checks for the presence of `method_missing` without also defining `respond_to_missing?` and falling back on `super`.

@example

#bad
def method_missing(...)
  ...
end

#good
def respond_to_missing?(...)
  ...
end

def method_missing(...)
  ...
  super
end

Constants

MSG

Public Instance Methods

on_method_def(node, method_name, _args, _body) click to toggle source
# File lib/rubbycop/cop/style/method_missing.rb, line 29
def on_method_def(node, method_name, _args, _body)
  return unless method_name == :method_missing

  check(node)
end

Private Instance Methods

calls_super?(node) click to toggle source
# File lib/rubbycop/cop/style/method_missing.rb, line 57
def calls_super?(node)
  node.descendants.any?(&:zsuper_type?)
end
check(node) click to toggle source
# File lib/rubbycop/cop/style/method_missing.rb, line 37
def check(node)
  return if calls_super?(node) && implements_respond_to_missing?(node)

  add_offense(node, :expression)
end
implements_respond_to_missing?(node) click to toggle source
# File lib/rubbycop/cop/style/method_missing.rb, line 61
def implements_respond_to_missing?(node)
  node.parent.each_child_node(node.type).any? do |sibling|
    if node.def_type?
      respond_to_missing_def?(sibling)
    elsif node.defs_type?
      respond_to_missing_defs?(sibling)
    end
  end
end
message(node) click to toggle source
# File lib/rubbycop/cop/style/method_missing.rb, line 43
def message(node)
  instructions = []

  unless implements_respond_to_missing?(node)
    instructions << 'define `respond_to_missing?`'.freeze
  end

  unless calls_super?(node)
    instructions << 'fall back on `super`'.freeze
  end

  format(MSG, instructions.join(' and '))
end