From 88b253739426d289126e9c537e9b0d8fa905871a Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Mon, 31 Aug 2015 15:16:32 -0300 Subject: [PATCH 1/3] [#263] Options for max function length --- src/elvis_style.erl | 29 +++++++++++---- test/examples/fail_max_function_length.erl | 16 ++++---- test/style_SUITE.erl | 43 +++++++++++++++++++--- 3 files changed, 67 insertions(+), 21 deletions(-) diff --git a/src/elvis_style.erl b/src/elvis_style.erl index 5b4d233..75982ac 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -431,27 +431,42 @@ max_module_length(Config, Target, RuleConfig) -> [elvis_result:item()]. max_function_length(Config, Target, 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), + Lines = binary:split(Src, <<"\n">>, [global, trim]), IsFunction = fun(Node) -> ktn_code:type(Node) == function end, Functions = elvis_code:find(IsFunction, Root), + + FilterFun = + fun(Line) -> + (CountComments orelse (not line_is_comment(Line))) + andalso (CountWhitespace + orelse (not line_is_whitespace(Line))) + end, + PairFun = fun(FunctionNode) -> Name = ktn_code:attr(name, FunctionNode), {Min, Max} = node_line_limits(FunctionNode), - L = (Max - Min) + 1, - {Name, L} + FunLines = lists:sublist(Lines, Min, Max - Min + 1), + FilteredLines = lists:filter(FilterFun, FunLines), + L = length(FilteredLines), + {Name, Min, L} end, - FunLenPairs = lists:map(PairFun, Functions), - MaxLengthPred = fun({_, L}) -> L > MaxLength end, - FunLenMaxPairs = lists:filter(MaxLengthPred, FunLenPairs), + + FunLenInfos = lists:map(PairFun, Functions), + MaxLengthPred = fun({_, _, L}) -> L > MaxLength end, + FunLenMaxPairs = lists:filter(MaxLengthPred, FunLenInfos), ResultFun = - fun({Name, L}) -> + fun({Name, StartPos, L}) -> Info = [Name, L, MaxLength], Msg = ?MAX_FUNCTION_LENGTH, - elvis_result:new(item, Msg, Info, 0) + elvis_result:new(item, Msg, Info, StartPos) end, lists:map(ResultFun, FunLenMaxPairs). diff --git a/test/examples/fail_max_function_length.erl b/test/examples/fail_max_function_length.erl index b5102b9..5fe666e 100644 --- a/test/examples/fail_max_function_length.erl +++ b/test/examples/fail_max_function_length.erl @@ -5,32 +5,32 @@ f5(_) -> %% 1 %% 2 %% 3 - %% 4 + ok. %% 5 f10(_) -> %% 1 %% 2 %% 3 %% 4 - %% 5 + %% 6 %% 7 - %% 8 + %% 9 - ok. + ok. %% 10 f15(_) -> %% 1 %% 2 %% 3 %% 4 - %% 5 + %% 6 %% 7 %% 8 %% 9 - %% 10 + %% 11 %% 12 - %% 13 + %% 14 - ok. + ok. %% 15 diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index 252d7b0..81e7cc9 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -387,19 +387,50 @@ verify_max_function_length(_Config) -> PathFail = "fail_max_function_length.erl", {ok, FileFail} = elvis_test_utils:find_file(SrcDirs, PathFail), - RuleConfig = #{max_length => 4}, + + CountAllRuleConfig = #{count_comments => true, count_whitespace => true}, + + ct:comment("Count whitespace and comment lines"), + RuleConfig = CountAllRuleConfig#{max_length => 4}, [_, _, _] = - elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig), + elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig), - RuleConfig1 = #{max_length => 9}, + RuleConfig1 = CountAllRuleConfig#{max_length => 9}, [_, _] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig1), - RuleConfig2 = #{max_length => 14}, + RuleConfig2 = CountAllRuleConfig#{max_length => 14}, [_] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig2), - RuleConfig3 = #{max_length => 15}, - [] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig3). + RuleConfig3 = CountAllRuleConfig#{max_length => 15}, + [] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig3), + + ct:comment("Don't count whitespace lines"), + WhitespaceRuleConfig = CountAllRuleConfig#{count_whitespace => false}, + + RuleConfig4 = WhitespaceRuleConfig#{max_length => 3}, + [_, _, _] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig4), + + RuleConfig5 = WhitespaceRuleConfig#{max_length => 7}, + [_, _] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig5), + + RuleConfig6 = WhitespaceRuleConfig#{max_length => 8}, + [_] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig6), + + RuleConfig7 = WhitespaceRuleConfig#{max_length => 11}, + [_] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig7), + + RuleConfig8 = WhitespaceRuleConfig#{max_length => 12}, + [] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig8), + + ct:comment("Don't count comment or whitespace lines"), + NoCountRuleConfig = WhitespaceRuleConfig#{count_comments => false}, + + RuleConfig9 = NoCountRuleConfig#{max_length => 1}, + [_, _, _] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig9), + + RuleConfig10 = NoCountRuleConfig#{max_length => 2}, + [] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig10). -spec results_are_ordered_by_line(config()) -> any(). results_are_ordered_by_line(_Config) -> From 6fe583d334273c8117b9702ff7a13c80902df958 Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Mon, 31 Aug 2015 15:20:12 -0300 Subject: [PATCH 2/3] [#263] Fix elvis comments --- test/style_SUITE.erl | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index 81e7cc9..9f0a4b8 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -409,10 +409,12 @@ verify_max_function_length(_Config) -> WhitespaceRuleConfig = CountAllRuleConfig#{count_whitespace => false}, RuleConfig4 = WhitespaceRuleConfig#{max_length => 3}, - [_, _, _] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig4), + [_, _, _] = + elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig4), RuleConfig5 = WhitespaceRuleConfig#{max_length => 7}, - [_, _] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig5), + [_, _] = + elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig5), RuleConfig6 = WhitespaceRuleConfig#{max_length => 8}, [_] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig6), @@ -427,7 +429,8 @@ verify_max_function_length(_Config) -> NoCountRuleConfig = WhitespaceRuleConfig#{count_comments => false}, RuleConfig9 = NoCountRuleConfig#{max_length => 1}, - [_, _, _] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig9), + [_, _, _] = + elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig9), RuleConfig10 = NoCountRuleConfig#{max_length => 2}, [] = elvis_style:max_function_length(ElvisConfig, FileFail, RuleConfig10). From 3a4ce04042de73705b208be9213116be4cecb22a Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Mon, 31 Aug 2015 15:23:32 -0300 Subject: [PATCH 3/3] [#263] Increase min_complexity for DRY --- elvis.config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/elvis.config b/elvis.config index 63b7b4b..f942fe8 100644 --- a/elvis.config +++ b/elvis.config @@ -28,7 +28,7 @@ }, {elvis_style, state_record_and_type}, {elvis_style, no_spec_with_records}, - {elvis_style, dont_repeat_yourself, #{min_complexity => 10}} + {elvis_style, dont_repeat_yourself, #{min_complexity => 20}} ] }, #{dirs => ["."],