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

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, cop_name, #cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_other_file, #parse, #parser_engine, #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?, #autocorrect_with_disable_uncorrectable?, #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
19
20
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 16

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

#cop_config_accessor?(node) ⇒ Object



23
24
25
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 23

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

#on_new_investigationObject



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

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



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

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