joeyates

joeyates

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

Showing Posts 1 to 10

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

arcanemachine

arcanemachine

I think that superfluous comment != inline comment. That item means to not add pointless comments.

I wish I could add inline comments without the formatter ruining it for me… I’m sure there’s a way, I just hate that I would have to go out of my way to do it.

Eiji

Eiji

“Trust me”, except obvious SPAM there is no such thing like “pointless comments”. It’s not about comments, but few times I was asked to make code more readable for people who don’t know Elixir language. When doing so I have realised what kind of naming is used in different communities and I think that the last task I want to see in the issue board is a task like that. :joy:

Eiji

Eiji

Depends what kind of naming we deal with. Please keep in mind that such naming is recommended not only by someone’s style guide, but also by official documentation and in that case I don’t mind as long as it would not produce false positives.

Trailing question mark (foo?) | Naming conventions @ Elixir documentation

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:

Eiji

Eiji

I don’t think you’re right here … includes does not mean all of. Also I was proposing to apply some rule only for a very specific use case.

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.

joeyates

joeyates OP

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

It automates formatting that is currently enforced by hand.

DaAnalyst

DaAnalyst

Appreciate the effort, but is there a way for the user to define their own rules?

For instance, I personally dislike a number of “standard” formatting practices and have my own way of formatting such cases.

Also, I would never let a formatter transform my functions calls into piped calls or the other way around b/c I use both of them purposefully. In my view, code formatting should mirror the intent and that’s how it should be used.

Where Next? Top

Trending in Announcing Top

wojtekmach
Hey everyone! Req is an HTTP client for Elixir that I’ve been working on for quite some time. There is already a lot of HTTP clients out...
New
handnot2
Samly can be used to enable SAML 2.0 Single Sign On in a Plug/Phoenix application. This library uses Erlang esaml to provide plug enabl...
New
woylie
Flop is an Elixir library that applies filtering, ordering and pagination parameters to your Ecto queries. offset-based pagination with...
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
fuelen
Hi all! I want to present a small library which provides a mix task for generating an Entity-Relationship Diagram for Ecto schemas. You...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
AstonJ
This showed up on my feed.. anyone heard of it? Just hype? Ox Alpha is a reasoning model designed for coding, sustained ag...
New
sergio
It’s not that it’s vocabulary is too advanced. It’s something worse. I get lost trying to follow even a paragraph written by Claude. It’...
New
sorenone
Today we’re releasing Oban for Python. Not an Oban client in Python. Not a pythonx wrapper embedded in Elixir. Nope, it’s a fully operati...
New
akoutmos
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
New
pferriby
Introductory paragraph I’ll be looking for a keen junior or someone that has a couple of years experience in the real world (so you’ve be...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews