Class: RuboCop::Cop::Cop
- 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
Instance Attribute Summary collapse
-
#offenses ⇒ Object
readonly
Returns the value of attribute offenses.
Attributes inherited from Base
Class Method Summary collapse
-
.all ⇒ Object
deprecated
Deprecated.
Use Registry.all
- .inherited(_subclass) ⇒ Object
- .joining_forces ⇒ Object
-
.qualified_cop_name(name, origin) ⇒ Object
deprecated
Deprecated.
Use Registry.qualified_cop_name
-
.registry ⇒ Object
deprecated
Deprecated.
Use Registry.global
- .support_autocorrect? ⇒ Boolean
Instance Method Summary collapse
- #add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block) ⇒ Object
-
#begin_investigation(processed_source, offset: 0, original: processed_source) ⇒ Object
private
Called before any investigation.
- #corrections ⇒ Object deprecated Deprecated.
- #find_location(node, loc) ⇒ Object
- #highlights ⇒ Object
- #messages ⇒ Object
-
#on_investigation_end ⇒ Object
Called after all on_…
-
#on_new_investigation ⇒ Object
Called before all on_…
-
#support_autocorrect? ⇒ Boolean
deprecated
Deprecated.
Use class method
Methods inherited from Base
#active_support_extensions_enabled?, #add_global_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, #initialize, #inspect, lint?, match?, #message, #on_other_file, #parse, #parser_engine, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_multiple_source?, #target_rails_version, #target_ruby_version
Methods included from ExcludeLimit
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
Constructor Details
This class inherits a constructor from RuboCop::Cop::Base
Instance Attribute Details
#offenses ⇒ Object (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
.all ⇒ Object
Use Registry.all
56 57 58 59 60 61 62 |
# 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) ⇒ Object
25 26 27 28 29 30 31 |
# 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 ⇒ Object
37 38 39 40 41 42 |
# 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) ⇒ Object
Use Registry.qualified_cop_name
65 66 67 68 69 70 71 |
# 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 ⇒ Object
Use Registry.global
47 48 49 50 51 52 53 |
# 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 |
.support_autocorrect? ⇒ Boolean
33 34 35 |
# File 'lib/rubocop/cop/cop.rb', line 33 def self.support_autocorrect? method_defined?(:autocorrect) end |
Instance Method Details
#add_offense(node_or_range, location: :expression, message: nil, severity: nil, &block) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# 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: , severity: severity) else super(range, 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
129 130 131 132 133 134 135 136 137 138 |
# 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 |
#corrections ⇒ Object
105 106 107 108 109 110 111 112 113 |
# 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 |
#find_location(node, loc) ⇒ Object
90 91 92 93 |
# 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 ⇒ Object
94 95 96 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 94 def highlights offenses.sort.map { |o| o.location.source } end |
#messages ⇒ Object
90 91 92 |
# File 'lib/rubocop/rspec/cop_helper.rb', line 90 def offenses.sort.map(&:message) end |
#on_investigation_end ⇒ Object
Called after all on_… have been called
122 123 124 125 |
# 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 ⇒ Object
Called before all on_… have been called
116 117 118 119 |
# File 'lib/rubocop/cop/cop.rb', line 116 def on_new_investigation investigate(processed_source) if respond_to?(:investigate) super end |
#support_autocorrect? ⇒ Boolean
Use class method
96 97 98 99 100 101 102 |
# 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 |