Skip to content

Commit

Permalink
[#15] Piggybacked improvements for #14 (Nesting Level).
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Jul 18, 2014
1 parent c94464d commit 899cb7d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
38 changes: 34 additions & 4 deletions src/elvis_code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

-export([
parse_tree/1,
past_nesting_limit/2
past_nesting_limit/2,
print_node/1,
print_node/2
]).

-export([
Expand Down Expand Up @@ -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) ->
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
32 changes: 31 additions & 1 deletion test/examples/fail_nesting_level.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
3 changes: 2 additions & 1 deletion test/rules_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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]).

0 comments on commit 899cb7d

Please sign in to comment.