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.

.builtin?

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

Cops (other than builtin) are encouraged to implement this.

.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_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, #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 25

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

Class Method Details

.all

Deprecated.

Use Registry.all

[ GitHub ]

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

def self.all
  Registry.all
end

.joining_forces

[ GitHub ]

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

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 49

def self.qualified_cop_name(name, origin)
  Registry.qualified_cop_name(name, origin)
end

.registry

Deprecated.

Use Registry.global

[ GitHub ]

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

def self.registry
  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 76

def support_autocorrect?
  # warn 'deprecated, use cop.class.support_autocorrect?' TODO
  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 53

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? && !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 121

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 103

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 117

def callback_argument(_range)
  @v0_argument
end

#correction_lambda (private)

[ GitHub ]

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

def correction_lambda
  return unless support_autocorrect?

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

#corrections

Deprecated.
[ GitHub ]

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

def corrections
  # warn 'Cop#corrections is deprecated' TODO
  return [] unless @last_corrector

  Legacy::CorrectionsProxy.new(@last_corrector)
end

#dedupe_on_node(node) (private)

[ GitHub ]

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

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 126

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 70

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 96

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 90

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 157

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 151

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