joeyates

joeyates

Green - format your code according to a style guide

Today I’m releasing Green - a style guide enforcer for Elixir.

Green acts as a plugin to Mix Format and automates the application of a full set of formatting rules.

Status

Currently, Green implements the set of rules defined in lexmag’s Elixir Style Guide.

For example, it transforms this:

foo(bar(baz(quux), 42))

into this

baz(quux) |> bar(42) |> foo()

See the relevant section of the docs for full details.

Usage

Add the library to your dependencies:

defp deps do
  [
    {:green, ">= 0.1.3", only: :dev}
  ]
end

Add the following to .formatter.exs:

plugins: [Green.Lexmag.ElixirStyleGuideFormatter]

Configuration

While Green aims to work with no configuration, there will be cases where a little is needed.

Specifically, Green sometimes needs help in recognising function (and macro) calls that shouldn’t be pipelined. One example is assert/1, which isn’t part of the default :locals_without_parens configuration. To avoid assert foo(1) being turned into 1 |> foo() |> assert(), add this to .formatter.exs:

locals_without_parens: [assert: 1],

Links

https://github.com/joeyates/green

Most Liked

Eiji

Eiji

Personally I really don’t like such a default behaviour. In my opinion you should support all assert-like macros based on the official documentation.


use, import, assign or require

Should be:

use, import, alias or require


:white_question_mark: Don’t use anonymous functions in pipelines (L3),

I guess you can easily consider it as outdated. I would recommend to rewrite it in this way:

# Bad
sentence
|> String.split(~r/\s/)
|> (fn words -> [@sentence_start | words] end).()
|> Enum.join(" ")

# Good
sentence
|> String.split(~r/\s/)
|> then(&[@sentence_start | &1])
|> Enum.join(" ")

:white_question_mark: Enforce predicate functions to end with a question mark (N3),

That should be easy as long as you require the @spec, simply remove prefix is_ and add ? suffix if such character is not already at the end of the function name.


:white_question_mark: Avoid superfluous comments (C2).

This is handled by the Elixir formatter. It moves the comment after the expression to above said expression.


:white_question_mark: Put the expression being tested by comparison on the left side (U1)

I would not implement this. I’m not sure if it’s too far. However it should be easy to implement assuming “standardised” describe block naming:

describe "foo/1" do
  test ":default works as same as in foo/0" do
    # Bad
    assert MyApp.foo() == MyApp.foo(:default)
    # Good
    assert MyApp.foo(:default) == MyApp.foo()
  end
end
garrison

garrison

You guys have been debating the comment rule but in fairness to the OP his docs explicitly marked that particular rule as being impossible to implement because it’s subjective.

I think what the rule is getting at is that new programmers can sometimes fall into a habit of over-commenting every line, even what it’s obvious what the code does. Everyone posting in this thread is probably experienced enough to have learned not to do that long ago :slight_smile:

slouchpie

slouchpie

This looks great. Does it work alongside :styler or do they diverge in places? I am on the phone so lazily asking without reading docs.

Last Post!

adworse

adworse

I really like it!
One small concern: I haven’t tested it yet, but if your algo forces use to be closer to the module’s headline, it’s not a safe behavior, because resulting code sometimes depends of the macro call placement

Where Next?

Popular in Announcing Top

kelvinst
Hey everyone! Well, we made this lib a while ago and now we decided to finally go out and public with it! It’s a tool for creating and m...
New
treble37
Just looking for a little feedback on a tiny helper library I built - Sometimes I find the need to convert maps with atom keys to maps w...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 30338 241
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
fuelen
Hey folks! Want to present a toolkit for writing command-line user interfaces. It provides a convenient interface for colorizing text...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
New
restlessronin
The repo is at GitHub - cyberchitta/openai_ex: Community maintained Elixir library for OpenAI API · GitHub. Docs are at OpenaiEx User Gu...
152 10719 134
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement