123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Formatter::MarkdownFormatter

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RuboCop::Formatter::BaseFormatter
Defined in: lib/rubocop/formatter/markdown_formatter.rb

Overview

This formatter displays the report data in markdown

Constant Summary

::RuboCop::PathUtil - Included

HIDDEN_FILE_PATTERN, SMART_PATH_CACHE

Class Method Summary

Instance Attribute Summary

BaseFormatter - Inherited

Instance Method Summary

::RuboCop::PathUtil - Included

#absolute?

Returns true for an absolute Unix or Windows path.

#glob?

Returns true for a glob.

#hidden_dir?, #hidden_file?, #hidden_file_in_not_hidden_dir?,
#match_path?

Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.

#maybe_hidden_file?

Loose check to reduce memory allocations.

#relative_path, #smart_path

TextUtil - Included

BaseFormatter - Inherited

#file_finished

Invoked at the end of inspecting each files.

#file_started

Invoked at the beginning of inspecting each files.

#finished

Invoked after all files are inspected or interrupted by user.

#started

Invoked once before any files are inspected.

Constructor Details

.new(output, options = {}) ⇒ MarkdownFormatter

[ GitHub ]

  
# 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

Instance Attribute Details

#files (readonly)

[ GitHub ]

  
# File 'lib/rubocop/formatter/markdown_formatter.rb', line 9

attr_reader :files, :summary

#summary (readonly)

[ GitHub ]

  
# File 'lib/rubocop/formatter/markdown_formatter.rb', line 9

attr_reader :files, :summary

Instance Method Details

#file_finished(file, offenses)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# File 'lib/rubocop/formatter/markdown_formatter.rb', line 26

def finished(inspected_files)
  summary.inspected_files = inspected_files
  render_markdown
end

#possible_ellipses(location) (private)

[ GitHub ]

  
# File 'lib/rubocop/formatter/markdown_formatter.rb', line 73

def possible_ellipses(location)
  location.single_line? ? '' : ' ...'
end

#render_markdown (private)

[ GitHub ]

  
# 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

#started(target_files)

[ GitHub ]

  
# File 'lib/rubocop/formatter/markdown_formatter.rb', line 17

def started(target_files)
  summary.target_files = target_files
end

#write_code(offense) (private)

[ GitHub ]

  
# 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) (private)

[ GitHub ]

  
# 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 (private)

[ GitHub ]

  
# 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) (private)

[ GitHub ]

  
# 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