Class: RuboCop::Cop::Registry

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

Overview

Registry that tracks all cops by their badge and department.

Instance Method Summary collapse

Constructor Details

#initialize(cops = []) ⇒ Registry

Returns a new instance of Registry.



25
26
27
28
29
30
31
# 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) }
end

Instance Method Details

#==(other) ⇒ Object



165
166
167
# File 'lib/rubocop/cop/registry.rb', line 165

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

#contains_cop_matching?(names) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#copsObject



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

def cops
  @registry.values
end

#department_missing?(badge, name) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/rubocop/cop/registry.rb', line 110

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.



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

def departments
  @departments.keys
end

#each(&block) ⇒ Object



179
180
181
# File 'lib/rubocop/cop/registry.rb', line 179

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

#enabled(config, only, only_safe = false) ⇒ Object



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

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

Returns:

  • (Boolean)


147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rubocop/cop/registry.rb', line 147

def enabled?(cop, config, only_safe)
  cfg = config.for_cop(cop)

  # cfg['Enabled'] might be a string `pending`, which is considered
  # disabled
  cop_enabled = cfg.fetch('Enabled') == true

  if only_safe
    cop_enabled && cfg.fetch('Safe', true)
  else
    cop_enabled
  end
end

#enlist(cop) ⇒ Object



33
34
35
36
37
38
# File 'lib/rubocop/cop/registry.rb', line 33

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?

Parameters:

Returns:

  • (Class, nil)


185
186
187
# File 'lib/rubocop/cop/registry.rb', line 185

def find_by_cop_name(cop_name)
  @cops_by_cop_name[cop_name].first
end

#lengthObject



137
138
139
# File 'lib/rubocop/cop/registry.rb', line 137

def length
  @registry.size
end

#namesObject



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

def names
  cops.map(&:cop_name)
end


114
115
116
117
118
119
120
# File 'lib/rubocop/cop/registry.rb', line 114

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, 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

Examples:

gives back a correctly qualified cop name


cops = RuboCop::Cop::Cop.all
cops.
  qualified_cop_name('Layout/EndOfLine') # => 'Layout/EndOfLine'

fixes incorrect namespaces


cops = RuboCop::Cop::Cop.all
cops.qualified_cop_name('Lint/EndOfLine') # => 'Layout/EndOfLine'

namespaces bare cop identifiers


cops = RuboCop::Cop::Cop.all
cops.qualified_cop_name('EndOfLine') # => 'Layout/EndOfLine'

passes back unrecognized cop names


cops = RuboCop::Cop::Cop.all
cops.qualified_cop_name('NotACop') # => 'NotACop'

Parameters:

  • name (String)

    Cop name extracted from config

  • path (String, nil)

    Path of file that ‘name` was extracted from

Returns:

  • (String)

    Qualified cop name

Raises:

  • (AmbiguousCopName)

    if a bare identifier with two possible namespaces is provided



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubocop/cop/registry.rb', line 94

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



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

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

#sort!Object



169
170
171
172
173
# File 'lib/rubocop/cop/registry.rb', line 169

def sort!
  @registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }]

  self
end

#to_hHash{String => Array<Class>}

Returns:

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


129
130
131
# File 'lib/rubocop/cop/registry.rb', line 129

def to_h
  @cops_by_cop_name
end

#unqualified_cop_namesObject



122
123
124
125
126
# File 'lib/rubocop/cop/registry.rb', line 122

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.

Returns:

  • (Registry)

    Cops for that specific department.



46
47
48
# File 'lib/rubocop/cop/registry.rb', line 46

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

#without_department(department) ⇒ Registry

Returns Cops not for a specific department.

Returns:

  • (Registry)

    Cops not for a specific department.



51
52
53
54
55
56
# File 'lib/rubocop/cop/registry.rb', line 51

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

  with(without_department.values.flatten)
end