Skip to content

Commit

Permalink
[inaka/elvis#342] function references shouldn't count as a level
Browse files Browse the repository at this point in the history
  • Loading branch information
jfacorro committed Mar 20, 2017
1 parent 09158e3 commit af54824
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
13 changes: 8 additions & 5 deletions src/elvis_code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ past_nesting_limit(Node, CurrentLevel, MaxLevel) when CurrentLevel > MaxLevel ->
past_nesting_limit(#{content := Content},
CurrentLevel,
MaxLevel) ->
Fun = fun(ChildNode = #{type := Type}) ->
Increment = level_increment(Type),
Fun = fun(ChildNode) ->
Increment = level_increment(ChildNode),
past_nesting_limit(ChildNode,
Increment + CurrentLevel,
MaxLevel)
Expand Down Expand Up @@ -216,14 +216,17 @@ function_names(#{type := root, content := Content}) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @private
%% @doc Takes a node type and determines its nesting level increment.
level_increment(Type) ->
%% @doc Takes a node and determines its nesting level increment.
level_increment(#{type := 'fun', content := _}) ->
1;
level_increment(#{type := 'fun'}) ->
0;
level_increment(#{type := Type}) ->
IncrementOne = [function,
'case',
'if',
try_case,
try_catch,
'fun',
named_fun,
receive_case
],
Expand Down
24 changes: 24 additions & 0 deletions test/examples/fail_nesting_level.erl
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,27 @@ exceed_with_list_compr() ->
end;
3 -> 3
end.

exceed_with_fun() ->
case 1 of
1 -> ok;
2 -> receive
1 -> ok;
2 -> ok;
3 -> fun() -> ok end;
4 -> ok
end;
3 -> 3
end.

dont_exceed_with_fun() ->
case 1 of
1 -> ok;
2 -> receive
1 -> ok;
2 -> ok;
3 -> fun erlang:display/1;
4 -> ok
end;
3 -> 3
end.
18 changes: 9 additions & 9 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ all() ->

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
ok = application:start(elvis),
_ = application:ensure_all_started(elvis),
Config.

-spec end_per_suite(config()) -> config().
Expand Down Expand Up @@ -277,14 +277,14 @@ verify_nesting_level(_Config) ->
Path = "fail_nesting_level.erl",
{ok, File} = elvis_test_utils:find_file(SrcDirs, Path),

[
#{line_num := 11},
#{line_num := 18},
#{line_num := 30},
#{line_num := 45},
#{line_num := 78},
#{line_num := 120},
#{line_num := 166}
[ #{line_num := 11}
, #{line_num := 18}
, #{line_num := 30}
, #{line_num := 45}
, #{line_num := 78}
, #{line_num := 120}
, #{line_num := 166}
, #{line_num := 182}
] = elvis_style:nesting_level(ElvisConfig, File, #{level => 3}),
[] = elvis_style:nesting_level( ElvisConfig
, File
Expand Down

0 comments on commit af54824

Please sign in to comment.