Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 5 additions & 112 deletions src/elvis_code.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
find/2,
find/3,
find_by_location/2,
find_by_names/2,
find_by_types/2,
find_by_types/3,
find_by_types_in_tokens/2,
Expand All @@ -15,12 +14,8 @@
]).
%% Specific
-export([
past_nesting_limit/2,
exported_functions/1,
exported_types/1,
function_names/1,
module_name/1,
print_node/1, print_node/2
print_node/1,
print_node/2
]).

-export_type([find_options/0]).
Expand All @@ -37,7 +32,7 @@
-spec find(fun((zipper:zipper(_)) -> boolean()), ktn_code:tree_node()) ->
[ktn_code:tree_node()].
find(Pred, Root) ->
find(Pred, Root, #{mode => node, traverse => content}).
find(Pred, Root, #{}).

%% @doc Find all nodes in the tree for which the predicate function returns
%% `true'. The options map has two keys:
Expand All @@ -55,7 +50,6 @@ find(Pred, Root) ->
%% </li>
%% </ul>
%% @end

-spec find(fun((zipper:zipper(_)) -> boolean()), ktn_code:tree_node(), find_options()) ->
[ktn_code:tree_node()].
find(Pred, Root, Opts) ->
Expand Down Expand Up @@ -145,16 +139,8 @@ find_by_location(Root, Location) ->
{ok, Node}
end.

find_by_names(Names, Root) ->
find(
fun(Node) ->
lists:member(ktn_code:attr(name, Node), Names)
end,
Root
).

find_by_types(Types, Root) ->
find_by_types(Types, Root, #{mode => node, traverse => content}).
find_by_types(Types, Root, #{}).

find_by_types(Types, Root, Opts) ->
find(
Expand Down Expand Up @@ -192,31 +178,12 @@ find_token(Root, Location) ->
{ok, Token}
end.

%%% Processing functions

%% @doc Takes a node and returns all nodes where the nesting limit is exceeded.
-spec past_nesting_limit(ktn_code:tree_node(), integer()) ->
[{ktn_code:tree_node(), integer()}].
past_nesting_limit(Node, MaxLevel) ->
ResultNodes = past_nesting_limit(Node, 1, MaxLevel),
lists:reverse(ResultNodes).

past_nesting_limit(Node, CurrentLevel, MaxLevel) when CurrentLevel > MaxLevel ->
[Node];
past_nesting_limit(#{content := Content}, CurrentLevel, MaxLevel) ->
Fun = fun(ChildNode) ->
Increment = level_increment(ChildNode),
past_nesting_limit(ChildNode, Increment + CurrentLevel, MaxLevel)
end,
lists:flatmap(Fun, Content);
past_nesting_limit(_Node, _CurrentLeve, _MaxLevel) ->
[].

%% @doc Debugging utility function.
-spec print_node(ktn_code:tree_node()) -> ok.
print_node(Node) ->
print_node(Node, 0).

%% @doc Debugging utility function.
-spec print_node(ktn_code:tree_node(), integer()) -> ok.
print_node(#{type := Type} = Node, CurrentLevel) ->
Type = ktn_code:type(Node),
Expand All @@ -226,77 +193,3 @@ print_node(#{type := Type} = Node, CurrentLevel) ->
ok = elvis_utils:info("~s - [~p] ~p~n", [Indentation, CurrentLevel, Type]),
_ = lists:map(fun(Child) -> print_node(Child, CurrentLevel + 1) end, Content),
ok.

%% @doc Takes the root node and returns the module's name.
-spec module_name(ktn_code:tree_node()) -> atom().
module_name(#{type := root, content := Content}) ->
Fun = fun(#{type := Type}) -> Type == module end,
case lists:filter(Fun, Content) of
[ModuleNode | _] ->
ktn_code:attr(value, ModuleNode);
[] ->
undefined
end.

%% @doc Takes the root node of a parse_tree and returns name and arity
%% of each exported function.
-spec exported_functions(ktn_code:tree_node()) -> [{atom(), integer()}].
exported_functions(#{type := root, content := Content}) ->
Fun = make_extractor_fun(exported_functions),
lists:flatmap(Fun, Content).

-spec exported_types(ktn_code:tree_node()) -> [{atom(), integer()}].
exported_types(#{type := root, content := Content}) ->
Fun = make_extractor_fun(exported_types),
lists:flatmap(Fun, Content).

%% @doc Takes the root node of a parse_tree and returns the name
%% of each function, whether exported or not.
-spec function_names(ktn_code:tree_node()) -> [atom()].
function_names(#{type := root, content := Content}) ->
Fun = make_extractor_fun(function_names),
lists:flatmap(Fun, Content).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Internal
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @private
%% @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, named_fun, receive_case],
case lists:member(Type, IncrementOne) of
true ->
1;
false ->
0
end.

%% @private
%% @doc Returns an anonymous Fun to be flatmapped over node content, as
%% appropriate for the exported function whose name is the argument given.
make_extractor_fun(exported_functions) ->
fun
(#{type := export} = Node) ->
ktn_code:attr(value, Node);
(_) ->
[]
end;
make_extractor_fun(exported_types) ->
fun
(#{type := export_type} = Node) ->
ktn_code:attr(value, Node);
(_) ->
[]
end;
make_extractor_fun(function_names) ->
fun
(#{type := function} = Node) ->
[ktn_code:attr(name, Node)];
(_) ->
[]
end.
1 change: 0 additions & 1 deletion src/elvis_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ is_invalid_rule({Module, RuleName}) ->
normalize(Config) when is_list(Config) ->
lists:map(fun do_normalize/1, Config).

%% @private
do_normalize(#{src_dirs := Dirs} = Config) ->
%% NOTE: Provided for backwards compatibility.
%% Rename 'src_dirs' key to 'dirs'.
Expand Down
13 changes: 0 additions & 13 deletions src/elvis_core.erl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ start() ->
{ok, _} = application:ensure_all_started(elvis_core),
ok.

%%% Rock Command

-spec rock(elvis_config:configs()) ->
ok | {fail, [{throw, term()} | elvis_result:file() | elvis_result:rule()]}.
rock(Config) ->
Expand Down Expand Up @@ -82,7 +80,6 @@ rock_this(Path, Config) ->
elvis_result_status(Results)
end.

%% @private
-spec do_parallel_rock(elvis_config:config()) ->
ok
| {fail, [{throw, term()} | elvis_result:file() | elvis_result:rule()]}.
Expand Down Expand Up @@ -119,7 +116,6 @@ do_rock(File, Config) ->
Results = apply_rules(Config, LoadedFile),
{ok, Results}.

%% @private
-spec load_file_data(elvis_config:configs() | elvis_config:config(), elvis_file:file()) ->
elvis_file:file().
load_file_data(Config, File) ->
Expand All @@ -134,7 +130,6 @@ load_file_data(Config, File) ->
File
end.

%% @private
-spec main([]) -> true | no_return().
main([]) ->
ok = application:load(elvis_core),
Expand All @@ -148,7 +143,6 @@ main([]) ->
%%% Private
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @private
-spec combine_results(
ok | {fail, [elvis_result:file()]},
ok | {fail, [elvis_result:file()]}
Expand All @@ -166,7 +160,6 @@ apply_rules_and_print(Config, File) ->
elvis_result:print_results(Results),
Results.

%% @private
-spec apply_rules(
elvis_config:configs() | elvis_config:config(),
File :: elvis_file:file()
Expand All @@ -180,24 +173,20 @@ apply_rules(Config, File) ->
lists:foldl(fun apply_rule/2, Acc, merge_rules({file, ParseTree}, lists:flatten(Rules))),
elvis_result:new(file, File, RulesResults).

%% @private
merge_rules({file, ParseTree}, ElvisConfigRules) ->
ElvisAttrs =
elvis_code:find(fun is_elvis_attr/1, ParseTree, #{traverse => content, mode => node}),
ElvisAttrRules = elvis_attr_rules(ElvisAttrs),
elvis_config:merge_rules(ElvisAttrRules, ElvisConfigRules).

%% @private
is_elvis_attr(Node) ->
ktn_code:type(Node) =:= elvis.

%% @private
elvis_attr_rules([] = _ElvisAttrs) ->
[];
elvis_attr_rules(ElvisAttrs) ->
[Rule || ElvisAttr <- ElvisAttrs, Rule <- ktn_code:attr(value, ElvisAttr)].

%% @private
-spec apply_rule({Mod, Fun} | {Mod, Fun, RuleCfg}, {Results, ElvisCfg, File}) -> Result when
Mod :: module(),
Fun :: atom(),
Expand Down Expand Up @@ -241,7 +230,6 @@ apply_rule({Module, Function, ConfigArgs}, {Result, Config, File}) ->
end,
{[RuleResult | Result], Config, File}.

%% @private
%% @doc Process a tules configuration argument and converts it to a map.
ensure_config_map(_, _, Map) when is_map(Map) ->
Map;
Expand All @@ -262,7 +250,6 @@ ensure_config_map(elvis_style, module_naming_convention, [Regex, IgnoreModules])
ensure_config_map(_, _, []) ->
#{}.

%% @private
elvis_result_status(Results) ->
case elvis_result:status(Results) of
fail ->
Expand Down
6 changes: 0 additions & 6 deletions src/elvis_file.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ module(#{path := Path}) ->
%% Private
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% @private
-spec resolve_parse_tree(string(), string() | binary(), module(), list()) ->
undefined | ktn_code:tree_node().
resolve_parse_tree(".erl", Content, Mod, Ignore) ->
Expand All @@ -152,7 +151,6 @@ resolve_parse_tree(".hrl", Content, Mod, Ignore) ->
resolve_parse_tree(_, _, _, _) ->
undefined.

%% @private
filter_tree_for(Tree, Mod, Ignore) when is_map(Tree) ->
TreeContent = maps:get(content, Tree, []),
Tree#{
Expand All @@ -176,7 +174,6 @@ filter_tree_for(Tree, Mod, Ignore) when is_map(Tree) ->
filter_tree_for(Tree, _Mod, _Ignore) ->
Tree.

%% @private
-spec find_encoding(Content :: binary()) -> atom().
find_encoding(Content) ->
case epp:read_encoding_from_binary(Content) of
Expand All @@ -186,7 +183,6 @@ find_encoding(Content) ->
Enc
end.

%% @private
-spec maybe_add_abstract_parse_tree(Config, File, Mod, Ignore) -> Res when
Config :: elvis_config:configs() | elvis_config:config(),
File :: file(),
Expand All @@ -204,7 +200,6 @@ maybe_add_abstract_parse_tree(
maybe_add_abstract_parse_tree(_Config, File, _Mod, _Ignore) ->
File.

%% @private
-spec get_abstract_parse_tree(BeamPath, Mod, Ignore) -> Res when
BeamPath :: file:filename(),
Mod :: module(),
Expand All @@ -214,7 +209,6 @@ get_abstract_parse_tree(BeamPath, Mod, Ignore) ->
AbstractSrc = get_abstract_source(BeamPath),
resolve_parse_tree(".erl", AbstractSrc, Mod, Ignore).

%% @private
-spec get_abstract_source(BeamPath) -> Res when
BeamPath :: file:filename() | binary(),
Res :: string().
Expand Down
1 change: 0 additions & 1 deletion src/elvis_gitignore.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ forbidden_patterns(_Config, #{path := Path}, RuleConfig) ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% .gitignore
%% @private
check_patterns_in_lines(_Lines, [], Results, _Mode) ->
{ok, Results};
check_patterns_in_lines(Lines, [Pattern | Rest], Results0, Mode) ->
Expand Down
8 changes: 0 additions & 8 deletions src/elvis_project.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ old_configuration_format(_Config, Target, _RuleConfig) ->

%%% Rebar

%% @private
get_deps(File) ->
{Src, _} = elvis_file:src(File),
Terms = ktn_code:consult(Src),
Expand All @@ -121,7 +120,6 @@ get_deps(File) ->
end.

%% Rebar3
%% @private
is_branch_dep({_AppName, {_SCM, _Location, {branch, _}}}) ->
true;
is_branch_dep({_AppName, {git_subdir, _Url, {branch, _}, _SubDir}}) ->
Expand All @@ -130,13 +128,11 @@ is_branch_dep({_AppName, {git_subdir, _Url, {branch, _}, _SubDir}}) ->
is_branch_dep({AppName, {raw, DepResourceSpecification}}) ->
is_branch_dep({AppName, DepResourceSpecification});
%% Rebar2
%% @private
is_branch_dep({_AppName, _Vsn, {_SCM, _Location, {branch, _}}}) ->
true;
is_branch_dep(_) ->
false.

%% @private
is_hex_dep(_AppName) when is_atom(_AppName) ->
true;
is_hex_dep({_AppName, _Vsn, {pkg, _PackageName}}) when
Expand All @@ -150,7 +146,6 @@ is_hex_dep({_AppName, _Vsn}) when is_atom(_AppName), is_list(_Vsn) ->
is_hex_dep(_) ->
false.

%% @private
is_not_git_dep({_AppName, {_SCM, Url, _Branch}}, Regex) ->
nomatch == re:run(Url, Regex, []);
is_not_git_dep(
Expand Down Expand Up @@ -179,7 +174,6 @@ is_not_git_dep({_AppName, _Vsn, {_SCM, Url, {BranchTagOrRefType, _Branch}}, _Opt
->
nomatch == re:run(Url, Regex, []).

%% @private
dep_to_result({AppName, _}, Message, {IgnoreDeps, Regex}) ->
case lists:member(AppName, IgnoreDeps) of
true ->
Expand All @@ -203,7 +197,6 @@ dep_to_result({AppName, _Vsn, GitInfo, _Opts}, Message, IgnoreDeps) ->

%% Old config

%% @private
is_old_config(ElvisConfig) ->
case proplists:get_value(config, ElvisConfig) of
undefined ->
Expand All @@ -218,7 +211,6 @@ is_old_config(ElvisConfig) ->
lists:filter(SrcDirsIsKey, Config) /= []
end.

%% @private
exists_old_rule(#{rules := Rules}) ->
Filter =
fun
Expand Down
Loading