Class: RuboCop::Cop::Registry
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
Enumerable
|
|
| Inherits: | Object |
| Defined in: | lib/rubocop/cop/registry.rb |
Overview
Registry that tracks all cops by their badge and department.
Class Attribute Summary
- .global readonly
Class Method Summary
- .all
- .new(cops = [], options = {}) ⇒ Registry constructor
- .qualified_cop?(name) ⇒ Boolean
- .qualified_cop_name(name, origin, warn: true)
- .reset!
-
.with_temporary_global(temp_global = global.dup)
Changes momentarily the global registry Intended for testing purposes.
Instance Attribute Summary
- #options readonly
- #warnings?(path) ⇒ Boolean readonly
Instance Method Summary
- #==(other)
- #contains_cop_matching?(names) ⇒ Boolean
- #cops
- #cops_for_department(department)
- #department?(name) ⇒ Boolean
- #department_missing?(badge, name) ⇒ Boolean
- #departments ⇒ Array<Symbol>
- #disabled(config)
- #dismiss(cop)
- #each(&block)
- #enabled(config)
- #enabled?(cop, config) ⇒ Boolean
- #enabled_pending_cop?(cop_cfg, config) ⇒ Boolean
- #enlist(cop)
- #find_by_cop_name(cop_name) ⇒ Class?
-
#find_cops_by_directive(directive)
When a cop name is given returns a single-element array with the cop class.
- #freeze
- #length
- #names
- #names_for_department(department)
- #print_department_missing_warning(name, path)
-
#qualified_cop_name(name, path, warn: true) ⇒ String
Convert a user provided cop name into a properly namespaced name.
- #qualify_badge(badge)
- #select(&block)
- #sort!
- #to_h ⇒ Hash{String => Array<Class>}
- #unqualified_cop_names
- #warnings readonly
- #with_department(department) ⇒ Registry
- #without_department(department) ⇒ Registry
- #clear_enrollment_queue private
- #emit_warning(path, message) private
- #initialize_copy(reg) private
- #registered?(badge) ⇒ Boolean private
- #resolve_badge(given_badge, real_badge, source_path, warn: true) private
- #with(cops) private
Constructor Details
.new(cops = [], options = {}) ⇒ Registry
Class Attribute Details
.global (readonly)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 272
attr_reader :global
Class Method Details
.all
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 22
def self.all global.without_department(:Test).cops end
.qualified_cop?(name) ⇒ Boolean
# File 'lib/rubocop/cop/registry.rb', line 44
def self.qualified_cop?(name) badge = Badge.parse(name) global.qualify_badge(badge).first == badge end
.qualified_cop_name(name, origin, warn: true)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 26
def self.qualified_cop_name(name, origin, warn: true) global.qualified_cop_name(name, origin, warn: warn) end
.reset!
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 40
def self.reset! @global = new end
.with_temporary_global(temp_global = global.dup)
Changes momentarily the global registry Intended for testing purposes
# File 'lib/rubocop/cop/registry.rb', line 32
def self.with_temporary_global(temp_global = global.dup) previous = @global @global = temp_global yield ensure @global = previous end
Instance Attribute Details
#options (readonly)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 49
attr_reader :, :warnings
#warnings?(path) ⇒ Boolean (readonly)
[ GitHub ]
# File 'lib/rubocop/cop/registry.rb', line 275
def warnings?(path) @warnings[path] end
Instance Method Details
#==(other)
[ GitHub ]#clear_enrollment_queue (private)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 285
def clear_enrollment_queue return if @enrollment_queue.empty? @enrollment_queue.each do |cop| @cops_by_badge[cop.badge] = cop @departments << cop.department end @enrollment_queue = [] end
#contains_cop_matching?(names) ⇒ Boolean
#cops
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 174
def cops clear_enrollment_queue @cops_by_badge.values end
#cops_for_department(department)
[ GitHub ]
#department?(name) ⇒ Boolean
# File 'lib/rubocop/cop/registry.rb', line 88
def department?(name) departments.include?(name.to_sym) end
#department_missing?(badge, name) ⇒ Boolean
# File 'lib/rubocop/cop/registry.rb', line 142
def department_missing?(badge, name) !badge.qualified? && unqualified_cop_names.include?(name) end
#departments ⇒ Array<Symbol>
# File 'lib/rubocop/cop/registry.rb', line 72
def departments clear_enrollment_queue @departments.to_a end
#disabled(config)
[ GitHub ]#dismiss(cop)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 67
def dismiss(cop) raise "Cop #{cop} could not be dismissed" unless @enrollment_queue.delete(cop) end
#each(&block)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 243
def each(&block) cops.each(&block) end
#emit_warning(path, message) (private)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 309
def emit_warning(path, ) Registry.global.warnings[path] = true warn "#{PathUtil.smart_path(path)}: Warning: #{}" end
#enabled(config)
[ GitHub ]
#enabled?(cop, config) ⇒ Boolean
# File 'lib/rubocop/cop/registry.rb', line 192
def enabled?(cop, config) return true if [:only]&.include?(cop.cop_name) # We need to use `cop_name` in this case, because `for_cop` uses caching # which expects cop names or cop classes as keys. cfg = config.for_cop(cop.cop_name) cop_enabled = cfg.fetch('Enabled') == true || enabled_pending_cop?(cfg, config) if .fetch(:safe, false) cop_enabled && cfg.fetch('Safe', true) else cop_enabled end end
#enabled_pending_cop?(cop_cfg, config) ⇒ Boolean
#enlist(cop)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 63
def enlist(cop) @enrollment_queue << cop end
#find_by_cop_name(cop_name) ⇒ Class?
# File 'lib/rubocop/cop/registry.rb', line 249
def find_by_cop_name(cop_name) clear_enrollment_queue badge = Badge.parse(cop_name) @cops_by_badge[badge] end
#find_cops_by_directive(directive)
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 258
def find_cops_by_directive(directive) cop = find_by_cop_name(directive) cop ? [cop] : cops_for_department(directive) end
#freeze
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 263
def freeze clear_enrollment_queue unqualified_cop_names # build cache super end
#initialize_copy(reg) (private)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 281
def initialize_copy(reg) initialize(reg.cops, reg.) end
#length
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 179
def length clear_enrollment_queue @cops_by_badge.size end
#names
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 215
def names clear_enrollment_queue @cops_by_badge.keys.map(&:to_s) end
#names_for_department(department)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 224
def names_for_department(department) cops_for_department(department).map(&:cop_name) end
#print_department_missing_warning(name, path)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 146
def print_department_missing_warning(name, path) = "no department given for #{name}." if path.end_with?('.rb') += ' Run `rubocop -a --only Migration/DepartmentName` to fix.' end emit_warning(path, ) end
#qualified_cop_name(name, path, warn: true) ⇒ String
Note:
Emits a warning if the provided name has an incorrect namespace
Convert a user provided cop name into a properly namespaced name
# File 'lib/rubocop/cop/registry.rb', line 128
def qualified_cop_name(name, path, warn: true) badge = Badge.parse(name) print_department_missing_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, warn: warn) else raise AmbiguousCopName.new(badge, path, potential_badges) end end
#qualify_badge(badge)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 161
def qualify_badge(badge) clear_enrollment_queue @departments .map { |department| badge.with_department(department) } .select { |potential_badge| registered?(potential_badge) } end
#registered?(badge) ⇒ Boolean (private)
# File 'lib/rubocop/cop/registry.rb', line 314
def registered?(badge) clear_enrollment_queue @cops_by_badge.key?(badge) end
#resolve_badge(given_badge, real_badge, source_path, warn: true) (private)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 299
def resolve_badge(given_badge, real_badge, source_path, warn: true) if warn && !given_badge.match?(real_badge) emit_warning(source_path, "#{given_badge} has the wrong namespace - " \ "replace it with #{given_badge.with_department(real_badge.department)}") end real_badge.to_s end
#select(&block)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 239
def select(&block) cops.select(&block) end
#sort!
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 232
def sort! clear_enrollment_queue @cops_by_badge = @cops_by_badge.sort_by { |badge, _cop| badge.cop_name }.to_h self end
#to_h ⇒ Hash{String => Array<Class>}
# File 'lib/rubocop/cop/registry.rb', line 169
def to_h clear_enrollment_queue @cops_by_badge.to_h { |_badge, cop| [cop.cop_name, [cop]] } end
#unqualified_cop_names
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 154
def unqualified_cop_names clear_enrollment_queue @unqualified_cop_names ||= Set.new(@cops_by_badge.keys.map { |badge| File.basename(badge.to_s) }) << 'RedundantCopDisableDirective' end
#warnings (readonly)
[ GitHub ]# File 'lib/rubocop/cop/registry.rb', line 49
attr_reader :, :warnings