# encoding: utf-8 module RuboCop module Cop class CorrectionNotPossible < Exception; end class AmbiguousCopName < Exception; end # Store for all cops with helper functions class CopStore < ::Array # @return [Array] list of types for current cops. def types @types = map(&:cop_type).uniq! unless defined? @types @types end # @return [Array] Cops for that specific type. def with_type(type) select { |c| c.cop_type == type } end # @return [Array] Cops not for a specific type. def without_type(type) reject { |c| c.cop_type == type } end end # 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 class Cop extend AST::Sexp include Util include IgnoredNode attr_reader :config, :offenses, :corrections attr_accessor :processed_source # TODO: Bad design. @all = CopStore.new def self.all @all.clone end 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 def self.non_rails @all.without_type(:rails) end def self.inherited(subclass) @all << subclass end def self.cop_name @cop_name ||= name.to_s.split('::').last(2).join('/') end def self.cop_type name.to_s.split('::')[-2].downcase.to_sym end def self.lint? cop_type == :lint end def self.rails? cop_type == :rails end def initialize(config = nil, options = nil) @config = config || Config.new @options = options || { auto_correct: false, debug: false } @offenses = [] @corrections = [] end def join_force?(_force_class) false end def cop_config @config.for_cop(self) end def autocorrect? @options[:auto_correct] && support_autocorrect? end def debug? @options[:debug] end def display_cop_names? debug? || @options[:display_cop_names] end def message(_node = nil) self.class::MSG end def support_autocorrect? respond_to?(:autocorrect, true) end 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 def config_to_allow_offenses Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] end def config_to_allow_offenses=(hash) Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] = hash end def cop_name self.class.cop_name end alias_method :name, :cop_name def include_file?(file) file_name_matches_any?(file, 'Include', true) end def exclude_file?(file) file_name_matches_any?(file, 'Exclude', false) end def relevant_file?(file) include_file?(file) && !exclude_file?(file) end private def file_name_matches_any?(file, parameter, default_result) patterns = cop_config && cop_config[parameter] return default_result unless patterns path = config.path_relative_to_config(file) patterns.any? do |pattern| match_path?(pattern, path, config.loaded_path) end end def enabled_line?(line_number) return true unless @processed_source @processed_source.comment_config .cop_enabled_at_line?(self, line_number) end def default_severity self.class.lint? ? :warning : :convention end def custom_severity severity = cop_config && cop_config['Severity'] return unless severity if Severity::NAMES.include?(severity.to_sym) severity.to_sym else warn "Warning: Invalid severity '#{severity}'. " + "Valid severities are #{Severity::NAMES.join(', ')}." .color(:red) end end end end end