Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sodapopcan opened this issue Jun 24, 2024 · 5 comments
Open

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

sodapopcan opened this issue Jun 24, 2024 · 5 comments

Comments

@sodapopcan
Copy link
Contributor

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.

  • Indent heredocs at the same level as their sigil.
    There are some considerations here as you can't blindly re-indent everything. I feel the safest is to assume that if the first line is indented to far then reduce everything below it by the same amount (unless it "reverse-overflows").

  • Remove parens from :locals_without_parens.
    I'm not entirely sure this is possible to reliably identify everything in their proper files. There is Mix.Tasks.Format.formatter_for_file/2 though I haven't played around with it too much and seems to return the same results for every file.

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

  • Correct boolean={true} HEEx attribute to simply boolean
    Not sure if you would want to get HEEx involved but the above always annoys me when I see it.

@NickNeck
Copy link
Member

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
Copy link
Contributor Author

sodapopcan commented Jun 26, 2024

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
Copy link
Member

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
Copy link
Contributor Author

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
Copy link
Member

NickNeck commented Jul 2, 2024

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants