class Rubocop::Formatter::JSONFormatter

This formatter formats the report data in JSON format.

Attributes

output_hash[R]

Public Class Methods

new(output) click to toggle source
Calls superclass method Rubocop::Formatter::BaseFormatter::new
# File lib/rubocop/formatter/json_formatter.rb, line 12
def initialize(output)
  super
  @output_hash = {
    metadata: metadata_hash,
       files: [],
     summary: { offence_count: 0 }
  }
end

Public Instance Methods

file_finished(file, offences) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

TODO: Consider better solution for Offence#real_column.

# File lib/rubocop/formatter/json_formatter.rb, line 62
def hash_for_location(offence)
  {
      line: offence.line,
    column: offence.real_column
  }
end
hash_for_offence(offence) click to toggle source
# 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,
    location: hash_for_location(offence)
  }
end
metadata_hash() click to toggle source
# File lib/rubocop/formatter/json_formatter.rb, line 35
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
started(target_files) click to toggle source
# File lib/rubocop/formatter/json_formatter.rb, line 21
def started(target_files)
  output_hash[:summary][:target_file_count] = target_files.count
end

Private Instance Methods

relative_path(path) click to toggle source
# File lib/rubocop/formatter/json_formatter.rb, line 71
def relative_path(path)
  Pathname.new(path).relative_path_from(Pathname.getwd).to_s
end