class RuboCop::Formatter::ViewerFormatter

This formatter formats the report data in Vue based interactive viewer.

Constants

APP_JS_PATH
TEMPLATE_PATH

Attributes

output_hash[R]

Public Class Methods

new(output, options = {}) click to toggle source
Calls superclass method
# File lib/rubocop/formatter/viewer_formatter.rb, line 18
def initialize(output, options = {})
  super
  @output_hash = {
      metadata: metadata_hash,
      files:    [],
      summary:  {offense_count: 0},
  }
end

Public Instance Methods

file_finished(file, offenses) click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 31
def file_finished(file, offenses)
  output_hash[:files] << hash_for_file(file, offenses)
  output_hash[:summary][:offense_count] += offenses.count
end
finished(inspected_files) click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 36
def finished(inspected_files)
  output_hash[:summary][:inspected_file_count] = inspected_files.count

  render_html
end
hash_for_file(file, offenses) click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 83
def hash_for_file(file, offenses)
  {
      path:     smart_path(file),
      offenses: offenses.map { |o| hash_for_offense(o) },
  }
end
hash_for_location(offense) click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 100
def hash_for_location(offense)
  {
      source:       source_lines(offense),
      start_line:   offense.line,
      last_line:    offense.last_line,
      start_column: offense.column,
      last_column:  offense.last_column,
      length:       offense.location.length,
  }
end
hash_for_offense(offense) click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 90
def hash_for_offense(offense)
  {
      severity:  offense.severity.name,
      message:   offense.message,
      cop_name:  offense.cop_name,
      corrected: offense.corrected?,
      location:  hash_for_location(offense),
  }
end
metadata_hash() click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 73
def metadata_hash
  {
      rubocop_version: RuboCop::Version::STRING,
      ruby_engine:     RUBY_ENGINE,
      ruby_version:    RUBY_VERSION,
      ruby_patchlevel: RUBY_PATCHLEVEL.to_s,
      ruby_platform:   RUBY_PLATFORM,
  }
end
render_html() click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 42
def render_html
  app_js = File.read(APP_JS_PATH, encoding: Encoding::UTF_8)

  context = ERBContext.new(output_hash, app_js)

  template = File.read(TEMPLATE_PATH, encoding: Encoding::UTF_8)

  erb = ERB.new(template)

  html = erb.result(context.binding)

  output.write html
end
source_lines(offense) click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 111
def source_lines(offense)
  (offense.line..offense.last_line).map do |line_num|
    offense.location.source_buffer.source_line(line_num)
  end.join("\n")
end
started(target_files) click to toggle source
# File lib/rubocop/formatter/viewer_formatter.rb, line 27
def started(target_files)
  output_hash[:summary][:target_file_count] = target_files.count
end