Skip to content

Commit

Permalink
fix clause_num and parse_clause_num functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan committed Oct 3, 2024
1 parent 1fb6553 commit 0781bb7
Showing 1 changed file with 22 additions and 24 deletions.
46 changes: 22 additions & 24 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,9 @@ max_function_clause_length(Config, Target, RuleConfig) ->
FunLines = lists:sublist(Lines, Min, Max - Min + 1),
FilteredLines = lists:filter(FilterClause, FunLines),
L = length(FilteredLines),
ClauseNumber = case_num(Result, Name, AccNum + 1),
{[{Min, Name, ClauseNumber, L} | Result], AccNum + 1}
ClauseNumber = clause_num(Result, Name, AccNum + 1),
StringClauseNumber = parse_clause_num(ClauseNumber),
{[{Min, Name, StringClauseNumber, L} | Result], ClauseNumber}
end,
{ClauseLenInfos, _} = lists:foldl(PairClause, {[], 0}, Clauses0),

Expand All @@ -942,28 +943,25 @@ max_function_clause_length(Config, Target, RuleConfig) ->
end,
lists:map(ResultFun, ClauseLenMaxPairs).

case_num([], _, 1) ->
"1st";
case_num([{_, LastClauseName, _, _}], Name, 2) ->
case_num(LastClauseName, Name, 2);
case_num([{_, LastClauseName, _, _} | _], Name, AccNum) ->
case_num(LastClauseName, Name, AccNum);
case_num(LastClauseName, Name, AccNum) when LastClauseName =:= Name ->
lists:flatten(parse_case_num(AccNum));
case_num(_, _, _) ->
"1st".

parse_case_num(Num) ->
parse_case_num(Num, Num rem 10).

parse_case_num(Num, 1) ->
io_lib:format("~pst", [Num]);
parse_case_num(Num, 2) ->
io_lib:format("~pnd", [Num]);
parse_case_num(Num, 3) ->
io_lib:format("~prd", [Num]);
parse_case_num(Num, _) ->
io_lib:format("~pth", [Num]).
clause_num([], _, 1) ->
1;
clause_num([{_, LastClauseName, _, _}], Name, 2) ->
clause_num(LastClauseName, Name, 2);
clause_num([{_, LastClauseName, _, _} | _], Name, AccNum) ->
clause_num(LastClauseName, Name, AccNum);
clause_num(LastClauseName, Name, AccNum) when LastClauseName =:= Name ->
AccNum;
clause_num(_, _, _) ->
1.

parse_clause_num(Num) when Num rem 10 == 1 ->
integer_to_list(Num) ++ "st";
parse_clause_num(Num) when Num rem 10 == 2 ->
integer_to_list(Num) ++ "nd";
parse_clause_num(Num) when Num rem 10 == 3 ->
integer_to_list(Num) ++ "rd";
parse_clause_num(Num) ->
integer_to_list(Num) ++ "th".

-spec max_function_length(elvis_config:config(),
elvis_file:file(),
Expand Down

0 comments on commit 0781bb7

Please sign in to comment.