Class: RuboCop::Formatter::JSONFormatter

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

Overview

This formatter formats the report data in JSON format.

Constant Summary

Constants included from PathUtil

PathUtil::HIDDEN_FILE_PATTERN

Instance Attribute Summary collapse

Attributes inherited from BaseFormatter

#options, #output

Instance Method Summary collapse

Methods included from PathUtil

absolute?, glob?, hidden_dir?, hidden_file?, hidden_file_in_not_hidden_dir?, match_path?, maybe_hidden_file?, pwd, relative_path, remote_file?, reset_pwd, smart_path

Methods inherited from BaseFormatter

#file_started

Constructor Details

#initialize(output, options = {}) ⇒ JSONFormatter

Returns a new instance of JSONFormatter.



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

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

Instance Attribute Details

#output_hash ⇒ Object (readonly)

Returns the value of attribute output_hash.



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

def output_hash
  @output_hash
end

Instance Method Details

#correction_for(offense) ⇒ Object



110
111
112
# File 'lib/rubocop/formatter/json_formatter.rb', line 110

def correction_for(offense)
  { safe: offense.correction_safe, edits: edits_for(offense) }
end

#edits_for(offense) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rubocop/formatter/json_formatter.rb', line 114

def edits_for(offense)
  buffer = offense.location.source_buffer

  offense.corrections.map do |correction|
    # Built as a range so the columns follow the same 1-based convention
    # as `hash_for_location`, end-exclusive column included.
    range = ::Parser::Source::Range.new(buffer, correction.begin_pos, correction.end_pos)

    hash_for_edit_range(range).merge!(
      begin_pos: correction.begin_pos,
      end_pos: correction.end_pos,
      replacement: correction.replacement
    )
  end
end

#file_finished(file, offenses) ⇒ Object



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

def file_finished(file, offenses)
  entry = hash_for_file(file, offenses)
  problems = @problems.delete(file)
  entry.merge!(problems) if problems

  output_hash[:files] << entry
  output_hash[:summary][:offense_count] += offenses.count
end

#file_problems(file, errors, warnings) ⇒ Object



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

def file_problems(file, errors, warnings)
  @problems[file] = hash_for_problems(file, errors, warnings)
end

#finished(inspected_files) ⇒ Object



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

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

#hash_for_edit_range(range) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/rubocop/formatter/json_formatter.rb', line 130

def hash_for_edit_range(range)
  {
    start_line: range.line,
    start_column: range.column + 1,
    last_line: range.last_line,
    last_column: range.last_column.zero? ? 1 : range.last_column
  }
end

#hash_for_file(file, offenses) ⇒ Object



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

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

#hash_for_location(offense) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rubocop/formatter/json_formatter.rb', line 139

def hash_for_location(offense)
  {
    start_line:   offense.line,
    start_column: offense.real_column,
    last_line:    offense.last_line,
    last_column:  offense.real_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) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rubocop/formatter/json_formatter.rb', line 85

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

  # Suppressed offenses appear only under `--display-suppressed`, so
  # these keys are additive for existing consumers.
  if offense.disabled?
    hash[:suppressed] = true
    hash[:justification] = offense.justification
  end

  # The edits autocorrection would make, so a consumer can apply or review
  # them without running autocorrection itself. Additive, and absent when
  # the offense has no correction.
  hash[:correction] = correction_for(offense) unless offense.corrections.empty?

  hash
end

#hash_for_problems(file, errors, warnings) ⇒ Object

A cop that crashes fails once per node it visits, so messages that differ only in their line and column are listed once, at the first place they happened. They name the file the way its entry does. The keys are absent when there's nothing to report, which keeps them additive for existing consumers.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rubocop/formatter/json_formatter.rb', line 64

def hash_for_problems(file, errors, warnings)
  return {} if errors.empty? && warnings.empty?

  path = smart_path(file)
  location = /#{Regexp.escape(File.expand_path(file))}((?::\d+)*)/

  { errors: errors, warnings: warnings }.filter_map do |key, messages|
    next if messages.empty?

    messages = messages.uniq { |message| message.gsub(location, '') }
    [key, messages.map { |message| message.gsub(location) { path + Regexp.last_match(1) } }]
  end.to_h
end

#metadata_hash ⇒ Object



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

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

#problem_counts(files) ⇒ Object



78
79
80
81
82
83
# File 'lib/rubocop/formatter/json_formatter.rb', line 78

def problem_counts(files)
  {
    error_count: files.sum { |f| f[:errors]&.size.to_i },
    warning_count: files.sum { |f| f[:warnings]&.size.to_i }
  }.reject { |_, count| count.zero? }
end

#started(target_files) ⇒ Object



19
20
21
# File 'lib/rubocop/formatter/json_formatter.rb', line 19

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