Skip to content

Commit

Permalink
add(max_function_arity): limit for non exported functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan committed Dec 15, 2024
1 parent e81a9d5 commit c073ad2
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 5 deletions.
4 changes: 3 additions & 1 deletion doc_rules/elvis_style/max_function_arity.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
```
19 changes: 15 additions & 4 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/examples/fail_max_non_exported_function_arity.erl
Original file line number Diff line number Diff line change
@@ -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}.
30 changes: 30 additions & 0 deletions test/examples/pass_max_non_exported_function_arity.erl
Original file line number Diff line number Diff line change
@@ -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"}.
15 changes: 15 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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().
Expand Down

0 comments on commit c073ad2

Please sign in to comment.