View Source CredoExt.Check.Readability.DoKeywordFunctionsLineConsistency (credo_ext v0.1.1)
Basics
This check is disabled by default.
Learn how to enable it via
.credo.exs
.
This check has a base priority of high
and works with any version of Elixir.
Explanation
Ensures that if one function starting with do:
keyword definition within the module spans multiple lines
(i.e., do:
on a new line), all similar function definitions with the same name must also be multi-line for consistency.
https://github.com/rrrene/credo/blob/master/CHANGELOG.md#add-ids-to-checks,
Checks are sorted by IDs, considering naming convention, the ID
of this custom check is EX31001
, where:
EX
- standing for elixir3
- standing for categoryreadability
1
- standing forCustom Credo Checks
, considering docs:The second digit is always 0 for Credo's standard checks
001
- standing for a unique identifier (max 999 checks percategory
as per docs)
Usage
Add the needed custom check into to your .credo.exs
file:
%{
configs: [
%{
checks: %{
enabled: [
..., # Other checks
{CredoExt.Check.Readability.DoKeywordFunctionsLineConsistency, []}
]
}
}
]
}
## Examples of different function definitions and how are they treated
defmodule SampleModule do
#do on same line
def get_type(%{type: type}), do: type |> Map.get(:coding) |> hd() |> Map.get(:code)
#do on same line
def get_type(%{
type: type,
value: value
}), do: type |> Map.get(:coding) |> hd() |> Map.get(:code)
#do on next line
def get_type(%{
type: :ok,
}),
do: %{coding: [%{code: :ok}]} |> Map.get(:coding) |> hd() |> Map.get(:code)
#do on same line
def get_type(%{
type: :ok_1,
}), do:
%{coding: [%{code: :ok_1}]} |> Map.get(:coding) |> hd() |> Map.get(:code)
#do on same line
def get_type(%{
type: :ok_2,
}), do: %{coding: [%{code: :ok_2}]}
|> Map.get(:coding)
|> hd()
|> Map.get(:code)
#do on same line
def get_type(%{
type: :ok_3,
}), do: :ok_3
#ignored because is a full body function
def get_type(%{
type: :ok_3,
}) do
:ok_3
end
# do on same line
defp get_type(nil, nil), do: nil
# do on same line
defp get_type(%{
item: "item"
}, nil), do: nil
# do on same line
defp get_type(
%{
item: "item",
item2: "item2"
},
%{another: "another"}
), do: nil
# do on next line
defp get_type(%{type: :test}, :ok),
do: :ok
# do on next line
defp get_type(%{
type: :test,
value: :error
}, :ok),
do: :ok
# ignored because it is a full body function
defp get_type(%{
type: :test,
value: :error2
}, :ok) do
:ok
end
# ignored because it is a full body function
defp get_type(%{
type: :test,
value: :error3
}, :ok) do
:ok
|> to_string()
|> IO.inspect(label: "---- ok ----", limit: :infinity)
end
# do on next line
defp get_type(nil, :ok),
do: :ok
# do on next line
defp get_type(nil, :err),
do: :err
|> to_string()
|> IO.inspect(label: "---- io ----", limit: :infinity)
end
## Check-Specific Parameters
There are no specific parameters for this check.
## General Parameters
Like with all checks, general params can be applied.
Parameters can be configured via the .credo.exs
config file.
Summary
Functions
Run the check on the given source file.
Functions
Run the check on the given source file.