lib/rubocop/cop/registry.rb in rubocop-1.39.0 vs lib/rubocop/cop/registry.rb in rubocop-1.40.0
- old
+ new
@@ -70,31 +70,31 @@
# Convert a user provided cop name into a properly namespaced name
#
# @example gives back a correctly qualified cop name
#
- # cops = RuboCop::Cop::Cop.all
- # cops.
- # qualified_cop_name('Layout/EndOfLine') # => 'Layout/EndOfLine'
+ # registry = RuboCop::Cop::Registry
+ # registry.qualified_cop_name('Layout/EndOfLine', '') # => 'Layout/EndOfLine'
#
# @example fixes incorrect namespaces
#
- # cops = RuboCop::Cop::Cop.all
- # cops.qualified_cop_name('Lint/EndOfLine') # => 'Layout/EndOfLine'
+ # registry = RuboCop::Cop::Registry
+ # registry.qualified_cop_name('Lint/EndOfLine', '') # => 'Layout/EndOfLine'
#
# @example namespaces bare cop identifiers
#
- # cops = RuboCop::Cop::Cop.all
- # cops.qualified_cop_name('EndOfLine') # => 'Layout/EndOfLine'
+ # registry = RuboCop::Cop::Registry
+ # registry.qualified_cop_name('EndOfLine', '') # => 'Layout/EndOfLine'
#
# @example passes back unrecognized cop names
#
- # cops = RuboCop::Cop::Cop.all
- # cops.qualified_cop_name('NotACop') # => 'NotACop'
+ # 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
@@ -156,11 +156,11 @@
def disabled(config)
reject { |cop| enabled?(cop, config) }
end
def enabled?(cop, config)
- return true if options.fetch(:only, []).include?(cop.cop_name)
+ 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)
@@ -180,12 +180,16 @@
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.select { |cop| cop.department == department.to_sym }.map(&:cop_name)
+ cops_for_department(department).map(&:cop_name)
end
def ==(other)
cops == other.cops
end
@@ -207,9 +211,17 @@
# @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