class RuboCop::Cop::Cop

@deprecated Use Cop::Base instead Legacy scaffold for Cops. See docs.rubocop.org/rubocop/v1_upgrade_notes.html

Monkey-patch Cop for tests to provide easy access to messages and highlights.

Constants

Correction

@deprecated

Attributes

offenses[R]

Public Class Methods

all() click to toggle source

@deprecated Use Registry.all

# File lib/rubocop/cop/cop.rb, line 87
def self.all
  Registry.all
end
joining_forces() click to toggle source
# File lib/rubocop/cop/cop.rb, line 52
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) click to toggle source

@deprecated Use Registry.qualified_cop_name

# File lib/rubocop/cop/cop.rb, line 92
def self.qualified_cop_name(name, origin)
  Registry.qualified_cop_name(name, origin)
end
registry() click to toggle source

@deprecated Use Registry.global

# File lib/rubocop/cop/cop.rb, line 82
def self.registry
  Registry.global
end
support_autocorrect?() click to toggle source
# File lib/rubocop/cop/cop.rb, line 48
def self.support_autocorrect?
  method_defined?(:autocorrect)
end

Public Instance Methods

add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block) click to toggle source
Calls superclass method RuboCop::Cop::Base#add_offense
# File lib/rubocop/cop/cop.rb, line 25
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)
  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
corrections() click to toggle source

@deprecated

# File lib/rubocop/cop/cop.rb, line 60
def corrections
  # warn 'Cop#corrections is deprecated' TODO
  return [] unless @last_corrector

  Legacy::CorrectionsProxy.new(@last_corrector)
end
find_location(node, loc) click to toggle source
# File lib/rubocop/cop/cop.rb, line 37
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() click to toggle source
# File lib/rubocop/rspec/cop_helper.rb, line 65
def highlights
  offenses.sort.map { |o| o.location.source }
end
messages() click to toggle source
# File lib/rubocop/rspec/cop_helper.rb, line 61
def messages
  offenses.sort.map(&:message)
end
on_investigation_end() click to toggle source

Called after all on_… have been called

Calls superclass method RuboCop::Cop::Base#on_investigation_end
# File lib/rubocop/cop/cop.rb, line 74
def on_investigation_end
  investigate_post_walk(processed_source) if respond_to?(:investigate_post_walk)
  super
end
on_new_investigation() click to toggle source

Called before all on_… have been called

Calls superclass method RuboCop::Cop::Base#on_new_investigation
# File lib/rubocop/cop/cop.rb, line 68
def on_new_investigation
  investigate(processed_source) if respond_to?(:investigate)
  super
end
support_autocorrect?() click to toggle source

@deprecated Use class method

# File lib/rubocop/cop/cop.rb, line 43
def support_autocorrect?
  # warn 'deprecated, use cop.class.support_autocorrect?' TODO
  self.class.support_autocorrect?
end

Private Instance Methods

apply_correction(corrector) click to toggle source
Calls superclass method RuboCop::Cop::Base#apply_correction
# File lib/rubocop/cop/cop.rb, line 109
def apply_correction(corrector)
  suppress_clobbering { super }
end
begin_investigation(processed_source) click to toggle source
Calls superclass method RuboCop::Cop::Base#begin_investigation
# File lib/rubocop/cop/cop.rb, line 98
def begin_investigation(processed_source)
  super
  @offenses = @current_offenses
  @last_corrector = @current_corrector
end
callback_argument(_range) click to toggle source

Override Base

# File lib/rubocop/cop/cop.rb, line 105
def callback_argument(_range)
  @v0_argument
end
correction_lambda() click to toggle source
# File lib/rubocop/cop/cop.rb, line 126
def correction_lambda
  return unless support_autocorrect?

  dedup_on_node(@v0_argument) { autocorrect(@v0_argument) }
end
dedup_on_node(node) { || ... } click to toggle source
# File lib/rubocop/cop/cop.rb, line 132
def dedup_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| ... } click to toggle source

Just for legacy

# File lib/rubocop/cop/cop.rb, line 114
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
suppress_clobbering() { || ... } click to toggle source
# File lib/rubocop/cop/cop.rb, line 139
def suppress_clobbering
  yield
rescue ::Parser::ClobberingError
  # ignore Clobbering errors
end