123456789_123456789_123456789_123456789_123456789_

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

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::RuboCop::Cop::Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
Inherits: RuboCop::Cop::Base
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

::RuboCop::Cop::Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

::RuboCop::Cop::ProjectIndexHelp - Included

BUILTIN_DOCUMENT_URI, FILE_URI_PREFIX, WINDOWS_DRIVE_PREFIX

Class Attribute Summary

::RuboCop::Cop::Base - Inherited

.gem_requirements, .lint?,
.support_autocorrect?

Returns if class supports autocorrect.

.support_multiple_source?

Override if your cop should be called repeatedly for multiple investigations Between calls to #on_new_investigation and on_investigation_end, the result of processed_source will remain constant.

Class Method Summary

::RuboCop::Cop::Base - Inherited

.autocorrect_incompatible_with

List of cops that should not try to autocorrect at the same time as this cop.

.badge

Naming.

.callbacks_needed, .cop_name, .department,
.documentation_url

Returns a url to view this cops documentation online.

.exclude_from_registry

Call for abstract Cop classes.

.inherited,
.joining_forces

Override and return the Force class(es) you need to join.

.match?

Returns true if the cop name or the cop namespace matches any of the given names.

.new,
.requires_gem

Register a version requirement for the given gem name.

.restrict_on_send

::RuboCop::ExcludeLimit - Extended

exclude_limit

Sets up a configuration option to have an exclude limit tracked.

transform

Instance Attribute Summary

Instance Method Summary

::RuboCop::Cop::ProjectIndexHelp - Included

#external_dependency_checksum, #compute_project_index_signature,
#definitions_in_other_files

Returns the definitions among definitions that live in a file other than the one being inspected, ordered by path and line.

#indexed_singleton_member

A namespace without any singleton method has no singleton-class declaration of its own, so the lookup starts from the first ancestor that has one; its find_member covers the rest of the chain.

#indexed_singleton_of

The declaration of `declaration’s singleton class, or nil when no singleton method is defined on it anywhere in the project.

#inherited_index_member?

Whether an ancestor of scope other than scope itself defines member_name.

#lexical_nesting_of

The lexical nesting the node’s constants resolve through, outermost first.

#prior_definition_in_other_file, #project_index_signature,
#resolve_constant_in_index

Resolves a constant node the way Ruby does: the first segment through the lexical nesting and every following segment inside the previous one.

#same_file?

::RuboCop::Cop::Base - Inherited

#add_global_offense

Adds an offense that has no particular location.

#add_offense

Adds an offense on the specified range (or node with an expression) Unless that offense is disabled for this range, a corrector will be yielded to provide the cop the opportunity to autocorrect the offense.

#begin_investigation

Called before any investigation.

#callbacks_needed,
#cop_config

Configuration Helpers.

#cop_name, #excluded_file?,
#external_dependency_checksum

This method should be overridden when a cop’s behavior depends on state that lives outside of these locations:

#inspect,
#message

Gets called if no message is specified when calling add_offense or add_global_offense Cops are discouraged to override this; instead pass your message directly.

#name

Alias for Base#cop_name.

#offenses,
#on_investigation_end

Called after all on_…​

#on_new_investigation

Called before all on_…​

#on_other_file

Called instead of all on_…​

#parse

There should be very limited reasons for a Cop to do it’s own parsing.

#parser_engine,
#ready

Called between investigations.

#relevant_file?,
#target_gem_version

Returns a gems locked versions (i.e.

#target_rails_version, #target_ruby_version, #annotate, #apply_correction, #attempt_correction,
#callback_argument

Reserved for Cop::Cop.

#complete_investigation

Called to complete an investigation.

#correct, #current_corrector,
#current_offense_locations

Reserved for Commissioner:

#current_offenses, #currently_disabled_lines, #custom_severity, #default_severity, #disable_uncorrectable, #enabled_line?, #file_name_matches_any?, #find_message, #find_severity, #matches_absolute_include_pattern?, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

::RuboCop::Cop::AutocorrectLogic - Included

::RuboCop::Cop::IgnoredNode - Included

Constructor Details

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

Instance Method Details

#const_receiver_declaration(node, receiver) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 117

def const_receiver_declaration(node, receiver)
  namespace = resolve_constant_in_index(receiver)
  return nil unless namespace.is_a?(Rubydex::Namespace)

  indexed_singleton_member(namespace, "#{node.method_name}()")
end

#deprecated?(declaration) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 153

def deprecated?(declaration)
  definitions = declaration.definitions.to_a

  definitions.any? && definitions.all?(&:deprecated?)
end

#deprecation_text(declaration) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 175

def deprecation_text(declaration)
  tag_comment = declaration.definitions.to_a.filter_map do |definition|
    definition.comments.to_a.map(&:string).find { |string| string.include?(DEPRECATED_TAG) }
  end.first

  tag_comment && tag_comment[DEPRECATION_DETAIL, 1]&.strip
end

#detail_for(declaration) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 168

def detail_for(declaration)
  text = deprecation_text(declaration)
  return '.' if text.nil? || text.empty?

  text.end_with?('.') ? ": #{text}" : ": #{text}."
end

#enclosing_namespace(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 124

def enclosing_namespace(node)
  namespace_node = node.each_ancestor(:class, :module).first
  return project_index['Object'] unless namespace_node

  @namespace_cache.fetch(namespace_node) do
    @namespace_cache[namespace_node] = resolve_constant_in_index(namespace_node.identifier)
  end
end

#implicit_receiver_declaration(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 106

def implicit_receiver_declaration(node)
  namespace = enclosing_namespace(node)
  return nil unless namespace.is_a?(Rubydex::Namespace)

  if singleton_context?(node)
    indexed_singleton_member(namespace, "#{node.method_name}()")
  else
    namespace.find_member("#{node.method_name}()")
  end
end

#method_declaration_for(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 96

def method_declaration_for(node)
  receiver = node.receiver

  if receiver.nil? || receiver.self_type?
    implicit_receiver_declaration(node)
  elsif receiver.const_type?
    const_receiver_declaration(node, receiver)
  end
end

#on_const(node)

[ GitHub ]

  
# 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_csend(node)

Alias for #on_send.

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 80

alias on_csend on_send

#on_new_investigation

[ GitHub ]

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

def on_new_investigation
  @namespace_cache = {}
  super
end

#on_send(node) Also known as: #on_csend

[ GitHub ]

  
# 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

#singleton_context?(node) ⇒ Boolean (private)

Whether an implicit-receiver call runs with the class or module itself as self (directly in a namespace body, in a singleton method, or inside class << self) rather than inside an instance method.

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 136

def singleton_context?(node)
  instance_def_seen = false

  node.each_ancestor do |ancestor|
    case ancestor.type
    when :defs, :sclass
      return true
    when :def
      instance_def_seen = true
    when :class, :module
      return !instance_def_seen
    end
  end

  false
end

#within_deprecated_definition?(node) ⇒ Boolean (private)

References from a definition that is itself documented as deprecated are allowed.

[ GitHub ]

  
# File 'lib/rubocop/cop/lint/deprecated_reference.rb', line 161

def within_deprecated_definition?(node)
  node.each_ancestor(:any_def, :class, :module).any? do |ancestor|
    comments = processed_source.ast_with_comments[ancestor]
    comments.any? { |comment| comment.text.include?(DEPRECATED_TAG) }
  end
end