diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f05d3256ecb..ab5b8edca978 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * [#7274](https://github.com/rubocop-hq/rubocop/issues/7274): Add new `Lint/SendWithMixinArgument` cop. ([@koic][]) * [#7272](https://github.com/rubocop-hq/rubocop/pull/7272): Show warning message if passed string to `Enabled`, `Safe`, `SafeAutocorrect`, and `AutoCorrect` keys in .rubocop.yml. ([@unasuke][]) +* [#7295](https://github.com/rubocop-hq/rubocop/pull/7295): Make it possible to set `StyleGuideBaseURL` per department. ([@koic][]) ### Bug fixes diff --git a/lib/rubocop/config.rb b/lib/rubocop/config.rb index fe7bcd7e19d5..2d1c7364ba9a 100644 --- a/lib/rubocop/config.rb +++ b/lib/rubocop/config.rb @@ -96,6 +96,10 @@ def for_cop(cop) @for_cop[cop.respond_to?(:cop_name) ? cop.cop_name : cop] end + def for_department(department_name) + @for_cop[department_name] + end + def for_all_cops @for_all_cops ||= self['AllCops'] || {} end diff --git a/lib/rubocop/cop/cop.rb b/lib/rubocop/cop/cop.rb index 89047d406b04..f3beb763fd4b 100644 --- a/lib/rubocop/cop/cop.rb +++ b/lib/rubocop/cop/cop.rb @@ -224,8 +224,8 @@ def excluded_file?(file) def annotate(message) RuboCop::Cop::MessageAnnotator.new( - config, cop_config, @options - ).annotate(message, name) + config, cop_name, cop_config, @options + ).annotate(message) end def file_name_matches_any?(file, parameter, default_result) diff --git a/lib/rubocop/cop/message_annotator.rb b/lib/rubocop/cop/message_annotator.rb index b762528c5d16..8bd671a36518 100644 --- a/lib/rubocop/cop/message_annotator.rb +++ b/lib/rubocop/cop/message_annotator.rb @@ -9,11 +9,11 @@ module Cop # # @example # RuboCop::Cop::MessageAnnotator.new( - # config, cop_config, @options - # ).annotate('message', 'Cop/CopName') + # config, cop_name, cop_config, @options + # ).annotate('message') # #=> 'Cop/CopName: message (http://example.org/styleguide)' class MessageAnnotator - attr_reader :options, :config, :cop_config + attr_reader :options, :config, :cop_name, :cop_config @style_guide_urls = {} @@ -29,6 +29,7 @@ class << self # :ExtraDetails [Boolean] Include cop details # :DisplayCopNames [Boolean] Include cop name # + # @param [String] cop_name for specific cop name # @param [Hash] cop_config configs for specific cop, from config#for_cop # @option cop_config [String] :StyleGuide Extension of base styleguide URL # @option cop_config [String] :Reference Full reference URL @@ -43,8 +44,9 @@ class << self # Include debug output # @option options [Boolean] :display_cop_names # Include cop name - def initialize(config, cop_config, options) + def initialize(config, cop_name, cop_config, options) @config = config + @cop_name = cop_name @cop_config = cop_config || {} @options = options end @@ -53,8 +55,8 @@ def initialize(config, cop_config, options) # based on params passed into initializer # # @return [String] annotated message - def annotate(message, name) - message = "#{name}: #{message}" if display_cop_names? + def annotate(message) + message = "#{cop_name}: #{message}" if display_cop_names? message += " #{details}" if extra_details? && details if display_style_guide? links = urls.join(', ') @@ -74,7 +76,7 @@ def style_guide_url return nil if url.nil? || url.empty? self.class.style_guide_urls[url] ||= begin - base_url = config.for_all_cops['StyleGuideBaseURL'] + base_url = style_guide_base_url if base_url.nil? || base_url.empty? url else @@ -83,6 +85,13 @@ def style_guide_url end end + def style_guide_base_url + department_name = cop_name.split('/').first + + config.for_department(department_name)['StyleGuideBaseURL'] || + config.for_all_cops['StyleGuideBaseURL'] + end + def display_style_guide? (options[:display_style_guide] || config.for_all_cops['DisplayStyleGuide']) && diff --git a/spec/rubocop/cop/message_annotator_spec.rb b/spec/rubocop/cop/message_annotator_spec.rb index 6750d9a9e62f..e6edfcc617be 100644 --- a/spec/rubocop/cop/message_annotator_spec.rb +++ b/spec/rubocop/cop/message_annotator_spec.rb @@ -3,11 +3,14 @@ RSpec.describe RuboCop::Cop::MessageAnnotator do let(:options) { {} } let(:config) { RuboCop::Config.new({}) } - let(:annotator) { described_class.new(config, config['Cop/Cop'], options) } + let(:cop_name) { 'Cop/Cop' } + let(:annotator) do + described_class.new(config, cop_name, config[cop_name], options) + end describe '#annotate' do subject(:annotate) do - annotator.annotate('message', 'Cop/Cop') + annotator.annotate('message') end context 'with default options' do @@ -55,9 +58,10 @@ describe 'with style guide url' do subject(:annotate) do - annotator.annotate('', 'Cop/Cop') + annotator.annotate('') end + let(:cop_name) { 'Cop/Cop' } let(:options) do { display_style_guide: true @@ -103,6 +107,28 @@ expect(annotate).to include('http://example.org/styleguide#target_based_url') end + context 'when a department other than AllCops is specified' do + let(:config) do + RuboCop::Config.new( + 'AllCops' => { + 'StyleGuideBaseURL' => 'http://example.org/styleguide' + }, + 'Foo' => { + 'StyleGuideBaseURL' => 'http://foo.example.org' + } + ) + end + + let(:cop_name) { 'Foo/Cop' } + let(:urls) { annotator.urls } + + it 'returns style guide url when it is specified' do + config['Foo/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') diff --git a/tasks/cops_documentation.rake b/tasks/cops_documentation.rake index bd93ffe310d6..0624f38910e5 100644 --- a/tasks/cops_documentation.rake +++ b/tasks/cops_documentation.rake @@ -155,7 +155,9 @@ task generate_cops_documentation: :yard_for_generate_documentation do def references(config, cop) cop_config = config.for_cop(cop) - urls = RuboCop::Cop::MessageAnnotator.new(config, cop_config, {}).urls + urls = RuboCop::Cop::MessageAnnotator.new( + config, cop.name, cop_config, {} + ).urls return '' if urls.empty? content = h3('References')