class RuboCop::Cop::Registry
Registry
that tracks all cops by their badge and department.
Public Class Methods
# File lib/rubocop/cop/registry.rb, line 25 def initialize(cops = []) @registry = {} @departments = {} @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] } cops.each { |cop| enlist(cop) } end
Public Instance Methods
# File lib/rubocop/cop/registry.rb, line 139 def ==(other) cops == other.cops end
# File lib/rubocop/cop/registry.rb, line 58 def contains_cop_matching?(names) cops.any? { |cop| cop.match?(names) } end
# File lib/rubocop/cop/registry.rb, line 112 def cops @registry.values end
@return [Array<Symbol>] list of departments for current cops.
# File lib/rubocop/cop/registry.rb, line 41 def departments @departments.keys end
# File lib/rubocop/cop/registry.rb, line 153 def each(&block) cops.each(&block) end
# File lib/rubocop/cop/registry.rb, line 120 def enabled(config, only, only_safe = false) select do |cop| only.include?(cop.cop_name) || enabled?(cop, config, only_safe) end end
# File lib/rubocop/cop/registry.rb, line 126 def enabled?(cop, config, only_safe) cfg = config.for_cop(cop) if only_safe cfg.fetch('Enabled') && cfg.fetch('Safe', true) else cfg.fetch('Enabled') end end
# File lib/rubocop/cop/registry.rb, line 33 def enlist(cop) @registry[cop.badge] = cop @departments[cop.department] ||= [] @departments[cop.department] << cop @cops_by_cop_name[cop.cop_name] << cop end
@param [String] cop_name @return [Class, nil]
# File lib/rubocop/cop/registry.rb, line 159 def find_by_cop_name(cop_name) @cops_by_cop_name[cop_name].first end
# File lib/rubocop/cop/registry.rb, line 116 def length @registry.size end
# File lib/rubocop/cop/registry.rb, line 135 def names cops.map(&:cop_name) end
Convert a user provided cop name into a properly namespaced name
@example gives back a correctly qualified cop name
cops = RuboCop::Cop::Cop.all cops. qualified_cop_name('Layout/EndOfLine') # => 'Layout/EndOfLine'
@example fixes incorrect namespaces
cops = RuboCop::Cop::Cop.all cops.qualified_cop_name('Lint/EndOfLine') # => 'Layout/EndOfLine'
@example namespaces bare cop identifiers
cops = RuboCop::Cop::Cop.all cops.qualified_cop_name('EndOfLine') # => 'Layout/EndOfLine'
@example passes back unrecognized cop names
cops = RuboCop::Cop::Cop.all cops.qualified_cop_name('NotACop') # => 'NotACop'
@param name [String] Cop
name extracted from config @param path [String, nil] Path of file that `name` was extracted from
@raise [AmbiguousCopName]
if a bare identifier with two possible namespaces is provided
@note Emits a warning if the provided name has an incorrect namespace
@return [String] Qualified cop name
# File lib/rubocop/cop/registry.rb, line 94 def qualified_cop_name(name, path) badge = Badge.parse(name) return name if registered?(badge) potential_badges = qualify_badge(badge) case potential_badges.size when 0 then name # No namespace found. Deal with it later in caller. when 1 then resolve_badge(badge, potential_badges.first, path) else raise AmbiguousCopName.new(badge, path, potential_badges) end end
# File lib/rubocop/cop/registry.rb, line 149 def select(&block) cops.select(&block) end
# File lib/rubocop/cop/registry.rb, line 143 def sort! @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }] self end
@return [Hash{String => Array<Class>}]
# File lib/rubocop/cop/registry.rb, line 108 def to_h @cops_by_cop_name end
@return [Registry] Cops for that specific department.
# File lib/rubocop/cop/registry.rb, line 46 def with_department(department) with(@departments.fetch(department, [])) end
@return [Registry] Cops not for a specific department.
# File lib/rubocop/cop/registry.rb, line 51 def without_department(department) without_department = @departments.dup without_department.delete(department) with(without_department.values.flatten) end
Private Instance Methods
# File lib/rubocop/cop/registry.rb, line 169 def qualify_badge(badge) @departments .map { |department, _| badge.with_department(department) } .select { |potential_badge| registered?(potential_badge) } end
# File lib/rubocop/cop/registry.rb, line 185 def registered?(badge) @registry.key?(badge) end
# File lib/rubocop/cop/registry.rb, line 175 def resolve_badge(given_badge, real_badge, source_path) unless given_badge.match?(real_badge) path = PathUtil.smart_path(source_path) warn "#{path}: #{given_badge} has the wrong namespace - " \ "should be #{real_badge.department}" end real_badge.to_s end
# File lib/rubocop/cop/registry.rb, line 165 def with(cops) self.class.new(cops) end