Skip to content

Commit

Permalink
Load only project config for InternalAffairs/UndefinedConfig
Browse files Browse the repository at this point in the history
Going through the default config is fine for RuboCop itself but
extensions need to inject their config for RuboCop to pick them up.

This makes it necessary to load the extension, even if the extension itself
doesn't use it, like `rubocop-rails` or `rubocop-capybara`.

Instead just load config/default.yml from `Dir.pwd` in isolation and use just that.
This would make rubocop/rubocop-capybara#133 and rubocop/rubocop-rails@288c7ce unnecessary

I tested this against a bunch of extensions and the cop seems to continue its job.
It also fixes a false positive in `rubocop-factory_bot`
  • Loading branch information
Earlopain committed Aug 19, 2024
1 parent 33e6431 commit b25b498
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/rubocop/cop/internal_affairs/undefined_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '`%<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 File.exist?(CONFIG_PATH)
ConfigLoader.load_yaml_configuration(CONFIG_PATH)
else
{}
end

# @!method cop_class_def(node)
def_node_search :cop_class_def, <<~PATTERN
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions spec/rubocop/cop/internal_affairs/undefined_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b25b498

Please sign in to comment.