diff --git a/.credo.exs b/.credo.exs index caf07d927..5d28d4e93 100644 --- a/.credo.exs +++ b/.credo.exs @@ -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: [ # diff --git a/guides/configuration/check_params.md b/guides/configuration/check_params.md index facb8dc3d..6efcf57ac 100644 --- a/guides/configuration/check_params.md +++ b/guides/configuration/check_params.md @@ -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. diff --git a/lib/credo/config_file.ex b/lib/credo/config_file.ex index 2f60abd5d..5ada217ef 100644 --- a/lib/credo/config_file.ex +++ b/lib/credo/config_file.ex @@ -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 @@ -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 # @@ -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: [] diff --git a/test/credo/config_file_test.exs b/test/credo/config_file_test.exs index f0a02c7c6..9b125e46c 100644 --- a/test/credo/config_file_test.exs +++ b/test/credo/config_file_test.exs @@ -562,6 +562,87 @@ defmodule Credo.ConfigFileTest do ] end + test "merge_checks treats true as default params" do + 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 + 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 + 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(".")