Skip to content

Commit

Permalink
add(max_function_arity): limit for non exported functions (#379)
Browse files Browse the repository at this point in the history
* add(max_function_arity): limit for non exported functions

* add same option

* fix doc
  • Loading branch information
bormilan authored Dec 16, 2024
1 parent 1ddf014 commit c87def1
Show file tree
Hide file tree
Showing 5 changed files with 90 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() | 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}}
```
21 changes: 17 additions & 4 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,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 @@ -935,18 +935,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;
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"}.
22 changes: 22 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1061,6 +1061,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().
Expand Down

0 comments on commit c87def1

Please sign in to comment.