Skip to content

Commit

Permalink
add forbidden for enclosed ones too
Browse files Browse the repository at this point in the history
  • Loading branch information
bormilan committed Dec 5, 2024
1 parent 101cd6e commit 485ad9d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
3 changes: 3 additions & 0 deletions doc_rules/elvis_style/atom_naming_convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All atoms should be named according to the regular expression provided.
Except if it matches with a defined `forbidden_regex`.
Atoms enclosed in apostrophes have special meaning and are thus handled
by a different configuration option (use `same` if you want the same value as `regex`).
For enclosed atoms we defined a different forbidden regex, called `forbidden_enclosed_regex`.

> Works on `.beam` file? Yes!
Expand All @@ -17,6 +18,8 @@ by a different configuration option (use `same` if you want the same value as `r
- default: `".*"`.
- `forbidden_regex :: string() | undefined`.
- default: `undefined`.
- `forbidden_enclosed_regex :: string() | undefined`.
- default: `undefined`.

## Example

Expand Down
32 changes: 27 additions & 5 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ default(no_common_caveats_call) ->
default(atom_naming_convention) ->
#{regex => "^[a-z](_?[a-z0-9]+)*(_SUITE)?$",
enclosed_atoms => ".*",
forbidden_regex => undefined};
forbidden_regex => undefined,
forbidden_enclosed_regex => undefined};
%% Not restrictive. Those who want more restrictions can set it like "^[^_]*$"
default(numeric_format) ->
#{regex => ".*",
Expand Down Expand Up @@ -1162,8 +1163,16 @@ atom_naming_convention(Config, Target, RuleConfig) ->
ForbiddenRegex = option(forbidden_regex, RuleConfig, atom_naming_convention),
RegexEnclosed =
specific_or_default(option(enclosed_atoms, RuleConfig, atom_naming_convention), Regex),
ForbiddenEnclosedRegex =
specific_or_default(option(forbidden_enclosed_regex, RuleConfig, atom_naming_convention),
Regex),
AtomNodes = elvis_code:find(fun is_atom_node/1, Root, #{traverse => all, mode => node}),
check_atom_names(Regex, ForbiddenRegex, RegexEnclosed, AtomNodes, []).
check_atom_names(Regex,
ForbiddenRegex,
RegexEnclosed,
ForbiddenEnclosedRegex,
AtomNodes,
[]).

-type no_init_lists_config() :: #{behaviours => [atom()]}.

Expand Down Expand Up @@ -1701,18 +1710,26 @@ is_exception_or_non_reversible(_) ->
false.

%% @private
check_atom_names(_Regex, _, _RegexEnclosed, [] = _AtomNodes, Acc) ->
check_atom_names(_Regex, _, _RegexEnclosed, _, [] = _AtomNodes, Acc) ->
Acc;
check_atom_names(Regex,
ForbiddenRegex,
ForbiddenRegexNormal,
RegexEnclosed,
ForbiddenRegexEnclosed,
[AtomNode | RemainingAtomNodes],
AccIn) ->
AtomName0 = ktn_code:attr(text, AtomNode),
ValueAtomName = ktn_code:attr(value, AtomNode),
{IsEnclosed, AtomName} = string_strip_enclosed(AtomName0),
IsExceptionClass = is_exception_or_non_reversible(ValueAtomName),
RE = re_compile_for_atom_type(IsEnclosed, Regex, RegexEnclosed),
ForbiddenRegex =
case IsEnclosed of
true ->
ForbiddenRegexEnclosed;
false ->
ForbiddenRegexNormal
end,
AccOut =
case re:run(
unicode:characters_to_list(AtomName, unicode), RE)
Expand Down Expand Up @@ -1747,7 +1764,12 @@ check_atom_names(Regex,
AccIn
end
end,
check_atom_names(Regex, ForbiddenRegex, RegexEnclosed, RemainingAtomNodes, AccOut).
check_atom_names(Regex,
ForbiddenRegexNormal,
RegexEnclosed,
ForbiddenRegexEnclosed,
RemainingAtomNodes,
AccOut).

%% @private
string_strip_enclosed([$' | Rest]) ->
Expand Down
22 changes: 21 additions & 1 deletion test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1593,12 +1593,32 @@ verify_atom_naming_convention(Config) ->

% forbidden
PathForbidden = "forbidden_atom_naming_convention." ++ Ext,
_ = case Group of
beam_files -> % 'or_THIS' getting stripped of enclosing '
[_, _, _, _] =
elvis_core_apply_rule(Config,
elvis_style,
atom_naming_convention,
#{regex => "^[a-z](_?[a-z0-9]+)*(_SUITE)?$",
forbidden_regex => "[0-9]"},
PathForbidden);
erl_files ->
[_, _, _] =
elvis_core_apply_rule(Config,
elvis_style,
atom_naming_convention,
#{regex => "^[a-z](_?[a-z0-9]+)*(_SUITE)?$",
forbidden_regex => "[0-9]"},
PathForbidden)
end,

[_, _, _, _] =
elvis_core_apply_rule(Config,
elvis_style,
atom_naming_convention,
#{regex => "^[a-z](_?[a-z0-9]+)*(_SUITE)?$",
forbidden_regex => "[0-9]"},
forbidden_regex => "[0-9]",
forbidden_enclosed_regex => "[0-9]"},
PathForbidden).

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

0 comments on commit 485ad9d

Please sign in to comment.