Class: RuboCop::Cop::Sane::DisallowMethods

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Defined in:
lib/rubocop/cop/sane/disallow_methods.rb

Overview

Enforces method replacements and prohibitions.

This cop checks for usage of specified methods and either suggests replacements (with auto-correction) or prohibits usage entirely.

Examples:

ReplaceMethods configuration

# .rubocop.yml
# Sane/DisallowMethods:
#   ReplaceMethods:
#     deliver_now:
#       with: deliver_later
#       reason: "`deliver_later` sends the email via background job"

# bad
UserMailer.welcome(user).deliver_now

# good
UserMailer.welcome(user).deliver_later

ProhibitedMethods configuration

# .rubocop.yml
# Sane/DisallowMethods:
#   ProhibitedMethods:
#     dangerous_method:
#       reason: "This method is deprecated and unsafe"

# bad
obj.dangerous_method

Constant Summary collapse

REPLACEABLE_METHOD_MSG =
"You should use `%<with>s` instead of `%<method_name>s` because %<reason>s"
PROHIBITED_METHOD_MSG =
"You should not use `%<method_name>s` because %<reason>s"

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/sane/disallow_methods.rb', line 41

def on_send(node)
  method_name = node.method_name
  replaceable = replace_methods[method_name]
  prohibited = prohibited_methods[method_name]

  if replaceable
    handle_replaceable_method(node, method_name, replaceable)
  elsif prohibited
    handle_prohibited_method(node, method_name, prohibited)
  end
end