Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#297): New rule: Prefer Unquoted Atoms #355

Merged
merged 8 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ identified with `(since ...)` for convenience purposes.
- [State Record and Type](doc_rules/elvis_style/state_record_and_type.md)
- [Used Ignored Variable](doc_rules/elvis_style/used_ignored_variable.md)
- [Variable Naming Convention](doc_rules/elvis_style/variable_naming_convention.md)
- [Prefer Unquoted Atoms](doc_rules/elvis_style/prefer_unquoted_atoms.md)
bormilan marked this conversation as resolved.
Show resolved Hide resolved

## Project rules

Expand Down
15 changes: 15 additions & 0 deletions doc_rules/elvis_style/prefer_unquoted_atoms.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Prefer unquoted atoms
bormilan marked this conversation as resolved.
Show resolved Hide resolved

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

> Works on `.beam` file? No.

## Options

- None.

## Example

```erlang
{elvis_style, prefer_unquoted_atoms, #{}}
```
46 changes: 45 additions & 1 deletion src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
atom_naming_convention/3, no_throw/3, no_dollar_space/3, no_author/3, no_import/3,
no_catch_expressions/3, no_single_clause_case/3, numeric_format/3, behaviour_spelling/3,
always_shortcircuit/3, consistent_generic_type/3, export_used_types/3,
no_match_in_condition/3, param_pattern_matching/3, private_data_types/3, option/3]).
no_match_in_condition/3, param_pattern_matching/3, private_data_types/3, option/3,
prefer_unquoted_atoms/3]).

-export_type([empty_rule_config/0]).
-export_type([ignorable/0]).
Expand Down Expand Up @@ -107,6 +108,9 @@
-define(ATOM_NAMING_CONVENTION_MSG,
"Atom ~p on line ~p does not respect the format "
"defined by the regular expression '~p'.").
-define(ATOM_PREFERRED_QUOTES_MSG,
"Atom ~p on line ~p is quoted "
"but quotes are not needed.").
-define(NO_THROW_MSG, "Usage of throw/1 on line ~p is not recommended").
-define(NO_DOLLAR_SPACE_MSG,
"'$ ' was found on line ~p. It's use is discouraged. "
Expand Down Expand Up @@ -1015,6 +1019,16 @@ atom_naming_convention(Config, Target, RuleConfig) ->
AtomNodes = elvis_code:find(fun is_atom_node/1, Root, #{traverse => all, mode => node}),
check_atom_names(Regex, RegexEnclosed, AtomNodes, []).

-spec prefer_unquoted_atoms(elvis_config:config(),
elvis_file:file(),
empty_rule_config()) ->
[elvis_result:item()].
prefer_unquoted_atoms(_Config, Target, _RuleConfig) ->
{Content, #{encoding := _Encoding}} = elvis_file:src(Target),
Tree = ktn_code:parse_tree(Content),
AtomNodes = elvis_code:find(fun is_atom_node/1, Tree, #{traverse => all, mode => node}),
check_atom_quotes(AtomNodes, []).

-spec no_throw(elvis_config:config(), elvis_file:file(), empty_rule_config()) ->
[elvis_result:item()].
no_throw(Config, Target, RuleConfig) ->
Expand Down Expand Up @@ -1456,6 +1470,36 @@ check_atom_names(Regex, RegexEnclosed, [AtomNode | RemainingAtomNodes], AccIn) -
end,
check_atom_names(Regex, RegexEnclosed, RemainingAtomNodes, AccOut).

%% @private
check_atom_quotes([] = _AtomNodes, Acc) ->
Acc;
check_atom_quotes([AtomNode | RemainingAtomNodes], AccIn) ->
AtomName = ktn_code:attr(text, AtomNode),
ValueAtomName = ktn_code:attr(value, AtomNode),

IsException = is_exception_prefer_quoted(ValueAtomName),

AccOut =
case unicode:characters_to_list(AtomName, unicode) of
[$' | _] when not IsException ->
Msg = ?ATOM_PREFERRED_QUOTES_MSG,
{Line, _} = ktn_code:attr(location, AtomNode),
Info = [AtomName, Line],
Result = elvis_result:new(item, Msg, Info, Line),
AccIn ++ [Result];
_ ->
AccIn
end,
check_atom_quotes(RemainingAtomNodes, AccOut).

%% @private
is_exception_prefer_quoted(Elem) ->
KeyWords =
['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'],
lists:member(Elem, KeyWords).

%% @private
string_strip_enclosed([$' | Rest]) ->
[$' | Reversed] = lists:reverse(Rest),
Expand Down
11 changes: 11 additions & 0 deletions test/examples/fail_quoted_atoms.erl
bormilan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-module(fail_quoted_atoms).

-export([test/1, test/2]).

-define(TEST(), test(1, default)).

test(_Test) -> {ok, test}.

test(_A, 'ugly_atom_name') -> 'why_use_quotes_here';
test(_A, default) -> ?TEST();
test(_A, _B) -> 'and'.
11 changes: 11 additions & 0 deletions test/examples/pass_unquoted_atoms.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-module(pass_unquoted_atoms).

-export([test/1, test/2]).

test(_Test) -> ok.

test(_A, nice_atom_name) -> perfect_atomname;
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'].

12 changes: 10 additions & 2 deletions test/style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
verify_always_shortcircuit/1, verify_consistent_generic_type/1, verify_no_types/1,
verify_no_specs/1, verify_export_used_types/1, verify_consistent_variable_casing/1,
verify_no_match_in_condition/1, verify_param_pattern_matching/1,
verify_private_data_types/1]).
verify_private_data_types/1, verify_unquoted_atoms/1]).
%% -elvis attribute
-export([verify_elvis_attr_atom_naming_convention/1, verify_elvis_attr_numeric_format/1,
verify_elvis_attr_dont_repeat_yourself/1, verify_elvis_attr_function_naming_convention/1,
Expand Down Expand Up @@ -82,7 +82,7 @@ groups() ->
verify_always_shortcircuit, verify_no_catch_expressions, verify_no_single_clause_case,
verify_no_macros, verify_export_used_types, verify_max_anonymous_function_arity,
verify_max_function_arity, verify_no_match_in_condition, verify_behaviour_spelling,
verify_param_pattern_matching, verify_private_data_types]}].
verify_param_pattern_matching, verify_private_data_types, verify_unquoted_atoms]}].

-spec init_per_suite(config()) -> config().
init_per_suite(Config) ->
Expand Down Expand Up @@ -1297,6 +1297,14 @@ verify_no_successive_maps(_Config) ->

-endif.

-spec verify_unquoted_atoms(config()) -> any().
verify_unquoted_atoms(Config) ->
PassPath = "pass_unquoted_atoms." ++ "erl",
[] = elvis_core_apply_rule(Config, elvis_style, prefer_unquoted_atoms, #{}, PassPath),

FailPath = "fail_quoted_atoms." ++ "erl",
[_, _] = elvis_core_apply_rule(Config, elvis_style, prefer_unquoted_atoms, #{}, FailPath).

-spec verify_atom_naming_convention(config()) -> any().
verify_atom_naming_convention(Config) ->
Group = proplists:get_value(group, Config, erl_files),
Expand Down
Loading