diff --git a/changelog/fix_an_error_when_using_changed_parameters_by_external_library.md b/changelog/fix_an_error_when_using_changed_parameters_by_external_library.md new file mode 100644 index 00000000000..8b1433ce8e4 --- /dev/null +++ b/changelog/fix_an_error_when_using_changed_parameters_by_external_library.md @@ -0,0 +1 @@ +* [#10831](https://github.com/rubocop/rubocop/pull/10831): Fix an error when using `changed_parameters` in obsoletion.yml by external library. ([@koic][]) diff --git a/lib/rubocop/config_obsoletion.rb b/lib/rubocop/config_obsoletion.rb index d78f536cb92..d3ae3950a0b 100644 --- a/lib/rubocop/config_obsoletion.rb +++ b/lib/rubocop/config_obsoletion.rb @@ -47,10 +47,15 @@ def reject_obsolete! # Default rules for obsoletions are in config/obsoletion.yml # Additional rules files can be added with `RuboCop::ConfigObsoletion.files << filename` - def load_rules + def load_rules # rubocop:disable Metrics/AbcSize rules = self.class.files.each_with_object({}) do |filename, hash| hash.merge!(YAML.safe_load(File.read(filename))) do |_key, first, second| - first.merge(second) + case first + when Hash + first.merge(second) + when Array + first.concat(second) + end end end diff --git a/spec/rubocop/config_obsoletion_spec.rb b/spec/rubocop/config_obsoletion_spec.rb index 3f544283a8a..a71154597ca 100644 --- a/spec/rubocop/config_obsoletion_spec.rb +++ b/spec/rubocop/config_obsoletion_spec.rb @@ -573,5 +573,28 @@ expect { config_obsoletion.reject_obsolete! }.not_to raise_error end end + + context 'when using `changed_parameters` by an external library' do + after { described_class.files = [described_class::DEFAULT_RULES_FILE] } + + let(:hash) { {} } + let(:external_obsoletions) do + create_file('external/obsoletions.yml', <<~YAML) + changed_parameters: + - cops: Rails/FindEach + parameters: IgnoredMethods + alternatives: + - AllowedMethods + - AllowedPatterns + severity: warning + YAML + end + + it 'allows the extracted cops' do + described_class.files << external_obsoletions + + expect { config_obsoletion.reject_obsolete! }.not_to raise_error + end + end end end