diff --git a/lib/rubocop/cop/internal_affairs/undefined_config.rb b/lib/rubocop/cop/internal_affairs/undefined_config.rb index bb248c80c4bb1..2823ea206e9e0 100644 --- a/lib/rubocop/cop/internal_affairs/undefined_config.rb +++ b/lib/rubocop/cop/internal_affairs/undefined_config.rb @@ -5,12 +5,22 @@ module Cop module InternalAffairs # Looks for references to a cop configuration key that isn't defined in config/default.yml. class UndefinedConfig < Base + class << self + include FileFinder + end + ALLOWED_CONFIGURATIONS = %w[ Safe SafeAutoCorrect AutoCorrect Severity StyleGuide Details Reference Include Exclude ].freeze RESTRICT_ON_SEND = %i[[] fetch].freeze MSG = '`%s` is not defined in the configuration for `%s` ' \ 'in `config/default.yml`.' + CONFIG_PATH = find_file_upwards('config/default.yml', Dir.pwd) + CONFIG = if File.exist?(CONFIG_PATH) + ConfigLoader.load_yaml_configuration(CONFIG_PATH) + else + {} + end # @!method cop_class_def(node) def_node_search :cop_class_def, <<~PATTERN @@ -31,7 +41,7 @@ def on_new_investigation 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) + @config_for_cop = CONFIG[@cop_class_name] || {} end def on_send(node) diff --git a/spec/rubocop/cop/internal_affairs/undefined_config_spec.rb b/spec/rubocop/cop/internal_affairs/undefined_config_spec.rb index 210b6a0a85a69..6caf43842e603 100644 --- a/spec/rubocop/cop/internal_affairs/undefined_config_spec.rb +++ b/spec/rubocop/cop/internal_affairs/undefined_config_spec.rb @@ -12,9 +12,8 @@ Defined: true YAML - allow(RuboCop::ConfigLoader).to receive(:default_configuration).and_return( - RuboCop::ConfigLoader.load_file('config/default.yml', check: false) - ) + stub_const('RuboCop::Cop::InternalAffairs::UndefinedConfig::CONFIG', + RuboCop::ConfigLoader.load_yaml_configuration('config/default.yml')) end it 'does not register an offense for implicit configuration keys' do