Skip to content

Commit

Permalink
Fix review comments:
Browse files Browse the repository at this point in the history
- Fix indentation
- Match out module when filtering functions
- Add test
  • Loading branch information
onno-vos-dev committed Feb 27, 2020
1 parent 364ad0a commit 68f111c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -162,30 +162,30 @@ function_naming_convention(Config, Target, RuleConfig) ->
{Root, _} = elvis_file:parse_tree(Config, Target),
ModuleName = elvis_code:module_name(Root),
IgnoredFuns = lists:filtermap(
fun({_ModuleName, Function}) -> {true, Function};
fun({Mod, Function}) when Mod =:= ModuleName -> {true, Function};
(_) -> false
end, Ignores),
case lists:member(ModuleName, Ignores) of
true -> [];
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)
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 | Rem]) ->
errors_for_function_names(Regex, [FunctionName | RemainingFuncNames]) ->
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, Rem)];
{match, _} -> errors_for_function_names(Regex, Rem)
[Result | errors_for_function_names(Regex, RemainingFuncNames)];
{match, _} -> errors_for_function_names(Regex, RemainingFuncNames)
end.

-type variable_naming_convention_config() :: #{regex => string(),
Expand Down
18 changes: 18 additions & 0 deletions test/examples/fail_function_naming_convention_ignored_function.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-module(fail_function_naming_convention_ignored_function).

-dialyzer({nowarn_function, bad_names_inside/0}).

-export([bad_names_inside/0]).

%% Function names must use only lowercase characters.
%% Words in function names must be separated with _.

%% Cf. https://github.com/inaka/erlang_guidelines#function-names

bad_names_inside() ->
camelCase(should, fail).

%% Private / hidden functions still checked

camelCase(Should, Fail) ->
[Should, Fail].
14 changes: 14 additions & 0 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ verify_function_naming_convention(_Config) ->
, {fail_function_naming_convention, 'no_predicates?'}
]
},
[_EmailError] = elvis_style:function_naming_convention(ElvisConfig, FileFail, RuleConfig3),

RuleConfig4 = #{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?'}
, {fail_function_naming_convention, user@location}
]
},
[] = elvis_style:function_naming_convention(ElvisConfig, FilePass, RuleConfig3),
IgnoredFail = "fail_function_naming_convention_ignored_function.erl",
{ok, FileFail} = elvis_test_utils:find_file(SrcDirs, PathFail),
[_EmailError] = elvis_style:function_naming_convention(ElvisConfig, FileFail, RuleConfig3).

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

0 comments on commit 68f111c

Please sign in to comment.