A scaffold for concrete cops.
The Cop class is meant to be extended.
Cops track offenses and can autocorrect them of 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
# File lib/rubocop/cop/cop.rb, line 80 def self.cop_name @cop_name ||= name.to_s.split('::').last(2).join('/') end
# File lib/rubocop/cop/cop.rb, line 84 def self.cop_type name.to_s.split('::')[-2].downcase.to_sym end
# File lib/rubocop/cop/cop.rb, line 76 def self.inherited(subclass) @all << subclass end
# File lib/rubocop/cop/cop.rb, line 88 def self.lint? cop_type == :lint end
# File lib/rubocop/cop/cop.rb, line 96 def initialize(config = nil, options = nil) @config = config || Config.new @options = options || { auto_correct: false, debug: false } @offenses = [] @corrections = [] end
# File lib/rubocop/cop/cop.rb, line 72 def self.non_rails @all.without_type(:rails) end
# File lib/rubocop/cop/cop.rb, line 59 def self.qualified_cop_name(name, origin) return name if name.include?('/') found_ns = @all.types.map(&:capitalize).select do |ns| @all.map(&:cop_name).include?("#{ns}/#{name}") end case found_ns.size when 0 then name # No namespace found. Deal with it later in caller. when 1 then "#{found_ns.first}/#{name}" else fail AmbiguousCopName, "`#{name}` used in #{origin}" end end
# File lib/rubocop/cop/cop.rb, line 132 def add_offense(node, loc, message = nil, severity = nil) location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc return unless enabled_line?(location.line) # Don't include the same location twice for one cop. return if @offenses.find { |o| o.location == location } severity = custom_severity || severity || default_severity message ||= message(node) message = display_cop_names? ? "#{name}: #{message}" : message corrected = begin autocorrect(node) if autocorrect? autocorrect? rescue CorrectionNotPossible false end @offenses << Offense.new(severity, location, message, name, corrected) yield if block_given? end
# File lib/rubocop/cop/cop.rb, line 112 def autocorrect? @options[:auto_correct] && support_autocorrect? end
# File lib/rubocop/cop/cop.rb, line 155 def config_to_allow_offenses Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] end
# File lib/rubocop/cop/cop.rb, line 159 def config_to_allow_offenses=(hash) Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] = hash end
# File lib/rubocop/cop/cop.rb, line 108 def cop_config @config.for_cop(self) end
# File lib/rubocop/cop/cop.rb, line 164 def cop_name self.class.cop_name end
# File lib/rubocop/cop/cop.rb, line 116 def debug? @options[:debug] end
# File lib/rubocop/cop/cop.rb, line 120 def display_cop_names? debug? || @options[:display_cop_names] end
# File lib/rubocop/cop/cop.rb, line 174 def exclude_file?(file) file_name_matches_any?(file, 'Exclude', false) end
# File lib/rubocop/cop/cop.rb, line 170 def include_file?(file) file_name_matches_any?(file, 'Include', true) end
# File lib/rubocop/cop/cop.rb, line 104 def join_force?(_force_class) false end
# File lib/rubocop/cop/cop.rb, line 124 def message(_node = nil) self.class::MSG end
Generated with the Darkfish Rdoc Generator 2.