Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
#
# {Credo.Check.Design.DuplicatedCode, false}
#
# To enable a check with default params put `true` or `[]` as second element:
#
# {Credo.Check.Readability.StrictModuleLayout, true}
#
checks: %{
enabled: [
#
Expand Down
6 changes: 6 additions & 0 deletions guides/configuration/check_params.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ All checks are configured using a two-element tuple:
{Credo.Check.Design.AliasUsage, if_nested_deeper_than: 2}
```

Passing `true` is equivalent to passing an empty list of parameters:

```elixir
{Credo.Check.Readability.StrictModuleLayout, true}
```

## General params

While `params` are defined by each check individually, there are a couple of general params provided by Credo, which work the same for each check.
Expand Down
10 changes: 7 additions & 3 deletions lib/credo/config_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,12 @@ defmodule Credo.ConfigFile do
checks: %{enabled: checks_other_enabled} = checks_other
})
when is_list(checks_other_enabled) do
disabled = disable_check_tuples(checks_other[:disabled])
other_disabled = normalize_check_tuples(checks_other[:disabled])
disabled = disable_check_tuples(other_disabled)

%{
enabled: checks_other_enabled |> normalize_check_tuples() |> Keyword.merge(disabled),
disabled: checks_other[:disabled] || []
disabled: other_disabled
}
end

Expand Down Expand Up @@ -426,7 +427,9 @@ defmodule Credo.ConfigFile do
end

def merge_checks(base, other) when is_list(base) and is_list(other) do
Keyword.merge(base, other)
base
|> normalize_check_tuples()
|> Keyword.merge(normalize_check_tuples(other))
end

#
Expand All @@ -445,6 +448,7 @@ defmodule Credo.ConfigFile do
end

defp normalize_check_tuple({name}), do: {name, []}
defp normalize_check_tuple({name, true}), do: {name, []}
defp normalize_check_tuple(tuple), do: tuple

defp disable_check_tuples(nil), do: []
Expand Down
81 changes: 81 additions & 0 deletions test/credo/config_file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,87 @@
]
end

test "merge_checks treats true as default params" do

Check failure on line 565 in test/credo/config_file_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test merge_checks treats true as default params (Credo.ConfigFileTest)
base = %ConfigFile{
checks: %{
enabled: [
{Credo.Check.Consistency.ExceptionNames, []},
{Credo.Check.Consistency.LineEndings, []}
],
disabled: [
{Credo.Check.Readability.StrictModuleLayout, []}
]
}
}

other = %ConfigFile{
checks: %{
enabled: [
{Credo.Check.Readability.StrictModuleLayout, true}
]
}
}

expected = %{
enabled: [
{Credo.Check.Readability.StrictModuleLayout, []}
],
disabled: []
}

assert_sorted_equality(expected, ConfigFile.merge_checks(base, other))
end

test "merge_checks treats true as default params for disabled checks" do

Check failure on line 596 in test/credo/config_file_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test merge_checks treats true as default params for disabled checks (Credo.ConfigFileTest)
base = %ConfigFile{
checks: %{
enabled: [
{Credo.Check.Consistency.ExceptionNames, []}
]
}
}

other = %ConfigFile{
checks: %{
enabled: [
{Credo.Check.Consistency.LineEndings, []}
],
disabled: [
{Credo.Check.Readability.StrictModuleLayout, true}
]
}
}

expected = %{
enabled: [
{Credo.Check.Consistency.LineEndings, []},
{Credo.Check.Readability.StrictModuleLayout, false}
],
disabled: [
{Credo.Check.Readability.StrictModuleLayout, []}
]
}

assert_sorted_equality(expected, ConfigFile.merge_checks(base, other))
end

test "merge_checks treats true as default params when merging check lists" do

Check failure on line 629 in test/credo/config_file_test.exs

View workflow job for this annotation

GitHub Actions / Test for lib/ changes

test merge_checks treats true as default params when merging check lists (Credo.ConfigFileTest)
base = [
{Credo.Check.Consistency.ExceptionNames, []},
{Credo.Check.Consistency.LineEndings, true}
]

other = [
{Credo.Check.Readability.StrictModuleLayout, true}
]

assert ConfigFile.merge_checks(base, other) == [
{Credo.Check.Consistency.ExceptionNames, []},
{Credo.Check.Consistency.LineEndings, []},
{Credo.Check.Readability.StrictModuleLayout, []}
]
end

test "loads .credo.exs from ./config subdirs in ascending directories as well" do
dirs = ConfigFile.relevant_directories(".")

Expand Down
Loading