Class: RuboCop::Cop::Cop

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/cop.rb,
lib/rubocop/rspec/cop_helper.rb

Overview

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

Defined Under Namespace

Classes: Correction

Constant Summary

Constants inherited from Base

Base::RESTRICT_ON_SEND

Instance Attribute Summary collapse

Attributes inherited from Base

#config, #processed_source

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, autocorrect_incompatible_with, badge, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, lint?, match?, #message, #on_other_file, #parse, #ready, #relevant_file?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

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 Attribute Details

#offensesObject (readonly)

Returns the value of attribute offenses.



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

def offenses
  @offenses
end

Class Method Details

.allObject

Deprecated.

Use Registry.all



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

def self.all
  Registry.all
end

.joining_forcesObject



29
30
31
32
33
34
# 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) ⇒ Object

Deprecated.

Use Registry.qualified_cop_name



49
50
51
# File 'lib/rubocop/cop/cop.rb', line 49

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

.registryObject

Deprecated.

Use Registry.global



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

def self.registry
  Registry.global
end

.support_autocorrect?Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called before any investigation



103
104
105
106
107
108
109
110
111
112
# 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

#correctionsObject

Deprecated.


82
83
84
85
86
87
# 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

#find_location(node, loc) ⇒ Object



70
71
72
73
# 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

#highlightsObject



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

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

#messagesObject



84
85
86
# File 'lib/rubocop/rspec/cop_helper.rb', line 84

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

#on_investigation_endObject

Called after all on_…​ have been called



96
97
98
99
# 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_investigationObject

Called before all on_…​ have been called



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

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

#support_autocorrect?Boolean

Deprecated.

Use class method

Returns:

  • (Boolean)


76
77
78
79
# File 'lib/rubocop/cop/cop.rb', line 76

def support_autocorrect?
  # warn 'deprecated, use cop.class.support_autocorrect?' TODO
  self.class.support_autocorrect?
end