class RuboCop::Cop::Lint::NestedMethodDefinition

This cop checks for nested method definitions.

@example

# `bar` definition actually produces methods in the same scope
# as the outer `foo` method. Furthermore, the `bar` method
# will be redefined every time `foo` is invoked.
def foo
  def bar
  end
end

Constants

MSG

Public Instance Methods

find_nested_defs(node) { |child| ... } click to toggle source
# File lib/rubocop/cop/lint/nested_method_definition.rb, line 38
def find_nested_defs(node, &block)
  node.each_child_node do |child|
    if child.def_type?
      yield child
    elsif child.defs_type?
      subject, = *child
      next if subject.lvar_type?
      yield child
    elsif !(eval_call?(child) || class_or_module_new_call?(child))
      find_nested_defs(child, &block)
    end
  end
end
on_method_def(node, _method_name, _args, _body) click to toggle source
# File lib/rubocop/cop/lint/nested_method_definition.rb, line 32
def on_method_def(node, _method_name, _args, _body)
  find_nested_defs(node) do |nested_def_node|
    add_offense(nested_def_node, :expression)
  end
end