lib/rubocop/cop/cop.rb in rubocop-0.30.1 vs lib/rubocop/cop/cop.rb in rubocop-0.31.0

- old
+ new

@@ -1,10 +1,9 @@ # 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<String>] list of types for current cops. @@ -20,10 +19,32 @@ # @return [Array<Cop>] Cops not for a specific type. def without_type(type) CopStore.new(reject { |c| c.cop_type == type }) end + + def qualified_cop_name(name, origin) + @cop_names ||= Set.new(map(&:cop_name)) + basename = File.basename(name) + found_ns = types.map(&:capitalize).select do |ns| + @cop_names.include?("#{ns}/#{basename}") + end + + case found_ns.size + when 0 then name # No namespace found. Deal with it later in caller. + when 1 then cop_name_with_namespace(name, origin, basename, found_ns[0]) + else fail AmbiguousCopName, "`#{basename}` used in #{origin}" + end + end + + def cop_name_with_namespace(name, origin, basename, found_ns) + if name != basename && found_ns != File.dirname(name).to_sym + warn "#{origin}: #{name} has the wrong namespace - should be " \ + "#{found_ns}" + end + "#{found_ns}/#{basename}" + end end # A scaffold for concrete cops. # # The Cop class is meant to be extended. @@ -57,31 +78,13 @@ def self.all @all.without_type(:test) end def self.qualified_cop_name(name, origin) - @cop_names ||= Set.new(all.map(&:cop_name)) - basename = File.basename(name) - found_ns = @all.types.map(&:capitalize).select do |ns| - @cop_names.include?("#{ns}/#{basename}") - end - - case found_ns.size - when 0 then name # No namespace found. Deal with it later in caller. - when 1 then cop_name_with_namespace(name, origin, basename, found_ns[0]) - else fail AmbiguousCopName, "`#{basename}` used in #{origin}" - end + @all.qualified_cop_name(name, origin) end - def self.cop_name_with_namespace(name, origin, basename, found_ns) - if name != basename && found_ns != File.dirname(name).to_sym - warn "#{origin}: #{name} has the wrong namespace - should be " \ - "#{found_ns}" - end - "#{found_ns}/#{basename}" - end - def self.non_rails all.without_type(:rails) end def self.inherited(subclass) @@ -133,10 +136,19 @@ style_guide_url && (@options[:display_style_guide] || config['AllCops'] && config['AllCops']['DisplayStyleGuide']) end + # Returns true if the cop name or the cop namespace matches any of the + # given names. + def self.match?(given_names) + return false unless given_names + + given_names.include?(cop_name) || + given_names.include?(cop_type.to_s.capitalize) + end + def message(_node = nil) self.class::MSG end def add_offense(node, loc, message = nil, severity = nil) @@ -160,13 +172,13 @@ def correct(node) return nil unless support_autocorrect? return false unless autocorrect? - autocorrect(node) + correction = autocorrect(node) + return false unless correction + @corrections << correction true - rescue CorrectionNotPossible - false end def config_to_allow_offenses Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] end