class RuboCop::Cop::Cop
A scaffold for concrete cops.
The Cop
class is meant to be extended.
Cops track offenses and can autocorrect them on the fly.
A commissioner object is responsible for traversing the AST
and invoking the specific callbacks on each cop. If a cop needs to do its own processing of the AST
or depends on something else, it should define the `#investigate` method and do the processing there.
@example
class CustomCop < Cop def investigate(processed_source) # Do custom processing end end
Monkey-patch Cop
for tests to provide easy access to messages and highlights.
Attributes
Public Class Methods
# File lib/rubocop/cop/cop.rb, line 43 def self.all registry.without_department(:Test).cops end
List of cops that should not try to autocorrect at the same time as this cop
@return [Array<RuboCop::Cop::Cop>]
@api public
# File lib/rubocop/cop/cop.rb, line 86 def self.autocorrect_incompatible_with [] end
# File lib/rubocop/cop/cop.rb, line 55 def self.badge @badge ||= Badge.for(name) end
# File lib/rubocop/cop/cop.rb, line 59 def self.cop_name badge.to_s end
# File lib/rubocop/cop/cop.rb, line 63 def self.department badge.department end
# File lib/rubocop/cop/cop.rb, line 51 def self.inherited(subclass) registry.enlist(subclass) end
# File lib/rubocop/cop/cop.rb, line 67 def self.lint? department == :Lint end
Returns true if the cop name or the cop namespace matches any of the given names.
# File lib/rubocop/cop/cop.rb, line 73 def self.match?(given_names) return false unless given_names given_names.include?(cop_name) || given_names.include?(department.to_s) end
# File lib/rubocop/cop/cop.rb, line 90 def initialize(config = nil, options = nil) @config = config || Config.new @options = options || { debug: false } @offenses = [] @corrections = [] @corrected_nodes = {} @corrected_nodes.compare_by_identity @processed_source = nil end
# File lib/rubocop/cop/cop.rb, line 47 def self.qualified_cop_name(name, origin) registry.qualified_cop_name(name, origin) end
Public Instance Methods
rubocop:disable Metrics/CyclomaticComplexity
# File lib/rubocop/cop/cop.rb, line 117 def add_offense(node, location: :expression, message: nil, severity: nil) loc = find_location(node, location) return if duplicate_location?(loc) severity = custom_severity || severity || default_severity message ||= message(node) message = annotate(message) status = enabled_line?(loc.line) ? correct(node) : :disabled @offenses << Offense.new(severity, loc, message, name, status) yield if block_given? && status != :disabled end
# File lib/rubocop/cop/cop.rb, line 156 def config_to_allow_offenses Formatter::DisabledConfigFormatter .config_to_allow_offenses[cop_name] ||= {} end
# File lib/rubocop/cop/cop.rb, line 161 def config_to_allow_offenses=(hash) Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] = hash end
# File lib/rubocop/cop/cop.rb, line 105 def cop_config # Use department configuration as basis, but let individual cop # configuration override. @cop_config ||= @config.for_cop(self.class.department.to_s) .merge(@config.for_cop(self)) end
# File lib/rubocop/cop/cop.rb, line 178 def cop_name @cop_name ||= self.class.cop_name end
# File lib/rubocop/cop/cop.rb, line 143 def correct(node) return :unsupported unless support_autocorrect? return :uncorrected unless autocorrect? return :already_corrected if @corrected_nodes.key?(node) @corrected_nodes[node] = true correction = autocorrect(node) return :uncorrected unless correction @corrections << correction :corrected end
# File lib/rubocop/cop/cop.rb, line 139 def duplicate_location?(location) @offenses.any? { |o| o.location == location } end
# File lib/rubocop/cop/cop.rb, line 189 def excluded_file?(file) !relevant_file?(file) end
rubocop:enable Metrics/CyclomaticComplexity
# File lib/rubocop/cop/cop.rb, line 134 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
# File lib/rubocop/rspec/cop_helper.rb, line 89 def highlights offenses.sort.map { |o| o.location.source } end
# File lib/rubocop/cop/cop.rb, line 101 def join_force?(_force_class) false end
# File lib/rubocop/cop/cop.rb, line 112 def message(_node = nil) self.class::MSG end
# File lib/rubocop/rspec/cop_helper.rb, line 85 def messages offenses.sort.map(&:message) end
# File lib/rubocop/cop/cop.rb, line 174 def parse(source, path = nil) ProcessedSource.new(source, target_ruby_version, path) end
# File lib/rubocop/cop/cop.rb, line 184 def relevant_file?(file) file_name_matches_any?(file, 'Include', true) && !file_name_matches_any?(file, 'Exclude', false) end
# File lib/rubocop/cop/cop.rb, line 170 def target_rails_version @config.target_rails_version end
# File lib/rubocop/cop/cop.rb, line 166 def target_ruby_version @config.target_ruby_version end
Private Instance Methods
# File lib/rubocop/cop/cop.rb, line 195 def annotate(message) RuboCop::Cop::MessageAnnotator.new( config, cop_config, @options ).annotate(message, name) end
# File lib/rubocop/cop/cop.rb, line 226 def custom_severity severity = cop_config['Severity'] return unless severity if Severity::NAMES.include?(severity.to_sym) severity.to_sym else message = "Warning: Invalid severity '#{severity}'. " \ "Valid severities are #{Severity::NAMES.join(', ')}." warn(Rainbow(message).red) end end
# File lib/rubocop/cop/cop.rb, line 222 def default_severity self.class.lint? ? :warning : :convention end
# File lib/rubocop/cop/cop.rb, line 216 def enabled_line?(line_number) return true if @options[:ignore_disable_comments] || !@processed_source @processed_source.comment_config.cop_enabled_at_line?(self, line_number) end
# File lib/rubocop/cop/cop.rb, line 201 def file_name_matches_any?(file, parameter, default_result) patterns = cop_config[parameter] return default_result unless patterns path = nil patterns.any? do |pattern| # Try to match the absolute path, as Exclude properties are absolute. next true if match_path?(pattern, file) # Try with relative path. path ||= config.path_relative_to_config(file) match_path?(pattern, path) end end