class RuboCop::Cop::Registry
Registry that tracks all cops by their badge and department.
Attributes
Public Class Methods
# File lib/rubocop/cop/registry.rb, line 238 def self.all global.without_department(:Test).cops end
# File lib/rubocop/cop/registry.rb, line 24 def initialize(cops = [], options = {}) @registry = {} @departments = {} @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] } @enrollment_queue = cops @options = options end
# File lib/rubocop/cop/registry.rb, line 242 def self.qualified_cop_name(name, origin) global.qualified_cop_name(name, origin) end
# File lib/rubocop/cop/registry.rb, line 256 def self.reset! @global = new end
Changes momentarily the global registry Intended for testing purposes
# File lib/rubocop/cop/registry.rb, line 248 def self.with_temporary_global(temp_global = global.dup) previous = @global @global = temp_global yield ensure @global = previous end
Public Instance Methods
# File lib/rubocop/cop/registry.rb, line 193 def ==(other) cops == other.cops end
# File lib/rubocop/cop/registry.rb, line 67 def contains_cop_matching?(names) cops.any? { |cop| cop.match?(names) } end
# File lib/rubocop/cop/registry.rb, line 142 def cops clear_enrollment_queue @registry.values end
# File lib/rubocop/cop/registry.rb, line 185 def cops_for_department(department) cops.select { |cop| cop.department == department.to_sym } end
@return [Boolean] Checks if given name is department
# File lib/rubocop/cop/registry.rb, line 63 def department?(name) departments.include? name.to_sym end
# File lib/rubocop/cop/registry.rb, line 117 def department_missing?(badge, name) !badge.qualified? && unqualified_cop_names.include?(name) end
@return [Array<Symbol>] list of departments for current cops.
# File lib/rubocop/cop/registry.rb, line 42 def departments clear_enrollment_queue @departments.keys end
# File lib/rubocop/cop/registry.rb, line 156 def disabled(config) reject { |cop| enabled?(cop, config) } end
# File lib/rubocop/cop/registry.rb, line 37 def dismiss(cop) raise "Cop #{cop} could not be dismissed" unless @enrollment_queue.delete(cop) end
# File lib/rubocop/cop/registry.rb, line 208 def each(&block) cops.each(&block) end
# File lib/rubocop/cop/registry.rb, line 152 def enabled(config) select { |cop| enabled?(cop, config) } end
# File lib/rubocop/cop/registry.rb, line 160 def enabled?(cop, config) return true if options[:only]&.include?(cop.cop_name) cfg = config.for_cop(cop) cop_enabled = cfg.fetch('Enabled') == true || enabled_pending_cop?(cfg, config) if options.fetch(:safe, false) cop_enabled && cfg.fetch('Safe', true) else cop_enabled end end
# File lib/rubocop/cop/registry.rb, line 174 def enabled_pending_cop?(cop_cfg, config) return false if @options[:disable_pending_cops] cop_cfg.fetch('Enabled') == 'pending' && (@options[:enable_pending_cops] || config.enabled_new_cops?) end
# File lib/rubocop/cop/registry.rb, line 33 def enlist(cop) @enrollment_queue << cop end
@param [String] cop_name @return [Class, nil]
# File lib/rubocop/cop/registry.rb, line 214 def find_by_cop_name(cop_name) to_h[cop_name].first end
When a cop name is given returns a single-element array with the cop class. When a department name is given returns an array with all the cop classes for that department.
# File lib/rubocop/cop/registry.rb, line 221 def find_cops_by_directive(directive) cop = find_by_cop_name(directive) cop ? [cop] : cops_for_department(directive) end
# File lib/rubocop/cop/registry.rb, line 226 def freeze clear_enrollment_queue unqualified_cop_names # build cache super end
# File lib/rubocop/cop/registry.rb, line 147 def length clear_enrollment_queue @registry.size end
# File lib/rubocop/cop/registry.rb, line 181 def names cops.map(&:cop_name) end
# File lib/rubocop/cop/registry.rb, line 189 def names_for_department(department) cops_for_department(department).map(&:cop_name) end
# File lib/rubocop/cop/registry.rb, line 121 def print_warning(name, path) message = "#{path}: Warning: no department given for #{name}." if path.end_with?('.rb') message += ' Run `rubocop -a --only Migration/DepartmentName` to fix.' end warn message end
Convert a user provided cop name into a properly namespaced name
@example gives back a correctly qualified cop name
registry = RuboCop::Cop::Registry registry.qualified_cop_name('Layout/EndOfLine', '') # => 'Layout/EndOfLine'
@example fixes incorrect namespaces
registry = RuboCop::Cop::Registry registry.qualified_cop_name('Lint/EndOfLine', '') # => 'Layout/EndOfLine'
@example namespaces bare cop identifiers
registry = RuboCop::Cop::Registry registry.qualified_cop_name('EndOfLine', '') # => 'Layout/EndOfLine'
@example passes back unrecognized cop names
registry = RuboCop::Cop::Registry registry.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 @param warn [Boolean] Print a warning if no department given for `name`
@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 103 def qualified_cop_name(name, path, warn: true) badge = Badge.parse(name) print_warning(name, path) if warn && department_missing?(badge, 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 204 def select(&block) cops.select(&block) end
# File lib/rubocop/cop/registry.rb, line 197 def sort! clear_enrollment_queue @registry = @registry.sort_by { |badge, _| badge.cop_name }.to_h self end
@return [Hash{String => Array<Class>}]
# File lib/rubocop/cop/registry.rb, line 137 def to_h clear_enrollment_queue @cops_by_cop_name end
# File lib/rubocop/cop/registry.rb, line 129 def unqualified_cop_names clear_enrollment_queue @unqualified_cop_names ||= Set.new(@cops_by_cop_name.keys.map { |qn| File.basename(qn) }) << 'RedundantCopDisableDirective' end
@return [Registry] Cops for that specific department.
# File lib/rubocop/cop/registry.rb, line 48 def with_department(department) clear_enrollment_queue with(@departments.fetch(department, [])) end
@return [Registry] Cops not for a specific department.
# File lib/rubocop/cop/registry.rb, line 54 def without_department(department) clear_enrollment_queue without_department = @departments.dup without_department.delete(department) with(without_department.values.flatten) end
Private Instance Methods
# File lib/rubocop/cop/registry.rb, line 266 def clear_enrollment_queue return if @enrollment_queue.empty? @enrollment_queue.each do |cop| @registry[cop.badge] = cop @departments[cop.department] ||= [] @departments[cop.department] << cop @cops_by_cop_name[cop.cop_name] << cop end @enrollment_queue = [] end
# File lib/rubocop/cop/registry.rb, line 262 def initialize_copy(reg) initialize(reg.cops, reg.options) end
# File lib/rubocop/cop/registry.rb, line 282 def qualify_badge(badge) clear_enrollment_queue @departments .map { |department, _| badge.with_department(department) } .select { |potential_badge| registered?(potential_badge) } end
# File lib/rubocop/cop/registry.rb, line 299 def registered?(badge) clear_enrollment_queue @registry.key?(badge) end
# File lib/rubocop/cop/registry.rb, line 289 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 278 def with(cops) self.class.new(cops) end