Skip to content

Commit

Permalink
refactor rule logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan committed Oct 24, 2024
1 parent 02881b8 commit 5d32c9c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
66 changes: 30 additions & 36 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1124,56 +1124,48 @@ atom_naming_convention(Config, Target, RuleConfig) ->
no_init_lists(Config, Target, RuleConfig) ->
Root = get_root(Config, Target, RuleConfig),

InitFuns =
ListInitClauses =
case is_relevant_behaviour(Root, RuleConfig) of
true ->
IsFunction = fun(Node) -> ktn_code:type(Node) == function end,
FunctionNodes = elvis_code:find(IsFunction, Root),
ProcessFun =
fun(FunctionNode) ->
Location = ktn_code:attr(location, FunctionNode),
Content = ktn_code:content(FunctionNode),
lists:map(fun(Elem) ->
Attributes = ktn_code:node_attr(pattern, Elem),
{Location, Attributes}
end,
Content)
IsInit1Function =
fun(Node) ->
ktn_code:type(Node) == function
andalso ktn_code:attr(name, Node) == init
andalso ktn_code:attr(arity, Node) == 1
end,
[Init1Fun] = elvis_code:find(IsInit1Function, Root),

FilterClauses =
fun(Clause) ->
[Attribute] = ktn_code:node_attr(pattern, Clause),
case is_list_node(Attribute) of
true ->
{true, ktn_code:attr(location, Clause)};
false ->
false
end
end,

lists:append(
lists:filtermap(fun(FunctionNode) ->
Name = ktn_code:attr(name, FunctionNode),
case Name of
init ->
{true, ProcessFun(FunctionNode)};
_ ->
false
end
end,
FunctionNodes));
Content = ktn_code:content(Init1Fun),
ListAttrClauses = lists:filtermap(FilterClauses, Content),
case length(ListAttrClauses) =:= length(Content) of
true ->
ListAttrClauses;
false ->
[]
end;
false ->
[]
end,

IsBreakRule =
lists:all(fun({_, Attributes}) ->
length(lists:filter(fun(Elem) -> is_list_node(Elem) end, Attributes)) =:= 1
end,
InitFuns),

ResultFun =
fun({Location, _}) ->
fun(Location) ->
Info = [Location],
Msg = ?NO_INIT_LISTS_MSG,
elvis_result:new(item, Msg, Info, Location)
end,

case IsBreakRule of
true ->
lists:map(ResultFun, InitFuns);
false ->
[]
end.
lists:map(ResultFun, ListInitClauses).

is_relevant_behaviour(Root, RuleConfig) ->
ConfigBehaviors = option(behaviours, RuleConfig, no_init_lists),
Expand All @@ -1188,6 +1180,8 @@ is_relevant_behaviour(Root, RuleConfig) ->

is_list_node(#{type := cons}) ->
true;
is_list_node(#{type := nil}) ->
true;
is_list_node(#{type := match, content := Content}) ->
lists:any(fun(Elem) -> is_list_node(Elem) end, Content);
is_list_node(_) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ start_link() ->
init(#{}) ->
ok.

init([_], udnefined) ->
init([_], undefined) ->
ok.

handle_cast(_, _) -> ok.
Expand Down
7 changes: 5 additions & 2 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1529,16 +1529,19 @@ verify_no_init_lists(Config) ->
FailPath = ExamplesDir ++ "fail_no_init_lists." ++ Ext,
FailPath2 = ExamplesDir ++ "fail_no_init_lists2." ++ Ext,
FailPath3 = ExamplesDir ++ "fail_no_init_lists3." ++ Ext,
% FailPath4 = ExamplesDir ++ "fail_no_init_lists4." ++ Ext,
FailPath4 = ExamplesDir ++ "fail_no_init_lists4." ++ Ext,

[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath),
[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath2),
[_, _, _] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath3),
% [_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath4),
[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath4),

PassPath = ExamplesDir ++ "pass_no_init_lists." ++ Ext,
PassPath2 = ExamplesDir ++ "pass_no_init_lists2." ++ Ext,
PassPath3 = ExamplesDir ++ "pass_no_init_lists3." ++ Ext,
PassPath4 = ExamplesDir ++ "pass_no_init_lists4." ++ Ext,
PassPath5 = ExamplesDir ++ "pass_no_init_lists5." ++ Ext,

[] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath),
[] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath2),
[] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath3),
Expand Down

0 comments on commit 5d32c9c

Please sign in to comment.