lib/rubocop/cop/lint/nested_method_definition.rb in rbhint-0.85.1.rc2 vs lib/rubocop/cop/lint/nested_method_definition.rb in rbhint-0.87.1.rc1
- old
+ new
@@ -57,35 +57,29 @@
class NestedMethodDefinition < Cop
MSG = 'Method definitions must not be nested. ' \
'Use `lambda` instead.'
def on_def(node)
- find_nested_defs(node) do |nested_def_node|
- add_offense(nested_def_node)
- end
- end
- alias on_defs on_def
+ subject, = *node
+ return if node.defs_type? && subject.lvar_type?
- private
+ def_ancestor = node.each_ancestor(:def, :defs).first
+ return unless def_ancestor
- 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)
+ within_scoping_def =
+ node.each_ancestor(:block, :sclass).any? do |ancestor|
+ scoping_method_call?(ancestor)
end
- end
+
+ add_offense(node) if def_ancestor && !within_scoping_def
end
+ alias on_defs on_def
+ private
+
def scoping_method_call?(child)
- eval_call?(child) || exec_call?(child) || child.sclass_type? ||
+ child.sclass_type? || eval_call?(child) || exec_call?(child) ||
class_or_module_or_struct_new_call?(child)
end
def_node_matcher :eval_call?, <<~PATTERN
(block (send _ {:instance_eval :class_eval :module_eval} ...) ...)
@@ -94,10 +88,10 @@
def_node_matcher :exec_call?, <<~PATTERN
(block (send _ {:instance_exec :class_exec :module_exec} ...) ...)
PATTERN
def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
- (block (send (const nil? {:Class :Module :Struct}) :new ...) ...)
+ (block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
PATTERN
end
end
end
end