Class: RuboCop::Cop::Registry
- Inherits:
-
Object
- Object
- RuboCop::Cop::Registry
- Includes:
- Enumerable
- Defined in:
- lib/rubocop/cop/registry.rb
Overview
Registry that tracks all cops by their badge and department.
Class Attribute Summary collapse
-
.global ⇒ Object
readonly
Returns the value of attribute global.
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
- .all ⇒ Object
- .qualified_cop_name(name, origin) ⇒ Object
- .reset! ⇒ Object
-
.with_temporary_global(temp_global = global.dup) ⇒ Object
Changes momentarily the global registry Intended for testing purposes.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #contains_cop_matching?(names) ⇒ Boolean
- #cops ⇒ Object
- #department_missing?(badge, name) ⇒ Boolean
-
#departments ⇒ Array<Symbol>
List of departments for current cops.
- #dismiss(cop) ⇒ Object
- #each(&block) ⇒ Object
- #enabled(config, only = [], only_safe: false) ⇒ Object
- #enabled?(cop, config, only_safe) ⇒ Boolean
- #enabled_pending_cop?(cop_cfg, config) ⇒ Boolean
- #enlist(cop) ⇒ Object
- #find_by_cop_name(cop_name) ⇒ Class?
- #freeze ⇒ Object
-
#initialize(cops = [], options = {}) ⇒ Registry
constructor
A new instance of Registry.
- #length ⇒ Object
- #names ⇒ Object
- #print_warning(name, path) ⇒ Object
-
#qualified_cop_name(name, path, warn: true) ⇒ String
Convert a user provided cop name into a properly namespaced name.
- #select(&block) ⇒ Object
- #sort! ⇒ Object
- #to_h ⇒ Hash{String => Array<Class>}
- #unqualified_cop_names ⇒ Object
-
#with_department(department) ⇒ Registry
Cops for that specific department.
-
#without_department(department) ⇒ Registry
Cops not for a specific department.
Constructor Details
#initialize(cops = [], options = {}) ⇒ Registry
Returns a new instance of Registry.
29 30 31 32 33 34 35 36 |
# File 'lib/rubocop/cop/registry.rb', line 29 def initialize(cops = [], = {}) @registry = {} @departments = {} @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] } @enrollment_queue = cops @options = end |
Class Attribute Details
.global ⇒ Object (readonly)
Returns the value of attribute global
216 217 218 |
# File 'lib/rubocop/cop/registry.rb', line 216 def global @global end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options
27 28 29 |
# File 'lib/rubocop/cop/registry.rb', line 27 def @options end |
Class Method Details
.all ⇒ Object
219 220 221 |
# File 'lib/rubocop/cop/registry.rb', line 219 def self.all global.without_department(:Test).cops end |
.qualified_cop_name(name, origin) ⇒ Object
223 224 225 |
# File 'lib/rubocop/cop/registry.rb', line 223 def self.qualified_cop_name(name, origin) global.qualified_cop_name(name, origin) end |
.reset! ⇒ Object
237 238 239 |
# File 'lib/rubocop/cop/registry.rb', line 237 def self.reset! @global = new end |
.with_temporary_global(temp_global = global.dup) ⇒ Object
Changes momentarily the global registry Intended for testing purposes
229 230 231 232 233 234 235 |
# File 'lib/rubocop/cop/registry.rb', line 229 def self.with_temporary_global(temp_global = global.dup) previous = @global @global = temp_global yield ensure @global = previous end |
Instance Method Details
#==(other) ⇒ Object
182 183 184 |
# File 'lib/rubocop/cop/registry.rb', line 182 def ==(other) cops == other.cops end |
#contains_cop_matching?(names) ⇒ Boolean
67 68 69 |
# File 'lib/rubocop/cop/registry.rb', line 67 def contains_cop_matching?(names) cops.any? { |cop| cop.match?(names) } end |
#cops ⇒ Object
142 143 144 145 |
# File 'lib/rubocop/cop/registry.rb', line 142 def cops clear_enrollment_queue @registry.values end |
#department_missing?(badge, name) ⇒ Boolean
117 118 119 |
# File 'lib/rubocop/cop/registry.rb', line 117 def department_missing?(badge, name) !badge.qualified? && unqualified_cop_names.include?(name) end |
#departments ⇒ Array<Symbol>
Returns list of departments for current cops.
47 48 49 50 |
# File 'lib/rubocop/cop/registry.rb', line 47 def departments clear_enrollment_queue @departments.keys end |
#dismiss(cop) ⇒ Object
42 43 44 |
# File 'lib/rubocop/cop/registry.rb', line 42 def dismiss(cop) raise "Cop #{cop} could not be dismissed" unless @enrollment_queue.delete(cop) end |
#each(&block) ⇒ Object
197 198 199 |
# File 'lib/rubocop/cop/registry.rb', line 197 def each(&block) cops.each(&block) end |
#enabled(config, only = [], only_safe: false) ⇒ Object
152 153 154 155 156 |
# File 'lib/rubocop/cop/registry.rb', line 152 def enabled(config, only = [], only_safe: false) select do |cop| only.include?(cop.cop_name) || enabled?(cop, config, only_safe) end end |
#enabled?(cop, config, only_safe) ⇒ Boolean
158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/rubocop/cop/registry.rb', line 158 def enabled?(cop, config, only_safe) cfg = config.for_cop(cop) cop_enabled = cfg.fetch('Enabled') == true || enabled_pending_cop?(cfg, config) if only_safe cop_enabled && cfg.fetch('Safe', true) else cop_enabled end end |
#enabled_pending_cop?(cop_cfg, config) ⇒ Boolean
171 172 173 174 175 176 |
# File 'lib/rubocop/cop/registry.rb', line 171 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
38 39 40 |
# File 'lib/rubocop/cop/registry.rb', line 38 def enlist(cop) @enrollment_queue << cop end |
#find_by_cop_name(cop_name) ⇒ Class?
203 204 205 |
# File 'lib/rubocop/cop/registry.rb', line 203 def find_by_cop_name(cop_name) to_h[cop_name].first end |
#freeze ⇒ Object
207 208 209 210 211 |
# File 'lib/rubocop/cop/registry.rb', line 207 def freeze clear_enrollment_queue unqualified_cop_names # build cache super end |
#length ⇒ Object
147 148 149 150 |
# File 'lib/rubocop/cop/registry.rb', line 147 def length clear_enrollment_queue @registry.size end |
#names ⇒ Object
178 179 180 |
# File 'lib/rubocop/cop/registry.rb', line 178 def names cops.map(&:cop_name) end |
#print_warning(name, path) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/rubocop/cop/registry.rb', line 121 def print_warning(name, path) = "#{path}: Warning: no department given for #{name}." if path.end_with?('.rb') += ' Run `rubocop -a --only Migration/DepartmentName` to fix.' end warn end |
#qualified_cop_name(name, path, warn: true) ⇒ String
Emits a warning if the provided name has an incorrect namespace
Convert a user provided cop name into a properly namespaced name
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# 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 |
#select(&block) ⇒ Object
193 194 195 |
# File 'lib/rubocop/cop/registry.rb', line 193 def select(&block) cops.select(&block) end |
#sort! ⇒ Object
186 187 188 189 190 191 |
# File 'lib/rubocop/cop/registry.rb', line 186 def sort! clear_enrollment_queue @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }] self end |
#to_h ⇒ Hash{String => Array<Class>}
137 138 139 140 |
# File 'lib/rubocop/cop/registry.rb', line 137 def to_h clear_enrollment_queue @cops_by_cop_name end |
#unqualified_cop_names ⇒ Object
129 130 131 132 133 134 |
# 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 |
#with_department(department) ⇒ Registry
Returns Cops for that specific department.
53 54 55 56 |
# File 'lib/rubocop/cop/registry.rb', line 53 def with_department(department) clear_enrollment_queue with(@departments.fetch(department, [])) end |
#without_department(department) ⇒ Registry
Returns Cops not for a specific department.
59 60 61 62 63 64 65 |
# File 'lib/rubocop/cop/registry.rb', line 59 def without_department(department) clear_enrollment_queue without_department = @departments.dup without_department.delete(department) with(without_department.values.flatten) end |