Class: RuboCop::Formatter::HTMLFormatter

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

Overview

This formatter saves the output as a html file.

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods included from PathUtil

match_path?, relative_path

Methods inherited from BaseFormatter

#file_started

Constructor Details

#initialize(output) ⇒ HTMLFormatter

Returns a new instance of HTMLFormatter.



14
15
16
17
18
19
20
21
# File 'lib/rubocop/formatter/html_formatter.rb', line 14

def initialize(output)
  super
  @output_hash = {
    metadata: ,
    files:    [],
    summary:  { offense_count: 0 }
  }
end

Instance Attribute Details

#output_hashObject (readonly)

Returns the value of attribute output_hash.



12
13
14
# File 'lib/rubocop/formatter/html_formatter.rb', line 12

def output_hash
  @output_hash
end

Instance Method Details

#file_finished(file, offenses) ⇒ Object



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

def file_finished(file, offenses)
  output_hash[:files] << hash_for_file(file, offenses)
  output_hash[:summary][:offense_count] += offenses.count
end

#finished(inspected_files) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/rubocop/formatter/html_formatter.rb', line 32

def finished(inspected_files)
  output_hash[:summary][:inspected_file_count] = inspected_files.count
  template = File.read(File
    .expand_path('../../../../assets/output.html.erb', __FILE__))
  erb = ERB.new(template)
  html_content = erb.result(binding)
  output.write html_content
end

#hash_for_file(file, offenses) ⇒ Object



51
52
53
54
55
56
# File 'lib/rubocop/formatter/html_formatter.rb', line 51

def hash_for_file(file, offenses)
  {
    path:     relative_path(file),
    offenses: offenses.map { |o| hash_for_offense(o) }
  }
end

#hash_for_location(offense) ⇒ Object

TODO: Consider better solution for Offense#real_column.



69
70
71
72
73
74
75
76
77
# File 'lib/rubocop/formatter/html_formatter.rb', line 69

def hash_for_location(offense)
  {
    line:   offense.line,
    column: offense.real_column,
    length: offense.location.length,
    source_line: offense.location.source_line,
    highlight: highlight_line(offense.location)
  }
end

#hash_for_offense(offense) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/rubocop/formatter/html_formatter.rb', line 58

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

#highlight_line(location) ⇒ Object



79
80
81
82
83
84
85
86
87
# File 'lib/rubocop/formatter/html_formatter.rb', line 79

def highlight_line(location)
  column_length = if location.begin.line == location.end.line
                    location.column_range.count
                  else
                    location.source_line.length - location.column
                  end

  ' ' * location.column + '^' * column_length
end

#metadata_hashObject



41
42
43
44
45
46
47
48
49
# File 'lib/rubocop/formatter/html_formatter.rb', line 41

def 
  {
    rubocop_version: RuboCop::Version::STRING,
    ruby_engine:     RUBY_ENGINE,
    ruby_version:    RUBY_VERSION,
    ruby_patchlevel: RUBY_PATCHLEVEL.to_s,
    ruby_platform:   RUBY_PLATFORM
  }
end

#started(target_files) ⇒ Object



23
24
25
# File 'lib/rubocop/formatter/html_formatter.rb', line 23

def started(target_files)
  output_hash[:summary][:target_file_count] = target_files.count
end