credo icon indicating copy to clipboard operation
credo copied to clipboard

add new check for pipe per line

Open rands0n opened this issue 4 years ago • 4 comments

Some style guide proposes:

# not preferred

1 |> Integer.to_string() |> String.to_integer()

# preferred

1
|> Integer.to_string()
|> String.to_integer()

This PR provides a check for that.

--

Credits by @bamorim

rands0n avatar Dec 23 '21 16:12 rands0n

@rrrene ping

rands0n avatar Mar 24 '22 14:03 rands0n

This looks good.

The last thing I am wondering is if we covered all edge cases in the tests though.

Are there things we could be missing? Like "exotic" combinations of single line and multi line pipes?

String.to_integer(1 |> Integer.to_string()) 
|> Integer.to_string()

rrrene avatar Mar 27 '22 08:03 rrrene

@rrrene that is a good point. The current approach just count pipes per line, so something like

value
|> do_something(:a |> to_string())
|> do_something_else()

Would fail. If we want this to be accepted, we would need to change. If we don't mind that much, we could suggest that this is changed either to an extracted variable or to something like

value
|> do_something(
  :a
  |> to_string()
)
|> do_seomthing_else()

bamorim avatar Mar 28 '22 14:03 bamorim

The implementation to support that case would require us parsing the AST and counting |> per line per tree depth, which is harder to implement and I'm not sure worth it.

bamorim avatar Mar 29 '22 08:03 bamorim

This is part of Credo v1.7.0-rc.1 :+1:

rrrene avatar Dec 31 '22 15:12 rrrene