123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Cop::MessageAnnotator

Relationships & Source Files
Inherits: Object
Defined in: lib/rubocop/cop/message_annotator.rb

Overview

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

#=> 'Cop/CopName: message (http://example.org/styleguide)'

Examples:

RuboCop::Cop::MessageAnnotator.new(
  config, cop_name, cop_config, @options
).annotate('message')

See Also:

  • #initialize

Class Attribute Summary

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(config, cop_name, cop_config, options) ⇒ MessageAnnotator

Parameters:

  • 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

  • cop_name (String)

    for specific cop name

  • cop_config (Hash)

    configs for specific cop, from config#for_cop

  • options (Hash, nil)

    optional

Options Hash (cop_config):

  • :StyleGuide (String)

    Extension of base styleguide URL

  • :Reference (String)

    Full reference URL

  • :Details (String)

Options Hash (options):

  • :display_style_guide (Boolean)

    Include style guide and reference URLs

  • :extra_details (Boolean)

    Include cop specific details

  • :debug (Boolean)

    Include debug output

  • :display_cop_names (Boolean)

    Include cop name

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 47

def initialize(config, cop_name, cop_config, options)
  @config = config
  @cop_name = cop_name
  @cop_config = cop_config || {}
  @options = options
end

Class Attribute Details

.style_guide_urls (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 21

attr_reader :style_guide_urls

Instance Attribute Details

#config (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 16

attr_reader :options, :config, :cop_name, :cop_config

#cop_config (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 16

attr_reader :options, :config, :cop_name, :cop_config

#cop_name (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 16

attr_reader :options, :config, :cop_name, :cop_config

#debug?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 111

def debug?
  options[:debug]
end

#display_cop_names?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 115

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?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 98

def display_style_guide?
  (options[:display_style_guide] || config.for_all_cops['DisplayStyleGuide']) && !urls.empty?
end

#extra_details?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 107

def extra_details?
  options[:extra_details] || config.for_all_cops['ExtraDetails']
end

#options (readonly)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 16

attr_reader :options, :config, :cop_name, :cop_config

Instance Method Details

#annotate(message) ⇒ String

Returns the annotated message, based on params passed into initializer

Returns:

  • (String)

    annotated message

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 58

def annotate(message)
  message = "#{cop_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

#details (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 124

def details
  details = cop_config && cop_config['Details']
  details.nil? || details.empty? ? nil : details
end

#reference_urls (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 102

def reference_urls
  urls = Array(cop_config['Reference'])
  urls.nil? || urls.empty? ? nil : urls.reject(&:empty?)
end

#style_guide_base_urlString (private)

Returns the base style guide URL from AllCops or the specific department

Returns:

  • (String)

    style guide URL

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 91

def style_guide_base_url
  department_name = cop_name.split('/')[0..-2].join('/')

  config.for_department(department_name)['StyleGuideBaseURL'] ||
    config.for_all_cops['StyleGuideBaseURL']
end

#style_guide_url (private)

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 74

def style_guide_url
  url = cop_config['StyleGuide']
  return nil if url.nil? || url.empty?

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

#urls

[ GitHub ]

  
# File 'lib/rubocop/cop/message_annotator.rb', line 68

def urls
  [style_guide_url, *reference_urls].compact
end