123456789_123456789_123456789_123456789_123456789_

Class: RuboCop::Formatter::JSONFormatter

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
Inherits: RuboCop::Formatter::BaseFormatter
Defined in: lib/rubocop/formatter/json_formatter.rb

Overview

This formatter formats the report data in JSON format.

Constant Summary

::RuboCop::PathUtil - Included

HIDDEN_FILE_PATTERN, SMART_PATH_CACHE

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::RuboCop::PathUtil - Included

#absolute?

Returns true for an absolute Unix or Windows path.

#glob?

Returns true for a glob.

#hidden_dir?, #hidden_file?, #hidden_file_in_not_hidden_dir?,
#match_path?

Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.

#maybe_hidden_file?

Loose check to reduce memory allocations.

#relative_path, #smart_path

BaseFormatter - Inherited

#file_finished

Invoked at the end of inspecting each files.

#file_started

Invoked at the beginning of inspecting each files.

#finished

Invoked after all files are inspected or interrupted by user.

#started

Invoked once before any files are inspected.

Constructor Details

.new(output, options = {}) ⇒ JSONFormatter

[ GitHub ]

  
# File 'lib/rubocop/formatter/json_formatter.rb', line 13

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

Instance Attribute Details

#output_hash (readonly)

[ GitHub ]

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

attr_reader :output_hash

Instance Method Details

#file_finished(file, offenses)

[ GitHub ]

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

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

#finished(inspected_files)

[ GitHub ]

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

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)

[ GitHub ]

  
# File 'lib/rubocop/formatter/json_formatter.rb', line 42

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

#hash_for_location(offense)

TODO: Consider better solution for Offense#real_column. The minimum value of start_column: real_column is 1. So, the minimum value of last_column should be 1. And non-zero value of last_column should be used as is.

[ GitHub ]

  
# File 'lib/rubocop/formatter/json_formatter.rb', line 64

def hash_for_location(offense)
  {
    start_line:   offense.line,
    start_column: offense.real_column,
    last_line:    offense.last_line,
    last_column:  offense.last_column.zero? ? 1 : 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)

[ GitHub ]

  
# File 'lib/rubocop/formatter/json_formatter.rb', line 49

def hash_for_offense(offense)
  {
    severity:    offense.severity.name,
    message:     offense.message,
    cop_name:    offense.cop_name,
    corrected:   offense.corrected?,
    correctable: offense.correctable?,
    location:    hash_for_location(offense)
  }
end

#metadata_hash

[ GitHub ]

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

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)

[ GitHub ]

  
# File 'lib/rubocop/formatter/json_formatter.rb', line 18

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