Class: RuboCop::Cop::Registry
- Inherits:
-
Object
- Object
- RuboCop::Cop::Registry
- Defined in:
- lib/rubocop/cop/registry.rb
Overview
Registry that tracks all cops by their badge and department.
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.
- #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?
-
#initialize(cops = [], options = {}) ⇒ Registry
constructor
A new instance of Registry.
- #length ⇒ Object
- #names ⇒ Object
- #print_warning(name, path) ⇒ Object
-
#qualified_cop_name(name, path, shall_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.
25 26 27 28 29 30 31 32 |
# File 'lib/rubocop/cop/registry.rb', line 25 def initialize(cops = [], = {}) @registry = {} @departments = {} @cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] } cops.each { |cop| enlist(cop) } @options = end |
Instance Method Details
#==(other) ⇒ Object
172 173 174 |
# File 'lib/rubocop/cop/registry.rb', line 172 def ==(other) cops == other.cops end |
#contains_cop_matching?(names) ⇒ Boolean
59 60 61 |
# File 'lib/rubocop/cop/registry.rb', line 59 def contains_cop_matching?(names) cops.any? { |cop| cop.match?(names) } end |
#cops ⇒ Object
134 135 136 |
# File 'lib/rubocop/cop/registry.rb', line 134 def cops @registry.values end |
#department_missing?(badge, name) ⇒ Boolean
111 112 113 |
# File 'lib/rubocop/cop/registry.rb', line 111 def department_missing?(badge, name) !badge.qualified? && unqualified_cop_names.include?(name) end |
#departments ⇒ Array<Symbol>
Returns list of departments for current cops.
42 43 44 |
# File 'lib/rubocop/cop/registry.rb', line 42 def departments @departments.keys end |
#each(&block) ⇒ Object
186 187 188 |
# File 'lib/rubocop/cop/registry.rb', line 186 def each(&block) cops.each(&block) end |
#enabled(config, only, only_safe = false) ⇒ Object
142 143 144 145 146 |
# File 'lib/rubocop/cop/registry.rb', line 142 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
148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/rubocop/cop/registry.rb', line 148 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
161 162 163 164 165 166 |
# File 'lib/rubocop/cop/registry.rb', line 161 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
34 35 36 37 38 39 |
# File 'lib/rubocop/cop/registry.rb', line 34 def enlist(cop) @registry[cop.badge] = cop @departments[cop.department] ||= [] @departments[cop.department] << cop @cops_by_cop_name[cop.cop_name] << cop end |
#find_by_cop_name(cop_name) ⇒ Class?
192 193 194 |
# File 'lib/rubocop/cop/registry.rb', line 192 def find_by_cop_name(cop_name) @cops_by_cop_name[cop_name].first end |
#length ⇒ Object
138 139 140 |
# File 'lib/rubocop/cop/registry.rb', line 138 def length @registry.size end |
#names ⇒ Object
168 169 170 |
# File 'lib/rubocop/cop/registry.rb', line 168 def names cops.map(&:cop_name) end |
#print_warning(name, path) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/rubocop/cop/registry.rb', line 115 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, shall_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
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/rubocop/cop/registry.rb', line 95 def qualified_cop_name(name, path, shall_warn = true) badge = Badge.parse(name) if shall_warn && department_missing?(badge, name) print_warning(name, path) end 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
182 183 184 |
# File 'lib/rubocop/cop/registry.rb', line 182 def select(&block) cops.select(&block) end |
#sort! ⇒ Object
176 177 178 179 180 |
# File 'lib/rubocop/cop/registry.rb', line 176 def sort! @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }] self end |
#to_h ⇒ Hash{String => Array<Class>}
130 131 132 |
# File 'lib/rubocop/cop/registry.rb', line 130 def to_h @cops_by_cop_name end |
#unqualified_cop_names ⇒ Object
123 124 125 126 127 |
# File 'lib/rubocop/cop/registry.rb', line 123 def unqualified_cop_names @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.
47 48 49 |
# File 'lib/rubocop/cop/registry.rb', line 47 def with_department(department) with(@departments.fetch(department, [])) end |
#without_department(department) ⇒ Registry
Returns Cops not for a specific department.
52 53 54 55 56 57 |
# File 'lib/rubocop/cop/registry.rb', line 52 def without_department(department) without_department = @departments.dup without_department.delete(department) with(without_department.values.flatten) end |