From 899cb7d851849f83957d58714831945d2bf4d19c Mon Sep 17 00:00:00 2001 From: Juan Facorro Date: Fri, 18 Jul 2014 11:35:37 -0300 Subject: [PATCH] [#15] Piggybacked improvements for #14 (Nesting Level). --- src/elvis_code.erl | 38 +++++++++++++++++++++++++--- test/examples/fail_nesting_level.erl | 32 ++++++++++++++++++++++- test/rules_SUITE.erl | 3 ++- 3 files changed, 67 insertions(+), 6 deletions(-) diff --git a/src/elvis_code.erl b/src/elvis_code.erl index 1e18f3c..8ede791 100644 --- a/src/elvis_code.erl +++ b/src/elvis_code.erl @@ -2,7 +2,9 @@ -export([ parse_tree/1, - past_nesting_limit/2 + past_nesting_limit/2, + print_node/1, + print_node/2 ]). -export([ @@ -75,6 +77,18 @@ past_nesting_limit(#{content := Content}, past_nesting_limit(_Node, _CurrentLeve, _MaxLevel) -> []. +%% @doc Debugging utility function. +-spec print_node(tree_node()) -> ok. +print_node(Node) -> + print_node(Node, 0). + +-spec print_node(tree_node(), integer()) -> ok. +print_node(Node = #{type := Type}, CurrentLevel) -> + Indentation = lists:duplicate(CurrentLevel * 4, 32), + {Line, _} = elvis_code:attr(location, Node), + lager:info("~s - [~p] ~p : ~p~n", + [Indentation, CurrentLevel, Type, Line]). + %% @private %% @doc Takes a node type and determines its nesting level increment. level_increment(Type) -> @@ -206,9 +220,19 @@ to_map({remote, Location, Module, Function}) -> %% case to_map({'case', Location, Expr, Clauses}) -> + CaseExpr = to_map({case_expr, Location, Expr}), + CaseClauses = to_map({case_clauses, Location, Clauses}), #{type => 'case', attrs => #{location => Location, expression => to_map(Expr)}, + content => [CaseExpr, CaseClauses]}; +to_map({case_expr, Location, Expr}) -> + #{type => case_expr, + attrs => #{location => Location}, + content => [to_map(Expr)]}; +to_map({case_clauses, Location, Clauses}) -> + #{type => case_clauses, + attrs => #{location => Location}, content => to_map(Clauses)}; %% fun @@ -359,15 +383,21 @@ to_map({Type, Location, Key, Value}) when %% List Comprehension to_map({lc, Location, Expr, GeneratorsFilters}) -> + LcExpr = to_map({lc_expr, Location, Expr}), + LcGenerators = to_map(GeneratorsFilters), #{type => lc, - attrs => #{location => Location, - expression => to_map(Expr)}, - content => to_map(GeneratorsFilters)}; + attrs => #{location => Location}, + content => [LcExpr | LcGenerators]}; + to_map({generate, Location, Pattern, Expr}) -> #{type => generate, attrs => #{location => Location, pattern => to_map(Pattern), expression => to_map(Expr)}}; +to_map({lc_expr, Location, Expr}) -> + #{type => lc_expr, + attrs => #{location => Location}, + content => [to_map(Expr)]}; %% Operation diff --git a/test/examples/fail_nesting_level.erl b/test/examples/fail_nesting_level.erl index 102047d..8eff925 100644 --- a/test/examples/fail_nesting_level.erl +++ b/test/examples/fail_nesting_level.erl @@ -124,7 +124,7 @@ exceed_with_receive() -> 3 -> 3 end. -exceed_with_receive_after() -> +dont_exceed_with_receive_after() -> case 1 of 1 -> ok; 2 -> receive @@ -140,3 +140,33 @@ exceed_with_receive_after() -> end; 3 -> 3 end. + +dont_exceed_with_list_compr() -> + case 1 of + 1 -> ok; + 2 -> receive + 1 -> ok; + 2 -> ok; + 3 -> ok + after + 1000 -> + [X || X <- [1, 2, 3]] + end; + 3 -> 3 + end. + +exceed_with_list_compr() -> + case 1 of + 1 -> ok; + 2 -> receive + 1 -> ok; + 2 -> ok; + 3 -> [case X of + 1 -> ok; + _ -> not_ok + end + || X <- [1, 2, 3]]; + 4 -> ok + end; + 3 -> 3 + end. diff --git a/test/rules_SUITE.erl b/test/rules_SUITE.erl index 3216142..2850ebc 100644 --- a/test/rules_SUITE.erl +++ b/test/rules_SUITE.erl @@ -126,4 +126,5 @@ verify_nesting_level(_Config) -> #{line_num := 28}, #{line_num := 43}, #{line_num := 76}, - #{line_num := 118}] = elvis_style:nesting_level(ElvisConfig, File, [3]). + #{line_num := 118}, + #{line_num := 164}] = elvis_style:nesting_level(ElvisConfig, File, [3]).