Class: RuboCop::Cop::G2::BanGlobalMethods

Inherits:
Cop
  • Object
show all
Defined in:
lib/rubocop/cop/g2/ban_global_methods.rb

Constant Summary collapse

MSG =
'Do not define top-level methods.'.freeze

Instance Method Summary collapse

Instance Method Details

#on_def(node) ⇒ Object Also known as: on_defs



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubocop/cop/g2/ban_global_methods.rb', line 19

def on_def(node)
  node.each_ancestor do |ancestor|
    return if ancestor.class_type? || ancestor.module_type?
    return if const_block?(ancestor)
    return if describe_block?(ancestor)

    if eval_block?(ancestor)
      evaled_obj = ancestor.children.first.receiver
      return unless evaled_obj.nil? || evaled_obj.self_type?
    end
  end

  add_offense node, message: MSG
end

#on_send(node) ⇒ Object



43
44
45
46
47
48
# File 'lib/rubocop/cop/g2/ban_global_methods.rb', line 43

def on_send(node)
  return unless define_method?(node) || send_define_method?(node)
  return unless node.receiver.nil? || node.receiver.self_type?

  on_def(node)
end