Skip to content

Commit

Permalink
[inaka#186] Add function_naming_convention/3 to src/elvis_style.erl
Browse files Browse the repository at this point in the history
Currently only covers exported functions
  • Loading branch information
Kevin C. Baird committed Sep 25, 2015
1 parent a08b726 commit 3c7f2eb
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-module(elvis_style).

-export([
function_naming_convention/3,
line_length/3,
no_tabs/3,
no_spaces/3,
Expand Down Expand Up @@ -68,6 +69,10 @@
"Use the '-callback' attribute instead of 'behavior_info/1' "
"on line ~p.").

-define(FUNCTION_NAMING_CONVENTION_MSG,
"The function ~p does not respect the format defined by the "
"regular expression '~p'.").

-define(MODULE_NAMING_CONVENTION_MSG,
"The module ~p does not respect the format defined by the "
"regular expression '~p'.").
Expand Down Expand Up @@ -102,6 +107,34 @@

-type empty_rule_config() :: #{}.

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

-spec function_naming_convention(elvis_config:config(),
elvis_file:file(),
function_naming_convention_config()) ->
[elvis_result:item()].
function_naming_convention(Config, Target, RuleConfig) ->
Regex = maps:get(regex, RuleConfig, ".*"),

{Root, _} = elvis_file:parse_tree(Config, Target),
ExportedFunctions = elvis_code:exported_functions(Root),
errors_for_function_names(Regex, ExportedFunctions).

errors_for_function_names(_Regex, []) -> [];
errors_for_function_names(Regex, [ExportedFunction | RemainingFuncs]) ->
{FunctionName, _FunctionArity} = ExportedFunction,
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, RemainingFuncs)];
{match, _} -> errors_for_function_names(Regex, RemainingFuncs)
end.

-type line_length_config() :: #{limit => integer(),
skip_comments => false | any | whole_line
}.
Expand Down

0 comments on commit 3c7f2eb

Please sign in to comment.