Skip to content

Commit

Permalink
Merge pull request #62 from sargun/master
Browse files Browse the repository at this point in the history
Make it so that the max_function_length style checker can ignore func…
  • Loading branch information
Brujo Benavides authored Oct 6, 2016
2 parents a6d5852 + fa30ca2 commit 387f664
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
23 changes: 18 additions & 5 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@

-type empty_rule_config() :: #{}.

-type max_function_length_config() :: #{ignore_functions => [function_spec()],
max_length => non_neg_integer()}.

-type max_module_length_config() :: #{count_comments => boolean(),
count_whitespace => boolean(),
ignore => [atom()],
Expand Down Expand Up @@ -519,20 +522,21 @@ max_module_length(Config, Target, RuleConfig) ->

-spec max_function_length(elvis_config:config(),
elvis_file:file(),
empty_rule_config()) ->
max_function_length_config()) ->
[elvis_result:item()].
max_function_length(Config, Target, RuleConfig) ->
IgnoreFunctions = maps:get(ignore_functions, RuleConfig, []),
MaxLength = maps:get(max_length, RuleConfig, 30),
CountComments = maps:get(count_comments, RuleConfig, false),
CountWhitespace = maps:get(count_whitespace, RuleConfig, false),

{Root, _} = elvis_file:parse_tree(Config, Target),
{Src, _} = elvis_file:src(Target),
ModuleName = elvis_code:module_name(Root),
Lines = binary:split(Src, <<"\n">>, [global, trim]),

IsFunction = fun(Node) -> ktn_code:type(Node) == function end,
Functions = elvis_code:find(IsFunction, Root),

Functions0 = elvis_code:find(IsFunction, Root),
FilterFun =
fun(Line) ->
(CountComments orelse (not line_is_comment(Line)))
Expand All @@ -549,8 +553,17 @@ max_function_length(Config, Target, RuleConfig) ->
L = length(FilteredLines),
{Name, Min, L}
end,

FunLenInfos = lists:map(PairFun, Functions),
IgnoreFunctionFilterFun =
fun(FunctionNode) ->
FunctionName = ktn_code:attr(name, FunctionNode),
FunctionArity = ktn_code:attr(arity, FunctionNode),
MFA = {ModuleName, FunctionName, FunctionArity},
MF = {ModuleName, FunctionName},
not (lists:member(MF, IgnoreFunctions)
orelse lists:member(MFA, IgnoreFunctions))
end,
Functions1 = lists:filter(IgnoreFunctionFilterFun, Functions0),
FunLenInfos = lists:map(PairFun, Functions1),
MaxLengthPred = fun({_, _, L}) -> L > MaxLength end,
FunLenMaxPairs = lists:filter(MaxLengthPred, FunLenInfos),

Expand Down
6 changes: 6 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,8 @@ verify_max_function_length(_Config) ->
SrcDirs = elvis_config:dirs(ElvisConfig),

PathFail = "fail_max_function_length.erl",
ModuleFail = fail_max_function_length,

{ok, FileFail} = elvis_test_utils:find_file(SrcDirs, PathFail),

CountAllRuleConfig = #{count_comments => true, count_whitespace => true},
Expand Down Expand Up @@ -507,6 +509,10 @@ verify_max_function_length(_Config) ->
RuleConfig10 = NoCountRuleConfig#{max_length => 2},
[] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig10),

IgnoredFunctions = [{ModuleFail, f15}, {ModuleFail, f10, 1}],
RuleConfig11 = RuleConfig5#{ignore_functions => IgnoredFunctions},
[] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig11),

{comment, ""}.

-spec verify_no_debug_call(config()) -> any().
Expand Down

0 comments on commit 387f664

Please sign in to comment.