Class: RuboCop::Cop::InternalAffairs::UndefinedConfig

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/internal_affairs/undefined_config.rb

Overview

Looks for references to a cop configuration key that isn't defined in config/default.yml.

Constant Summary collapse

ALLOWED_CONFIGURATIONS =
%w[
  Safe SafeAutoCorrect AutoCorrect Severity StyleGuide Details Reference Include Exclude
].freeze
RESTRICT_ON_SEND =
%i[[] fetch].freeze
MSG =
'`%<name>s` is not defined in the configuration for `%<cop>s` ' \
'in `config/default.yml`.'

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods inherited from Base

#add_global_offense, #add_offense, autocorrect_incompatible_with, badge, #callbacks_needed, callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?

Methods included from RuboCop::Cop::IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#cop_class_def(node) ⇒ Object


16
17
18
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 16

def_node_search :cop_class_def, <<~PATTERN
  (class _ (const _ {:Base :Cop}) ...)
PATTERN

#cop_config_accessor?(node) ⇒ Object


21
22
23
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 21

def_node_matcher :cop_config_accessor?, <<~PATTERN
  (send (send nil? :cop_config) {:[] :fetch} ${str sym}...)
PATTERN

#on_new_investigationObject


25
26
27
28
29
30
31
32
33
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 25

def on_new_investigation
  super
  return unless processed_source.ast

  cop_class = cop_class_def(processed_source.ast).first
  return unless (@cop_class_name = extract_cop_name(cop_class))

  @config_for_cop = RuboCop::ConfigLoader.default_configuration.for_cop(@cop_class_name)
end

#on_send(node) ⇒ Object


35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 35

def on_send(node)
  return unless cop_class_name
  return unless (config_name_node = cop_config_accessor?(node))
  return if always_allowed?(config_name_node)
  return if configuration_key_defined?(config_name_node)

  message = format(MSG, name: config_name_node.value, cop: cop_class_name)
  add_offense(config_name_node, message: message)
end