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
Attributes
config[R]
corrections[R]
offenses[R]
processed_source[RW]
Public Class Methods
all()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 81 def self.all @all.without_type(:test) end
cop_name()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 97 def self.cop_name @cop_name ||= name.split('::').last(2).join('/') end
cop_type()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 101 def self.cop_type @cop_type ||= name.split('::')[-2].downcase.to_sym end
inherited(subclass)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 93 def self.inherited(subclass) @all << subclass end
lint?()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 105 def self.lint? cop_type == :lint end
match?(given_names)
click to toggle source
Returns true if the cop name or the cop namespace matches any of the given names.
# File lib/rubocop/cop/cop.rb, line 111 def self.match?(given_names) return false unless given_names given_names.include?(cop_name) || given_names.include?(cop_type.to_s.capitalize) end
new(config = nil, options = nil)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 118 def initialize(config = nil, options = nil) @config = config || Config.new @options = options || { debug: false } @offenses = [] @corrections = [] end
non_rails()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 89 def self.non_rails all.without_type(:rails) end
qualified_cop_name(name, origin)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 85 def self.qualified_cop_name(name, origin) @all.qualified_cop_name(name, origin) end
Public Instance Methods
add_offense(node, loc, message = nil, severity = nil) { || ... }
click to toggle source
# File lib/rubocop/cop/cop.rb, line 157 def add_offense(node, loc, message = nil, severity = nil) location = loc.is_a?(Symbol) ? node.loc.public_send(loc) : loc # Don't include the same location twice for one cop. return if @offenses.any? { |o| o.location == location } severity = custom_severity || severity || default_severity message ||= message(node) message = annotate_message(message) status = enabled_line?(location.line) ? correct(node) : :disabled @offenses << Offense.new(severity, location, message, name, status) yield if block_given? && status != :disabled end
config_to_allow_offenses()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 184 def config_to_allow_offenses Formatter::DisabledConfigFormatter .config_to_allow_offenses[cop_name] ||= {} end
config_to_allow_offenses=(hash)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 189 def config_to_allow_offenses=(hash) Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] = hash end
cop_config()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 130 def cop_config @cop_config ||= @config.for_cop(self) end
cop_name()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 202 def cop_name @cop_name ||= self.class.cop_name end
Also aliased as: name
correct(node)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 174 def correct(node) return :unsupported unless support_autocorrect? return :uncorrected unless autocorrect? correction = autocorrect(node) return :uncorrected unless correction @corrections << correction :corrected end
debug?()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 134 def debug? @options[:debug] end
details()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 227 def details details = cop_config && cop_config['Details'] (details.nil? || details.empty?) ? nil : details end
display_cop_names?()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 138 def display_cop_names? debug? || @options[:display_cop_names] || @config.for_all_cops['DisplayCopNames'] end
display_style_guide?()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 143 def display_style_guide? (style_guide_url || reference_url) && (@options[:display_style_guide] || config.for_all_cops['DisplayStyleGuide']) end
excluded_file?(file)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 213 def excluded_file?(file) !relevant_file?(file) end
extra_details?()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 149 def extra_details? @options[:extra_details] || config.for_all_cops['ExtraDetails'] end
join_force?(_force_class)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 126 def join_force?(_force_class) false end
message(_node = nil)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 153 def message(_node = nil) self.class::MSG end
parse(source, path = nil)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 198 def parse(source, path = nil) ProcessedSource.new(source, target_ruby_version, path) end
reference_url()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 222 def reference_url url = cop_config['Reference'] (url.nil? || url.empty?) ? nil : url end
relevant_file?(file)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 208 def relevant_file?(file) file_name_matches_any?(file, 'Include', true) && !file_name_matches_any?(file, 'Exclude', false) end
style_guide_url()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 217 def style_guide_url url = cop_config['StyleGuide'] (url.nil? || url.empty?) ? nil : url end
target_ruby_version()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 194 def target_ruby_version @config.for_all_cops['TargetRubyVersion'] end
Private Instance Methods
annotate_message(message)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 234 def annotate_message(message) message = "#{name}: #{message}" if display_cop_names? message += " #{details}" if extra_details? if display_style_guide? links = [style_guide_url, reference_url].compact.join(', ') message = "#{message} (#{links})" end message end
custom_severity()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 267 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
default_severity()
click to toggle source
# File lib/rubocop/cop/cop.rb, line 263 def default_severity self.class.lint? ? :warning : :convention end
enabled_line?(line_number)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 258 def enabled_line?(line_number) return true unless @processed_source @processed_source.comment_config.cop_enabled_at_line?(self, line_number) end
file_name_matches_any?(file, parameter, default_result)
click to toggle source
# File lib/rubocop/cop/cop.rb, line 244 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, config.loaded_path) # Try with relative path. path ||= config.path_relative_to_config(file) match_path?(pattern, path, config.loaded_path) end end