Skip to content

Commit

Permalink
feat(#297): fix suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Milán Bór authored and Milán Bór committed Sep 12, 2024
1 parent 3aef3ee commit 198544b
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion doc_rules/elvis_style/prefer_unquoted_atoms.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prefer unquoted atoms

Do not use quotes on atoms that are not need it.
Do not use quotes on atoms that don't need to be quoted.

> Works on `.beam` file? No.
Expand Down
15 changes: 5 additions & 10 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1474,22 +1474,17 @@ check_atom_names(Regex, RegexEnclosed, [AtomNode | RemainingAtomNodes], AccIn) -
check_atom_quotes([] = _AtomNodes, Acc) ->
Acc;
check_atom_quotes([AtomNode | RemainingAtomNodes], AccIn) ->
Regex = "^'([a-z_0-9)]+)'$",
RegexEnclosed = ".*",
AtomName0 = ktn_code:attr(text, AtomNode),
AtomName = ktn_code:attr(text, AtomNode),
ValueAtomName = ktn_code:attr(value, AtomNode),
{IsEnclosed, AtomName} = string_strip_enclosed(AtomName0),

IsException = is_exception_prefer_quoted(ValueAtomName),
RE = re_compile_for_atom_type(IsEnclosed, Regex, RegexEnclosed),

AccOut =
case re:run(
unicode:characters_to_list(AtomName, unicode), RE)
of
{match, _Captured} when not IsException ->
case unicode:characters_to_list(AtomName, unicode) of
[$' | _] when not IsException ->
Msg = ?ATOM_PREFERRED_QUOTES_MSG,
{Line, _} = ktn_code:attr(location, AtomNode),
Info = [AtomName0, Line, RegexEnclosed],
Info = [AtomName, Line],
Result = elvis_result:new(item, Msg, Info, Line),
AccIn ++ [Result];
_ ->
Expand Down
4 changes: 3 additions & 1 deletion test/examples/pass_unquoted_atoms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
test(_Test) -> ok.

test(_A, nice_atom_name) -> perfect_atomname;
test(_A, _B) -> unquoted_atom.
test(_Reserved, _Words) -> ['after', 'and', 'andalso', 'band', 'begin', 'bnot', 'bor', 'bsl', 'bsr', 'bxor', 'case',
'catch', 'cond', 'div', 'end', 'fun', 'if', 'let', 'not', 'of', 'or', 'orelse', 'receive',
'rem', 'try', 'when', 'xor'].

0 comments on commit 198544b

Please sign in to comment.