Class: RuboCop::Cop::Lint::DeprecatedReference

Inherits:
Base
  • Object
show all
Includes:
ProjectIndexHelp
Defined in:
lib/rubocop/cop/lint/deprecated_reference.rb

Overview

Checks for calls to methods and references to constants that are documented as deprecated with a YARD @deprecated tag.

The check is powered by the project-wide index, so it only runs when AllCops/UseProjectIndex is enabled and the rubydex gem is installed. Without the index the cop does nothing.

Only references that can be resolved without type inference are checked: constants, method calls without an explicit receiver (or with self), which are looked up in the enclosing class or module and its ancestry, and calls whose receiver is a constant, which are looked up in that namespace's singleton class. Calls on arbitrary objects are not checked.

References made from a definition that is itself deprecated are allowed, so deprecated implementations can keep calling each other.

Examples:

# Given a deprecated method and constant:
#
#   class Api
#     # @deprecated Use `#new_method` instead.
#     def old_method
#     end
#
#     # @deprecated
#     OLD_TIMEOUT = 10
#   end

# bad
class Client < Api
  def call
    old_method
  end

  def timeout
    OLD_TIMEOUT
  end
end

# good
class Client < Api
  def call
    new_method
  end

  def timeout
    NEW_TIMEOUT
  end
end

Constant Summary collapse

METHOD_MSG =
'Method `%<name>s` is deprecated%<detail>s'
CONSTANT_MSG =
'Constant `%<name>s` is deprecated%<detail>s'
DEPRECATED_TAG =
'@deprecated'
DEPRECATION_DETAIL =
/#{DEPRECATED_TAG}\s+(.+)/.freeze

Constants included from ProjectIndexHelp

ProjectIndexHelp::BUILTIN_DOCUMENT_URI, ProjectIndexHelp::FILE_URI_PREFIX, ProjectIndexHelp::WINDOWS_DRIVE_PREFIX

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

Instance Method Summary collapse

Methods included from ProjectIndexHelp

#external_dependency_checksum

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_investigation_end, #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

cop_dir_for, #exclude_limit, read_limits

Methods included from AutocorrectLogic

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

Methods included from 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_const(node) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 82

def on_const(node)
  return unless project_index
  return if node.parent&.defined_module

  declaration = resolve_constant_in_index(node)
  return unless declaration && deprecated?(declaration)
  return if within_deprecated_definition?(node)

  message = format(CONSTANT_MSG, name: node.const_name, detail: detail_for(declaration))
  add_offense(node, message: message)
end

#on_new_investigationObject



65
66
67
68
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 65

def on_new_investigation
  @namespace_cache = {}
  super
end

#on_send(node) ⇒ Object Also known as: on_csend



70
71
72
73
74
75
76
77
78
79
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 70

def on_send(node)
  return unless project_index

  declaration = method_declaration_for(node)
  return unless declaration && deprecated?(declaration)
  return if within_deprecated_definition?(node)

  message = format(METHOD_MSG, name: node.method_name, detail: detail_for(declaration))
  add_offense(node.loc.selector, message: message)
end