Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion RULES.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Most, if not all, of the rules will present (opinionated) documentation sections
- [Numeric Format](doc_rules/elvis_style/numeric_format.md)
- [Operator Spaces](doc_rules/elvis_style/operator_spaces.md)
- [Param Pattern-Matching](doc_rules/elvis_style/param_pattern_matching.md)
- [Prefer Unquoted Atoms](doc_rules/elvis_text_style/prefer_unquoted_atoms.md)
- [Prefer Unquoted Atoms](doc_rules/elvis_style/prefer_unquoted_atoms.md)
- [Private Data Types](doc_rules/elvis_style/private_data_types.md)
- [Simplify Anonymous Functions](doc_rules/elvis_style/simplify_anonymous_functions.md)
- [State Record And Type](doc_rules/elvis_style/state_record_and_type.md)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Prefer Unquoted Atoms [![](https://img.shields.io/badge/since-4.0.0-blue)](https://github.com/inaka/elvis_core/releases/tag/4.0.0)

> [!NOTE]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

> This rule belonged to the `elvis_text_style` namespace before [4.2.0](https://github.com/inaka/elvis_core/releases/tag/4.2.0).

Atoms should not be quoted unless syntactically necessary.

## Quick fix
Expand All @@ -13,5 +16,5 @@ Use an Erlang code formatter that enforces strict rules for quoted atoms.
## Example configuration

```erlang
{elvis_text_style, prefer_unquoted_atoms, #{}}
{elvis_style, prefer_unquoted_atoms, #{}}
```
2 changes: 1 addition & 1 deletion elvis.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
{elvis_file, module, 1},
{elvis_style, is_call, 2},
{elvis_style, is_allowed_macro, 2},
{elvis_text_style, doesnt_need_quotes, 1}
{elvis_style, doesnt_need_quotes, 1}
]
}}
],
Expand Down
4 changes: 2 additions & 2 deletions src/elvis_ruleset.erl
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,13 @@ elvis_style_stricter_rules() ->
elvis_rule:new(elvis_style, no_common_caveats_call),
elvis_rule:new(elvis_style, no_init_lists),
elvis_rule:new(elvis_style, no_macros),
elvis_rule:new(elvis_style, prefer_unquoted_atoms),
elvis_rule:new(elvis_style, state_record_and_type)
].

elvis_text_style_stricter_rules() ->
[
elvis_rule:new(elvis_text_style, no_redundant_blank_lines),
elvis_rule:new(elvis_text_style, prefer_unquoted_atoms)
elvis_rule:new(elvis_text_style, no_redundant_blank_lines)
].

beam_files_rules() ->
Expand Down
32 changes: 32 additions & 0 deletions src/elvis_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
no_boolean_in_comparison/2,
no_operation_on_same_value/2,
no_receive_without_timeout/2,
prefer_unquoted_atoms/2,
guard_operators/2,
simplify_anonymous_functions/2
]).
Expand Down Expand Up @@ -1940,6 +1941,37 @@ is_not_acceptable_number(NumberNode, Regex) ->
Number = ktn_code:attr(text, NumberNode),
Number =/= undefined andalso re_run(Number, Regex) =:= nomatch.

prefer_unquoted_atoms(Rule, ElvisConfig) ->
{nodes, AtomNodes} = elvis_code:find(#{
of_types => [atom],
inside => elvis_code:root(Rule, ElvisConfig),
filtered_by => fun doesnt_need_quotes/1,
traverse => all
}),

lists:map(
fun(AtomNode) ->
elvis_result:new_item(
"unnecessarily quoted atom ~s was found; prefer removing the quotes when "
"not syntactically required",
[ktn_code:attr(text, AtomNode)],
#{node => AtomNode}
)
end,
AtomNodes
).

doesnt_need_quotes(AtomNode) ->
AtomName0 = ktn_code:attr(text, AtomNode),
case re:run(AtomName0, "^'[a-z][a-zA-Z0-9_@]*'$", [{capture, none}]) of
match ->
AtomName = string:trim(AtomName0, both, "'"),
Atom = list_to_atom(AtomName),
Atom =/= 'maybe' andalso not erl_scan:f_reserved_word(Atom);
_ ->
false
end.

behaviour_spelling(Rule, ElvisConfig) ->
Spelling = elvis_rule:option(spelling, Rule),

Expand Down
32 changes: 0 additions & 32 deletions src/elvis_text_style.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
line_length/2,
no_tabs/2,
no_trailing_whitespace/2,
prefer_unquoted_atoms/2,
no_redundant_blank_lines/2
]).

Expand Down Expand Up @@ -62,37 +61,6 @@ no_trailing_whitespace(Rule, _ElvisConfig) ->
elvis_rule:def(Rule)
).

prefer_unquoted_atoms(Rule, ElvisConfig) ->
{nodes, AtomNodes} = elvis_code:find(#{
of_types => [atom],
inside => elvis_code:root(Rule, ElvisConfig),
filtered_by => fun doesnt_need_quotes/1,
traverse => all
}),

lists:map(
fun(AtomNode) ->
elvis_result:new_item(
"unnecessarily quoted atom ~s was found; prefer removing the quotes when "
"not syntactically required",
[ktn_code:attr(text, AtomNode)],
#{node => AtomNode}
)
end,
AtomNodes
).

doesnt_need_quotes(AtomNode) ->
AtomName0 = ktn_code:attr(text, AtomNode),
case re:run(AtomName0, "^'[a-z][a-zA-Z0-9_@]*'$", [{capture, none}]) of
match ->
AtomName = string:trim(AtomName0, both, "'"),
Atom = list_to_atom(AtomName),
Atom =/= 'maybe' andalso not erl_scan:f_reserved_word(Atom);
_ ->
false
end.

no_redundant_blank_lines(Rule, _ElvisConfig) ->
MaxLines = elvis_rule:option(max_lines, Rule) + 1,
{Src, _} = elvis_file:src(elvis_rule:file(Rule)),
Expand Down
4 changes: 2 additions & 2 deletions test/text_style_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,13 @@ verify_unquoted_atoms(Config) ->
PassPath = "pass_unquoted_atoms." ++ "erl",
[] =
elvis_test_utils:elvis_core_apply_rule(
Config, elvis_text_style, prefer_unquoted_atoms, #{}, PassPath
Config, elvis_style, prefer_unquoted_atoms, #{}, PassPath
),

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

verify_redundant_blank_lines(Config) ->
Expand Down
Loading