Green
An Elixir code formatter.
Currently, it can enforce lexmag's Elixir style guide.
Motivation
The standard Elixir formatter only enforces a small number of certain rules. This project aims to enforce enough rules so that the "house style" of a project can be enforced by the formatter.
Name
The name "Green" is a reference to bikeshedding, i.e. "All bike shed shoul be green."
Status
Currently, Green provides Green.Lexmag.ElixirStyleGuideFormatter
which,
as far as is possible, implements the rules of lexmag's style guide
that are not already implemented by mix format
.
Limitations
Two rules change the order of lines:
PreferPipelines
groupsuse
,import
,assign
andrequire
statements,RemoveNilFromStructDefinition
places the list ofnil
-default fields at the top of the struct definition.
Where possible, when shifting lines around, Green will try to keep associated comments with the lines that follow them. However, this is not always possible. Please check the output of the formatter to ensure that comments are in the correct place.
Roadmap
Implement the other well-known Elixir style guides:
Lexmag.ElixirStyleGuideFormatter
Usage
Add the following to your mix.exs
:
defp deps do
[
{:green, "~> (See the badge above)"}
]
end
Modify .formatter.exs
to include the following:
[
plugins: [Green.Lexmag.ElixirStyleGuideFormatter]
]
Limitations
The implemented rules are marked with a check ✅
.
When it is possible to transform the code to match the style guide, Green will
do so. However, it is not always possible to do so. In these cases, Green will
leave the code as it is and print a warning. These are marked with an
exclamation mark ❗
.
Rules that are not implemented are marked with a question mark ❔
. This
includes rules where there is no way to objectively judge them, such as
usefulness of comments.
Rules that are implemented by the compiler or mix format
are marked with
a thumbs up 👍
.
Linting
✅ Transform nested function calls into pipelines (L1),
✅ Transform one-element pipelines into function calls (L2),
❔ Don't use anonymous functions in pipelines (L3),
✅ Transform unless...else...
into if...else...
(L4),
✅ Transform if...else nil
into if...
(L5),
✅ Ensure match-all condition of cond
has true
(L6),
❔ Use and
and or
instead of &&
and ||
when the arguments are boolean (L7),
✅ Use <>
instead of bitstrings when pattern-matching binaries (L8).
Naming
❗ Enforce snake_case for atoms, functions, variables, attributes (N1),
👍 Enforce CamelCase for modules (N2),
❔ Enforce predicate functions to end with a question mark (N3),
✅ Enforce snake_case for directories and files (N4),
❗ Disallow one-letter variable names (N5).
Comments
❔ Use only critical comments (C1),
❔ Avoid superfluous comments (C2).
Modules
✅ Group and order use
, import
, assign
and require
statements (M1),
✅ Replace the current module name with __MODULE__
(M2).
Notes:
- M1:
This transformation does not take into account comments. Any comment before
or on the same line as a
use
,import
,assign
orrequire
statement will be left where it is.
Regular Expressions
❔ Prefer pattern matching over regular expressions (R1),
❔ Use non-capturing regular expressions (R2).
Structs
✅ Don't specify nil
default values for defstruct
fields (S1).
Exceptions
❗ Use Error
suffix for exception names (E1),
❔ Use non-capitalized error messages (except for Mix error messages) without trailing punctuation (E2).
ExUnit
❔ Put the expression being tested by comparison on the left side (U1).