forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_text_formatter.rb
168 lines (140 loc) · 4.76 KB
/
simple_text_formatter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# frozen_string_literal: true
require_relative 'colorizable'
require_relative 'text_util'
module RuboCop
module Formatter
# A basic formatter that displays only files with offenses.
# Offenses are displayed at compact form - just the
# location of the problem and the associated message.
class SimpleTextFormatter < BaseFormatter
include Colorizable
include PathUtil
COLOR_FOR_SEVERITY = {
refactor: :yellow,
convention: :yellow,
warning: :magenta,
error: :red,
fatal: :red
}.freeze
def started(_target_files)
@total_offense_count = 0
@total_correction_count = 0
@total_correctable_count = 0
end
def file_finished(file, offenses)
return if offenses.empty?
count_stats(offenses)
report_file(file, offenses)
end
def finished(inspected_files)
report_summary(inspected_files.count,
@total_offense_count,
@total_correction_count,
@total_correctable_count)
end
def report_file(file, offenses)
output.puts yellow("== #{smart_path(file)} ==")
offenses.each do |o|
output.printf(
"%<severity>s:%3<line>d:%3<column>d: %<message>s\n",
severity: colored_severity_code(o),
line: o.line,
column: o.real_column,
message: message(o)
)
end
end
def report_summary(file_count, offense_count, correction_count, correctable_count)
report = Report.new(file_count,
offense_count,
correction_count,
correctable_count,
rainbow,
safe_auto_correct: @options[:safe_auto_correct])
output.puts
output.puts report.summary
end
private
def count_stats(offenses)
@total_offense_count += offenses.count
corrected = offenses.count(&:corrected?)
@total_correction_count += corrected
@total_correctable_count += offenses.count(&:correctable?) - corrected
end
def colored_severity_code(offense)
color = COLOR_FOR_SEVERITY[offense.severity.name]
colorize(offense.severity.code, color)
end
def annotate_message(msg)
msg.gsub(/`(.*?)`/m, yellow('\1'))
end
def message(offense)
message =
if offense.corrected_with_todo?
green('[Todo] ')
elsif offense.corrected?
green('[Corrected] ')
else
''
end
"#{message}#{annotate_message(offense.message)}"
end
# A helper class for building the report summary text.
class Report
include Colorizable
include TextUtil
# rubocop:disable Metrics/ParameterLists
def initialize(
file_count, offense_count, correction_count, correctable_count, rainbow,
safe_auto_correct: false
)
@file_count = file_count
@offense_count = offense_count
@correction_count = correction_count
@correctable_count = correctable_count
@rainbow = rainbow
@safe_auto_correct = safe_auto_correct
end
# rubocop:enable Metrics/ParameterLists
def summary
if @correction_count.positive?
if @correctable_count.positive?
"#{files} inspected, #{offenses} detected, #{corrections} corrected,"\
" #{correctable}"
else
"#{files} inspected, #{offenses} detected, #{corrections} corrected"
end
elsif @correctable_count.positive?
"#{files} inspected, #{offenses} detected, #{correctable}"
else
"#{files} inspected, #{offenses} detected"
end
end
private
attr_reader :rainbow
def files
pluralize(@file_count, 'file')
end
def offenses
text = pluralize(@offense_count, 'offense', no_for_zero: true)
color = @offense_count.zero? ? :green : :red
colorize(text, color)
end
def corrections
text = pluralize(@correction_count, 'offense')
color = @correction_count == @offense_count ? :green : :cyan
colorize(text, color)
end
def correctable
if @safe_auto_correct
text = pluralize(@correctable_count, 'more offense')
"#{colorize(text, :yellow)} can be corrected with `rubocop -A`"
else
text = pluralize(@correctable_count, 'offense')
"#{colorize(text, :yellow)} auto-correctable"
end
end
end
end
end
end