forked from rrrene/credo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
summary.ex
187 lines (151 loc) · 4.56 KB
/
summary.ex
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
defmodule Credo.CLI.Output.Summary do
@moduledoc false
# This module is responsible for printing the summary at the end of the analysis.
@category_wording [
{:consistency, "consistency issue", "consistency issues"},
{:warning, "warning", "warnings"},
{:refactor, "refactoring opportunity", "refactoring opportunities"},
{:readability, "code readability issue", "code readability issues"},
{:design, "software design suggestion", "software design suggestions"}
]
@cry_for_help "Please report incorrect results: https://github.com/rrrene/credo/issues"
alias Credo.CLI.Output
alias Credo.CLI.Output.FirstRunHint
alias Credo.CLI.Output.UI
alias Credo.Execution
alias Credo.SourceFile
def print(
_source_files,
%Execution{format: "flycheck"},
_time_load,
_time_run
) do
nil
end
def print(_source_files, %Execution{format: "oneline"}, _time_load, _time_run) do
nil
end
def print(source_files, exec, time_load, time_run) do
issues = Execution.get_issues(exec)
source_file_count = exec |> Execution.get_source_files() |> Enum.count()
checks_count = count_checks(exec)
UI.puts()
UI.puts([:faint, @cry_for_help])
UI.puts()
UI.puts([:faint, format_time_spent(checks_count, source_file_count, time_load, time_run)])
UI.puts(summary_parts(source_files, issues))
UI.puts()
print_priority_hint(exec)
print_first_run_hint(exec)
end
defp print_first_run_hint(%Execution{cli_options: %{switches: %{first_run: true}}} = exec) do
FirstRunHint.call(exec)
end
defp print_first_run_hint(exec), do: exec
defp count_checks(exec) do
{result, _only_matching, _ignore_matching} = Execution.checks(exec)
Enum.count(result)
end
defp print_priority_hint(%Execution{min_priority: min_priority})
when min_priority >= 0 do
UI.puts([
:faint,
"Showing priority issues: ↑ ↗ → (use `mix credo explain` to explain issues, `mix credo --help` for options)."
])
end
defp print_priority_hint(_) do
UI.puts([
:faint,
"Use `mix credo explain` to explain issues, `mix credo --help` for options."
])
end
defp format_time_spent(check_count, source_file_count, time_load, time_run) do
time_run = time_run |> div(10_000)
time_load = time_load |> div(10_000)
formatted_total = format_in_seconds(time_run + time_load)
time_to_load = format_in_seconds(time_load)
time_to_run = format_in_seconds(time_run)
total_in_seconds =
case formatted_total do
"1.0" -> "1 second"
value -> "#{value} seconds"
end
checks =
if check_count == 1 do
"1 check"
else
"#{check_count} checks"
end
source_files =
if source_file_count == 1 do
"1 file"
else
"#{source_file_count} files"
end
breakdown = "#{time_to_load}s to load, #{time_to_run}s running #{checks} on #{source_files}"
"Analysis took #{total_in_seconds} (#{breakdown})"
end
defp format_in_seconds(t) do
if t < 10 do
"0.0#{t}"
else
t = div(t, 10)
"#{div(t, 10)}.#{rem(t, 10)}"
end
end
defp category_count(issues, category) do
issues
|> Enum.count(&(&1.category == category))
end
defp summary_parts(source_files, issues) do
parts =
@category_wording
|> Enum.flat_map(&summary_part(&1, issues))
parts =
parts
|> List.update_at(Enum.count(parts) - 1, fn last_part ->
String.replace(last_part, ", ", "")
end)
parts =
if Enum.empty?(parts) do
"no issues"
else
parts
end
[
:green,
"#{scope_count(source_files)} mods/funs, ",
:reset,
"found ",
parts,
"."
]
end
defp summary_part({category, singular, plural}, issues) do
color = Output.check_color(category)
case category_count(issues, category) do
0 -> []
1 -> [color, "1 #{singular}, "]
x -> [color, "#{x} #{plural}, "]
end
end
defp scope_count(%SourceFile{} = source_file) do
Credo.Code.prewalk(source_file, &scope_count_traverse/2, 0)
end
defp scope_count([]), do: 0
defp scope_count(source_files) when is_list(source_files) do
source_files
|> Enum.map(&Task.async(fn -> scope_count(&1) end))
|> Enum.map(&Task.await/1)
|> Enum.reduce(&(&1 + &2))
end
@def_ops [:defmodule, :def, :defp, :defmacro]
for op <- @def_ops do
defp scope_count_traverse({unquote(op), _, _} = ast, count) do
{ast, count + 1}
end
end
defp scope_count_traverse(ast, count) do
{ast, count}
end
end