Class: RuboCop::Cop::Workit::RestrictOnSend

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/workit/restrict_on_send.rb

Overview

Check for ‘RESTRICT_ON_SEND` is defined if `on_send` or `after_send` are defined.

Examples:

# bad
class FooCop
  def on_send(node)
    # ...
  end
end

# good
class FooCop
  RESTRICT_ON_SEND = %i[bad_method].freeze
  def on_send(node)
    # ...
  end
end

Constant Summary collapse

MSG =
"Consider defined `RESTRICT_ON_SEND` for optimization."
NEED_RESTRICT_ON_SEND =
%i[on_send after_send].freeze

Instance Method Summary collapse

Instance Method Details

#class_node(node) ⇒ Object



38
39
40
41
42
43
44
45
46
# File 'lib/rubocop/cop/workit/restrict_on_send.rb', line 38

def class_node(node)
  return if node.parent.nil?

  if node.parent.class_type?
    node.parent
  else
    class_node(node.parent)
  end
end

#defined_restrict_on_send?(node) ⇒ Object



29
# File 'lib/rubocop/cop/workit/restrict_on_send.rb', line 29

def_node_search :defined_restrict_on_send?, "(casgn nil? :RESTRICT_ON_SEND ...)"

#on_def(node) ⇒ Object



31
32
33
34
35
36
# File 'lib/rubocop/cop/workit/restrict_on_send.rb', line 31

def on_def(node)
  return unless NEED_RESTRICT_ON_SEND.include?(node.method_name)
  return unless (class_node = class_node(node))

  add_offense(class_node) unless defined_restrict_on_send?(class_node)
end