hrzndhrn / recode

A linter with autocorrection and a refactoring tool.
MIT License
281 stars 15 forks source link

Allow optional config for Recode.Task.Format #87

Closed NickNeck closed 3 months ago

NickNeck commented 3 months ago

closes #86

@szymonkozak you can now add an optional config for Recode.Task.Format:

...
  tasks: [
    # the default, formats code with Sourceror 
    # {Recode.Task.Format, []}, 

    # formats code with Sourceror 
    # {Recode.Task.Format, config: [formatter: :sourceror]}, 

    # formats code with the Elixir formatter
    {Recode.Task.Format, config: [formatter: :elixir]}, 

    # deactivates the task
    # {Recode.Task.Format, active: false}, 
...
coveralls commented 3 months ago

Pull Request Test Coverage Report for Build 9fc53b564e8c150ce9f171fe1453d47bb6aff209

Details


Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/recode/config.ex 3 4 75.0%
lib/recode/task/format.ex 10 15 66.67%
<!-- Total: 13 19 68.42% -->
Totals Coverage Status
Change from base Build 5f62f15458ac2deb1a9af7bb7c8ff224f6cef641: -0.5%
Covered Lines: 938
Relevant Lines: 1026

💛 - Coveralls
szymonkozak commented 3 months ago

Hi, thanks for the quick response! I have tested your solution.

Unfortunately after deactivating Format tasks it only doesn't report Format issues when running with autocorrect: false and dry: true. If autocorrect is set to true and dry to false, code is formatted despite of Format tasks being inactive.

Also, when using recode as a formatter, it stills adds some empty lines in some places, where they shouldn't. Even if the Format task is deactivated, or changed to {Recode.Task.Format, config: [formatter: :elixir]},

I have experimented with this code and I have discovered, that deactivating Format won't help in my issue because Recode.Task.AliasOrder formats the code anyway, even when Format task was disabled.

Do you know any other ideas how to handle that? I would like to use recode only as a alias sorter working together with mix format, but right know it changes too many things that mix format doesn't change in my project.

NickNeck commented 3 months ago

Yes, when recode changes a file the file will also be formatted. The reason for this is that Recode manipulates the AST and we can only create the corresponding code if we format the AST. That is also the reason for the pre-formatter, so that all formatting changes are made in a first step. The solution would be a change in Sourceror, I have opened a PR over there (https://github.com/doorgan/sourceror/pull/127).

Do you have a code example thats will be not formatted correctly? I could test it against my changes in Sourceror.

szymonkozak commented 3 months ago

Thank you. Sure, there are examples. All these are related to comments only.

Example 1:

defmodule X do
  alias B
  alias A

  def y do
    1
    # test
  end
end

is changed to:

defmodule X do
  alias A
  alias B

  def y do
    1

    # test
  end
end

So adds a new empty line before # test comment.

Example 2:

defmodule Y do
  alias B
  alias A

  def c do
    (fn y ->
      case z do
        p ->
          # test
           d

         u ->
           s
       end
     end).()
  end
end

is changed to:

defmodule Y do
  alias A
  alias B

  def c do
    (fn y ->
       # test
       case z do
         p ->
           d

         u ->
           s
       end
     end).()
  end
end

So moves the comment a few lines up.

Example 3 (the more complex one)

defmodule Z do
  alias B
  alias A

  def a do
    # test1

    # test2

    # line1
    # line2

    :ok
  end

  # test

  defp z do
    1
  end
end

is changed to:

defmodule Z do
  alias A
  alias B

  def a do
    # test1
    # test2

    # line1
    # line2

    # test
    :ok
  end

  defp z do
    1
  end
end

As you can see the # test comment is moving from outside the function to the inside.

Example 4:

defmodule U do
  use Ecto.Schema

  alias B
  alias A

  schema "x" do
    field(:a, :string)
    field(:b, :string)
    # field(:c, :string)

    field(:d, :string)
  end
end

is changed to:

defmodule U do
  use Ecto.Schema

  alias A
  alias B

  schema "x" do
    field(:a, :string)
    field(:b, :string)

    # field(:c, :string)

    field(:d, :string)
  end
end

There is a new empty line before commented field.

Without using recode, mix format doesn't change anything in this code. I hope that will help you, if you need something more let me know.

NickNeck commented 3 months ago

Thank you for the examples. One of four is fixed. I will continue to try to fix this.