Skip to content

Commit bd2e246

Browse files
committed
fix bugs and suggestions
1 parent 1a95664 commit bd2e246

File tree

5 files changed

+160
-10
lines changed

5 files changed

+160
-10
lines changed

src/elvis_style.erl

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,15 +2769,14 @@ macro_definition_parentheses(Rule, ElvisConfig) ->
27692769
-spec strict_module_layout(elvis_rule:t(), elvis_config:t()) -> [elvis_result:item()].
27702770
strict_module_layout(Rule, ElvisConfig) ->
27712771
Nodes = ktn_code:content(elvis_code:root(Rule, ElvisConfig)),
2772+
Order = elvis_rule:option(order, Rule),
27722773

27732774
NotInPlace =
27742775
fun(Node, {Prev, Result}) ->
2775-
Order = elvis_rule:option(order, Rule),
2776-
27772776
Type = ktn_code:type(Node),
27782777
Category = get_category(Type),
27792778

2780-
case not ignore(Type, Category, Order) of
2779+
case not ignore_category(Type, Category, Order) of
27812780
true -> valid_order(Category, Prev, Node, Result, Order);
27822781
false -> {Prev, Result}
27832782
end
@@ -2786,7 +2785,7 @@ strict_module_layout(Rule, ElvisConfig) ->
27862785
{_, ResultNodes} = lists:foldl(NotInPlace, {nil, []}, Nodes),
27872786

27882787
lists:map(
2789-
fun({CorrectPrev, ActualPrev, NodeTypeCategory, Node}) ->
2788+
fun({ActualPrev, NodeTypeCategory, Node}) ->
27902789
elvis_result:new_item(
27912790
"A ~p type element was found after a ~p type element."
27922791
" That doesn't respect the expected module layout: ~p",
@@ -2808,11 +2807,11 @@ valid_order(Category, Prev, Node, Result, Order) ->
28082807
Category =:= Prev
28092808
of
28102809
true -> {Category, Result};
2811-
false -> {Category, [{CorrectPrev, Prev, Category, Node} | Result]}
2810+
false -> {Category, [{Prev, Category, Node} | Result]}
28122811
end.
28132812

2814-
ignore(Type, Category, Order) ->
2815-
lists:member(Type, [comment, ifdef, ifndef, 'else', 'if', elif, endif]) orelse
2813+
ignore_category(Type, Category, Order) ->
2814+
lists:member(Type, [comment, ifdef, ifndef, else_attr, 'if', elif, endif]) orelse
28162815
not lists:member(Category, Order).
28172816

28182817
get_category(Type) ->
@@ -2827,9 +2826,16 @@ get_category(Type) ->
28272826
{function, body},
28282827
{spec, body},
28292828
{export, exports},
2830-
{export_type, exports},
2829+
{export_type, export_types},
28312830
{define, defines},
2832-
{comment, comment}
2831+
{comment, comment},
2832+
{doc, comment},
2833+
{moduledoc, moduledoc},
2834+
{import, import},
2835+
{compile, compile},
2836+
{vsn, vsn},
2837+
{behaviour, behaviour},
2838+
{record, record}
28332839
],
28342840
proplists:get_value(Type, Categories, custom).
28352841

test/examples/fail_strict_module_layout.erl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,27 @@
55
-export_type([my_type/0]).
66

77
-export([some_fun/0]).
8+
-import(test_module, []).
9+
10+
-behaviour(gen_event).
11+
-export([init/1, handle_event/2]).
812

913
-type my_type() :: ok.
14+
-export([handle_call/2]).
1015

1116
-mixin([]).
17+
-doc("doc").
18+
-vsn(1.0).
19+
-moduledoc("").
1220

1321
-spec some_fun() -> my_type().
1422
some_fun() -> ok.
1523

1624
-dialyzer({nowarn_function, []}).
1725

26+
-compile("").
27+
28+
handle_call(_, _) -> ok.
29+
handle_event(_, _) -> ok.
30+
init(_) -> ok.
31+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-module(fail_strict_module_layout_allways).
2+
3+
-type bananas() :: bananas.
4+
-export_type([bananas/0, tomatoes/0]).
5+
-type tomatoes() :: tomatoes.
6+

test/examples/pass_strict_module_layout.erl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
-module(pass_strict_module_layout).
2+
-moduledoc("").
3+
4+
-vsn(1.0).
25

36
-include("./prefer_include.hrl").
47

@@ -8,6 +11,8 @@
811
-hank([]).
912
-import(test_module, []).
1013

14+
-behaviour(gen_event).
15+
1116
-type my_type() :: ok.
1217
-opaque my_opaque() :: definition.
1318

@@ -16,11 +21,26 @@
1621
-export_type([name/0]).
1722
-endif.
1823

24+
-if(ok).
25+
-type apple() :: apple.
26+
-export_type([apple/0]).
27+
-elif(notok).
28+
-type orange() :: orange.
29+
-export_type([orange/0]).
30+
-else.
31+
-type banana() :: banana.
32+
-export_type([banana/0]).
33+
-endif.
34+
1935
-export_type([my_opaque/0]).
2036
-export([some_fun/0, some_fun_2/0, some_fun_3/0, some_fun_4/0]).
37+
-export([init/1, handle_call/2, handle_event/2]).
38+
2139

2240
-define(MACRO, ok).
2341

42+
-doc("doc").
43+
2444
-spec some_fun() -> my_type().
2545
some_fun() -> ?MACRO.
2646

@@ -32,3 +52,7 @@ some_fun_3() -> ok.
3252

3353
-spec some_fun_4() -> my_type().
3454
some_fun_4() -> ok.
55+
56+
handle_call(_, _) -> ok.
57+
handle_event(_, _) -> ok.
58+
init(_) -> ok.

test/style_SUITE.erl

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3223,14 +3223,62 @@ verify_strict_module_layout(Config) ->
32233223
Config, elvis_style, strict_module_layout, #{order => Order}, PassPath
32243224
),
32253225

3226+
% causing trouble with a poor order
3227+
[
3228+
#{line_num := 2},
3229+
#{line_num := 4},
3230+
#{line_num := 14},
3231+
#{line_num := 21},
3232+
#{line_num := 42},
3233+
#{line_num := 44}
3234+
] =
3235+
elvis_test_utils:elvis_core_apply_rule(
3236+
Config,
3237+
elvis_style,
3238+
strict_module_layout,
3239+
#{order => [module, vsn, moduledoc, behaviour, comment, export_types, exports, body]},
3240+
PassPath
3241+
),
3242+
3243+
% empty order list, same as disabling thiss rule
3244+
[] =
3245+
elvis_test_utils:elvis_core_apply_rule(
3246+
Config, elvis_style, strict_module_layout, #{order => []}, PassPath
3247+
),
3248+
32263249
FailModule = fail_strict_module_layout,
32273250
FailPath = atom_to_list(FailModule) ++ "." ++ Ext,
32283251

3229-
[_, _, _, _, _, _] =
3252+
[_, _, _, _, _, _, _] =
32303253
elvis_test_utils:elvis_core_apply_rule(
32313254
Config, elvis_style, strict_module_layout, #{order => Order}, FailPath
32323255
),
32333256

3257+
% check a lot of stuff
3258+
[_, _, _, _, _, _, _, _, _, _, _, _] =
3259+
elvis_test_utils:elvis_core_apply_rule(
3260+
Config,
3261+
elvis_style,
3262+
strict_module_layout,
3263+
#{
3264+
order => [
3265+
module,
3266+
moduledoc,
3267+
vsn,
3268+
compile,
3269+
behaviour,
3270+
record,
3271+
import,
3272+
types,
3273+
comment,
3274+
exports,
3275+
body
3276+
]
3277+
},
3278+
FailPath
3279+
),
3280+
3281+
% let's check just a few parts
32343282
[] =
32353283
elvis_test_utils:elvis_core_apply_rule(
32363284
Config, elvis_style, strict_module_layout, #{order => [module]}, FailPath
@@ -3243,6 +3291,58 @@ verify_strict_module_layout(Config) ->
32433291
strict_module_layout,
32443292
#{order => [module, exports, body]},
32453293
FailPath
3294+
),
3295+
3296+
% empty order list, same as disabling thiss rule
3297+
[] =
3298+
elvis_test_utils:elvis_core_apply_rule(
3299+
Config, elvis_style, strict_module_layout, #{order => []}, FailPath
3300+
),
3301+
3302+
% this allways will fail
3303+
AllwaysFailModule = fail_strict_module_layout_allways,
3304+
AllwaysFailPath = atom_to_list(AllwaysFailModule) ++ "." ++ Ext,
3305+
3306+
[#{line_num := 5}] =
3307+
elvis_test_utils:elvis_core_apply_rule(
3308+
Config,
3309+
elvis_style,
3310+
strict_module_layout,
3311+
#{order => [module, types, export_types]},
3312+
AllwaysFailPath
3313+
),
3314+
3315+
[#{line_num := 3}, #{line_num := 4}] =
3316+
elvis_test_utils:elvis_core_apply_rule(
3317+
Config,
3318+
elvis_style,
3319+
strict_module_layout,
3320+
#{order => [module, export_types, types]},
3321+
AllwaysFailPath
3322+
),
3323+
3324+
% if we don't add the module part, this will not fail
3325+
% because 'type' will not have any defined previous elements,
3326+
% so it basically not violate the rule
3327+
[] =
3328+
elvis_test_utils:elvis_core_apply_rule(
3329+
Config,
3330+
elvis_style,
3331+
strict_module_layout,
3332+
#{order => [types, export_types]},
3333+
AllwaysFailPath
3334+
),
3335+
3336+
% repeating a keyword does not have any effect,
3337+
% because we get the first entry from the order list when checking
3338+
% if the the previous element matches with the right order or not
3339+
[#{line_num := 5}] =
3340+
elvis_test_utils:elvis_core_apply_rule(
3341+
Config,
3342+
elvis_style,
3343+
strict_module_layout,
3344+
#{order => [module, types, export_types, types]},
3345+
AllwaysFailPath
32463346
).
32473347

32483348
verify_elvis_attr_atom_naming_convention(Config) ->

0 commit comments

Comments
 (0)