lib/rubocop/cop/cop.rb in rubocop-0.35.1 vs lib/rubocop/cop/cop.rb in rubocop-0.36.0
- old
+ new
@@ -1,17 +1,17 @@
# encoding: utf-8
+# frozen_string_literal: true
module RuboCop
module Cop
- class AmbiguousCopName < Exception; end
+ class AmbiguousCopName < RuboCop::Error; 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
- @types
+ @types ||= map(&:cop_type).uniq!
end
# @return [Array<Cop>] Cops for that specific type.
def with_type(type)
CopStore.new(select { |c| c.cop_type == type })
@@ -30,11 +30,12 @@
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}"
+ else fail AmbiguousCopName, "Ambiguous cop name `#{basename}` used in" \
+ "#{origin} needs namespace qualifier."
end
end
def cop_name_with_namespace(name, origin, basename, found_ns)
if name != basename && found_ns != File.dirname(name).to_sym
@@ -63,12 +64,13 @@
# def investigate(processed_source)
# # Do custom processing
# end
# end
class Cop
- extend AST::Sexp
+ extend RuboCop::Sexp
extend NodePattern::Macros
+ include RuboCop::Sexp
include Util
include IgnoredNode
include AutocorrectLogic
attr_reader :config, :offenses, :corrections
@@ -102,12 +104,17 @@
def self.lint?
cop_type == :lint
end
- def self.rails?
- cop_type == :rails
+ # 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 initialize(config = nil, options = nil)
@config = config || Config.new
@options = options || { debug: false }
@@ -142,19 +149,10 @@
def extra_details?
@options[:extra_details] ||
config['AllCops'] && config['AllCops']['ExtraDetails']
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)
@@ -192,14 +190,22 @@
def config_to_allow_offenses=(hash)
Formatter::DisabledConfigFormatter.config_to_allow_offenses[cop_name] =
hash
end
+ def target_ruby_version
+ @config['AllCops'] && @config['AllCops']['TargetRubyVersion']
+ end
+
+ def parse(source, path = nil)
+ ProcessedSource.new(source, target_ruby_version, path)
+ end
+
def cop_name
self.class.cop_name
end
- alias_method :name, :cop_name
+ alias name cop_name
def relevant_file?(file)
file_name_matches_any?(file, 'Include', true) &&
!file_name_matches_any?(file, 'Exclude', false)
end