Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add(max_function_arity): limit for non exported functions #379

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,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 @@ -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().
Expand Down