Credo
Credo is the static analysis tool of Elixir. It reads the source without running it and reports a long function, a name that says nothing, a module without a doc and the other habits a team agrees on. The formatter fixes whitespace; Credo reads the shape of the code.
The issue a run reports
The project is generated, and one module has no @moduledoc. A strict run names the check, the file and the line:
mix new credo_demo
* creating README.md
* creating .formatter.exs
* creating .gitignore
* creating mix.exs
* creating lib
* creating lib/credo_demo.ex
* creating test
* creating test/test_helper.exs
* creating test/credo_demo_test.exs
Your Mix project was created successfully.
You can use "mix" to compile it, test it, and more:
cd credo_demo
mix test
Run "mix help" for more commands.
credo_demo/lib/credo_demo.ex
defmodule CredoDemo do def hello do :world end end
cd credo_demo
credo --strict lib/credo_demo.ex
Code Readability
┃
┃ [R] → Modules should have a @moduledoc tag.
┃ lib/credo_demo.ex:1:11 #(CredoDemo)
2 mods/funs, found 1 code readability issue.
The check is Credo.Check.Readability.ModuleDoc, marked [R] for readability. The line above the report and the timing below it change between runs, so the block keeps the issue and the summary.
The fix
A module that is not a public library still states its intent: @moduledoc false marks it as internal, and the check is satisfied. A second module with the line is clean:
credo_demo/lib/credo_fixed.ex
defmodule CredoFixed do @moduledoc false def hello do :world end end
credo --strict lib/credo_fixed.ex
2 mods/funs, found no issues.
The summary is the line a pipeline reads. credo --strict exits with a non-zero status when an issue is found and 0 when the list is empty, so the same command is the gate before a commit.
The config file
.credo.exs lists the checks and can disable one that does not fit the project. The file is generated by mix credo gen.config; a team that keeps a check off writes why in the comment above it. The formatter has its own file, and the two run together in a pipeline:
mix format --check-formatted
The command printed nothing and exited 0. Without a path, credo --strict reads the project and prints the same kind of report; it was run above on one file. The debugging lesson is the last tool of the chapter: it finds the cause of a failure a lint cannot see.
The commands on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), and Credo 1.7.19, on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does Credo read?
Answer
The source code, without running it
Credo is a static analysis tool: it reads the source and reports style and design issues.
-
What does
credo --strictdo?Answer
Runs every check of the configuration, including the stricter ones
Strict mode turns on the checks the default mode leaves out.
-
Which check reports a module with no
@moduledoc?Answer
Credo.Check.Readability.ModuleDoc
ModuleDoc is the readability check for the module documentation.
-
How does a module mark itself as internal?
Answer
@moduledoc false
@moduledoc false satisfies the doc check without a public description.
-
What does this program do when Credo checks it?
defmodule M do def hello do :world end endAnswer
It reports Modules should have a @moduledoc tag
The module has no @moduledoc, which the readable-doc check reports.
-
What does this program do when Credo checks it?
defmodule M do @moduledoc false def hello do :world end endAnswer
It reports no ModuleDoc issue
@moduledoc false is the internal marker and satisfies the check.
-
Which file configures the checks of a project?
Answer
.credo.exs
The Credo configuration lists the checks and their options.
-
What exit status does Credo give when it finds an issue in strict mode?
Answer
A non-zero status
A non-zero status is how a pipeline learns that the check failed.
-
Why is Credo run together with the formatter?
Answer
The formatter fixes whitespace; Credo reads the shape of the code
The two tools cover layout and design, which are different questions.
-
What does a clean Credo run print in its summary?
Answer
found no issues.
The summary names the number of modules and says no issues were found.
Автор статьи: Arthur Isaev