From c19136ff892ca0c45bcc86215bc9e030c4c44d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=B3r=20Mil=C3=A1n?= <71042542+bormilan@users.noreply.github.com> Date: Mon, 11 Nov 2024 08:22:50 +0100 Subject: [PATCH] fix(no_successive_maps): prevent failing on nested maps (#372) * fix(no_successive_maps): prevent failing on nested maps * fix otp 27 --- src/elvis_style.erl | 3 +- test/examples/fail_no_successive_maps2.erl | 41 ++++++++++++++++++++++ test/style_SUITE.erl | 10 ++++-- 3 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 test/examples/fail_no_successive_maps2.erl diff --git a/src/elvis_style.erl b/src/elvis_style.erl index c9a5718..e52ba9f 100644 --- a/src/elvis_style.erl +++ b/src/elvis_style.erl @@ -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 -> diff --git a/test/examples/fail_no_successive_maps2.erl b/test/examples/fail_no_successive_maps2.erl new file mode 100644 index 0000000..9ced0bf --- /dev/null +++ b/test/examples/fail_no_successive_maps2.erl @@ -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. diff --git a/test/style_SUITE.erl b/test/style_SUITE.erl index 4508200..08be829 100644 --- a/test/style_SUITE.erl +++ b/test/style_SUITE.erl @@ -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, [] =