Skip to content

Commit c87def1

Browse files
authored
add(max_function_arity): limit for non exported functions (#379)
* add(max_function_arity): limit for non exported functions * add same option * fix doc
1 parent 1ddf014 commit c87def1

5 files changed

+90
-5
lines changed

doc_rules/elvis_style/max_function_arity.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ but it applies to regular functions only (not anonymous ones).
1212

1313
- `max_arity :: non_neg_integer()`.
1414
- default: `8`.
15+
- `non_exported_max_arity :: non_neg_integer() | same`.
16+
- default: `8`.
1517

1618
## Example
1719

1820
```erlang
1921
{elvis_style, max_function_arity}
2022
%% or
21-
{elvis_style, max_function_arity, #{max_arity => 10}}
23+
{elvis_style, max_function_arity, #{max_arity => 10, non_exported_max_arity => same}}
2224
```

src/elvis_style.erl

+17-4
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ default(max_module_length) ->
209209
default(max_anonymous_function_arity) ->
210210
#{max_arity => 5};
211211
default(max_function_arity) ->
212-
#{max_arity => 8};
212+
#{max_arity => 8, non_exported_max_arity => 10};
213213
default(max_function_length) ->
214214
#{max_length => 30,
215215
count_comments => false,
@@ -935,18 +935,31 @@ max_anonymous_function_arity(Config, Target, RuleConfig) ->
935935
end,
936936
Funs).
937937

938-
-type max_function_arity_config() :: #{max_arity => non_neg_integer()}.
938+
-type max_function_arity_config() ::
939+
#{max_arity => non_neg_integer(), non_exported_max_arity => pos_integer()}.
939940

940941
-spec max_function_arity(elvis_config:config(),
941942
elvis_file:file(),
942943
max_function_arity_config()) ->
943944
[elvis_result:item()].
944945
max_function_arity(Config, Target, RuleConfig) ->
945-
MaxArity = option(max_arity, RuleConfig, max_function_arity),
946+
ExportedMaxArity = option(max_arity, RuleConfig, max_function_arity),
947+
NonExportedMaxArity =
948+
specific_or_default(option(non_exported_max_arity, RuleConfig, max_function_arity),
949+
ExportedMaxArity),
946950
Root = get_root(Config, Target, RuleConfig),
947951
IsFunction = fun(Node) -> ktn_code:type(Node) == function end,
948952
Functions = elvis_code:find(IsFunction, Root),
949-
lists:filtermap(fun(Function) ->
953+
lists:filtermap(fun(#{attrs := #{arity := Arity, name := Name}} = Function) ->
954+
IsExported =
955+
lists:member({Name, Arity}, elvis_code:exported_functions(Root)),
956+
MaxArity =
957+
case IsExported of
958+
true ->
959+
ExportedMaxArity;
960+
false ->
961+
NonExportedMaxArity
962+
end,
950963
case ktn_code:attr(arity, Function) of
951964
Arity when Arity =< MaxArity ->
952965
false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-module(fail_max_non_exported_function_arity).
2+
3+
-export([f/0, f/1]).
4+
5+
f() ->
6+
f(1).
7+
8+
f(1) ->
9+
f(1, 2).
10+
11+
f(1, 2) ->
12+
f(1, 2, 3).
13+
14+
f(1, 2, 3) ->
15+
f(1, 2, 3, 4).
16+
17+
f(1, 2, 3, 4) ->
18+
{1, 2, 3, 4, 5}.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
-module(pass_max_non_exported_function_arity).
2+
3+
-export([f/0, f/1, f/2]).
4+
5+
f() ->
6+
f(1).
7+
8+
f(1) ->
9+
f(1, 2).
10+
11+
f(1, 2) ->
12+
f(1, 2, 3).
13+
14+
f(1, 2, 3) ->
15+
f(1, 2, 3, 4).
16+
17+
f(1, 2, 3, 4) ->
18+
f(1, 2, 3, 4, 5).
19+
20+
f(1, 2, 3, 4, 5) ->
21+
f(1, 2, 3, 4, 5, six).
22+
23+
f(1, 2, 3, 4, 5, Six) ->
24+
f(1, 2, 3, 4, 5, Six, seven).
25+
26+
f(1, 2, 3, 4, 5, Six, seven) ->
27+
f(1, 2, 3, 4, 5, Six, seven, "eight").
28+
29+
f(1, 2, 3, 4, 5, Six, seven, "eight") ->
30+
{1, 2, 3, 4, 5, Six, seven, "eight"}.

test/style_SUITE.erl

+22
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,28 @@ verify_max_function_arity(Config) ->
10611061
#{max_arity => -1},
10621062
PathFail),
10631063

1064+
PathNonExportedPass = "pass_max_non_exported_function_arity." ++ Ext,
1065+
[] =
1066+
elvis_core_apply_rule(Config,
1067+
elvis_style,
1068+
max_function_arity,
1069+
#{max_arity => 3, non_exported_max_arity => 9},
1070+
PathNonExportedPass),
1071+
1072+
PathNonExportedFail = "fail_max_non_exported_function_arity." ++ Ext,
1073+
[_, _] =
1074+
elvis_core_apply_rule(Config,
1075+
elvis_style,
1076+
max_function_arity,
1077+
#{max_arity => 1, non_exported_max_arity => 2},
1078+
PathNonExportedFail),
1079+
1080+
[_, _, _, _, _] =
1081+
elvis_core_apply_rule(Config,
1082+
elvis_style,
1083+
max_function_arity,
1084+
#{max_arity => 3, non_exported_max_arity => same},
1085+
PathNonExportedPass),
10641086
ok.
10651087

10661088
-spec verify_max_anonymous_function_arity(config()) -> any().

0 commit comments

Comments
 (0)