class RuboCop::Formatter::MarkdownFormatter

This formatter displays the report data in markdown

Attributes

files[R]
summary[R]

Public Class Methods

new(output, options = {}) click to toggle source
Calls superclass method RuboCop::Formatter::BaseFormatter::new
# File lib/rubocop/formatter/markdown_formatter.rb, line 11
def initialize(output, options = {})
  super
  @files = []
  @summary = Struct.new(:offense_count, :inspected_files, :target_files).new(0)
end

Public Instance Methods

file_finished(file, offenses) click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 21
def file_finished(file, offenses)
  files << Struct.new(:path, :offenses).new(file, offenses)
  summary.offense_count += offenses.count
end
finished(inspected_files) click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 26
def finished(inspected_files)
  summary.inspected_files = inspected_files
  render_markdown
end
started(target_files) click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 17
def started(target_files)
  summary.target_files = target_files
end

Private Instance Methods

possible_ellipses(location) click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 73
def possible_ellipses(location)
  location.single_line? ? '' : ' ...'
end
render_markdown() click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 33
def render_markdown
  n_files = pluralize(summary.inspected_files.count, 'file')
  n_offenses = pluralize(summary.offense_count, 'offense', no_for_zero: true)

  output.write "# RuboCop Inspection Report\n\n"
  output.write "#{n_files} inspected, #{n_offenses} detected:\n\n"
  write_file_messages
end
write_code(offense) click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 67
def write_code(offense)
  code = offense.location.source_line + possible_ellipses(offense.location)

  output.write "    ```rb\n    #{code}\n    ```\n\n" unless code.blank?
end
write_context(offense) click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 61
def write_context(offense)
  output.write(
    "  * **Line # #{offense.location.line} - #{offense.severity}:** #{offense.message}\n\n"
  )
end
write_file_messages() click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 42
def write_file_messages
  files.each do |file|
    next if file.offenses.empty?

    write_heading(file)
    file.offenses.each do |offense|
      write_context(offense)
      write_code(offense)
    end
  end
end
write_heading(file) click to toggle source
# File lib/rubocop/formatter/markdown_formatter.rb, line 54
def write_heading(file)
  filename = relative_path(file.path)
  n_offenses = pluralize(file.offenses.count, 'offense')

  output.write "### #{filename} - (#{n_offenses})\n"
end