/
registry.rb
315 lines (256 loc) · 8.71 KB
/
registry.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# frozen_string_literal: true
module RuboCop
module Cop
# Error raised when an unqualified cop name is used that could
# refer to two or more cops under different departments
class AmbiguousCopName < RuboCop::Error
MSG = 'Ambiguous cop name `%<name>s` used in %<origin>s needs ' \
'department qualifier. Did you mean %<options>s?'
def initialize(name, origin, badges)
super(
format(MSG, name: name, origin: origin, options: badges.to_a.join(' or '))
)
end
end
# Registry that tracks all cops by their badge and department.
class Registry
include Enumerable
def self.all
global.without_department(:Test).cops
end
def self.qualified_cop_name(name, origin)
global.qualified_cop_name(name, origin)
end
# Changes momentarily the global registry
# Intended for testing purposes
def self.with_temporary_global(temp_global = global.dup)
previous = @global
@global = temp_global
yield
ensure
@global = previous
end
def self.reset!
@global = new
end
def self.qualified_cop?(name)
badge = Badge.parse(name)
global.qualify_badge(badge).first == badge
end
attr_reader :options
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
def enlist(cop)
@enrollment_queue << cop
end
def dismiss(cop)
raise "Cop #{cop} could not be dismissed" unless @enrollment_queue.delete(cop)
end
# @return [Array<Symbol>] list of departments for current cops.
def departments
clear_enrollment_queue
@departments.keys
end
# @return [Registry] Cops for that specific department.
def with_department(department)
clear_enrollment_queue
with(@departments.fetch(department, []))
end
# @return [Registry] Cops not for a specific department.
def without_department(department)
clear_enrollment_queue
without_department = @departments.dup
without_department.delete(department)
with(without_department.values.flatten)
end
# @return [Boolean] Checks if given name is department
def department?(name)
departments.include?(name.to_sym)
end
def contains_cop_matching?(names)
cops.any? { |cop| cop.match?(names) }
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
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
def department_missing?(badge, name)
!badge.qualified? && unqualified_cop_names.include?(name)
end
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
def unqualified_cop_names
clear_enrollment_queue
@unqualified_cop_names ||=
Set.new(@cops_by_cop_name.keys.map { |qn| File.basename(qn) }) <<
'RedundantCopDisableDirective'
end
def qualify_badge(badge)
clear_enrollment_queue
@departments
.map { |department, _| badge.with_department(department) }
.select { |potential_badge| registered?(potential_badge) }
end
# @return [Hash{String => Array<Class>}]
def to_h
clear_enrollment_queue
@cops_by_cop_name
end
def cops
clear_enrollment_queue
@registry.values
end
def length
clear_enrollment_queue
@registry.size
end
def enabled(config)
@enabled_cache[config] ||= select { |cop| enabled?(cop, config) }
end
def disabled(config)
@disabled_cache[config] ||= reject { |cop| enabled?(cop, config) }
end
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
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
def names
cops.map(&:cop_name)
end
def cops_for_department(department)
cops.select { |cop| cop.department == department.to_sym }
end
def names_for_department(department)
cops_for_department(department).map(&:cop_name)
end
def ==(other)
cops == other.cops
end
def sort!
clear_enrollment_queue
@registry = @registry.sort_by { |badge, _| badge.cop_name }.to_h
self
end
def select(&block)
cops.select(&block)
end
def each(&block)
cops.each(&block)
end
# @param [String] cop_name
# @return [Class, nil]
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.
def find_cops_by_directive(directive)
cop = find_by_cop_name(directive)
cop ? [cop] : cops_for_department(directive)
end
def freeze
clear_enrollment_queue
unqualified_cop_names # build cache
super
end
@global = new
class << self
attr_reader :global
end
private
def initialize_copy(reg)
initialize(reg.cops, reg.options)
end
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
def with(cops)
self.class.new(cops)
end
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
def registered?(badge)
clear_enrollment_queue
@registry.key?(badge)
end
end
end
end