Skip to content

Commit

Permalink
Allow {Module, Function} ignores in function_naming_convention/3
Browse files Browse the repository at this point in the history
  • Loading branch information
onno-vos-dev committed Feb 27, 2020
1 parent 5fd30f5 commit 364ad0a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
30 changes: 19 additions & 11 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@
ignore => [atom()],
max_length => integer()}.

-type function_naming_convention_spec() :: module() | {module(), atom()}.
-type function_naming_convention_config() :: #{regex => string(),
ignore => [module()]
ignore => [function_naming_convention_spec()]
}.

-spec function_naming_convention(elvis_config:config(),
Expand All @@ -157,27 +158,34 @@
[elvis_result:item()].
function_naming_convention(Config, Target, RuleConfig) ->
Regex = maps:get(regex, RuleConfig, ".*"),
IgnoreModules = maps:get(ignore, RuleConfig, []),

Ignores = maps:get(ignore, RuleConfig, []),
{Root, _} = elvis_file:parse_tree(Config, Target),
ModuleName = elvis_code:module_name(Root),
case lists:member(ModuleName, IgnoreModules) of
false ->
FunctionNames = elvis_code:function_names(Root),
errors_for_function_names(Regex, FunctionNames);
true -> []
IgnoredFuns = lists:filtermap(
fun({_ModuleName, Function}) -> {true, Function};
(_) -> false
end, Ignores),
case lists:member(ModuleName, Ignores) of
true -> [];
false ->
FunctionNames0 = elvis_code:function_names(Root),
FunctionNames = lists:filter(
fun(FunctionNames) ->
not lists:member(FunctionNames, IgnoredFuns)
end, FunctionNames0),
errors_for_function_names(Regex, FunctionNames)
end.

errors_for_function_names(_Regex, []) -> [];
errors_for_function_names(Regex, [FunctionName | RemainingFuncNames]) ->
errors_for_function_names(Regex, [FunctionName | Rem]) ->
FunctionNameStr = atom_to_list(FunctionName),
case re:run(FunctionNameStr, Regex) of
nomatch ->
Msg = ?FUNCTION_NAMING_CONVENTION_MSG,
Info = [FunctionNameStr, Regex],
Result = elvis_result:new(item, Msg, Info, 1),
[Result | errors_for_function_names(Regex, RemainingFuncNames)];
{match, _} -> errors_for_function_names(Regex, RemainingFuncNames)
[Result | errors_for_function_names(Regex, Rem)];
{match, _} -> errors_for_function_names(Regex, Rem)
end.

-type variable_naming_convention_config() :: #{regex => string(),
Expand Down
12 changes: 11 additions & 1 deletion test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ verify_function_naming_convention(_Config) ->

{ok, FileFail} = elvis_test_utils:find_file(SrcDirs, PathFail),
[] =
elvis_style:function_naming_convention(ElvisConfig, FilePass, RuleConfig2).
elvis_style:function_naming_convention(ElvisConfig, FilePass, RuleConfig2),

RuleConfig3 = #{regex => "^([a-z][a-z0-9]*_?)*$",
ignore => [ {fail_function_naming_convention, camelCase}
, {fail_function_naming_convention, 'ALL_CAPS'}
, {fail_function_naming_convention, 'Initial_cap'}
, {fail_function_naming_convention, 'ok-for-lisp'}
, {fail_function_naming_convention, 'no_predicates?'}
]
},
[_EmailError] = elvis_style:function_naming_convention(ElvisConfig, FileFail, RuleConfig3).

-spec verify_variable_naming_convention(config()) -> any().
verify_variable_naming_convention(_Config) ->
Expand Down

0 comments on commit 364ad0a

Please sign in to comment.