Class: Rubocop::Formatter::JSONFormatter

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

Overview

This formatter formats the report data in JSON format.

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#output

Instance Method Summary collapse

Methods inherited from BaseFormatter

#file_started

Constructor Details

#initialize(output) ⇒ JSONFormatter

Returns a new instance of JSONFormatter.



12
13
14
15
16
17
18
19
# File 'lib/rubocop/formatter/json_formatter.rb', line 12

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

Instance Attribute Details

#output_hashObject (readonly)

Returns the value of attribute output_hash.



10
11
12
# File 'lib/rubocop/formatter/json_formatter.rb', line 10

def output_hash
  @output_hash
end

Instance Method Details

#file_finished(file, offences) ⇒ Object



25
26
27
28
# File 'lib/rubocop/formatter/json_formatter.rb', line 25

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

#finished(inspected_files) ⇒ Object



30
31
32
33
# File 'lib/rubocop/formatter/json_formatter.rb', line 30

def finished(inspected_files)
  output_hash[:summary][:inspected_file_count] = inspected_files.count
  output.write output_hash.to_json
end

#hash_for_file(file, offences) ⇒ Object



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

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

#hash_for_location(offence) ⇒ Object

TODO: Consider better solution for Offence#real_column.



63
64
65
66
67
68
# File 'lib/rubocop/formatter/json_formatter.rb', line 63

def hash_for_location(offence)
  {
    line:   offence.line,
    column: offence.real_column
  }
end

#hash_for_offence(offence) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/formatter/json_formatter.rb', line 52

def hash_for_offence(offence)
  {
    severity: offence.severity,
    message:  offence.message,
    cop_name: offence.cop_name,
    corrected: offence.corrected?,
    location: hash_for_location(offence)
  }
end

#metadata_hashObject



35
36
37
38
39
40
41
42
43
# File 'lib/rubocop/formatter/json_formatter.rb', line 35

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



21
22
23
# File 'lib/rubocop/formatter/json_formatter.rb', line 21

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