Class: RuboCop::Formatter::HTMLFormatter

Inherits:
BaseFormatter show all
Defined in:
lib/rubocop/formatter/html_formatter.rb

Overview

This formatter saves the output as an html file.

Defined Under Namespace

Classes: Color, ERBContext

Constant Summary collapse

ELLIPSES =
'<span class="extra-code">...</span>'.freeze
TEMPLATE_PATH =
File.expand_path('../../../assets/output.html.erb', __dir__)

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#options, #output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#file_started

Constructor Details

#initialize(output, options = {}) ⇒ HTMLFormatter



31
32
33
34
35
# File 'lib/rubocop/formatter/html_formatter.rb', line 31

def initialize(output, options = {})
  super
  @files = []
  @summary = OpenStruct.new(offense_count: 0)
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files



29
30
31
# File 'lib/rubocop/formatter/html_formatter.rb', line 29

def files
  @files
end

#summaryObject (readonly)

Returns the value of attribute summary



29
30
31
# File 'lib/rubocop/formatter/html_formatter.rb', line 29

def summary
  @summary
end

Instance Method Details

#file_finished(file, offenses) ⇒ Object



41
42
43
44
# File 'lib/rubocop/formatter/html_formatter.rb', line 41

def file_finished(file, offenses)
  files << OpenStruct.new(path: file, offenses: offenses)
  summary.offense_count += offenses.count
end

#finished(inspected_files) ⇒ Object



46
47
48
49
50
# File 'lib/rubocop/formatter/html_formatter.rb', line 46

def finished(inspected_files)
  summary.inspected_files = inspected_files

  render_html
end

#render_htmlObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rubocop/formatter/html_formatter.rb', line 52

def render_html
  context = ERBContext.new(files, summary)

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

  # The following condition is workaround for until Ruby 2.6 is released.
  # https://github.com/ruby/ruby/commit/cc777d09f44fa909a336ba14f3aa802ffe16e010
  erb = if RUBY_VERSION >= '2.6'
          ERB.new(template, trim_mode: '-')
        else
          ERB.new(template, nil, '-')
        end
  html = erb.result(context.binding)

  output.write html
end

#started(target_files) ⇒ Object



37
38
39
# File 'lib/rubocop/formatter/html_formatter.rb', line 37

def started(target_files)
  summary.target_files = target_files
end