class RuboCop::Formatter::JSONFormatter

This formatter formats the report data in JSON format.

Attributes

output_hash[R]

Public Class Methods

new(output, options = {}) click to toggle source
Calls superclass method RuboCop::Formatter::BaseFormatter::new
# File lib/rubocop/formatter/json_formatter.rb, line 14
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/json_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) click to toggle source
# File lib/rubocop/formatter/json_formatter.rb, line 32
def finished(inspected_files)
  output_hash[:summary][:inspected_file_count] = inspected_files.count
  output.write output_hash.to_json
end
hash_for_file(file, offenses) click to toggle source
# File lib/rubocop/formatter/json_formatter.rb, line 47
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

TODO: Consider better solution for Offense#real_column.

# File lib/rubocop/formatter/json_formatter.rb, line 65
def hash_for_location(offense)
  {
    start_line:   offense.line,
    start_column: offense.real_column,
    last_line:    offense.last_line,
    last_column:  offense.last_column,
    length:       offense.location.length,
    # `line` and `column` exist for compatibility.
    # Use `start_line` and `start_column` instead.
    line:         offense.line,
    column:       offense.real_column
  }
end
hash_for_offense(offense) click to toggle source
# File lib/rubocop/formatter/json_formatter.rb, line 54
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/json_formatter.rb, line 37
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 23
def started(target_files)
  output_hash[:summary][:target_file_count] = target_files.count
end