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

New rule: Prefer Unquoted Atoms #297

Closed
paulo-ferraz-oliveira opened this issue Mar 1, 2023 · 11 comments
Closed

New rule: Prefer Unquoted Atoms #297

paulo-ferraz-oliveira opened this issue Mar 1, 2023 · 11 comments
Labels
Milestone

Comments

@paulo-ferraz-oliveira
Copy link
Collaborator

paulo-ferraz-oliveira commented Mar 1, 2023

Name

prefer_unquoted_atoms

Brief Description

Prefer unquoted atoms unless quotes are necessary.

Reasoning

This one is not a technical concern, but a readability concern. The code will behave identical in both ways.

Refactoring Proposal

elvis shall warn that your atoms are quoted when they don't have to be.

Note: if you're using a formatter this could/should already be taken into account by it.

Origin

Inspired by Credo's https://hexdocs.pm/credo/Credo.Check.Readability.PreferUnquotedAtoms.html.

@paulo-ferraz-oliveira paulo-ferraz-oliveira changed the title Prefer Unquoted Atoms New rule: Prefer Unquoted Atoms Mar 1, 2023
@elbrujohalcon elbrujohalcon modified the milestones: eventually 🙄, 3.1.0 Mar 1, 2023
@paulo-ferraz-oliveira
Copy link
Collaborator Author

One thing I remembered is that language constructs need to be quoted as atoms. e.g. 'maybe' (quoted) is not the same as maybe (unquoted). That'll have to be taken into account when implementing.

@elbrujohalcon
Copy link
Member

Maybe other formatters would leave atoms as-they-found-them, but at least rebar3_format (with the default formatter) will apply this concept appropriately (i.e., only atoms that need to be quoted will be quoted).

@elbrujohalcon
Copy link
Member

Not saying "no" to the rule, just that it may be low priority since other tools cover this aspect.

@bormilan
Copy link
Contributor

bormilan commented Aug 31, 2024

I had a long session discovering how to implement this, and I found a problem. I made a test file that contains quoted atoms that are not required to be quoted. I dug deep into the code of elvis and ktn_code, and I found out that quotes disappear if they are not needed.

Example file:
image

And this is the output of the file read by the function: elvis_file:src
image

What do I miss here?

Edit: I found out that it reads the .beam of the file that's the problem if I'm right.

@paulo-ferraz-oliveira
Copy link
Collaborator Author

If you're analysing from BEAM this might not apply; if you're analysing from source it's still interesting.

@bormilan
Copy link
Contributor

bormilan commented Sep 2, 2024

I spent much time exploring how these work in the repo, but I'm still confused. I see that we have several rules that require .erl files not .beam for example macro naming conventions. But I also see that in the style_SUITE.erl, we only have a group called beam_files which contains only test that uses .beam files. I'm not sure why other test cases exist but have never been used. Or do I miss something important? I wanted to see how other rules read .erl files, bc this issue only can be solved in that way, but I'm struggling to find one.

Edit: I started working with the elvis_file:src function, and maybe elvis_utils:split_all_lines or something like that, to wrap the code and find the atoms. Things like elvis_file:parse_tree not working here (I'm not really sure, but I guess the bc of the structure).

Edit2: nevermind, I found out that ktn_code:parse_treeworks well, so I don't need to make things by hand.

bormilan pushed a commit to bormilan/elvis_core that referenced this issue Sep 2, 2024
@bormilan
Copy link
Contributor

bormilan commented Sep 2, 2024

I think I made good progress today, it's not perfect but works. Could you please provide me with some suggestions?
main...bormilan:elvis_core:feature/prefer-unquoted-atoms

@paulo-ferraz-oliveira
Copy link
Collaborator Author

paulo-ferraz-oliveira commented Sep 2, 2024

A while back, rules only supported analysis on .erl files. Along came #138 and changed that. Around that time we introduced " Works on .beam file? " in the documentation, which should exist for all rules. I can't remember if there're rules that are .beam alone, but it's possible; on the other hand, most rules (if not all) should work on .erl files (and not necessarily compiled).

I'm not sure why other test cases exist but have never been used.

all/0 states this:

-spec all() -> [atom()].
all() ->
    Exports = ?MODULE:module_info(exports),
    [F || {F, _} <- Exports, not lists:member(F, ?EXCLUDED_FUNS)] ++ [{group, beam_files}].

and it outputs 120+ ., which should correspond to the actual number of test cases. If only the group-identified tests ran we'd have only around 30+ tests; there's some 🪄 in the

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Common test
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

section of the identified file.

I think I made good progress today, it's not perfect but works. Could you please provide me with some suggestions?
main...bormilan:elvis_core:feature/prefer-unquoted-atoms

You should open a pull request (e.g. as a draft) so we can discuss the implementation details there.

Edit: I briefly looked at the implementation; it seems to go in the right direction. A pull request will allow us to make inline comments that'll help you better.

bormilan pushed a commit to bormilan/elvis_core that referenced this issue Sep 10, 2024
bormilan pushed a commit to bormilan/elvis_core that referenced this issue Sep 10, 2024
bormilan pushed a commit to bormilan/elvis_core that referenced this issue Sep 12, 2024
elbrujohalcon added a commit that referenced this issue Sep 16, 2024
@elbrujohalcon elbrujohalcon modified the milestones: 4.1.0, 4.0.0 Sep 16, 2024
@bormilan
Copy link
Contributor

bormilan commented Nov 4, 2024

This can be closed, isn't it?

@elbrujohalcon
Copy link
Member

I… think so. @paulo-ferraz-oliveira ?

@paulo-ferraz-oliveira
Copy link
Collaborator Author

Depends... if we want to do nothing, assuming this is a concern for other tools, sure 😄.

If, at the same time, the tools we use to parse the code for analysis already remove quotes when not required then we'd have to change those, and that's not something we wanna do 👍.

So, yeah, I'll close it. If it becomes a hassle, in the future, we have history already.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants