-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathelvis_utils.erl
219 lines (189 loc) · 7.19 KB
/
elvis_utils.erl
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
-module(elvis_utils).
-compile({no_auto_import, [error/2]}).
%% Rules
-export([check_lines/3, check_lines_with_context/4, indentation/3, check_nodes/3]).
%% General
-export([erlang_halt/1, to_str/1, split_all_lines/1, split_all_lines/2]).
%% Output
-export([info/1, info/2, notice/1, notice/2, error/1, error/2, error_prn/1, error_prn/2,
warn_prn/2, parse_colors/1]).
-export_type([file/0, line_content/0]).
-type file() :: #{path => string(), content => binary()}.
-type line_content() :: {integer(), integer()}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Public
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% @doc Takes a binary that holds source code and applies
%% Fun to each line. Fun takes 2 or 3 arguments (the line
%% as a binary, the line number and the optional supplied Args) and
%% returns 'no_result' or {'ok', Result}.
-spec check_lines(binary(), fun(), term()) -> [elvis_result:item()].
check_lines(Src, Fun, Args) ->
Lines = split_all_lines(Src),
check_lines(Lines, Fun, Args, [], 1).
%% @doc Checks each line calling fun and providing the previous and next
%% lines based on the context tuple {Before, After}.
-spec check_lines_with_context(binary(), fun(), term(), line_content()) ->
[elvis_result:item()].
check_lines_with_context(Src, Fun, Args, Ctx) ->
Lines = split_all_lines(Src),
LinesContext = context(Lines, Ctx),
check_lines(LinesContext, Fun, Args, [], 1).
%% @private
check_lines([], _Fun, _Args, Results, _Num) ->
lists:flatten(
lists:reverse(Results));
check_lines([Line | Lines], Fun, Args, Results, Num) ->
FunRes =
case is_function(Fun, 3) of
true ->
Fun(Line, Num, Args);
false ->
Fun(Line, Num)
end,
case FunRes of
{ok, Result} ->
check_lines(Lines, Fun, Args, [Result | Results], Num + 1);
no_result ->
check_lines(Lines, Fun, Args, Results, Num + 1)
end.
%% @private
context(List, CtxCount) ->
context(List, [], CtxCount, []).
context([], _Past, _CtxCount, Results) ->
lists:reverse(Results);
context([Current | Future], Past, {PrevCount, NextCount} = CtxCount, Results) ->
Prev = lists:sublist(Past, PrevCount),
Next = lists:sublist(Future, NextCount),
Item = {Current, lists:reverse(Prev), Next},
context(Future, [Current | Past], CtxCount, [Item | Results]).
%% @doc Takes a binary that holds source code and applies
%% Fun to each line. Fun takes 3 arguments (the line
%% as a binary, the line number and the supplied Args) and
%% returns 'no_result' or {'ok', Result}.
-spec check_nodes(ktn_code:tree_node(), fun(), [term()]) -> [elvis_result:item()].
check_nodes(RootNode, Fun, Args) ->
ChildNodes = ktn_code:content(RootNode),
check_nodes(ChildNodes, Fun, Args, []).
%% @private
check_nodes([], _Fun, _Args, Results) ->
FlatResults = lists:flatten(Results),
lists:reverse(FlatResults);
check_nodes([Node | Nodes], Fun, Args, Results) ->
case Fun(Node, Args) of
[] ->
check_nodes(Nodes, Fun, Args, Results);
Result ->
check_nodes(Nodes, Fun, Args, [Result | Results])
end.
%% @doc This is defined so that it can be mocked for tests.
-spec erlang_halt(integer()) -> no_return().
erlang_halt(Code) ->
halt(Code).
-spec to_str(binary() | list() | atom() | integer()) -> string().
to_str(Arg) when is_binary(Arg) ->
unicode:characters_to_list(Arg);
to_str(Arg) when is_atom(Arg) ->
atom_to_list(Arg);
to_str(Arg) when is_integer(Arg) ->
integer_to_list(Arg);
to_str(Arg) when is_list(Arg) ->
Arg.
-spec split_all_lines(binary()) -> [binary(), ...].
split_all_lines(Binary) ->
split_all_lines(Binary, []).
-spec split_all_lines(binary(), list()) -> [binary()].
split_all_lines(Binary, Opts) ->
binary:split(Binary, [<<"\r\n">>, <<"\n">>], [global | Opts]).
%% @doc Takes a line, a character and a count, returning the indentation level
%% invalid if the number of character is not a multiple of count.
-spec indentation(binary() | string(), char(), integer()) -> invalid | integer().
indentation(Line, Char, Count) ->
LineStr = to_str(Line),
Regex = "^" ++ [Char] ++ "*",
{match, [{0, Len} | _]} = re:run(LineStr, Regex),
case Len rem Count of
0 ->
Len div Count;
_ ->
invalid
end.
-spec info(string()) -> ok.
info(Message) ->
info(Message, []).
-spec info(string(), [term()]) -> ok.
info(Message, Args) ->
ColoredMessage = Message ++ "{{reset}}~n",
print_info(ColoredMessage, Args).
-spec notice(string()) -> ok.
notice(Message) ->
notice(Message, []).
-spec notice(string(), [term()]) -> ok.
notice(Message, Args) ->
ColoredMessage = "{{white-bold}}" ++ Message ++ "{{reset}}~n",
print_info(ColoredMessage, Args).
-spec error(string()) -> ok.
error(Message) ->
error(Message, []).
-spec error(string(), [term()]) -> ok.
error(Message, Args) ->
ColoredMessage = "{{white-bold}}" ++ Message ++ "{{reset}}~n",
print(ColoredMessage, Args).
-spec error_prn(string()) -> ok.
error_prn(Message) ->
error_prn(Message, []).
-spec error_prn(string(), [term()]) -> ok.
error_prn(Message, Args) ->
ColoredMessage = "{{red}}Error: {{reset}}" ++ Message ++ "{{reset}}~n",
print(ColoredMessage, Args).
-spec warn_prn(string(), [term()]) -> ok.
warn_prn(Message, Args) ->
ColoredMessage = "{{magenta}}Warning: {{reset}}" ++ Message ++ "{{reset}}~n",
print(ColoredMessage, Args).
-spec print_info(string(), [term()]) -> ok.
print_info(Message, Args) ->
case elvis_config:from_application_or_config(verbose, false) of
true ->
print(Message, Args);
false ->
ok
end.
-spec print(string(), [term()]) -> ok.
print(Message, Args) ->
case elvis_config:from_application_or_config(no_output, false) of
true ->
ok;
_ ->
Output = io_lib:format(Message, Args),
EscapedOutput = escape_format_str(Output),
io:format(parse_colors(EscapedOutput))
end.
-spec parse_colors(string()) -> string().
parse_colors(Message) ->
Colors =
#{"red" => "\e[0;31m",
"red-bold" => "\e[1;31m",
"green" => "\e[0;32m",
"green-bold" => "\e[1;32m",
"white" => "\e[0;37m",
"white-bold" => "\e[1;37m",
"magenta" => "\e[1;35m",
"reset" => "\e[0m"},
Opts = [global, {return, list}],
case elvis_config:from_application_or_config(output_format, colors) of
P when P =:= plain orelse P =:= parsable ->
re:replace(Message, "{{.*?}}", "", Opts);
colors ->
Fun = fun(Key, Acc) ->
Regex = ["{{", Key, "}}"],
Color = maps:get(Key, Colors),
re:replace(Acc, Regex, Color, Opts)
end,
lists:foldl(Fun, Message, maps:keys(Colors))
end.
-spec escape_format_str(string()) -> string().
escape_format_str(String) ->
Binary = list_to_binary(String),
Result = re:replace(Binary, "[^~]~", "~~", [global]),
ResultBin = iolist_to_binary(Result),
binary_to_list(ResultBin).