Skip to content

Commit 51b5593

Browse files
authored
Error message formatting (#7)
* Add format result for errors * Fix tests * Error format updating * Fix sheldon candidates format * Update README sheldon example * README: added badges * Update error message * Update README * Fix error type place * Change output of word in error
1 parent 78607e4 commit 51b5593

File tree

5 files changed

+88
-46
lines changed

5 files changed

+88
-46
lines changed

README.md

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# rebar3_sheldon
2+
[![Hex.pm Version][hexpm version]][hexpm]
3+
[![Hex.pm Downloads][hexpm downloads]][hexpm]
4+
[![Build Status][gh badge]][gh]
5+
[![Erlang Versions][erlang version badge]][gh]
6+
27

38
A rebar plugin for spellchecking.
49

@@ -13,52 +18,19 @@ $ rebar3 compile
1318
Add the plugin to your rebar config:
1419

1520
```erlang
16-
{project_plugins, [{rebar3_sheldon, "~> 0.1.0"}]}.
21+
{project_plugins, [{rebar3_sheldon, "~> 0.1.2"}]}.
1722
```
1823

1924
Then just call your plugin directly in an existing application:
2025
```sh
2126
$ rebar3 spellcheck
2227
===> Fetching rebar3_sheldon
2328
===> Compiling rebar3_sheldon
24-
$ ./rebar3 spellcheck
25-
===> spellcheck detect warning emits:[#{filename => "src/application.erl",
26-
line => 2,
27-
reason =>
28-
#{bazinga => <<"Too bad Leonard">>,
29-
misspelled_words =>
30-
[#{candidates =>
31-
["commit","commot",
32-
"comdt","comet","compt",
33-
"comte","comdt","comet",
34-
"compt","comte","comm",
35-
"comm.","comma","comme",
36-
"commo","commy","scomm"],
37-
line_number => 1,
38-
word => "Commt"}]},
39-
string => "Commt",type => string},
40-
#{filename => "test/shot_SUITE.erl",
41-
line => 1,
42-
reason =>
43-
#{bazinga =>
44-
<<"I'm exceedingly smart. I graduated college at fourteen. While my brother was getting an STD, I was getting a Ph.D. Penicillin can't take this away.">>,
45-
misspelled_words =>
46-
[#{candidates =>
47-
["speeling","speiling",
48-
"spelding","spelling",
49-
"sperling","spieling",
50-
"apeling","pealing",
51-
"peeling","pelting",
52-
"perling","petling",
53-
"sealing","seeling",
54-
"selfing","seling",
55-
"selling","setling",
56-
"sapling","sipling",
57-
"spiling","spaeing",
58-
"spewing"],
59-
line_number => 1,
60-
word => "Speling"}]},
61-
string => "Speling",type => string}]
29+
===> Youre welcome. And if he has twins, we can do all kinds of neat experiments on them.:
30+
test/test_SUITE.erl:1: The word "Speling" in string is unknown. Maybe you wanted to use "speeling" or "speiling" or ....?
31+
test/test_SUITE.erl:2: The word "Commt" in string is unknown. Maybe you wanted to use "commit" or "commot" or "comdt" ...?
32+
test/test_SUITE.erl:2: The word "fdfdf" in string is unknown.
33+
test/test_SUITE.erl:3: The word "Unicode" in comment is unknown. Maybe you wanted to use "uncoded"?
6234
```
6335
6436
## Config
@@ -71,3 +43,11 @@ Example:
7143
{options, #{dummy => option}}
7244
]}.
7345
```
46+
47+
<!-- Badges -->
48+
[hexpm]: https://hex.pm/packages/rebar3_sheldon
49+
[hexpm version]: https://img.shields.io/hexpm/v/rebar3_sheldon.svg?style=flat-square
50+
[hexpm downloads]: https://img.shields.io/hexpm/dt/rebar3_sheldon.svg?style=flat-square
51+
[gh]: https://github.com/vkatsuba/rebar3_sheldon/actions/workflows/ci.yml
52+
[gh badge]: https://img.shields.io/github/workflow/status/vkatsuba/rebar3_sheldon/CI?style=flat-square
53+
[erlang version badge]: https://img.shields.io/badge/erlang-23.0%20to%2024.1-blue.svg?style=flat-square

src/rebar3_sheldon_ast.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ spellcheck([Filename | T], Acc, RegEx) ->
2929
ok ->
3030
Res;
3131
Warnings ->
32-
[#{filename => Filename,
32+
[#{file => Filename,
3333
line => Line,
3434
type => Type,
3535
string => Str,

src/rebar3_sheldon_prv.erl

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ do(State) ->
5959
[] ->
6060
{ok, State};
6161
Warnings -> %% @TODO: sheldon will return warning for TODO word
62-
{error, io_lib:format("spellcheck detect warning emits: ~p", [Warnings])}
62+
[#{reason := #{bazinga := SheldonMsg}} | _] = Warnings,
63+
{error, format_results({unicode:characters_to_list(SheldonMsg), Warnings})}
6364
end.
6465

6566
-spec format_error(any()) -> iolist().
@@ -119,3 +120,58 @@ get_ignored_files(SpellcheckConfig) ->
119120
-spec get_ignore_regex(list()) -> [string()].
120121
get_ignore_regex(SpellcheckConfig) ->
121122
proplists:get_value(ignore_regex, SpellcheckConfig, undefined).
123+
124+
%% =============================================================================
125+
%% Error formatter
126+
%% =============================================================================
127+
128+
-spec format_results({string(), [maps:map()]}) -> string().
129+
format_results({SheldonMsg, Results}) ->
130+
lists:foldr(fun(Result, Acc) -> [Acc, format_result(Result)] end,
131+
SheldonMsg ++ ":\n",
132+
Results).
133+
134+
-spec format_result(maps:map()) -> io:data().
135+
format_result(#{reason := #{misspelled_words := Misspelled}} = Data) ->
136+
format_sheldon(Misspelled, Data, []).
137+
138+
-spec format_text(string(), list()) -> string().
139+
format_text(Text, Args) ->
140+
Formatted = io_lib:format(Text, Args),
141+
unicode:characters_to_list(Formatted).
142+
143+
-spec format_sheldon(list(), maps:map(), list()) -> string().
144+
format_sheldon([], _, Acc) ->
145+
Acc;
146+
format_sheldon([#{candidates := [], word := Word} | T],
147+
#{file := File,
148+
line := Line,
149+
type := Type} =
150+
Data,
151+
Acc) ->
152+
NewAcc =
153+
[Acc,
154+
format_text("~ts:~tp: The word ~p in ~p is unknown.", [File, Line, Word, Type]),
155+
$\n],
156+
format_sheldon(T, Data, NewAcc);
157+
format_sheldon([#{candidates := Candidates, word := Word} | T],
158+
#{file := File,
159+
line := Line,
160+
type := Type} =
161+
Data,
162+
Acc) ->
163+
FormatCandidates = format_sheldon_candidates(Candidates, []),
164+
NewAcc =
165+
[Acc,
166+
format_text("~ts:~tp: The word ~p in ~p is unknown. Maybe you wanted to use ~ts?",
167+
[File, Line, Word, Type, FormatCandidates]),
168+
$\n],
169+
format_sheldon(T, Data, NewAcc).
170+
171+
-spec format_sheldon_candidates([any()], [[[any()] | char()]]) -> [[[any()] | char()]].
172+
format_sheldon_candidates([], Acc) ->
173+
Acc;
174+
format_sheldon_candidates([Candidate], Acc) ->
175+
[Acc, format_text("~p", [Candidate])];
176+
format_sheldon_candidates([Candidate | T], Acc) ->
177+
format_sheldon_candidates(T, [Acc, format_text("~p or ", [Candidate])]).

test/rebar3_sheldon_SUITE.erl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,10 @@ emits_warnings(_Config) ->
5454
Files = {files, ["src/test_warning.erl"]},
5555
State2 = rebar_state:set(State1, spellcheck, [Files]),
5656
%% Our parsers don't crash on unparseable or non-existent files
57-
{error, ErrorMsg} = spellcheck(State2),
57+
Error = spellcheck(State2),
58+
ErrorMsg = get_error_msg(Error),
5859
%% Check warning message
59-
?assertEqual(ErrorMsg, string:find(ErrorMsg, "spellcheck detect warning emits:")).
60+
?assertEqual(ErrorMsg, string:find(ErrorMsg, "src/test_warning.erl:3:")).
6061

6162
-spec ignore_regex(ct_suite:ct_config()) -> ok | no_return().
6263
ignore_regex(_Config) ->
@@ -74,9 +75,10 @@ unicode(_Config) ->
7475
{ok, State1} = init(),
7576
Files = {files, ["src/test_unicode.erl"]},
7677
State2 = rebar_state:set(State1, spellcheck, [Files]),
77-
{error, ErrorMsg} = spellcheck(State2),
78+
Error = spellcheck(State2),
79+
ErrorMsg = get_error_msg(Error),
7880
%% Check warning message
79-
?assertEqual(ErrorMsg, string:find(ErrorMsg, "spellcheck detect warning emits:")).
81+
?assertEqual(ErrorMsg, string:find(ErrorMsg, "src/test_unicode.erl:8:")).
8082

8183
%% =============================================================================
8284
%% Helpers
@@ -112,3 +114,7 @@ init_test_app() ->
112114
"src/*_unicode.erl"]},
113115
IgnoreRegEx = {ignore_regex, "[_@./#&+-=*]"},
114116
rebar_state:set(State1, spellcheck, [Files, IgnoredFiles, IgnoreRegEx]).
117+
118+
-spec get_error_msg(tuple()) -> any().
119+
get_error_msg({error, [_, [_, Error, _]]}) ->
120+
Error.

test/test_app/src/test_warning.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-module(test_warning).
22

3-
% Word commt is wrong
3+
% Word commt is fffrong

0 commit comments

Comments
 (0)