Elvis
Elvis is the style checker of the Erlang world. It reads the source, not the beam, and reports a rule, a line and a message for every violation. The rules are a set the config chooses, so a team decides what it wants to enforce instead of arguing in a review. This lesson writes a config, runs the checker on a clean module, and reads the report that a CI job keeps.
The config
elvis.config names the folders, the file filter and the ruleset. The erl_files ruleset is the one for an Erlang source: it checks spacing, naming, line length, tabs and a handful of constructs. A rule that a project does not want is switched off by name, and the reason belongs in a comment next to it. This config switches off no_debug_call, because a teaching project prints with io:format/2 on purpose:
elvis.config
[ {elvis, [ {config, [ #{dirs => ["."], filter => "*.erl", ruleset => erl_files, rules => [ {elvis_style, no_debug_call, disable} ]} ]}, {output_format, parsable} ]} ].
The second section is the report format. parsable prints one line per finding, which is the form a build log and an editor both read; the default is a coloured table for a human at a terminal.
A clean run
A file that follows every rule produces no output and exits 0. The module below has one function per clause, a space on each side of the operator, and no tab:
good.erl
-module(good). -export([main/0, add/2]). add(A, B) -> A + B. main() -> io:format("~p~n", [add(2, 3)]).
erlc good.erl
erl -noshell -eval 'good:main(), halt().'
5
elvis rock -c elvis.config --output-format parsable good.erl
echo $?
0
The checker prints nothing and the status is 0. That is the whole contract a build script needs: no output, status 0. A run over a folder uses elvis rock with no file, and the config decides what it reads.
The parsable line
A violation is one line: the file, the line, the rule and the message. A+B instead of A + B produces two lines from one rule, one for each missing space, and the status becomes 1. The command below is not run on this page because the bad module is not shown in full:
elvis rock -c elvis.config --output-format parsable bad.erl
bad.erl:5:operator_spaces:Missing space to the right of "+" on line 5 bad.erl:5:operator_spaces:Missing space to the left of "+" on line 5
The two lines are the real output of a run on a file with that operator. The first field is the file, the second the line, the third the rule and the rest the message, so a script splits on the first three colons and gets a position an editor can open. The rule name is stable and is the part to search for when the message alone is not clear.
The rules that matter
Four rules catch most of what a beginner writes. operator_spaces
wants one space on each side of a binary operator. line_length
keeps a line under the configured limit, which is 100 by default.
no_tabs rejects a tab in the source, because two editors show it
at different widths. function_naming_convention wants a
lowercase name, which is where the trailing underscore of an EUnit generator trips the
checker.
A rule is not a law. The course itself runs Elvis with a config that disables
no_import, no_if_expression and a few
more, because a lesson shows those constructs on purpose; every disabled rule is a
decision with a reason, not a way to make a red build green. The
static checks lesson puts
this run into the job that checks every change.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
elvis.configchoose?Answer
The folders, the file filter and the rules
The config names what to read and which ruleset and rule overrides apply.
-
Which ruleset is the one for an Erlang source file?
Answer
erl_fileserl_filesholds the rules for.erlsources; other filesets have their own. -
What does the
parsableoutput format print?Answer
One line per finding as file, line, rule and message
A parsable line is
file:line:rule:message, which a script and an editor read. -
What does a clean Elvis run print and answer?
Answer
Nothing and status 0
No violation means no output; the exit status 0 is the result a build reads.
-
What does this module print?
-module(q1). -export([main/0, add/2]). add(A, B) -> A + B. main() -> io:format("~p~n", [add(2, 3)]).Answer
5The function adds 2 and 3, and the program prints 5.
-
What does this module print?
-module(q2). -export([main/0, label/1]). label(0) -> zero; label(N) when N > 0 -> positive; label(_N) -> negative. main() -> io:format("~p~n", [label(-1)]).Answer
negativeThe last clause matches the negative number and returns the atom
negative. -
What does this module print?
-module(q3). -export([main/0, double/1]). double(X) -> X * 2. main() -> io:format("~p~n", [double(21)]).Answer
42The function doubles its argument, and the program prints 42.
-
What does this module print?
-module(q4). -export([main/0, count/1]). count([]) -> 0; count([_Head | Tail]) -> 1 + count(Tail). main() -> io:format("~p~n", [count([a, b, c])]).Answer
3The recursive clauses count one per item, so a list of three gives 3.
-
Which rule reports
A+B?Answer
operator_spacesoperator_spaceswants one space on each side of a binary operator. -
How is a rule switched off in the config?
Answer
With
{elvis_style, rule_name, disable}The rules list holds one tuple per override, and
disableturns the rule off.
The examples on this page were compiled and run with Erlang/OTP 27 (erts-15.2.7.14); Elvis 4.0.0 ran the style check on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Автор статьи: Arthur Isaev