Class: RuboCop::Cop::InternalAffairs::OnSendWithoutOnCSend

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb

Overview

Checks for cops that define ‘on_send` without define `on_csend`.

Although in some cases it can be predetermined that safe navigation will never be used with the code checked by a specific cop, in general it is good practice to handle safe navigation methods if handling any ‘send` node.

NOTE: It is expected to disable this cop for cops that check for method calls on receivers that cannot be nil (‘self`, a literal, a constant), and method calls that will never have a receiver (ruby keywords like `raise`, macros like `attr_reader`, DSL methods, etc.), and other checks that wouldn’t make sense to support safe navigation.

Examples:

# bad
class MyCop < RuboCop::Cop:Base
  def on_send(node)
    # ...
  end
end

# good - explicit method definition
class MyCop < RuboCop::Cop:Base
  def on_send(node)
    # ...
  end

  def on_csend(node)
    # ...
  end
end

# good - alias
class MyCop < RuboCop::Cop:Base
  def on_send(node)
    # ...
  end
  alias on_csend on_send
end

# good - alias_method
class MyCop < RuboCop::Cop:Base
  def on_send(node)
    # ...
  end
  alias_method :on_csend, :on_send
end

Constant Summary collapse

RESTRICT_ON_SEND =
%i[alias_method].freeze
MSG =
'Cop defines `on_send` but not `on_csend`.'

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from RuboCop::Cop::IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#on_alias(node) ⇒ Object

[View source]

73
74
75
76
# File 'lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb', line 73

def on_alias(node)
  @on_send_definition = node if node.new_identifier.value == :on_send
  @on_csend_definition = node if node.new_identifier.value == :on_csend
end

#on_def(node) ⇒ Object

[View source]

68
69
70
71
# File 'lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb', line 68

def on_def(node)
  @on_send_definition = node if node.method?(:on_send)
  @on_csend_definition = node if node.method?(:on_csend)
end

#on_investigation_endObject

[View source]

62
63
64
65
66
# File 'lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb', line 62

def on_investigation_end
  return unless @on_send_definition && !@on_csend_definition

  add_offense(@on_send_definition)
end

#on_new_investigationObject

[View source]

57
58
59
60
# File 'lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb', line 57

def on_new_investigation
  @on_send_definition = nil
  @on_csend_definition = nil
end

#on_send(node) ⇒ Object

rubocop:disable InternalAffairs/OnSendWithoutOnCSend

[View source]

78
79
80
81
82
83
84
85
86
# File 'lib/rubocop/cop/internal_affairs/on_send_without_on_csend.rb', line 78

def on_send(node) # rubocop:disable InternalAffairs/OnSendWithoutOnCSend
  new_identifier = node.first_argument
  return unless new_identifier.basic_literal?

  new_identifier = new_identifier.value

  @on_send_definition = node if new_identifier == :on_send
  @on_csend_definition = node if new_identifier == :on_csend
end