class RuboCop::Formatter::CheckstyleFormatter

This formatter reports in Checkstyle format.

Public Instance Methods

file_finished(file, offences) click to toggle source
# File lib/rubocop/formatter/checkstyle_formatter.rb, line 19
def file_finished(file, offences)
  REXML::Element.new('file', @checkstyle).tap do |f|
    path_name = file
    path_name = relative_path(path_name) if !ENV.has_key?('RUBOCOP_CHECKSTYLE_FORMATTER_ABSOLUTE_PATH') && defined?(relative_path)
    f.attributes['name'] = path_name
    add_offences(f, offences)
  end
end
finished(_inspected_files) click to toggle source
# File lib/rubocop/formatter/checkstyle_formatter.rb, line 28
def finished(_inspected_files)
  @document.write(output, 2)
end
started(_target_file) click to toggle source
# File lib/rubocop/formatter/checkstyle_formatter.rb, line 12
def started(_target_file)
  @document = REXML::Document.new.tap do |d|
    d << REXML::XMLDecl.new
  end
  @checkstyle = REXML::Element.new('checkstyle', @document)
end

Private Instance Methods

add_offences(parent, offences) click to toggle source
# File lib/rubocop/formatter/checkstyle_formatter.rb, line 34
def add_offences(parent, offences)
  offences.each do |offence|
    REXML::Element.new('error', parent).tap do |e|
      e.add_attributes(offence_attributes(offence))
    end
  end
end
offence_attributes(offence) click to toggle source
# File lib/rubocop/formatter/checkstyle_formatter.rb, line 42
def offence_attributes(offence)
  {
    'line' => offence.line,
    'column' => offence.column,
    'severity' => to_checkstyle_severity(offence.severity.to_s),
    'message' => offence.message,
    'source' => 'com.puppycrawl.tools.checkstyle.' + offence.cop_name
  }
end
to_checkstyle_severity(rubocop_severity) click to toggle source

TODO be able to configure severity mapping

# File lib/rubocop/formatter/checkstyle_formatter.rb, line 53
def to_checkstyle_severity(rubocop_severity)
  case rubocop_severity.to_s
  when 'fatal', 'error' then 'error'
  when 'warning' then 'warning'
  when 'convention', 'refactor' then 'info'
  else 'warning'
  end
end