Skip to content

Commit

Permalink
refactor some logic + new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan committed Nov 20, 2024
1 parent cc49c25 commit 0dca68f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
18 changes: 3 additions & 15 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1230,14 +1230,7 @@ ms_transform_included(Config, Target, RuleConfig) ->
-spec get_fun_2_ms_calls(ktn_code:tree_node()) -> [any()].
get_fun_2_ms_calls(Root) ->
IsFun2MsFunctionCall =
fun(Node) ->
case ktn_code:type(Node) == call of
true ->
is_ets_fun2ms(Node);
false ->
false
end
end,
fun(Node) -> ktn_code:type(Node) == call andalso is_ets_fun2ms(Node) end,

Functions = elvis_code:find(IsFun2MsFunctionCall, Root),
ProcessResult = fun(Node) -> ktn_code:attr(location, Node) end,
Expand All @@ -1250,7 +1243,7 @@ is_ets_fun2ms(Node) ->
Fun2 = ktn_code:node_attr(function, Fun),
Module = ktn_code:node_attr(module, Fun),

{ets, fun2ms} == {ktn_code:attr(value, Module), ktn_code:attr(value, Fun2)}.
ets == ktn_code:attr(value, Module) andalso fun2ms == ktn_code:attr(value, Fun2).

-spec is_include_ms_transform(ktn_code:tree_node()) -> boolean().
is_include_ms_transform(Root) ->
Expand All @@ -1259,12 +1252,7 @@ is_include_ms_transform(Root) ->
andalso ktn_code:attr(value, Node) == "stdlib/include/ms_transform.hrl"
end,

case elvis_code:find(Fun, Root) of
[_] ->
true;
_ ->
false
end.
[] /= elvis_code:find(Fun, Root).

-spec no_throw(elvis_config:config(), elvis_file:file(), empty_rule_config()) ->
[elvis_result:item()].
Expand Down
5 changes: 5 additions & 0 deletions test/examples/custom_ms_transform_included.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-module(custom_ms_transform_included).

-export([test/0]).

test() -> {this, is, "not", ets, my:fun2ms("It's my own")}.
8 changes: 8 additions & 0 deletions test/examples/double_include_ms_transform.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-module(double_include_ms_transform).

-include_lib("stdlib/include/ms_transform.hrl").
-include_lib("stdlib/include/ms_transform.hrl").

-export([test/0]).

test() -> ets:fun2ms(fun(_) -> ok end).
7 changes: 7 additions & 0 deletions test/examples/included_but_not_used_ms_transform.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-module(included_but_not_used_ms_transform).

-include_lib("stdlib/include/ms_transform.hrl").

-export([test/0]).

test() -> ok.
20 changes: 20 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,26 @@ verify_ms_transform_included(Config) ->
PassPath = "pass_ms_transform_included." ++ Ext,
[] = elvis_core_apply_rule(Config, elvis_style, ms_transform_included, #{}, PassPath),

CustomFunctionPath = "custom_ms_transform_included." ++ Ext,
[] =
elvis_core_apply_rule(Config,
elvis_style,
ms_transform_included,
#{},
CustomFunctionPath),

IncludedButNotUsed = "included_but_not_used_ms_transform." ++ Ext,
[] =
elvis_core_apply_rule(Config,
elvis_style,
ms_transform_included,
#{},
IncludedButNotUsed),

DoubleInclude = "double_include_ms_transform." ++ Ext,
[] =
elvis_core_apply_rule(Config, elvis_style, ms_transform_included, #{}, DoubleInclude),

FailPath = "fail_ms_transform_included." ++ Ext,
[_] = elvis_core_apply_rule(Config, elvis_style, ms_transform_included, #{}, FailPath),
ok.
Expand Down

0 comments on commit 0dca68f

Please sign in to comment.