Class: RuboCop::Cop::MessageAnnotator

Inherits:
Object
  • Object
show all
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_config, @options
).annotate('message', 'Cop/CopName')

See Also:

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, cop_config, options) ⇒ MessageAnnotator

Returns a new instance of 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_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



46
47
48
49
50
# File 'lib/rubocop/cop/message_annotator.rb', line 46

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

Class Attribute Details

.style_guide_urlsObject (readonly)

Returns the value of attribute style_guide_urls.



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

def style_guide_urls
  @style_guide_urls
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#cop_configObject (readonly)

Returns the value of attribute cop_config.



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

def cop_config
  @cop_config
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#annotate(message, name) ⇒ String

Returns the annotated message, based on params passed into initializer

Returns:

  • (String)

    annotated message



56
57
58
59
60
61
62
63
64
# 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

#urlsObject



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

def urls
  [style_guide_url, reference_url].compact
end