From 9443318b571952c9e8da6bc88c1c7d6804fa40c2 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Fri, 9 Dec 2022 10:07:54 -0500 Subject: [PATCH 1/9] Allow inherit_from to accept a glob --- ...new_allow_inherit_from_to_accept_a_glob.md | 1 + docs/modules/ROOT/pages/configuration.adoc | 7 +++ lib/rubocop/config_loader_resolver.rb | 10 +++- spec/rubocop/config_loader_spec.rb | 47 +++++++++++++++++++ 4 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 changelog/new_allow_inherit_from_to_accept_a_glob.md diff --git a/changelog/new_allow_inherit_from_to_accept_a_glob.md b/changelog/new_allow_inherit_from_to_accept_a_glob.md new file mode 100644 index 00000000000..1ff6b243cbe --- /dev/null +++ b/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][]) diff --git a/docs/modules/ROOT/pages/configuration.adoc b/docs/modules/ROOT/pages/configuration.adoc index a37dc237906..c7dc572ff88 100644 --- a/docs/modules/ROOT/pages/configuration.adoc +++ b/docs/modules/ROOT/pages/configuration.adoc @@ -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 diff --git a/lib/rubocop/config_loader_resolver.rb b/lib/rubocop/config_loader_resolver.rb index fd8d9a9e20b..020fa28ea07 100644 --- a/lib/rubocop/config_loader_resolver.rb +++ b/lib/rubocop/config_loader_resolver.rb @@ -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 diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index 199a1b34623..f9d48de6efc 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -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' } From 7d22c2793e09716bbca49e07a6fbbcc08348827f Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 12 Dec 2022 12:54:36 -0500 Subject: [PATCH 2/9] Always glob out --- lib/rubocop/config_loader_resolver.rb | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/rubocop/config_loader_resolver.rb b/lib/rubocop/config_loader_resolver.rb index 020fa28ea07..f3c6490ce04 100644 --- a/lib/rubocop/config_loader_resolver.rb +++ b/lib/rubocop/config_loader_resolver.rb @@ -206,13 +206,7 @@ def merge_hashes?(base_hash, derived_hash, key) end def base_configs(path, inherit_from, file) - inherit_froms = Array(inherit_from).compact.flat_map do |f| - if f.match?(/[*{\[?]/) - Dir.glob(f) - else - f - end - end + inherit_froms = Dir.glob(Array(inherit_from).compact) configs = inherit_froms.map do |f| ConfigLoader.load_file(inherited_file(path, f, file)) From ca8205aad00618dd16c594488f4cd9b92bc65b32 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 12 Dec 2022 12:56:15 -0500 Subject: [PATCH 3/9] rubocop --- spec/rubocop/config_loader_spec.rb | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index f9d48de6efc..427eaef0786 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -413,7 +413,6 @@ - 'spec/models/expense_spec.rb' YAML - create_file('packages/package_three/.rubocop_todo.yml', <<~YAML) Style/For: Exclude: @@ -423,11 +422,11 @@ 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'), - ]) + 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 From 929f62d1439df6ff453f28c4eaee65d56c17c30d Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 12 Dec 2022 14:39:57 -0500 Subject: [PATCH 4/9] Go back to using glob functionality --- lib/rubocop/config_loader_resolver.rb | 4 +++- lib/rubocop/path_util.rb | 7 ++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/rubocop/config_loader_resolver.rb b/lib/rubocop/config_loader_resolver.rb index f3c6490ce04..15086ac4f44 100644 --- a/lib/rubocop/config_loader_resolver.rb +++ b/lib/rubocop/config_loader_resolver.rb @@ -206,7 +206,9 @@ def merge_hashes?(base_hash, derived_hash, key) end def base_configs(path, inherit_from, file) - inherit_froms = Dir.glob(Array(inherit_from).compact) + inherit_froms = Array(inherit_from).compact.flat_map do |f| + PathUtils.glob?(f) ? Dir.glob(f) : f + end configs = inherit_froms.map do |f| ConfigLoader.load_file(inherited_file(path, f, file)) diff --git a/lib/rubocop/path_util.rb b/lib/rubocop/path_util.rb index 53e660ccc3c..8360d05fdd0 100644 --- a/lib/rubocop/path_util.rb +++ b/lib/rubocop/path_util.rb @@ -40,7 +40,7 @@ def match_path?(pattern, path) matches = if pattern == path true - elsif pattern.match?(/[*{\[?]/) + elsif glob?(pattern) File.fnmatch?(pattern, path, File::FNM_PATHNAME | File::FNM_EXTGLOB) end @@ -62,6 +62,11 @@ def absolute?(path) %r{\A([A-Z]:)?/}i.match?(path) end + # Returns true for a glob + def glob?(path) + path.match?(/[*{\[?]/) + end + def hidden_file_in_not_hidden_dir?(pattern, path) hidden_file?(path) && File.fnmatch?( From 093290c4d4ee50c81f2768a4b34a6ded8eb761f5 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 12 Dec 2022 14:53:02 -0500 Subject: [PATCH 5/9] fix typo --- lib/rubocop/config_loader_resolver.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rubocop/config_loader_resolver.rb b/lib/rubocop/config_loader_resolver.rb index 15086ac4f44..910c1273963 100644 --- a/lib/rubocop/config_loader_resolver.rb +++ b/lib/rubocop/config_loader_resolver.rb @@ -207,7 +207,7 @@ def merge_hashes?(base_hash, derived_hash, key) def base_configs(path, inherit_from, file) inherit_froms = Array(inherit_from).compact.flat_map do |f| - PathUtils.glob?(f) ? Dir.glob(f) : f + PathUtil.glob?(f) ? Dir.glob(f) : f end configs = inherit_froms.map do |f| From e57f3e5c8d05f01087d90aaa7262da3888f3abe5 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Mon, 19 Dec 2022 08:53:29 -0500 Subject: [PATCH 6/9] code review --- docs/modules/ROOT/pages/configuration.adoc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/modules/ROOT/pages/configuration.adoc b/docs/modules/ROOT/pages/configuration.adoc index c7dc572ff88..03b3616d8a3 100644 --- a/docs/modules/ROOT/pages/configuration.adoc +++ b/docs/modules/ROOT/pages/configuration.adoc @@ -115,12 +115,15 @@ inherit_from: ---- `inherit_from` also accepts a glob, for example: + [source,yaml] ---- inherit_from: - packages/*/.rubocop_todo.yml ---- +The example above is one potential use-case: allowing components within your repo to organize their own `.rubocop_todo.yml` files. + == Inheriting configuration from a remote URL The optional `inherit_from` directive can contain a full url to a remote From e1d0c02a506b451d743cf469a03f2738e84d7e8b Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Tue, 20 Dec 2022 14:25:49 -0500 Subject: [PATCH 7/9] rubocop --- spec/rubocop/config_loader_spec.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index 427eaef0786..efca20c83a6 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -421,12 +421,13 @@ 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') - ]) + expected = [ + 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') + ] + expect(configuration_from_file['Style/For']['Exclude']).to match_array(expected) end end From e5bafbee90297147eadbeddc501e32570c6caa8b Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Wed, 21 Dec 2022 08:34:41 -0500 Subject: [PATCH 8/9] test --- blah | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 blah diff --git a/blah b/blah new file mode 100644 index 00000000000..e69de29bb2d From 770dc77828bf4656b576081eccaf733ad03262f9 Mon Sep 17 00:00:00 2001 From: Alex Evanczuk Date: Wed, 21 Dec 2022 08:34:44 -0500 Subject: [PATCH 9/9] Revert "test" This reverts commit e5bafbee90297147eadbeddc501e32570c6caa8b. --- blah | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 blah diff --git a/blah b/blah deleted file mode 100644 index e69de29bb2d..00000000000