forked from rubocop/rubocop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_text_formatter.rb
136 lines (109 loc) · 3.54 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
# frozen_string_literal: true
require 'rubocop/formatter/colorizable'
require 'rubocop/formatter/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
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)
end
def report_file(file, offenses)
output.puts yellow("== #{smart_path(file)} ==")
offenses.each do |o|
output.printf("%s:%3d:%3d: %s\n",
colored_severity_code(o),
o.line, o.real_column, message(o))
end
end
def report_summary(file_count, offense_count, correction_count)
report = Report.new(file_count,
offense_count,
correction_count,
rainbow)
output.puts
output.puts report.summary
end
private
def count_stats(offenses)
@total_offense_count += offenses.count
@total_correction_count += offenses.count(&:corrected?)
end
def smart_path(path)
# Ideally, we calculate this relative to the project root.
base_dir = Dir.pwd
if path.start_with? base_dir
relative_path(path, base_dir)
else
path
end
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(/`(.*?)`/, yellow('\1'))
end
def message(offense)
message = offense.corrected? ? green('[Corrected] ') : ''
"#{message}#{annotate_message(offense.message)}"
end
# A helper class for building the report summary text.
class Report
include Colorizable
include TextUtil
def initialize(file_count, offense_count, correction_count, rainbow)
@file_count = file_count
@offense_count = offense_count
@correction_count = correction_count
@rainbow = rainbow
end
def summary
if @correction_count > 0
"#{files} inspected, #{offenses} detected, #{corrections} corrected"
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
end
end
end
end