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

Inherits:
Base
  • Object
show all
Extended by:
FileFinder
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`.'
CONFIG_PATH =
find_file_upwards('config/default.yml', Dir.pwd)
CONFIG =
if CONFIG_PATH
  begin
    original_debug = ConfigLoader.debug
    ConfigLoader.debug = false
    ConfigLoader.load_yaml_configuration(CONFIG_PATH)
  ensure
    ConfigLoader.debug = original_debug
  end
else
  {}
end

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from FileFinder

find_file_upwards, find_last_file_upwards, traverse_directories_upwards

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?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #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


32
33
34
35
36
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 32

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


39
40
41
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 39

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

#on_new_investigationObject


43
44
45
46
47
48
49
50
51
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 43

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 = CONFIG[@cop_class_name] || {}
end

#on_send(node) ⇒ Object


53
54
55
56
57
58
59
60
61
# File 'lib/rubocop/cop/internal_affairs/undefined_config.rb', line 53

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