Class: RuboCop::ConfigObsoletion Private
Overview
This class handles obsolete configuration.
Constant Summary
-
COP_RULE_CLASSES =
# File 'lib/rubocop/config_obsoletion.rb', line 8{ 'renamed' => RenamedCop, 'removed' => RemovedCop, 'split' => SplitCop, 'extracted' => ExtractedCop }.freeze -
DEFAULT_RULES_FILE =
# File 'lib/rubocop/config_obsoletion.rb', line 7File.join(ConfigLoader::RUBOCOP_HOME, 'config', 'obsoletion.yml')
-
LOAD_RULES_CACHE =
private
# File 'lib/rubocop/config_obsoletion.rb', line 18
— a cache, written to at runtime
{} -
PARAMETER_RULE_CLASSES =
# File 'lib/rubocop/config_obsoletion.rb', line 14{ 'changed_parameters' => ChangedParameter, 'changed_enforced_styles' => ChangedEnforcedStyles }.freeze
Class Attribute Summary
- .files rw Internal use only
Class Method Summary
- .deprecated_cop_name?(name) ⇒ Boolean Internal use only
- .deprecated_names_for(cop) Internal use only
- .global Internal use only
- .legacy_cop_names Internal use only
- .new(config) ⇒ ConfigObsoletion constructor Internal use only
- .reset! Internal use only
- .rules_cache_key Internal use only
Instance Attribute Summary
Instance Method Summary
- #deprecated_cop_name?(name) ⇒ Boolean Internal use only
- #legacy_cop_names Internal use only
-
#messages
Internal use only
Messages for every violated rule, regardless of severity, without raising.
- #reject_obsolete! Internal use only
- #cop_rules private Internal use only
-
#load_cop_rules(rules)
private
Internal use only
Cop rules are keyed by the name of the original cop.
-
#load_parameter_rules(rules)
private
Internal use only
Parameter rules may apply to multiple cops and multiple parameters and are given as an array.
-
#load_rules
private
Internal use only
Default rules for obsoletions are in config/obsoletion.yml Additional rules files can be added with
RuboCop::ConfigObsoletion.files << filename. - #obsoletions private Internal use only
Constructor Details
.new(config) ⇒ ConfigObsoletion
# File 'lib/rubocop/config_obsoletion.rb', line 66
def initialize(config) @config = config @rules = load_rules @warnings = [] end
Class Attribute Details
.files (rw)
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 24
attr_accessor :files
Class Method Details
.deprecated_cop_name?(name) ⇒ Boolean
# File 'lib/rubocop/config_obsoletion.rb', line 45
def deprecated_cop_name?(name) global.deprecated_cop_name?(name) end
.deprecated_names_for(cop)
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 49
def deprecated_names_for(cop) @deprecated_names ||= {} return @deprecated_names[cop] if @deprecated_names.key?(cop) @deprecated_names[cop] = global.rules.filter_map do |rule| next unless rule.cop_rule? next unless rule.respond_to?(:new_name) next unless rule.new_name == cop rule.old_name end end
.global
[ GitHub ].legacy_cop_names
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 40
def legacy_cop_names # Used by DepartmentName#qualified_legacy_cop_name global.legacy_cop_names end
.reset!
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 30
def reset! @global = nil @deprecated_names = {} LOAD_RULES_CACHE[rules_cache_key] = nil end
.rules_cache_key
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 36
def rules_cache_key files.hash end
Instance Attribute Details
#rules (readonly)
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 21
attr_reader :rules, :warnings
#warnings (readonly)
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 21
attr_reader :rules, :warnings
Instance Method Details
#cop_rules (private)
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 160
def cop_rules rules.select(&:cop_rule?) end
#deprecated_cop_name?(name) ⇒ Boolean
# File 'lib/rubocop/config_obsoletion.rb', line 93
def deprecated_cop_name?(name) legacy_cop_names.include?(name) end
#legacy_cop_names
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 88
def legacy_cop_names # Used by DepartmentName#qualified_legacy_cop_name cop_rules.map(&:old_name) end
#load_cop_rules(rules) (private)
Cop rules are keyed by the name of the original cop
# File 'lib/rubocop/config_obsoletion.rb', line 121
def load_cop_rules(rules) rules.flat_map do |rule_type, data| data.filter_map do |cop_name, configuration| next unless configuration # allow configurations to be disabled with `CopName: ~` COP_RULE_CLASSES[rule_type].new(@config, cop_name, configuration) end end end
#load_parameter_rules(rules) (private)
Parameter rules may apply to multiple cops and multiple parameters and are given as an array. Each combination is turned into a separate rule object.
# File 'lib/rubocop/config_obsoletion.rb', line 134
def load_parameter_rules(rules) rules.flat_map do |rule_type, data| data.flat_map do |configuration| cops = Array(configuration['cops']) parameters = Array(configuration['parameters']) cops.product(parameters).map do |cop, parameter| PARAMETER_RULE_CLASSES[rule_type].new(@config, cop, parameter, configuration) end end end end
#load_rules (private)
Default rules for obsoletions are in config/obsoletion.yml
Additional rules files can be added with RuboCop::ConfigObsoletion.files << filename
# File 'lib/rubocop/config_obsoletion.rb', line 101
def load_rules # rubocop:disable Metrics/AbcSize rules = LOAD_RULES_CACHE[self.class.rules_cache_key] ||= self.class.files.each_with_object({}) do |filename, hash| hash.merge!(YAML.safe_load(File.read(filename)) || {}) do |_key, first, second| case first when Hash first.merge(second) when Array first.concat(second) end end end cop_rules = rules.slice(*COP_RULE_CLASSES.keys) parameter_rules = rules.slice(*PARAMETER_RULE_CLASSES.keys) load_cop_rules(cop_rules).concat(load_parameter_rules(parameter_rules)) end
#messages
Messages for every violated rule, regardless of severity, without raising. Used for configuration the user does not own (a plugin’s defaults), where an obsolete key should be reported but must not stop the run. Extracted cops are left out: whether their gem is loaded is only known once every plugin has been integrated, and this is called while plugins are still loading.
# File 'lib/rubocop/config_obsoletion.rb', line 84
def rules.grep_v(ExtractedCop).select(&:violated?).map(&:) end
#obsoletions (private)
[ GitHub ]# File 'lib/rubocop/config_obsoletion.rb', line 147
def obsoletions rules.map do |rule| next unless rule.violated? if rule.warning? @warnings.push(rule.) next end rule. end end
#reject_obsolete!
# File 'lib/rubocop/config_obsoletion.rb', line 72
def reject_obsolete! = obsoletions.flatten.compact return if .empty? raise ValidationError, .join("\n") end