Marcus

Marcus

Recode - A linter with autocorrection and a refactoring tool

Recode is an experimental package to lint, autocorrect and refactor code. The package uses the great Sourceror library by @dorgan.

So far, there are only a small number of autocorrections and just one refactoring command.

The following example runs recode in a project with some nonsense code:

> mix recode --dry
Found 11 files, including 2 scripts.
.............................................................................
 File: lib/my_code.ex
[Specs 15/3] Functions should have a @spec type specification.

 File: lib/my_code/alias_expansion.ex
Updates: 1
Changed by: AliasExpansion
001   |defmodule MyCode.AliasExpansion do
002 - |  alias MyCode.{PipeFunOne, SinglePipe}
002 + |  alias MyCode.PipeFunOne
003 + |  alias MyCode.SinglePipe
004   |
005   |  def foo(x) do
[Specs 5/3] Functions should have a @spec type specification.

 File: lib/my_code/alias_order.ex
Updates: 2
Changed by: AliasOrder, AliasExpansion
012   |
013   |defmodule Mycode.AliasOrder do
014 - |  alias MyCode.SinglePipe
014 + |  alias MyCode.Echo
015 + |  alias MyCode.Foxtrot
016   |  alias MyCode.PipeFunOne
017 - |  alias MyCode.{Foxtrot, Echo}
017 + |  alias MyCode.SinglePipe
018   |
019   |  @doc false

 File: lib/my_code/fun.ex
Updates: 1
Changed by: Format
002   |  @moduledoc false
003   |
004 - |
005 - |
006 - |
007 - |
008 - |
004   |  def noop(x), do: x
005   |end

 File: lib/my_code/multi.ex
Updates: 2
Changed by: SinglePipe, PipeFunOne
007   |
008   |  def pipe(x) do
009 - |    x |> double |> double()
009 + |    x |> double() |> double()
010   |  end
011   |
012   |  def single(x) do
013 - |    x |> double()
013 + |    double(x)
014   |  end
015   |

 File: lib/my_code/pipe_fun_one.ex
Updates: 1
Changed by: PipeFunOne
005   |
006   |  def pipe(x) do
007 - |    x |> double |> double()
007 + |    x |> double() |> double()
008   |  end
009   |end

 File: lib/my_code/singel_pipe.ex
Updates: 1
Changed by: SinglePipe
005   |
006   |  def single_pipe(x) do
007 - |    x |> double()
007 + |    double(x)
008   |  end
009   |
010 - |  def reverse(a), do: a |> Enum.reverse()
010 + |  def reverse(a), do: Enum.reverse(a)
011   |end
012   |

 File: test/my_code_test.exs
Updates: 1
Changed by: TestFileExt
Moved from: test/my_code_test.ex

The refactoring task:

> mix recode.rename --dry MyCode.SinglePipe.double dbl
Found 11 files, including 2 scripts.
...........
 File: lib/my_code/alias_expansion.ex
Updates: 1
Changed by: Rename
003   |
004   |  def foo(x) do
005 - |    SinglePipe.double(x) + PipeFunOne.double(x)
005 + |    SinglePipe.dbl(x) + PipeFunOne.double(x)
006   |  end
007   |end

 File: lib/my_code/alias_order.ex
Updates: 1
Changed by: Rename
018   |  @doc false
019   |  def foo do
020 - |    {SinglePipe.double(2), PipeFunOne.double(3)}
020 + |    {SinglePipe.dbl(2), PipeFunOne.double(3)}
021   |  end
022   |

 File: lib/my_code/singel_pipe.ex
Updates: 1
Changed by: Rename
002   |  @moduledoc false
003   |
004 - |  def double(x), do: x + x
004 + |  def dbl(x), do: x + x
005   |
006   |  def single_pipe(x) do
007 - |    x |> double()
007 + |    x |> dbl()
008   |  end
009   |

Differences to Credo

recode was started as a plugin for credo. Unfortunately it was not possible
to implement autocorrection as a plugin because the traversation of the code does
not support changing the code.

Maybe some code lines from recode could be used as inspiration for credo
to bring the autocorrect feature to credo.

Other differences:

  • recode requiers Elixir 1.12, credo requiers Elixir 1.7
  • recode has autocorrection
  • credo has much more checkers
  • credo is faster
  • credo has more features

Most Liked

Marcus

Marcus

I have release 0.5.0 of recode. The new version adds the Recode.FormatterPlugin. This plugin allows you to run Recode autocorrecting tasks together when executing mix format. See the documentation of Recode.FormatterPlugin for how to set this up.

Marcus

Marcus

0.4.0

The new release removes the mix task recode.rename to get focus on the linting and auto-correction.

The package was divided. The part that takes care of the files, sources and the project is now in rewrite.

The output for a --dry/--verbose run has been updated:

> cd examples/my_code
> mix recode --dry
Found 13 files, including 2 scripts.
...........................................................................................
 File: lib/my_code.ex
[Specs 15/3] Functions should have a @spec type specification.

 File: lib/my_code/alias_expansion.ex
Updates: 1
Changed by: AliasExpansion
1 1   |defmodule MyCode.AliasExpansion do
2   - |  alias MyCode.{PipeFunOne, SinglePipe}
  2 + |  alias MyCode.PipeFunOne
  3 + |  alias MyCode.SinglePipe
3 4   |
4 5   |  def foo(x) do
   ...|
[Specs 5/3] Functions should have a @spec type specification.

 File: lib/my_code/alias_order.ex
Updates: 2
Changed by: AliasOrder, AliasExpansion
     ...|
12 12   |
13 13   |defmodule Mycode.AliasOrder do
14    - |  alias MyCode.SinglePipe
   14 + |  alias MyCode.Echo
   15 + |  alias MyCode.Foxtrot
15 16   |  alias MyCode.PipeFunOne
16    - |  alias MyCode.{Foxtrot, Echo}
   17 + |  alias MyCode.SinglePipe
17 18   |
18 19   |  @doc false
     ...|

 File: lib/my_code/fun.ex
Updates: 1
Changed by: Format
     ...|
 2  2   |  @moduledoc false
 3  3   |
 4    - |
 5    - |
 6    - |
 7    - |
 8    - |
 9  4   |  def noop(x), do: x
10  5   |end
     ...|

 File: lib/my_code/multi.ex
Updates: 2
Changed by: SinglePipe, PipeFunOne
     ...|
 7  7   |
 8  8   |  def pipe(x) do
 9    - |    x |> double |> double()
    9 + |    x |> double() |> double()
10 10   |  end
11 11   |
12 12   |  def single(x) do
13    - |    x |> double()
   13 + |    double(x)
14 14   |  end
15 15   |
     ...|

 File: lib/my_code/pipe_fun_one.ex
Updates: 1
Changed by: PipeFunOne
     ...|
 5  5   |
 6  6   |  def pipe(x) do
 7    - |    x |> double |> double()
    7 + |    x |> double() |> double()
 8  8   |  end
 9  9   |end
     ...|

 File: lib/my_code/same_line.ex
[Specs 2/3] Functions should have a @spec type specification.

 File: lib/my_code/singel_pipe.ex
Updates: 1
Changed by: SinglePipe
     ...|
 5  5   |
 6  6   |  def single_pipe(x) do
 7    - |    x |> double()
    7 + |    double(x)
 8  8   |  end
 9  9   |
10    - |  def reverse(a), do: a |> Enum.reverse()
   10 + |  def reverse(a), do: Enum.reverse(a)
11 11   |end
12 12   |

 File: test/my_code_test.exs
Updates: 1
Changed by: TestFileExt
Moved from: test/my_code_test.ex
Marcus

Marcus

Recode version 0.6.0 is now available.

The new version adds

  • the mix task recode.help to print a list of all recode tasks or the doc for a given recode task
  • the mix task recode.update.config to update an existing recode config.
  • aliases for all options available for mix recode.
  • Recode.Task.TagFIXME to check for FIXME tags.
  • Recode.Task.TagTODO to check for TODO tags.
  • Recode.Task.Nesting to report issues when functions are nested too deep.
  • Recode.Task.FilterCount to rewrite Enum.filter(fn ...) |> Enum.count() to Enum.count(fn ...).
  • Recode.Task.Dbg to remove dbg calls or report issues for such calls.
  • Recode.Task.IOInspect to remove IO.inspect calls or report issues for such calls.

The tasks Dbg and IOInspect are configured with autocorrect: false in the default config. To remove all calls to dbg and IO.inspect, you can use mix recode like this:

> mix recode -av -t IOInspect -t Dbg
Found 18 files, including 3 scripts.
....................................
 File: lib/my_code/multi.ex
Updates: 2
Changed by: Dbg, IOInspect
     ...|
 7  7   |
 8  8   |  def pipe(x) do
 9    - |    x |> double |> double() |> dbg()
    9 + |    x |> double |> double()
10 10   |  end
11 11   |
     ...|
22 22   |    |> Enum.filter(fn x -> rem(x, 2) == 0 end)
23 23   |    |> Enum.count()
24    - |    |> IO.inspect()
25 24   |  end
26 25   |end
     ...|

Finished in 0.04 seconds.

The -av stands for the switches --autocorrect and --verbose. The switch
--verbose causes recode to display all changes as a diff on the console.

An example for FilterCount:

> mix recode -d -t FilterCount
Found 18 files, including 3 scripts.
..................
 File: lib/my_code/multi.ex
Updates: 1
Changed by: FilterCount
     ...|
20 20   |  def my_count(list) do
21 21   |    list
22    - |    |> Enum.filter(fn x -> rem(x, 2) == 0 end)
23    - |    |> Enum.count()
   22 + |    |> Enum.count(fn x -> rem(x, 2) == 0 end)
24 23   |    |> IO.inspect()
25 24   |  end
     ...|

Finished in 0.04 seconds.

Where Next?

Popular in Announcing Top

tmbb
PhoenixWS - Websockets over Phoenix Channels Source code on Github here: GitHub - tmbb/phoenix_ws: Websockets implemented over Phoenix Ch...
New
josevalim
Yes, yet another parser combinator library! Most of the parser combinators in the ecosystem are either compile-time, often using AST tra...
159 19228 141
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
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
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 18474 194
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
nikokozak
Hello all, I’ve been working on Svonix - a library for quickly integrating Svelte components into Phoenix views. It’s a much-needed succ...
New
archan937
It is a well-know topic within the Elixir community: “To mock or not to mock? :)” Every alchemist probably has his / her own opinion con...
New
zachdaniel
Ash Framework What is Ash? Ash Framework is a declarative, resource-oriented application development framework for Elixir. A resource can...
New
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36128 110
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 47930 226
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

We're in Beta

About us Mission Statement