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
Public Class Methods
Source
# 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
@deprecated Use Registry.all
Source
# 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
Calls superclass method
RuboCop::Cop::Base::inherited
Source
# 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
Source
# 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
@deprecated Use Registry.qualified_cop_name
Source
# 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
@deprecated Use Registry.global
Source
# File lib/rubocop/cop/cop.rb, line 33 def self.support_autocorrect? method_defined?(:autocorrect) end
Public Instance Methods
Source
# 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
Calls superclass method
RuboCop::Cop::Base#add_offense
Source
# 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
Called before any investigation @api private
Calls superclass method
RuboCop::Cop::Base#begin_investigation
Source
# 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
@deprecated
Source
# 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
Source
# File lib/rubocop/rspec/cop_helper.rb, line 106 def highlights offenses.sort.map { |o| o.location.source } end
Source
# File lib/rubocop/rspec/cop_helper.rb, line 102 def messages offenses.sort.map(&:message) end
Source
# 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
Called after all on_… have been called
Calls superclass method
RuboCop::Cop::Base#on_investigation_end
Source
# File lib/rubocop/cop/cop.rb, line 116 def on_new_investigation investigate(processed_source) if respond_to?(:investigate) super end
Called before all on_… have been called
Calls superclass method
RuboCop::Cop::Base#on_new_investigation
Source
# 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
@deprecated Use class method
Private Instance Methods
Source
# File lib/rubocop/cop/cop.rb, line 147 def apply_correction(corrector) suppress_clobbering { super } end
Calls superclass method
RuboCop::Cop::Base#apply_correction
Source
# File lib/rubocop/cop/cop.rb, line 143 def callback_argument(_range) @v0_argument end
Override Base
Source
# 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
Source
# 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
Source
# 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
Just for legacy
Source
# 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
Source
# File lib/rubocop/cop/cop.rb, line 177 def suppress_clobbering yield rescue ::Parser::ClobberingError # ignore Clobbering errors end