From fa30ca298426e66b295693326548cc0a6f5d9d9d Mon Sep 17 00:00:00 2001 From: Sargun Dhillon Date: Wed, 5 Oct 2016 14:55:48 -0700 Subject: [PATCH] Make it so that the max_function_length style checker can ignore functions --- src/elvis_style.erl | 23 ++++++++++++++++++----- test/style_SUITE.erl | 6 ++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/elvis_style.erl b/src/elvis_style.erl index 38d43600..85032b0a 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -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()], @@ -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))) @@ -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), diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index 773b74ee..f1dbada7 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -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}, @@ -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().