Skip to content

Commit

Permalink
fix(no_successive_maps): prevent failing on nested maps (#372)
Browse files Browse the repository at this point in the history
* fix(no_successive_maps): prevent failing on nested maps

* fix otp 27
  • Loading branch information
bormilan authored Nov 11, 2024
1 parent 919fa6f commit c19136f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,8 @@ no_successive_maps(Config, Target, RuleConfig) ->
Root = get_root(Config, Target, RuleConfig),
Predicate = fun(Node) -> ktn_code:type(Node) == map end,
ResultFun = result_node_line_fun(?NO_SUCCESSIVE_MAPS_MSG),
case elvis_code:find(Predicate, Root) of
FindOpts = #{mode => node, traverse => all},
case elvis_code:find(Predicate, Root, FindOpts) of
[] ->
[];
MapExprs ->
Expand Down
41 changes: 41 additions & 0 deletions test/examples/fail_no_successive_maps2.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-module(fail_no_successive_maps2).

-export([test1/0, test2/0]).

-if(?OTP_RELEASE<27).
test1() ->
_ = #{
x => [
#{a => b, c => d},
#{a => b, c => d} % expected a warning here
#{a => b, c => d},
#{a => b, c => d}
]
}.
-else.
test1() ->
#{}.
-endif.

-if(?OTP_RELEASE<27).
test2() ->
_ = #{
map => #{
x => [
#{a => b, c => d},
#{a => b, c => d} % expected a warning here
#{a => b, c => d},
#{a => b, c => d}
],
y => [
#{a => b, c => d},
#{a => b, c => d} % expected a warning here
#{a => b, c => d},
#{a => b, c => d}
]
}
}.
-else.
test2() ->
#{}.
-endif.
10 changes: 8 additions & 2 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1358,13 +1358,19 @@ verify_no_successive_maps(Config) ->

Module = fail_no_successive_maps,
Path = atom_to_list(Module) ++ "." ++ Ext,

Path2 = "fail_no_successive_maps2." ++ Ext,
_ = case Group of
beam_files ->
[_, _, _] =
elvis_core_apply_rule(Config, elvis_style, no_successive_maps, #{}, Path);
elvis_core_apply_rule(Config, elvis_style, no_successive_maps, #{}, Path),
[_, _, _] =
elvis_core_apply_rule(Config, elvis_style, no_successive_maps, #{}, Path2);
erl_files ->
[#{line_num := 7}, #{line_num := 8}, #{line_num := 9}] =
elvis_core_apply_rule(Config, elvis_style, no_successive_maps, #{}, Path)
elvis_core_apply_rule(Config, elvis_style, no_successive_maps, #{}, Path),
[#{line_num := 10}, #{line_num := 26}, #{line_num := 32}] =
elvis_core_apply_rule(Config, elvis_style, no_successive_maps, #{}, Path2)
end,

[] =
Expand Down

0 comments on commit c19136f

Please sign in to comment.