lib/rubocop/cop/cop.rb in rubocop-0.18.1 vs lib/rubocop/cop/cop.rb in rubocop-0.19.0

- old
+ new

@@ -1,11 +1,9 @@ # encoding: utf-8 module Rubocop module Cop - class CorrectionNotPossible < Exception; end - # Store for all cops with helper functions class CopStore < ::Array # @return [Array<String>] list of types for current cops. def types @types = map(&:cop_type).uniq! unless defined? @types @@ -25,11 +23,11 @@ # A scaffold for concrete cops. # # The Cop class is meant to be extended. # - # Cops track offences and can autocorrect them of the fly. + # 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 @@ -45,11 +43,11 @@ class Cop extend AST::Sexp include Util include IgnoredNode - attr_reader :config, :offences, :corrections + attr_reader :config, :offenses, :corrections attr_accessor :processed_source # TODO: Bad design. @all = CopStore.new def self.all @@ -82,11 +80,11 @@ def initialize(config = nil, options = nil) @config = config || Config.new @options = options || { auto_correct: false, debug: false } - @offences = [] + @offenses = [] @corrections = [] end def cop_config @config.for_cop(self) @@ -110,36 +108,35 @@ def support_autocorrect? respond_to?(:autocorrect, true) end - def add_offence(node, loc, message = nil, severity = nil) + def add_offense(node, loc, message = nil, severity = nil) location = loc.is_a?(Symbol) ? node.loc.send(loc) : loc - return if disabled_line?(location.line) + 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 ? message : message(node) + message ||= message(node) message = display_cop_names? ? "#{name}: #{message}" : message - corrected = begin - autocorrect(node) if autocorrect? - autocorrect? - rescue CorrectionNotPossible - false - end - @offences << Offence.new(severity, location, message, name, corrected) + autocorrect(node) if autocorrect? + @offenses << Offense.new(severity, location, message, name, + autocorrect?) yield if block_given? end - def config_to_allow_offences - Formatter::DisabledConfigFormatter.config_to_allow_offences[cop_name] + def config_to_allow_offenses + Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] end - def config_to_allow_offences=(hash) - Formatter::DisabledConfigFormatter.config_to_allow_offences[cop_name] = + def config_to_allow_offenses=(hash) + Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] = hash end def cop_name self.class.cop_name @@ -166,28 +163,27 @@ return default_result unless patterns path = relative_path(processed_source.buffer.name) patterns.any? { |pattern| match_path?(pattern, path) } end - def disabled_line?(line_number) - return false unless @processed_source - disabled_lines = @processed_source.disabled_lines_for_cops[name] - return false unless disabled_lines - disabled_lines.include?(line_number) + 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'] if severity - if Offence::SEVERITIES.include?(severity.to_sym) + if Offense::SEVERITIES.include?(severity.to_sym) severity.to_sym else warn "Warning: Invalid severity '#{severity}'. " + - "Valid severities are #{Offence::SEVERITIES.join(', ')}." + "Valid severities are #{Offense::SEVERITIES.join(', ')}." .color(:red) end end end end