Skip to content

Commit

Permalink
Merge pull request #260 from inaka/jfacorro.152.max_module_length
Browse files Browse the repository at this point in the history
[Closes #152] Max module length rule
  • Loading branch information
Brujo Benavides committed Aug 28, 2015
2 parents e316259 + bb87c47 commit 9642bbe
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 7 deletions.
32 changes: 31 additions & 1 deletion src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
module_naming_convention/3,
state_record_and_type/3,
no_spec_with_records/3,
dont_repeat_yourself/3
dont_repeat_yourself/3,
max_module_length/3
]).

-define(LINE_LENGTH_MSG, "Line ~p is too long: ~s.").
Expand Down Expand Up @@ -86,6 +87,10 @@
"The code in the following (LINE, COL) locations has "
"the same structure: ~s.").

-define(MAX_MODULE_LENGTH,
"The code for module ~p has ~p lines which exceeds the "
"maximum of ~p.").

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Rules
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -377,6 +382,31 @@ dont_repeat_yourself(Config, Target, RuleConfig) ->
lists:map(ResultFun, Nodes)
end.

-spec max_module_length(elvis_config:config(),
elvis_file:file(),
empty_rule_config()) ->
[elvis_result:item()].
max_module_length(Config, Target, RuleConfig) ->
MaxLength = maps:get(max_length, RuleConfig, 500),
IgnoreModules = maps:get(ignore, RuleConfig, []),

{Root, _} = elvis_file:parse_tree(Config, Target),
{Src, _} = elvis_file:src(Target),

ModuleName = elvis_code:module_name(Root),
Lines = binary:split(Src, <<"\n">>, [global, trim]),
Ignored = lists:member(ModuleName, IgnoreModules),

case length(Lines) of
L when L > MaxLength, not Ignored ->
Info = [ModuleName, L, MaxLength],
Msg = ?MAX_MODULE_LENGTH,
Result = elvis_result:new(item, Msg, Info, 0),
[Result];
_ ->
[]
end.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Private
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down
15 changes: 15 additions & 0 deletions test/examples/fail_max_module_length.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-module(fail_max_module_length).

-export([f/1]).











f(_) -> ok.
4 changes: 2 additions & 2 deletions test/examples/fail_operator_spaces.erl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ tag_filters(DocName, #{conn := Conn} = State) ->
"ORDER BY tag_name "],
Values = [],
case {Conn, Sql, Values} of
{ok, Maps} ->
{ok, Maps, _} ->
{ok, {raw, Maps}, State};
{error, Error} ->
{error, Error, _} ->
{error, Error, State}
end.
26 changes: 22 additions & 4 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
verify_state_record_and_type/1,
verify_no_spec_with_records/1,
verify_dont_repeat_yourself/1,
verify_order_line_results/1
verify_max_module_length/1,
%% Non-rule
results_are_ordered_by_line/1
]).

-define(EXCLUDED_FUNS,
Expand Down Expand Up @@ -333,8 +335,24 @@ verify_dont_repeat_yourself(_Config) ->
{ok, FilePass} = elvis_test_utils:find_file(SrcDirs, PathPass),
[] = elvis_style:dont_repeat_yourself(ElvisConfig, FilePass, RuleConfig5).

-spec verify_order_line_results(config()) -> any().
verify_order_line_results(_Config) ->
-spec verify_max_module_length(config()) -> any().
verify_max_module_length(_Config) ->
ElvisConfig = elvis_config:default(),
SrcDirs = elvis_config:dirs(ElvisConfig),

PathFail = "fail_max_module_length.erl",
{ok, FileFail} = elvis_test_utils:find_file(SrcDirs, PathFail),
RuleConfig = #{max_length => 10},
[_] = elvis_style:max_module_length(ElvisConfig, FileFail, RuleConfig),

RuleConfig1 = #{max_length => 14},
[_] = elvis_style:max_module_length(ElvisConfig, FileFail, RuleConfig1),

RuleConfig2 = #{max_length => 15},
[] = elvis_style:max_module_length(ElvisConfig, FileFail, RuleConfig2).

-spec results_are_ordered_by_line(config()) -> any().
results_are_ordered_by_line(_Config) ->
ElvisConfig = elvis_config:default(),
{fail, Results} = elvis:rock(ElvisConfig),
true = lists:all(fun(X) -> X end, is_item_line_sort(Results)).
Expand All @@ -358,4 +376,4 @@ is_list_sort([#{line_num := Line1} | T1]) ->
case Line1 =< Line2 of
true -> is_list_sort(T1);
false -> false
end.
end.

0 comments on commit 9642bbe

Please sign in to comment.