From dd9c99e3e405c535460697b39d1940ddb42b8acf Mon Sep 17 00:00:00 2001 From: Koichi ITO Date: Mon, 25 Jul 2022 00:33:22 +0900 Subject: [PATCH] Fix an error when using `changed_parameters` by external library This PR fixes the following error when using `changed_parameters` in obsoletion.yml by external library. ```console NoMethodError: undefined method `merge' for [{"cops"=>["Layout/SpaceAroundOperators", "Style/SpaceAroundOperators"], "parameters"=>"MultiSpaceAllowedForOperators", "reason"=>"If your intention was to allow extra spaces for alignment, please use `AllowFoy first.merge(second) ^^^^^^ /Users/koic/src/github.com/rubocop/rubocop-rails/lib/rubocop/rails/inject.rb:11:in `new' /Users/koic/src/github.com/rubocop/rubocop-rails/lib/rubocop/rails/inject.rb:11:in `defaults!' /Users/koic/src/github.com/rubocop/rubocop-rails/lib/rubocop-rails.rb:13:in `' ``` --- ..._changed_parameters_by_external_library.md | 1 + lib/rubocop/config_obsoletion.rb | 9 ++++++-- spec/rubocop/config_obsoletion_spec.rb | 23 +++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 changelog/fix_an_error_when_using_changed_parameters_by_external_library.md 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