Skip to content

Commit

Permalink
Fix StyleGuideBaseURL not functioning with nested departments
Browse files Browse the repository at this point in the history
Currently you can set the StyleGuideBaseURL config for cops that ship
within RuboCop or other RuboCop plugins that use a DEPT/COP format. If
the project has nested departments this doesn't work as the message
annotator does not properly split the cop name to determine the
department to lookup.

With this change just the cop name is split from the full DEPT/COP
string allowing the StyleGuideBaseURL to function with nested
departments.

We have nested departments in Cookstyle like `Chef/Deprecations/Foo` and
we'd like to be able to enable this config by default so we can point
our users at our documentation.

Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 authored and bbatsov committed Jan 31, 2021
1 parent 5870d6d commit 3ef92bb
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/fix_fix_styleguidebaseurl_not_functioning.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* [#9452](https://github.com/rubocop-hq/rubocop/pull/9452): Fix `StyleGuideBaseURL` not functioning with nested departments. ([@tas50][])
5 changes: 4 additions & 1 deletion lib/rubocop/cop/message_annotator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,11 @@ def style_guide_url
end
end

# Returns the base style guide URL from AllCops or the specific department
#
# @return [String] style guide URL
def style_guide_base_url
department_name = cop_name.split('/').first
department_name = cop_name.split('/')[0..-2].join('/')

config.for_department(department_name)['StyleGuideBaseURL'] ||
config.for_all_cops['StyleGuideBaseURL']
Expand Down
22 changes: 22 additions & 0 deletions spec/rubocop/cop/message_annotator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@
end
end

context 'when a nested department is specified' do
let(:config) do
RuboCop::Config.new(
'AllCops' => {
'StyleGuideBaseURL' => 'http://example.org/styleguide'
},
'Foo/Bar' => {
'StyleGuideBaseURL' => 'http://foo.example.org'
}
)
end

let(:cop_name) { 'Foo/Bar/Cop' }
let(:urls) { annotator.urls }

it 'returns style guide url when it is specified' do
config['Foo/Bar/Cop'] = { 'StyleGuide' => '#target_style_guide' }

expect(urls).to eq(%w[http://foo.example.org#target_style_guide])
end
end

it 'can use a path-based setting' do
config['Cop/Cop'] = { 'StyleGuide' => 'cop/path/rule#target_based_url' }
expect(annotate).to include('http://example.org/cop/path/rule#target_based_url')
Expand Down

0 comments on commit 3ef92bb

Please sign in to comment.