Class: RuboCop::Cop::Registry

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubocop/cop/registry.rb

Overview

Registry that tracks all cops by their badge and department.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cops = [], options = {}) ⇒ Registry

Returns a new instance of Registry.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rubocop/cop/registry.rb', line 46

def initialize(cops = [], options = {})
  @registry = {}
  @departments = {}
  @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] }

  @enrollment_queue = cops
  @options = options

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

Class Attribute Details

.globalObject (readonly)

Returns the value of attribute global.



260
261
262
# File 'lib/rubocop/cop/registry.rb', line 260

def global
  @global
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



44
45
46
# File 'lib/rubocop/cop/registry.rb', line 44

def options
  @options
end

Class Method Details

.allObject



22
23
24
# File 'lib/rubocop/cop/registry.rb', line 22

def self.all
  global.without_department(:Test).cops
end

.qualified_cop_name(name, origin) ⇒ Object



26
27
28
# File 'lib/rubocop/cop/registry.rb', line 26

def self.qualified_cop_name(name, origin)
  global.qualified_cop_name(name, origin)
end

.reset!Object



40
41
42
# File 'lib/rubocop/cop/registry.rb', line 40

def self.reset!
  @global = new
end

.with_temporary_global(temp_global = global.dup) ⇒ Object

Changes momentarily the global registry Intended for testing purposes



32
33
34
35
36
37
38
# 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 Method Details

#==(other) ⇒ Object



218
219
220
# File 'lib/rubocop/cop/registry.rb', line 218

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

#contains_cop_matching?(names) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/rubocop/cop/registry.rb', line 92

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

#copsObject



167
168
169
170
# File 'lib/rubocop/cop/registry.rb', line 167

def cops
  clear_enrollment_queue
  @registry.values
end

#cops_for_department(department) ⇒ Object



210
211
212
# File 'lib/rubocop/cop/registry.rb', line 210

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

#department?(name) ⇒ Boolean

Returns Checks if given name is department.

Returns:

  • (Boolean)

    Checks if given name is department



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

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

#department_missing?(badge, name) ⇒ Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/rubocop/cop/registry.rb', line 142

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

#departmentsArray<Symbol>

Returns list of departments for current cops.

Returns:

  • (Array<Symbol>)

    list of departments for current cops.



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

def departments
  clear_enrollment_queue
  @departments.keys
end

#disabled(config) ⇒ Object



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

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

#dismiss(cop) ⇒ Object



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

def dismiss(cop)
  raise "Cop #{cop} could not be dismissed" unless @enrollment_queue.delete(cop)
end

#each(&block) ⇒ Object



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

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

#enabled(config) ⇒ Object



177
178
179
# File 'lib/rubocop/cop/registry.rb', line 177

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

#enabled?(cop, config) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rubocop/cop/registry.rb', line 185

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

#enabled_pending_cop?(cop_cfg, config) ⇒ Boolean

Returns:

  • (Boolean)


199
200
201
202
203
204
# File 'lib/rubocop/cop/registry.rb', line 199

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) ⇒ Object



58
59
60
# File 'lib/rubocop/cop/registry.rb', line 58

def enlist(cop)
  @enrollment_queue << cop
end

#find_by_cop_name(cop_name) ⇒ Class?

Parameters:

Returns:

  • (Class, nil)


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

def find_by_cop_name(cop_name)
  to_h[cop_name].first
end

#find_cops_by_directive(directive) ⇒ Object

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.



246
247
248
249
# File 'lib/rubocop/cop/registry.rb', line 246

def find_cops_by_directive(directive)
  cop = find_by_cop_name(directive)
  cop ? [cop] : cops_for_department(directive)
end

#freezeObject



251
252
253
254
255
# File 'lib/rubocop/cop/registry.rb', line 251

def freeze
  clear_enrollment_queue
  unqualified_cop_names # build cache
  super
end

#lengthObject



172
173
174
175
# File 'lib/rubocop/cop/registry.rb', line 172

def length
  clear_enrollment_queue
  @registry.size
end

#namesObject



206
207
208
# File 'lib/rubocop/cop/registry.rb', line 206

def names
  cops.map(&:cop_name)
end

#names_for_department(department) ⇒ Object



214
215
216
# File 'lib/rubocop/cop/registry.rb', line 214

def names_for_department(department)
  cops_for_department(department).map(&:cop_name)
end


146
147
148
149
150
151
152
# File 'lib/rubocop/cop/registry.rb', line 146

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

#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) (defaults to: true)

    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



128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/rubocop/cop/registry.rb', line 128

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

#select(&block) ⇒ Object



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

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

#sort!Object



222
223
224
225
226
227
# File 'lib/rubocop/cop/registry.rb', line 222

def sort!
  clear_enrollment_queue
  @registry = @registry.sort_by { |badge, _| badge.cop_name }.to_h

  self
end

#to_hHash{String => Array<Class>}

Returns:

  • (Hash{String => Array<Class>})


162
163
164
165
# File 'lib/rubocop/cop/registry.rb', line 162

def to_h
  clear_enrollment_queue
  @cops_by_cop_name
end

#unqualified_cop_namesObject



154
155
156
157
158
159
# File 'lib/rubocop/cop/registry.rb', line 154

def unqualified_cop_names
  clear_enrollment_queue
  @unqualified_cop_names ||=
    Set.new(@cops_by_cop_name.keys.map { |qn| File.basename(qn) }) <<
    'RedundantCopDisableDirective'
end

#with_department(department) ⇒ Registry

Returns Cops for that specific department.

Returns:

  • (Registry)

    Cops for that specific department.



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

def with_department(department)
  clear_enrollment_queue
  with(@departments.fetch(department, []))
end

#without_department(department) ⇒ Registry

Returns Cops not for a specific department.

Returns:

  • (Registry)

    Cops not for a specific department.



79
80
81
82
83
84
85
# File 'lib/rubocop/cop/registry.rb', line 79

def without_department(department)
  clear_enrollment_queue
  without_department = @departments.dup
  without_department.delete(department)

  with(without_department.values.flatten)
end