Skip to content

Commit

Permalink
Fix an error when using changed_parameters by external library
Browse files Browse the repository at this point in the history
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 `<top (required)>'
```
  • Loading branch information
koic committed Jul 24, 2022
1 parent 4537491 commit dd9c99e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
@@ -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][])
9 changes: 7 additions & 2 deletions lib/rubocop/config_obsoletion.rb
Expand Up @@ -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

Expand Down
23 changes: 23 additions & 0 deletions spec/rubocop/config_obsoletion_spec.rb
Expand Up @@ -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

0 comments on commit dd9c99e

Please sign in to comment.