elixir_style_guide
elixir_style_guide copied to clipboard
How do we use this style guide in an automated way?
Right now I have Atom, Credo, and the atom-credo linter package installed.
Is there a way for us Elixir devs to use this style guide in an automated way?
Having something like https://atom.io/packages/elm-format would be pretty cool 😆
Is there a way for us Elixir devs to use this style guide in an automated way?
It should be possible to write custom Credo checks for many of the style recommendations in this guide, and package them up for use in any Elixir project. Would this be of interest? Do you think it would make sense to include them here, or to create a separate repo/project for that purpose?
Having something like https://atom.io/packages/elm-format would be pretty cool 😆
Not quite the same, but have you tried https://github.com/lpil/dogma ?
I found this repo using the trending feature for Github, so I imagine that a lot of Elixir devs agree this this is a nice way to write Elixir. I would love to be able to install a package in Atom and run all these style checks against my open file.
In Javascript I use prettier. In Ruby I use rubocop. In Go I use go fmt. In Elixir...eh... wat do?!
@sergiotapia You already mentioned atom-credo. That runs mix credo
and reports the output, right? In theory it should work with a customized Credo config.
It would be a fair amount of work to write credo checks for this style guide, and either upstream them to credo or create a separate package. But it should be doable!
We have a google summer of code student working on an elixir formatter. It won't follow this style guide, but it will be fairly similar.
First of all, sorry for posting in this old issue, but since it was open I though that would not be that bad.
I've been playing around with Credo and the Formatter to see what things are not automated. I detected a few things you can check on this repository
Source Code Layout
Neither credo nor formatter caught it
-
It fails on space between long dos As seen here
-
It doesn't enforce parenthesis on pipes As seen here But the compiler emits a warning, so it may be enough.
-
It doesn't enforce pipe in favor of nested call As seen here
Formatter didn't caught, but Credo did
-
Enforce bare variables as the first element on pipes As seen here
-
Prefer normal call when piping only once As seen here However, I had to add
{Credo.Check.Readability.SinglePipe}
to.credo.exs
because it was not in the generated credo config
Syntax
Neither credo nor formatter caught it
-
Does not enforce true as last cond As seen here
-
Does not add
()
on zero-arity calls As seen here However the compiler does emit a warning -
Prefer keyword list syntax sugar As seen here
Formatter didn't caught, but Credo did
-
Unless with else As seen here
Comments
Only problems with comment grammar (which is totally understandeable), as seen here
Modules
Almost nothing was caught. I think only space after defmodule
was solved automatically.
I think that the order is something really nice to add to Credo. Will think about it
Typespecs
Neither credo nor formatter caught it
- Add spaces between typespecs As seen here
Structs
Neither credo nor formatter caught it
- It doesn't change from
key: nil
to:key
As seen here
Improving
There is probably a lot of places we can improve that I haven't tested. I also dind't got into naming because it is a nasty space.
One thing we could do to automate it better is:
Add Checks to Credo
This is the list of checks I think is not that hard to implement on Credo
- Prefer pipes than nested calls (if more than one nest)
-
Avoid truthy values on
cond
, prefertrue
- Prefer keyword list syntax sugar
- Module Attribute Ordering
- Spaces between typespecs when @typedoc is present
-
Prefer list defstruct when some fields default to
nil
Maybe we should create some issues on Credo repo and see if they think these checks are reasonable and then we can try to implement the checks (we could ship the checks in a different repo as well)
What do you guys think?
There is a discussion on rrene/credo#540 about the module ordering
Looks like there is a PR out for module attribute ordering: https://github.com/rrrene/credo/pull/588 :tada:
I have a package called credo_contrib which implements a lot of the rules from this style guide (including module attribute ordering). I'll take a look at the list above and see if there are any more I can add. Contributions welcome also!
I have created https://github.com/rrrene/credo/pull/933 to enforce https://github.com/christopheradams/elixir_style_guide#pipe-operator 🎉
I have created https://github.com/rrrene/credo/pull/935 to enforce the https://github.com/christopheradams/elixir_style_guide#true-as-last-condition
I have created rrrene/credo#933 to enforce https://github.com/christopheradams/elixir_style_guide#pipe-operator tada
This is great work @Stratus3D, please keep us up to date on your credo PRs.