Skip to content

Commit

Permalink
Allow inherit_from to accept a glob
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Evanczuk committed Dec 9, 2022
1 parent 3b4db02 commit 9443318
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/new_allow_inherit_from_to_accept_a_glob.md
@@ -0,0 +1 @@
* [#11261](https://github.com/rubocop/rubocop/pull/11261): Allow inherit_from to accept a glob. ([@alexevanczuk][])
7 changes: 7 additions & 0 deletions docs/modules/ROOT/pages/configuration.adoc
Expand Up @@ -114,6 +114,13 @@ inherit_from:
- ../conf/.rubocop.yml
----

`inherit_from` also accepts a glob, for example:
[source,yaml]
----
inherit_from:
- packages/*/.rubocop_todo.yml
----

== Inheriting configuration from a remote URL

The optional `inherit_from` directive can contain a full url to a remote
Expand Down
10 changes: 9 additions & 1 deletion lib/rubocop/config_loader_resolver.rb
Expand Up @@ -206,7 +206,15 @@ def merge_hashes?(base_hash, derived_hash, key)
end

def base_configs(path, inherit_from, file)
configs = Array(inherit_from).compact.map do |f|
inherit_froms = Array(inherit_from).compact.flat_map do |f|
if f.match?(/[*{\[?]/)
Dir.glob(f)
else
f
end
end

configs = inherit_froms.map do |f|
ConfigLoader.load_file(inherited_file(path, f, file))
end

Expand Down
47 changes: 47 additions & 0 deletions spec/rubocop/config_loader_spec.rb
Expand Up @@ -384,6 +384,53 @@
end
end

context 'when a file inherits from multiple files using a glob' do
let(:file_path) { '.rubocop.yml' }

before do
create_file(file_path, <<~YAML)
inherit_from:
- packages/*/.rubocop_todo.yml
inherit_mode:
merge:
- Exclude
Style/For:
Exclude:
- spec/requests/group_invite_spec.rb
YAML

create_file('packages/package_one/.rubocop_todo.yml', <<~YAML)
Style/For:
Exclude:
- 'spec/models/group_spec.rb'
YAML

create_file('packages/package_two/.rubocop_todo.yml', <<~YAML)
Style/For:
Exclude:
- 'spec/models/expense_spec.rb'
YAML


create_file('packages/package_three/.rubocop_todo.yml', <<~YAML)
Style/For:
Exclude:
- 'spec/models/order_spec.rb'
YAML
end

it 'gets the Exclude merging the inherited one' do
expect(configuration_from_file['Style/For']['Exclude']).to match_array([
File.expand_path('packages/package_two/spec/models/expense_spec.rb'),
File.expand_path('packages/package_one/spec/models/group_spec.rb'),
File.expand_path('packages/package_three/spec/models/order_spec.rb'),
File.expand_path('spec/requests/group_invite_spec.rb'),
])
end
end

context 'when a file inherits and overrides a hash with nil' do
let(:file_path) { '.rubocop.yml' }

Expand Down

0 comments on commit 9443318

Please sign in to comment.