123456789_123456789_123456789_123456789_123456789_

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(cops = [], options = {}) ⇒ Registry

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 51

def initialize(cops = [], options = {})
  @departments = Set.new
  @cops_by_badge = {}
  @lazy_loaded_cops_by_badge = {}

  @enrollment_queue = cops
  @options = options

  @enabled_cache = {}.compare_by_identity
  @disabled_cache = {}.compare_by_identity
  @warnings = {}
end

Class Attribute Details

.global (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 283

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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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 :options, :warnings

#warnings?(path) ⇒ Boolean (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 286

def warnings?(path)
  @warnings[path]
end

Instance Method Details

#==(other)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 237

def ==(other)
  cops == other.cops
end

#clear_enrollment_queue (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 296

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

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 99

def contains_cop_matching?(names)
  cops.any? { |cop| cop.match?(names) }
end

#cops

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 182

def cops
  clear_enrollment_queue
  load_all_lazy_cops
  @cops_by_badge.values
end

#cops_for_department(department)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 229

def cops_for_department(department)
  cops.select { |cop| cop.department == department.to_sym }
end

#department?(name) ⇒ Boolean

Returns:

  • (Boolean)

    Checks if given name is department

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 95

def department?(name)
  departments.include?(name.to_sym)
end

#department_missing?(badge, name) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 149

def department_missing?(badge, name)
  !badge.qualified? && unqualified_cop_names.include?(name)
end

#departmentsArray<Symbol>

Returns:

  • (Array<Symbol>)

    list of departments for current cops.

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 79

def departments
  clear_enrollment_queue
  @departments.to_a
end

#disabled(config)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 197

def disabled(config)
  @disabled_cache[config] ||= reject { |cop| enabled?(cop, config) }
end

#dismiss(cop)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 74

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 253

def each(&block)
  cops.each(&block)
end

#emit_warning(path, message) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 331

def emit_warning(path, message)
  Registry.global.warnings[path] = true
  warn "#{PathUtil.smart_path(path)}: Warning: #{message}"
end

#enabled(config)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 193

def enabled(config)
  @enabled_cache[config] ||= select { |cop| enabled?(cop, config) }
end

#enabled?(cop, config) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 201

def enabled?(cop, config)
  return true if options[: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 options.fetch(:safe, false)
    cop_enabled && cfg.fetch('Safe', true)
  else
    cop_enabled
  end
end

#enabled_pending_cop?(cop_cfg, config) ⇒ Boolean

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 217

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

#enlist(cop)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 70

def enlist(cop)
  @enrollment_queue << cop
end

#find_by_cop_name(cop_name) ⇒ Class?

Parameters:

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 259

def find_by_cop_name(cop_name)
  clear_enrollment_queue
  badge = Badge.parse(cop_name)
  @cops_by_badge[badge] || load_lazy_cop(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.

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 268

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 273

def freeze
  clear_enrollment_queue
  load_all_lazy_cops
  unqualified_cop_names # build cache
  super
end

#initialize_copy(reg) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 292

def initialize_copy(reg)
  initialize(reg.cops, reg.options)
end

#lazy_load(cop_name, constant_name)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 64

def lazy_load(cop_name, constant_name)
  badge = Badge.parse(cop_name)
  @departments << badge.department
  @lazy_loaded_cops_by_badge[badge] = constant_name
end

#length

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 188

def length
  clear_enrollment_queue
  @cops_by_badge.size + @lazy_loaded_cops_by_badge.size
end

#load_all_lazy_cops (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 306

def load_all_lazy_cops
  @lazy_loaded_cops_by_badge.each_key { |badge| load_lazy_cop(badge) }
end

#load_lazy_cop(badge) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 310

def load_lazy_cop(badge)
  constant_name = @lazy_loaded_cops_by_badge.delete(badge)
  return unless constant_name

  @cops_by_badge[badge] = Kernel.const_get(constant_name)
end

#names

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 224

def names
  clear_enrollment_queue
  @cops_by_badge.keys.map(&:to_s) | @lazy_loaded_cops_by_badge.keys.map(&:to_s)
end

#names_for_department(department)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 233

def names_for_department(department)
  cops_for_department(department).map(&:cop_name)
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

Examples:

gives back a correctly qualified cop name

registry = RuboCop::Cop::Registry
registry.qualified_cop_name('Layout/EndOfLine', '') # => 'Layout/EndOfLine'

fixes incorrect namespaces

registry = RuboCop::Cop::Registry
registry.qualified_cop_name('Lint/EndOfLine', '') # => 'Layout/EndOfLine'

namespaces bare cop identifiers

registry = RuboCop::Cop::Registry
registry.qualified_cop_name('EndOfLine', '') # => 'Layout/EndOfLine'

passes back unrecognized cop names

registry = RuboCop::Cop::Registry
registry.qualified_cop_name('NotACop', '') # => 'NotACop'

Parameters:

  • name (String)

    Cop name extracted from config

  • path (String, nil)

    Path of file that name was extracted from

  • warn (Boolean)

    Print a warning if no department given for name

Returns:

  • (String)

    Qualified cop name

Raises:

  • (AmbiguousCopName)

    if a bare identifier with two possible namespaces is provided

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 135

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 168

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)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 336

def registered?(badge)
  clear_enrollment_queue
  @cops_by_badge.key?(badge) || @lazy_loaded_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 321

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 249

def select(&block)
  cops.select(&block)
end

#sort!

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 241

def sort!
  clear_enrollment_queue
  load_all_lazy_cops
  @cops_by_badge = @cops_by_badge.sort_by { |badge, _cop| badge.cop_name }.to_h

  self
end

#to_hHash{String => Array<Class>}

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 176

def to_h
  clear_enrollment_queue
  load_all_lazy_cops
  @cops_by_badge.to_h { |_badge, cop| [cop.cop_name, [cop]] }
end

#unqualified_cop_names

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 161

def unqualified_cop_names
  clear_enrollment_queue
  @unqualified_cop_names ||=
    (@cops_by_badge.keys | @lazy_loaded_cops_by_badge.keys)
    .to_set { |badge| File.basename(badge.to_s) } << 'RedundantCopDisableDirective'
end

#warnings (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 49

attr_reader :options, :warnings

#with(cops) (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 317

def with(cops)
  self.class.new(cops)
end

#with_department(department) ⇒ Registry

Returns:

  • (Registry)

    Cops for that specific department.

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 85

def with_department(department)
  with(cops.select { |cop| cop.department == department })
end

#without_department(department) ⇒ Registry

Returns:

  • (Registry)

    Cops not for a specific department.

[ GitHub ]

  
# File 'lib/rubocop/cop/registry.rb', line 90

def without_department(department)
  with(cops.reject { |cop| cop.department == department })
end