Sebb
July 2, 2022, 2:54pm
1
The checks PipeChainStart
and ABC-Size
allow a parameter excluded_functions
.
This does not seem to work.
https://hexdocs.pm/credo/Credo.Check.Refactor.PipeChainStart.html
I have this function credo does not like:
def hello do
Kernel.+(1, 1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
|> Kernel.+(1)
end
credo complains:
┃ [F] → Function is too complex (ABC size is 31, max is 30).
┃ lib/credo_check.ex:3:7 #(CredoCheck.hello)
┃ [F] → Pipe chain should start with a raw value.
┃ lib/credo_check.ex:5 #(CredoCheck.hello)
with this config:
{Credo.Check.Refactor.PipeChainStart, [excluded_functions: ["hello"]]},
{Credo.Check.Refactor.ABCSize, [excluded_functions: ["hello"]]},
I also tried
:hello
"CredoCheck.hello"
"hello/0"
~r/hello/
Based on the tests for PipeChainStart
, excluded_functions
is a list of functions that are allowed to start a chain, not functions that are allowed to have a chain in them:
You likely want inline config comments on hello
instead.
3 Likes
Sebb
July 4, 2022, 3:56pm
3
thanks for looking into this.
Makes sense for Pipe, but for ABC excluded_functions
should exclude functions that are allowed to be complex, right?
Looks like Pipe has similar functionality, for similar reasons:
test "it should NOT count functions given to ignore for abc size" do
source = """
def fun() do
Favorite
|> where(user_id: ^user.id)
|> join(:left, [f], t in Template, f.entity_id == t.id and f.entity_type == "template")
|> join(:left, [f, t], d in Document, f.entity_id == d.id and f.entity_type == "document")
|> join(:left, [f, t, d], dt in Template, dt.id == d.template_id)
|> join(:left, [f, t, d, dt], c in Category, c.id == t.category_id or c.id == dt.category_id)
|> select([f, t, d, dt, c], c)
|> distinct(true)
|> Repo.all()
end
"""
assert rounded_abc_size(source, ["where", "join", "select", "distinct"]) == 1
end
1 Like
Hi but how can I exclude a function?
For example I have
┃ [F] → Pipe chain should start with a raw value.
(row in Transaction)
|> from(where: &1.id == ^transaction_id)
|> Worms.Repo.one()
How can I exclude the from
function to be warned by credo?
This may be an unwelcome tangent, but credo would not complain if you wrote this code in the more idiomatic way
Worms.Repo.get(Transaction, transaction_id)
1 Like
The example is contrived. I don’t understand how people can’t see this. It’s meant to be silly…
Sometimes a function is only complex if you can’t read. Which is perfectly sane for a linter to catch. But the OP code isn’t complex.