diff --git a/doc_rules/elvis_style/max_function_arity.md b/doc_rules/elvis_style/max_function_arity.md index f70d167..3067ec4 100644 --- a/doc_rules/elvis_style/max_function_arity.md +++ b/doc_rules/elvis_style/max_function_arity.md @@ -12,11 +12,13 @@ but it applies to regular functions only (not anonymous ones). - `max_arity :: non_neg_integer()`. - default: `8`. +- `non_exported_max_arity :: non_neg_integer() | same`. + - default: `8`. ## Example ```erlang {elvis_style, max_function_arity} %% or -{elvis_style, max_function_arity, #{max_arity => 10}} +{elvis_style, max_function_arity, #{max_arity => 10, non_exported_max_arity => same}} ``` diff --git a/src/elvis_style.erl b/src/elvis_style.erl index 36f274e..944a4c2 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -197,7 +197,7 @@ default(max_module_length) -> default(max_anonymous_function_arity) -> #{max_arity => 5}; default(max_function_arity) -> - #{max_arity => 8}; + #{max_arity => 8, non_exported_max_arity => 10}; default(max_function_length) -> #{max_length => 30, count_comments => false, @@ -884,18 +884,31 @@ max_anonymous_function_arity(Config, Target, RuleConfig) -> end, Funs). --type max_function_arity_config() :: #{max_arity => non_neg_integer()}. +-type max_function_arity_config() :: + #{max_arity => non_neg_integer(), non_exported_max_arity => pos_integer()}. -spec max_function_arity(elvis_config:config(), elvis_file:file(), max_function_arity_config()) -> [elvis_result:item()]. max_function_arity(Config, Target, RuleConfig) -> - MaxArity = option(max_arity, RuleConfig, max_function_arity), + ExportedMaxArity = option(max_arity, RuleConfig, max_function_arity), + NonExportedMaxArity = + specific_or_default(option(non_exported_max_arity, RuleConfig, max_function_arity), + ExportedMaxArity), Root = get_root(Config, Target, RuleConfig), IsFunction = fun(Node) -> ktn_code:type(Node) == function end, Functions = elvis_code:find(IsFunction, Root), - lists:filtermap(fun(Function) -> + lists:filtermap(fun(#{attrs := #{arity := Arity, name := Name}} = Function) -> + IsExported = + lists:member({Name, Arity}, elvis_code:exported_functions(Root)), + MaxArity = + case IsExported of + true -> + ExportedMaxArity; + false -> + NonExportedMaxArity + end, case ktn_code:attr(arity, Function) of Arity when Arity =< MaxArity -> false; diff --git a/test/examples/fail_max_non_exported_function_arity.erl b/test/examples/fail_max_non_exported_function_arity.erl new file mode 100644 index 0000000..07d8cc5 --- /dev/null +++ b/test/examples/fail_max_non_exported_function_arity.erl @@ -0,0 +1,18 @@ +-module(fail_max_non_exported_function_arity). + +-export([f/0, f/1]). + +f() -> + f(1). + +f(1) -> + f(1, 2). + +f(1, 2) -> + f(1, 2, 3). + +f(1, 2, 3) -> + f(1, 2, 3, 4). + +f(1, 2, 3, 4) -> + {1, 2, 3, 4, 5}. diff --git a/test/examples/pass_max_non_exported_function_arity.erl b/test/examples/pass_max_non_exported_function_arity.erl new file mode 100644 index 0000000..548c523 --- /dev/null +++ b/test/examples/pass_max_non_exported_function_arity.erl @@ -0,0 +1,30 @@ +-module(pass_max_non_exported_function_arity). + +-export([f/0, f/1, f/2]). + +f() -> + f(1). + +f(1) -> + f(1, 2). + +f(1, 2) -> + f(1, 2, 3). + +f(1, 2, 3) -> + f(1, 2, 3, 4). + +f(1, 2, 3, 4) -> + f(1, 2, 3, 4, 5). + +f(1, 2, 3, 4, 5) -> + f(1, 2, 3, 4, 5, six). + +f(1, 2, 3, 4, 5, Six) -> + f(1, 2, 3, 4, 5, Six, seven). + +f(1, 2, 3, 4, 5, Six, seven) -> + f(1, 2, 3, 4, 5, Six, seven, "eight"). + +f(1, 2, 3, 4, 5, Six, seven, "eight") -> + {1, 2, 3, 4, 5, Six, seven, "eight"}. diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index 6a9c3e4..c5f9d32 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -1033,6 +1033,28 @@ verify_max_function_arity(Config) -> #{max_arity => -1}, PathFail), + PathNonExportedPass = "pass_max_non_exported_function_arity." ++ Ext, + [] = + elvis_core_apply_rule(Config, + elvis_style, + max_function_arity, + #{max_arity => 3, non_exported_max_arity => 9}, + PathNonExportedPass), + + PathNonExportedFail = "fail_max_non_exported_function_arity." ++ Ext, + [_, _] = + elvis_core_apply_rule(Config, + elvis_style, + max_function_arity, + #{max_arity => 1, non_exported_max_arity => 2}, + PathNonExportedFail), + + [_, _, _, _, _] = + elvis_core_apply_rule(Config, + elvis_style, + max_function_arity, + #{max_arity => 3, non_exported_max_arity => same}, + PathNonExportedPass), ok. -spec verify_max_anonymous_function_arity(config()) -> any().