class RuboCop::Cop::MessageAnnotator

Message Annotator class annotates a basic offense message based on params passed into initializer.

@see initialize

@example

 RuboCop::Cop::MessageAnnotator.new(
   config, cop_config, @options
 ).annotate('message', 'Cop/CopName')
#=> 'Cop/CopName: message (http://example.org/styleguide)'

Attributes

style_guide_urls[R]
config[R]
cop_config[R]
options[R]

Public Class Methods

new(config, cop_config, options) click to toggle source

@param config [RuboCop::Config] Check configs for all cops

@note Message Annotator specifically checks the
  following config options for_all_cops
  :StyleGuideBaseURL [String] URL for styleguide
  :DisplayStyleGuide [Boolean] Include styleguide and reference URLs
  :ExtraDetails [Boolean] Include cop details
  :DisplayCopNames [Boolean] Include 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 @option cop_config [String] :Details

@param [Hash, nil] options optional @option options [Boolean] :display_style_guide

Include style guide and reference URLs

@option options [Boolean] :extra_details

Include cop specific details

@option options [Boolean] :debug

Include debug output

@option options [Boolean] :display_cop_names

Include cop name
# File lib/rubocop/cop/message_annotator.rb, line 46
def initialize(config, cop_config, options)
  @config = config
  @cop_config = cop_config || {}
  @options = options
end

Public Instance Methods

annotate(message, name) click to toggle source

Returns the annotated message, based on params passed into initializer

@return [String] annotated message

# File lib/rubocop/cop/message_annotator.rb, line 56
def annotate(message, name)
  message = "#{name}: #{message}" if display_cop_names?
  message += " #{details}" if extra_details? && details
  if display_style_guide?
    links = urls.join(', ')
    message = "#{message} (#{links})"
  end
  message
end
urls() click to toggle source
# File lib/rubocop/cop/message_annotator.rb, line 66
def urls
  [style_guide_url, *reference_urls].compact
end

Private Instance Methods

debug?() click to toggle source
# File lib/rubocop/cop/message_annotator.rb, line 101
def debug?
  options[:debug]
end
details() click to toggle source
# File lib/rubocop/cop/message_annotator.rb, line 114
def details
  details = cop_config && cop_config['Details']
  details.nil? || details.empty? ? nil : details
end
display_cop_names?() click to toggle source
# File lib/rubocop/cop/message_annotator.rb, line 105
def display_cop_names?
  return true if debug?
  return false if options[:display_cop_names] == false
  return true if options[:display_cop_names]
  return false if options[:format] == 'json'

  config.for_all_cops['DisplayCopNames']
end
display_style_guide?() click to toggle source
# File lib/rubocop/cop/message_annotator.rb, line 86
def display_style_guide?
  (options[:display_style_guide] ||
   config.for_all_cops['DisplayStyleGuide']) &&
    !urls.empty?
end
extra_details?() click to toggle source
# File lib/rubocop/cop/message_annotator.rb, line 97
def extra_details?
  options[:extra_details] || config.for_all_cops['ExtraDetails']
end
reference_urls() click to toggle source
# File lib/rubocop/cop/message_annotator.rb, line 92
def reference_urls
  urls = Array(cop_config['Reference'])
  urls.nil? || urls.empty? ? nil : urls.reject(&:empty?)
end
style_guide_url() click to toggle source
# File lib/rubocop/cop/message_annotator.rb, line 72
def style_guide_url
  url = cop_config['StyleGuide']
  return nil if url.nil? || url.empty?

  self.class.style_guide_urls[url] ||= begin
    base_url = config.for_all_cops['StyleGuideBaseURL']
    if base_url.nil? || base_url.empty?
      url
    else
      URI.join(base_url, url).to_s
    end
  end
end