Skip to content

Commit

Permalink
little fixes and change logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan committed Oct 11, 2024
1 parent 299ed09 commit 0653f03
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 46 deletions.
49 changes: 18 additions & 31 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
-module(elvis_style).

-feature(maybe_expr, enable).

-export([default/1, function_naming_convention/3, variable_naming_convention/3,
consistent_variable_casing/3, macro_names/3, macro_module_names/3, no_macros/3,
no_specs/3, no_types/3, no_block_expressions/3, operator_spaces/3, no_space/3,
Expand Down Expand Up @@ -31,10 +33,8 @@
no_match_in_condition_config/0, behaviour_spelling_config/0,
param_pattern_matching_config/0, private_data_type_config/0]).

-hank([{unnecessary_function_arguments, [{no_init_lists, 3}]}]).

-define(NO_INIT_LISTS_MSG,
"Do not use a list as the parameter for the 'init' callback at position ~p.").
% -define(NO_INIT_LISTS_MSG,
% "Do not use a list as the parameter for the 'init' callback at position ~p.").
-define(INVALID_MACRO_NAME_REGEX_MSG,
"The macro named ~p on line ~p does not respect the format "
"defined by the regular expression '~p'.").
Expand Down Expand Up @@ -149,6 +149,8 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-spec default(Rule :: atom()) -> DefaultRuleConfig :: term().
default(no_init_lists) ->
#{behaviours => [gen_server, gen_statem]};
default(macro_names) ->
#{regex => "^[A-Z](_?[A-Z0-9]+)*$"};
default(operator_spaces) ->
Expand Down Expand Up @@ -252,8 +254,8 @@ default(RuleWithEmptyDefault)
RuleWithEmptyDefault == always_shortcircuit;
RuleWithEmptyDefault == no_space_after_pound;
RuleWithEmptyDefault == export_used_types;
RuleWithEmptyDefault == consistent_variable_casing;
RuleWithEmptyDefault == no_init_lists ->
RuleWithEmptyDefault == consistent_variable_casing ->
% RuleWithEmptyDefault == no_init_lists ->
#{}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -1035,35 +1037,20 @@ atom_naming_convention(Config, Target, RuleConfig) ->
AtomNodes = elvis_code:find(fun is_atom_node/1, Root, #{traverse => all, mode => node}),
check_atom_names(Regex, RegexEnclosed, AtomNodes, []).

-spec no_init_lists(elvis_config:config(), elvis_file:file(), empty_rule_config()) ->
[elvis_result:item()].
no_init_lists(_Config, Target, _RuleConfig) ->
Root = get_root(#{}, Target, #{}),

IsFunction = fun(Node) -> ktn_code:type(Node) == function end,
FunctionNodes = elvis_code:find(IsFunction, Root),
-type no_init_lists_config() :: #{behaviours => [atom()]}.

PairFun =
fun(FunctionNode) ->
Name = ktn_code:attr(name, FunctionNode),
Location = ktn_code:attr(location, FunctionNode),
[Content] = ktn_code:content(FunctionNode),
Attributes = ktn_code:node_attr(pattern, Content),
{Name, Location, [Attr || #{type := Type} = Attr <- Attributes, Type == cons]}
end,
-spec no_init_lists(elvis_config:config(), elvis_file:file(), no_init_lists_config()) ->
[elvis_result:item()].
no_init_lists( Config , Target , RuleConfig ) -> Root = get_root( Config , Target , RuleConfig ) , Behaviors = option( behaviours , RuleConfig , no_init_lists ) , IsBehaviour = fun ( Node ) -> ktn_code : type( Node ) == behaviour end , FunListAttributes = maybe [ BehaviourNode ] ?= elvis_code : find( IsBehaviour , Root ) , true ?= lists : member( ktn_code : attr( value , BehaviourNode ) , Behaviors ) , IsFunction = fun ( Node ) -> ktn_code : type( Node ) == function end , FunctionNodes = elvis_code : find( IsFunction , Root ) , PairFun = fun ( FunctionNode ) -> Name = ktn_code : attr( name , FunctionNode ) , Location = ktn_code : attr( location , FunctionNode ) , [ Content ] = ktn_code : content( FunctionNode ) , Attributes = ktn_code : node_attr( pattern , Content ) , { Name , Location , [ Attr || #{ type := Type } = Attr <- Attributes , Type == cons ] } end , FunListAttributeInfos = lists : map( PairFun , FunctionNodes ) , FilterFun = fun ( { Name , _ , Args } ) -> length( Args ) =:= 1 andalso Name =:= init end , lists : filter( FilterFun , FunListAttributeInfos ) else _ -> [ ] end , ResultFun = fun ( { _ , Location , _ } ) -> Info = [ Location ] , Msg = "asd" , elvis_result : new( item , Msg , Info , Location ) end , lists : map( ResultFun , FunListAttributes ) .

FunListAttributeInfos = lists:map(PairFun, FunctionNodes),
% io:format(user, "-----~n", []),
% io:format(user, "~p~n", [ktn_code:attr(value, BehaviourNode)]),
% io:format(user, "~p~n", [lists:member(ktn_code:attr(value, BehaviourNode), Behaviors)]),
% io:format(user, "-----~n", []),

FilterFun = fun({Name, _, C}) -> length(C) > 0 andalso Name =:= init end,
FunListAttributes = lists:filter(FilterFun, FunListAttributeInfos),
% --

ResultFun =
fun({_, Location, _}) ->
Info = [Location],
Msg = ?NO_INIT_LISTS_MSG,
elvis_result:new(item, Msg, Info, Location)
end,
lists:map(ResultFun, FunListAttributes).
% Msg = ?NO_INIT_LISTS_MSG,

-spec no_throw(elvis_config:config(), elvis_file:file(), empty_rule_config()) ->
[elvis_result:item()].
Expand Down
15 changes: 15 additions & 0 deletions test/examples/fail_no_init_lists.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-module(fail_no_init_lists).

-behaviour(gen_server).

-export([start_link/1, init/1, handle_cast/2, handle_call/3]).

start_link(AParam) ->
gen_server:start_link(?MODULE, [AParam], []).

init([_AParam]) ->
ok.

handle_cast(_, _) -> ok.

handle_call(_, _, _) -> ok.
15 changes: 15 additions & 0 deletions test/examples/pass_no_init_lists.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-module(pass_no_init_lists).

-behaviour(gen_server).

-export([start_link/0, init/1, handle_cast/2, handle_call/3]).

start_link() ->
gen_server:start_link(?MODULE, undefined, []).

init(_) ->
ok.

handle_cast(_, _) -> ok.

handle_call(_, _, _) -> ok.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
-module(fail_verify_no_init_lists).
-module(pass_no_init_lists2).

-export([start_link/1, init/1]).

start_link(AParam) ->
gen_server:start_link(?MODULE, [AParam], []).

init([_AParam]) ->
ok.
init([_AParam]) -> ok.
17 changes: 17 additions & 0 deletions test/examples/pass_no_init_lists3.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-module(pass_no_init_lists3).

-behaviour(gen_statem).

-export([start_link/1, init/1, handle_cast/2, handle_call/3, callback_mode/0]).

start_link(B) ->
gen_server:start_link(?MODULE, [B], []).

init([_B]) ->
ok.

handle_cast(_, _) -> ok.

handle_call(_, _, _) -> ok.

callback_mode() -> ok.
9 changes: 0 additions & 9 deletions test/examples/pass_verify_no_init_lists.erl

This file was deleted.

14 changes: 11 additions & 3 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1452,13 +1452,21 @@ verify_atom_naming_convention(Config) ->
verify_no_init_lists(Config) ->
Ext = proplists:get_value(test_file_ext, Config, "erl"),

FailPath = "fail_verify_no_init_lists." ++ Ext,
FailPath = "fail_no_init_lists." ++ Ext,

[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath),

PassPath = "pass_verify_no_init_lists." ++ Ext,
PassPath = "pass_no_init_lists." ++ Ext,

[] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath).
[] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath),

PassPath2 = "pass_no_init_lists2." ++ Ext,

[] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, PassPath2),

FailPath3 = "pass_no_init_lists3." ++ Ext,

[_] = elvis_core_apply_rule(Config, elvis_style, no_init_lists, #{}, FailPath3).

-spec verify_no_throw(config()) -> any().
verify_no_throw(Config) ->
Expand Down

0 comments on commit 0653f03

Please sign in to comment.