123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::Cop Deprecated

Relationships & Source Files
Namespace Children
Classes:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base, ::RuboCop::ExcludeLimit, NodePattern::Macros, RuboCop::AST::Sexp
Instance Chain:
self, Base, AutocorrectLogic, IgnoredNode, ::RuboCop::Util, RuboCop::AST::Sexp
Inherits: RuboCop::Cop::Base
Defined in: lib/rubocop/cop/cop.rb,
lib/rubocop/rspec/cop_helper.rb

Overview

Deprecated.

Use Cop::Base instead

Constant Summary

Base - Inherited

EMPTY_OFFENSES, RESTRICT_ON_SEND

Class Attribute Summary

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

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

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, #range_for_original, #range_from_node_or_range,
#reset_investigation

Actually private methods.

#use_corrector

AutocorrectLogic - Included

IgnoredNode - Included

Constructor Details

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

Class Attribute Details

.support_autocorrect?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 33

def self.support_autocorrect?
  method_defined?(:autocorrect)
end

Class Method Details

.all

Deprecated.

Use Registry.all

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 56

def self.all
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
    `Cop.all` is deprecated. Use `Registry.all` instead.
  WARNING

  Registry.all
end

.inherited(_subclass)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 25

def self.inherited(_subclass)
  super
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
    Inheriting from `RuboCop::Cop::Cop` is deprecated. Use `RuboCop::Cop::Base` instead.
    For more information, see https://docs.rubocop.org/rubocop/v1_upgrade_notes.html.
  WARNING
end

.joining_forces

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 37

def self.joining_forces
  return unless method_defined?(:join_force?)

  cop = new
  Force.all.select { |force_class| cop.join_force?(force_class) }
end

.qualified_cop_name(name, origin)

Deprecated.

Use Registry.qualified_cop_name

[ GitHub ]

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

def self.qualified_cop_name(name, origin)
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
    `Cop.qualified_cop_name` is deprecated. Use `Registry.qualified_cop_name` instead.
  WARNING

  Registry.qualified_cop_name(name, origin)
end

.registry

Deprecated.

Use Registry.global

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 47

def self.registry
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
    `Cop.registry` is deprecated. Use `Registry.global` instead.
  WARNING

  Registry.global
end

Instance Attribute Details

#offenses (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 12

attr_reader :offenses

#support_autocorrect?Boolean (readonly)

Deprecated.

Use class method

[ GitHub ]

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

def support_autocorrect?
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
    `support_autocorrect?` is deprecated. Use `cop.class.support_autocorrect?`.
  WARNING

  self.class.support_autocorrect?
end

Instance Method Details

#add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 73

def add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block)
  @v0_argument = node_or_range
  range = find_location(node_or_range, location)

  # Since this range may be generated from Ruby code embedded in some
  # template file, we convert it to location info in the original file.
  range = range_for_original(range)

  if block.nil? && !self.class.support_autocorrect?
    super(range, message: message, severity: severity)
  else
    super(range, message: message, severity: severity) do |corrector|
      emulate_v0_callsequence(corrector, &block)
    end
  end
end

#apply_correction(corrector) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 147

def apply_correction(corrector)
  suppress_clobbering { super }
end

#begin_investigation(processed_source, offset: 0, original: processed_source)

This method is for internal use only.

Called before any investigation

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 129

def begin_investigation(processed_source, offset: 0, original: processed_source)
  super
  @offenses = current_offenses
  @last_corrector = @current_corrector

  # We need to keep track of the original source and offset,
  # because `processed_source` here may be an embedded code in it.
  @current_offset = offset
  @current_original = original
end

#callback_argument(_range) (private)

Override Base

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 143

def callback_argument(_range)
  @v0_argument
end

#correction_lambda (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 164

def correction_lambda
  return unless self.class.support_autocorrect?

  dedupe_on_node(@v0_argument) { autocorrect(@v0_argument) }
end

#corrections

Deprecated.
[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 105

def corrections
  warn Rainbow(<<~WARNING).yellow, uplevel: 1
    `Cop#corrections` is deprecated.
  WARNING

  return [] unless @last_corrector

  Legacy::CorrectionsProxy.new(@last_corrector)
end

#dedupe_on_node(node) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 170

def dedupe_on_node(node)
  @corrected_nodes ||= {}.compare_by_identity
  yield unless @corrected_nodes.key?(node)
ensure
  @corrected_nodes[node] = true
end

#emulate_v0_callsequence(corrector) {|corrector| ... } (private)

Just for legacy

Yields:

  • (corrector)
[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 152

def emulate_v0_callsequence(corrector)
  lambda = correction_lambda
  yield corrector if block_given?
  unless corrector.empty?
    raise 'Your cop must inherit from Cop::Base and extend AutoCorrector'
  end

  return unless lambda

  suppress_clobbering { lambda.call(corrector) }
end

#find_location(node, loc)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 90

def find_location(node, loc)
  # Location can be provided as a symbol, e.g.: `:keyword`
  loc.is_a?(Symbol) ? node.loc.public_send(loc) : loc
end

#highlights

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 94

def highlights
  offenses.sort.map { |o| o.location.source }
end

#messages

[ GitHub ]

  
# File 'lib/rubocop/rspec/cop_helper.rb', line 90

def messages
  offenses.sort.map(&:message)
end

#on_investigation_end

Called after all on_…​ have been called

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 122

def on_investigation_end
  investigate_post_walk(processed_source) if respond_to?(:investigate_post_walk)
  super
end

#on_new_investigation

Called before all on_…​ have been called

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 116

def on_new_investigation
  investigate(processed_source) if respond_to?(:investigate)
  super
end

#range_for_original(range) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 183

def range_for_original(range)
  ::Parser::Source::Range.new(
    @current_original.buffer,
    range.begin_pos + @current_offset,
    range.end_pos + @current_offset
  )
end

#suppress_clobbering (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/cop.rb', line 177

def suppress_clobbering
  yield
rescue ::Parser::ClobberingError
  # ignore Clobbering errors
end