class RuboCop::Cop::Lint::NestedMethodDefinition

end
end
end
def bar
class << self
def foo
# good
@example
end
end
end
def bar
self.module_exec do
def foo
end
end
end
def bar
self.class_eval do
def foo
# good
@example
end
bar.call
bar = -> { puts ‘hello’ }
def foo
# good
@example
end
end
def bar
def foo
# will be redefined every time ‘foo` is invoked.
# as the outer `foo` method. Furthermore, the `bar` method
# `bar` definition actually produces methods in the same scope
# bad
@example
This cop checks for nested method definitions.

def find_nested_defs(node, &block)

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 !scoping_method_call?(child)
      find_nested_defs(child, &block)
    end
  end
end

def on_def(node)

def on_def(node)
  find_nested_defs(node) do |nested_def_node|
    add_offense(nested_def_node)
  end
end

def scoping_method_call?(child)

def scoping_method_call?(child)
  eval_call?(child) || exec_call?(child) || child.sclass_type? ||
    class_or_module_or_struct_new_call?(child)
end