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

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


:grey_question: 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(" ")

:grey_question: 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.


:grey_question: Avoid superfluous comments (C2).

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


:grey_question: 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.

cmo

cmo

I don’t think I want my formatter renaming my functions.

Sure it moves them. It certainly doesn’t detect if it is superfluous and delete it. Probably out of scope for a formatter.

joeyates

joeyates

Styler has its own style, while Green aims to implement the existing popular style guides.

It automates formatting that is currently enforced by hand.

Where Next?

Popular in Libraries Top

praveenperera
FastRSS Parse RSS feeds very quickly: This is rust NIF built using rustler Uses the RSS rust crate to do the actual RSS parsing Speed...
New
benlime
LiveMotion enables high performance animations declared on the server and run on the client. As a follow up to my previous thread A libr...
New
MRdotB
I needed to reuse React components from my Chrome extension in my Phoenix/LiveView backend. I noticed that for Svelte/Vue, there are live...
New
Eiji
ExApi is a library that I’m developing now and hope release soon This library will allow to: list all apis list all api implementation...
New
gabrielpoca
Hello everyone! I want to share with you something that I’m really proud of: https://stillstatic.io/ Still is a static site builder for...
New
mtrudel
Bandit is an HTTP server for Plug and WebSock apps. Bandit is written entirely in Elixir and is built atop Thousand Island. It can serve...
New
KallDrexx
For a good number of months I've been working on creating a very basic RTMP live video streaming server. Now that I have a very, very ba...
New
anshuman23
Hello all, I have been working on my proposed project called Tensorflex as part of Google Summer of Code 2018.. Tensorflex can be used f...
New
mischov
import Meeseeks.CSS html = HTTPoison.get!("https://news.ycombinator.com/").body for story <- Meeseeks.all(html, css("tr.athing")) do...
New
mplatts
With HEEX released we decided to start a components library using Tailwind CSS - check it out here: Petal Components. We also have a boi...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Sub Categories:

We're in Beta

About us Mission Statement