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 20, 2024
1 parent 33e6431 commit c4a1c36
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 10 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,21 @@ module Cop
module InternalAffairs
# Looks for references to a cop configuration key that isn't defined in config/default.yml.
class UndefinedConfig < Base
# FileFinder is private API not expected to be used by cops but its fine for InternalAffairs
extend FileFinder

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
ConfigLoader.load_yaml_configuration(CONFIG_PATH)
else
{}
end

# @!method cop_class_def(node)
def_node_search :cop_class_def, <<~PATTERN
Expand All @@ -31,7 +40,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
10 changes: 10 additions & 0 deletions spec/rubocop/file_finder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
it 'returns nil when file is not found' do
expect(finder.find_file_upwards('file2', 'dir')).to be_nil
end

context 'when searching for a file inside a directory' do
it 'returns a file to be found upwards' do
expect(finder.find_file_upwards('dir/file', Dir.pwd)).to eq(File.expand_path('file', 'dir'))
end

it 'returns nil when file is not found' do
expect(finder.find_file_upwards('dir2/file', Dir.pwd)).to be_nil
end
end
end

describe '#find_last_file_upwards' do
Expand Down

0 comments on commit c4a1c36

Please sign in to comment.