From c073ad2f605baaf17e2d29b6c0f764d9ffe2f525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mil=C3=A1n=20B=C3=B3r?= Date: Sun, 15 Dec 2024 16:39:03 +0100 Subject: [PATCH 1/3] add(max_function_arity): limit for non exported functions --- doc_rules/elvis_style/max_function_arity.md | 4 ++- src/elvis_style.erl | 19 +++++++++--- .../fail_max_non_exported_function_arity.erl | 18 +++++++++++ .../pass_max_non_exported_function_arity.erl | 30 +++++++++++++++++++ test/style_SUITE.erl | 15 ++++++++++ 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 test/examples/fail_max_non_exported_function_arity.erl create mode 100644 test/examples/pass_max_non_exported_function_arity.erl diff --git a/doc_rules/elvis_style/max_function_arity.md b/doc_rules/elvis_style/max_function_arity.md index f70d167e..b97eb2c0 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()`. + - 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 => 12}} ``` diff --git a/src/elvis_style.erl b/src/elvis_style.erl index 36f274ed..c22298f2 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,29 @@ 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 = option(non_exported_max_arity, RuleConfig, max_function_arity), 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 00000000..07d8cc5d --- /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 00000000..548c5230 --- /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 6a9c3e41..c15e3ac6 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -1033,6 +1033,21 @@ 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), ok. -spec verify_max_anonymous_function_arity(config()) -> any(). From 9f1c988b78a249debc4170c4c9efaf58d1727632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mil=C3=A1n=20B=C3=B3r?= Date: Sun, 15 Dec 2024 16:51:53 +0100 Subject: [PATCH 2/3] add same option --- doc_rules/elvis_style/max_function_arity.md | 2 +- src/elvis_style.erl | 4 +++- test/style_SUITE.erl | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/doc_rules/elvis_style/max_function_arity.md b/doc_rules/elvis_style/max_function_arity.md index b97eb2c0..1ca953c9 100644 --- a/doc_rules/elvis_style/max_function_arity.md +++ b/doc_rules/elvis_style/max_function_arity.md @@ -12,7 +12,7 @@ but it applies to regular functions only (not anonymous ones). - `max_arity :: non_neg_integer()`. - default: `8`. -- `non_exported_max_arity :: non_neg_integer()`. +- `non_exported_max_arity :: non_neg_integer() | same`. - default: `8`. ## Example diff --git a/src/elvis_style.erl b/src/elvis_style.erl index c22298f2..944a4c2a 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -893,7 +893,9 @@ max_anonymous_function_arity(Config, Target, RuleConfig) -> [elvis_result:item()]. max_function_arity(Config, Target, RuleConfig) -> ExportedMaxArity = option(max_arity, RuleConfig, max_function_arity), - NonExportedMaxArity = option(non_exported_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), diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index c15e3ac6..c5f9d32a 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -1048,6 +1048,13 @@ verify_max_function_arity(Config) -> 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(). From d0486fd74e6d060ebb35c904cd1d94d448e5f987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mil=C3=A1n=20B=C3=B3r?= Date: Sun, 15 Dec 2024 16:53:03 +0100 Subject: [PATCH 3/3] fix doc --- doc_rules/elvis_style/max_function_arity.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc_rules/elvis_style/max_function_arity.md b/doc_rules/elvis_style/max_function_arity.md index 1ca953c9..3067ec4e 100644 --- a/doc_rules/elvis_style/max_function_arity.md +++ b/doc_rules/elvis_style/max_function_arity.md @@ -20,5 +20,5 @@ but it applies to regular functions only (not anonymous ones). ```erlang {elvis_style, max_function_arity} %% or -{elvis_style, max_function_arity, #{max_arity => 10, non_exported_max_arity => 12}} +{elvis_style, max_function_arity, #{max_arity => 10, non_exported_max_arity => same}} ```