hrzndhrn / recode

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

Feature Requests: Heredocs/Locals without Parens/HEEx attrs? #88

Open sodapopcan opened 1 week ago

sodapopcan commented 1 week ago

Hey hey, thanks for Recode! I started writing some of these rules myself before I remember it existed (d'oh).

These are merely suggestions based on my top annoyances that were not covered by Recode. I'm happy to take the implementation of any of them.

NickNeck commented 1 week ago

Thank you for your suggestions. A PR is always welcome if you want to tackle one of your suggestions yourself.

Some comments:

Indent heredocs at the same level as their sigil.

I am not sure if this can be handled in recode. When recode changes the AST the new AST goes to the Elixir formatter and the code will be formatted with the formatter rules. Maybe this is something that has to go in a custom formatter or in something like FreedomFormater.

Remove parens from :locals_without_parens.

Great Idea. That should be possible in recode. The Mix.Tasks.Format.formatter_for_file/2 is the right function to get the keyword list for locals_without_parens. The function returns different results for different files only when the project contains more than one .formatter.exs.

Correct if foo == bar, do: true, else: false to foo == bar

That should be easy and it is a good candidate for a first recode-task.

Correct boolean={true} HEEx attribute to simply boolean

Recode should also handle HEEx files. For now I haven't tried to change such files.

sodapopcan commented 1 week ago

Hey Marcus!

The function returns different results for different files only when the project contains more than one .formatter.exs.

Ah that makes sense, thanks for explaining that! I have half-way done implementation of this (it just doesn't take arity into account) so happy to fix it up and make a PR. I can certainly also handle the comparison one.

As for heredoc formatting, Elixir's formatter leave indentation alone so long as it's legal. I have a naive implementation that I could work on a bit if you're interested. To clarify I'm looking to turn this:

@moduledoc """
  I'm a doc
"""

to this:

@moduledoc """
I'm a doc
"""

Not a big deal if you aren't interested, though.

Thanks and I'll be in touch!

NickNeck commented 6 days ago

Ah, now I understand the thing with the heredoc. For HD in general this makes less sense, because we can't know if someone did it for a good reason. Maybe a task like DocFormatting would be useful.

sodapopcan commented 4 days ago

Ya, basically doc formatting, but I'm also thinking for LiveView as well:

~H"""
  <div>
  </div>
"""

becomes:

~H"""
<div>
</div>
"""

I agree it's harder to figure out here. As far as I can tell, the only good heuristic is to see if the first line is indented too far then just un-indent every line by that much unless it's illegal to do.

For example:

@doc """
  Hello!

  Oh hi

I'm back here for some reason

  I'm a list:
    - one
    - two
    - three
"""

would become:

@doc """
Hello!

Oh hi

I'm back here for some reason

I'm a list:
  - one
  - two
  - three
"""

But:

@doc """
I'm a doc.

  The rest of me is
  written like this for
  some reason...
"""

...would just be left alone.

The point of this corrector is just to catch people doing the extra indent on every line which is very common. Anyone who does something like that last example are people who just like to watch the world burn AFAIC and there's not much you can do about them 😅

The indentation level could even be configurable!

NickNeck commented 3 days ago

I think ~H will be formatted by Phoenix.LiveView.HTMLFormatter.

A checker for @doc could be useful for a few people.